MIRA
ResourceName.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_RESOURCENAME_H_
48 #define _MIRA_RESOURCENAME_H_
49 
50 #include <string>
51 #include <ostream>
52 #include <istream>
53 
54 #include <error/Exceptions.h>
55 
57 
58 namespace mira {
59 
61 
68 {
69 private:
70 
72  ResourceName(const std::string& name, bool dontNormalize) : mName(name) {}
73 
74 public:
77 
79  ResourceName(const std::string& name) : mName(normalize(name)) {
80  if(!validate(mName))
81  MIRA_THROW(XInvalidParameter,
82  "Trying to create ResourceName from invalid name: '"
83  << mName << "'");
84  }
85 
87  ResourceName(const char* name) : mName(normalize(std::string(name))) {
88  if(!validate(mName))
89  MIRA_THROW(XInvalidParameter,
90  "Trying to create ResourceName from invalid name: '"
91  << mName << "'");
92  }
93 
94  ResourceName(const ResourceName& other) : mName(other.mName) {}
95 
96 public:
97 
99  const ResourceName& operator=(const std::string& other) {
100  mName = normalize(other);
101  return *this;
102  }
103 
104  const ResourceName& operator=(const ResourceName& other) {
105  mName = other.mName;
106  return *this;
107  }
108 
110 #if defined(MIRA_GNUC_VERSION)
111 # if MIRA_GNUC_VERSION >= 40601
112  operator std::string() const { return mName; }
113 # else
114  operator const std::string&() const { return mName; }
115 # endif
116 #else
117  // this should be the preferred version, that is supported by GCC < 4.6.1 and VC 2010
118  operator const std::string&() const { return mName; }
119 #endif
120 
122  const std::string& str() const { return mName; }
123 
124 public:
125 
131  ResourceName parent() const;
132 
134  std::string leaf() const;
135 
137  bool isAncestorOf(const ResourceName& other) const;
138 
140  bool isParentOf(const ResourceName& other) const;
141 
143  bool isSuccessorOf(const ResourceName& other) const;
144 
146  bool isChildOf(const ResourceName& other) const;
147 
148 public:
149 
152  bool operator<(const ResourceName& other) const { return mName < other.mName; }
153  bool operator<=(const ResourceName& other) const { return mName <= other.mName; }
154  bool operator>(const ResourceName& other) const { return mName > other.mName; }
155  bool operator>=(const ResourceName& other) const { return mName >= other.mName; }
156  bool operator==(const ResourceName& other) const { return mName == other.mName; }
157  bool operator!=(const ResourceName& other) const { return mName != other.mName; }
159 
160 public:
161 
163  std::string::value_type operator[](std::string::size_type i) { return mName[i]; }
164 
165 public:
166  template<typename Reflector>
167  void reflect(Reflector& r) {
168  r.delegate(mName);
169  }
170 
174  std::string operator/(const std::string& other) const {
175  if(isRoot()) // we are the root namespace
176  return mName + other;
177 
178  // concatenate both names
179  return mName + "/" + other;
180  }
181 
185  ResourceName operator/(const ResourceName& other) const {
186  return ResourceName(*this / other.mName, true);
187  }
188 
190  const ResourceName& operator/=(const ResourceName& other) {
191  if(isRoot())
192  mName += other.mName;
193  else
194  mName += "/" + other.mName;
195  return *this;
196  }
197 
201  bool isRoot() const {
202  return mName.size()==1 && mName[0]=='/';
203  }
204 
208  bool isFullyQualified() const {
209  if(mName.empty())
210  return false;
211 
212  return mName[0]=='/';
213  }
214 
215 public:
216 
217  static ResourceName makeFullyQualified(const ResourceName& name,
218  const ResourceName& ns);
219 
220 public:
221 
222  friend std::ostream& operator<<(std::ostream& os, const ResourceName& name)
223  {
224  os << name.mName;
225  return os;
226  }
227 
228  friend std::istream& operator>>(std::istream& is, ResourceName& name)
229  {
230  std::string s;
231  is >> s;
232  name = s;
233  return is;
234  }
235 
236 private:
237 
238  static std::string normalize(const std::string& name);
239 
241  static bool validate(const std::string& name);
242 
243 private:
245  std::string mName;
246 };
247 
249 
250 template<typename SerializerTag>
251 class IsTransparentSerializable<ResourceName,SerializerTag> : public std::true_type {};
252 
254 
255 }
256 
257 #endif
const ResourceName & operator=(const ResourceName &other)
Definition: ResourceName.h:104
bool isRoot() const
Returns true, if this is the root namespace "/".
Definition: ResourceName.h:201
Type trait that indicates whether a type should be serialized "transparently", i.e.
Definition: IsTransparentSerializable.h:81
ResourceName(const char *name)
Variant for normal C-Strings.
Definition: ResourceName.h:87
ResourceName(const std::string &name)
Creates a normalized resource from a name.
Definition: ResourceName.h:79
bool operator<=(const ResourceName &other) const
Definition: ResourceName.h:153
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
friend std::istream & operator>>(std::istream &is, ResourceName &name)
Definition: ResourceName.h:228
ResourceName operator/(const ResourceName &other) const
Concatenates two resource names separated by a &#39;/&#39;.
Definition: ResourceName.h:185
STL namespace.
const ResourceName & operator/=(const ResourceName &other)
Concatenates this with another resource names separated by a &#39;/&#39;.
Definition: ResourceName.h:190
Provides type trait that indicates whether a type should be serialized "transparently".
static ResourceName makeFullyQualified(const ResourceName &name, const ResourceName &ns)
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
bool operator==(const ResourceName &other) const
Definition: ResourceName.h:156
friend std::ostream & operator<<(std::ostream &os, const ResourceName &name)
Definition: ResourceName.h:222
bool isFullyQualified() const
Returns true, if the name is fully qualified.
Definition: ResourceName.h:208
Commonly used exception classes.
const ResourceName & operator=(const std::string &other)
Assignment for strings.
Definition: ResourceName.h:99
bool operator>(const ResourceName &other) const
Definition: ResourceName.h:154
Class for storing/combining/managing resource names consisting of namespaces and names separated by &#39;...
Definition: ResourceName.h:67
ResourceName(const ResourceName &other)
Definition: ResourceName.h:94
ResourceName parent() const
Returns parent namespace.
std::string leaf() const
Returns the leaf, i.e. the bottom most name (the string after the last / ).
void reflect(Reflector &r)
Definition: ResourceName.h:167
const std::string & str() const
Returns the underlying string containing the name.
Definition: ResourceName.h:122
std::string::value_type operator[](std::string::size_type i)
Returns the i-th character in the name.
Definition: ResourceName.h:163
bool isParentOf(const ResourceName &other) const
Returns true, if this node is the direct ancestor (parent) of "other" in the namespace hierarchy...
bool operator>=(const ResourceName &other) const
Definition: ResourceName.h:155
bool isSuccessorOf(const ResourceName &other) const
Returns true, if this node is a successor of "other" in the namespace hierarchy.
ResourceName()
Creates default empty resource name.
Definition: ResourceName.h:76
bool operator<(const ResourceName &other) const
Definition: ResourceName.h:152
bool isAncestorOf(const ResourceName &other) const
Returns true, if this node is an ancestor of "other" in the namespace hierarchy.
bool operator!=(const ResourceName &other) const
Definition: ResourceName.h:157
bool isChildOf(const ResourceName &other) const
Returns true, if this node is the direct successor (child) of "other" in the namespace hierarchy...
std::string operator/(const std::string &other) const
Concatenates two resource names separated by a &#39;/&#39;.
Definition: ResourceName.h:174