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 #include <fw/FrameworkExports.h>
58 
59 namespace mira {
60 
62 
69 {
70 private:
71 
73  ResourceName(const std::string& name, bool dontNormalize) : mName(name) {}
74 
75 public:
78 
80  ResourceName(const std::string& name) : mName(normalize(name)) {
81  if(!validate(mName))
82  MIRA_THROW(XInvalidParameter,
83  "Trying to create ResourceName from invalid name: '"
84  << mName << "'");
85  }
86 
88  ResourceName(const char* name) : mName(normalize(std::string(name))) {
89  if(!validate(mName))
90  MIRA_THROW(XInvalidParameter,
91  "Trying to create ResourceName from invalid name: '"
92  << mName << "'");
93  }
94 
95  ResourceName(const ResourceName& other) : mName(other.mName) {}
96 
97 public:
98 
100  const ResourceName& operator=(const std::string& other) {
101  mName = normalize(other);
102  return *this;
103  }
104 
105  const ResourceName& operator=(const ResourceName& other) {
106  mName = other.mName;
107  return *this;
108  }
109 
111 #if defined(MIRA_GNUC_VERSION)
112 # if MIRA_GNUC_VERSION >= 40601
113  operator std::string() const { return mName; }
114 # else
115  operator const std::string&() const { return mName; }
116 # endif
117 #else
118  // this should be the preferred version, that is supported by GCC < 4.6.1 and VC 2010
119  operator const std::string&() const { return mName; }
120 #endif
121 
123  const std::string& str() const { return mName; }
124 
125 public:
126 
132  ResourceName parent() const;
133 
135  std::string leaf() const;
136 
138  bool isAncestorOf(const ResourceName& other) const;
139 
141  bool isParentOf(const ResourceName& other) const;
142 
144  bool isSuccessorOf(const ResourceName& other) const;
145 
147  bool isChildOf(const ResourceName& other) const;
148 
149 public:
150 
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; }
158  bool operator!=(const ResourceName& other) const { return mName != other.mName; }
160 
161 public:
162 
164  std::string::value_type operator[](std::string::size_type i) { return mName[i]; }
165 
166 public:
167  template<typename Reflector>
168  void reflect(Reflector& r) {
169  r.delegate(mName);
170  }
171 
175  std::string operator/(const std::string& other) const {
176  if(isRoot()) // we are the root namespace
177  return mName + other;
178 
179  // concatenate both names
180  return mName + "/" + other;
181  }
182 
186  ResourceName operator/(const ResourceName& other) const {
187  return ResourceName(*this / other.mName, true);
188  }
189 
191  const ResourceName& operator/=(const ResourceName& other) {
192  if(isRoot())
193  mName += other.mName;
194  else
195  mName += "/" + other.mName;
196  return *this;
197  }
198 
202  bool isRoot() const {
203  return mName.size()==1 && mName[0]=='/';
204  }
205 
209  bool isFullyQualified() const {
210  if(mName.empty())
211  return false;
212 
213  return mName[0]=='/';
214  }
215 
216 public:
217 
218  static ResourceName makeFullyQualified(const ResourceName& name,
219  const ResourceName& ns);
220 
221 public:
222 
223  friend std::ostream& operator<<(std::ostream& os, const ResourceName& name)
224  {
225  os << name.mName;
226  return os;
227  }
228 
229  friend std::istream& operator>>(std::istream& is, ResourceName& name)
230  {
231  std::string s;
232  is >> s;
233  name = s;
234  return is;
235  }
236 
237 private:
238 
239  static std::string normalize(const std::string& name);
240 
242  static bool validate(const std::string& name);
243 
244 private:
246  std::string mName;
247 };
248 
250 
251 template<typename SerializerTag>
252 class IsTransparentSerializable<ResourceName,SerializerTag> : public std::true_type {};
253 
255 
256 }
257 
258 #endif
const ResourceName & operator=(const ResourceName &other)
Definition: ResourceName.h:105
bool isRoot() const
Returns true, if this is the root namespace "/".
Definition: ResourceName.h:202
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:88
ResourceName(const std::string &name)
Creates a normalized resource from a name.
Definition: ResourceName.h:80
bool operator<=(const ResourceName &other) const
Definition: ResourceName.h:154
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:229
ResourceName operator/(const ResourceName &other) const
Concatenates two resource names separated by a &#39;/&#39;.
Definition: ResourceName.h:186
STL namespace.
const ResourceName & operator/=(const ResourceName &other)
Concatenates this with another resource names separated by a &#39;/&#39;.
Definition: ResourceName.h:191
Provides type trait that indicates whether a type should be serialized "transparently".
Framework export macro declaration.
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
bool operator==(const ResourceName &other) const
Definition: ResourceName.h:157
friend std::ostream & operator<<(std::ostream &os, const ResourceName &name)
Definition: ResourceName.h:223
bool isFullyQualified() const
Returns true, if the name is fully qualified.
Definition: ResourceName.h:209
Commonly used exception classes.
const ResourceName & operator=(const std::string &other)
Assignment for strings.
Definition: ResourceName.h:100
bool operator>(const ResourceName &other) const
Definition: ResourceName.h:155
Class for storing/combining/managing resource names consisting of namespaces and names separated by &#39;...
Definition: ResourceName.h:68
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
ResourceName(const ResourceName &other)
Definition: ResourceName.h:95
void reflect(Reflector &r)
Definition: ResourceName.h:168
const std::string & str() const
Returns the underlying string containing the name.
Definition: ResourceName.h:123
std::string::value_type operator[](std::string::size_type i)
Returns the i-th character in the name.
Definition: ResourceName.h:164
bool operator>=(const ResourceName &other) const
Definition: ResourceName.h:156
ResourceName()
Creates default empty resource name.
Definition: ResourceName.h:77
bool operator<(const ResourceName &other) const
Definition: ResourceName.h:153
bool operator!=(const ResourceName &other) const
Definition: ResourceName.h:158
std::string operator/(const std::string &other) const
Concatenates two resource names separated by a &#39;/&#39;.
Definition: ResourceName.h:175