MIRA
GraphElement.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2013,
3  Neuroinformatics and Cognitive Robotics Labs at TU Ilmenau, Germany
4 
5 All rights reserved.
6 
7 Copying, resale, or redistribution, with or without modification, is strictly
8 prohibited.
9 
10 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
11 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
12 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
13 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
14 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES,
15 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 */
17 
26 #pragma once
27 
28 #include <string>
29 
30 #include <platform/Types.h>
31 #include <factory/Factory.h>
32 
33 #include <topomap/TopoMapFwd.h>
34 
35 namespace viros { namespace topomap {
36 
38 
41 {
42 public:
43  virtual ~GraphElement() {}
44 
45  template<typename Reflector>
46  void reflect(Reflector& r)
47  {
48  // nothing
49  }
50 
51  virtual GraphElementPtr findElement(const std::string& predicate) {
52  return GraphElementPtr();
53  }
54 
55  virtual const GraphElementPtr findElement(const std::string& predicate) const {
56  return findElement(predicate);
57  }
58 };
59 
61 {
62 public:
63 
64  NamedGraphElement(const std::string& prefix, int);
65  NamedGraphElement(const std::string& name) : mName(name) {}
66 
67  template<typename Reflector>
68  void reflect(Reflector& r)
69  {
71  r.member("Name", mName, "");
72  }
73 
74 public:
75 
76  virtual void setName(const std::string& name) {
77  mName = name;
78  }
79 
80  virtual const std::string& getName() const {
81  return mName;
82  }
83 
84 protected:
85  std::string mName;
86 
87  static volatile uint32 sID;
88 };
89 
90 
92 {
93 public:
94 
95  AttributedGraphElement(const std::string& prefix, int) : NamedGraphElement(prefix, 0) {}
96  AttributedGraphElement(const std::string& name) : NamedGraphElement(name) {}
97 
98  virtual ~AttributedGraphElement();
99 
100  template<typename Reflector>
101  void reflect(Reflector& r)
102  {
104  r.member("Attributes", mAttributes, "");
105  }
106 
107 public:
108 
112  virtual void setAttribute(const std::string& name, AttributePtr attribute);
113 
115  virtual void removeAttribute(const std::string& name, const mira::Class& attrClass);
116 
118  template <typename AttributeClass>
119  void removeAttribute(const std::string& name) {
120  removeAttribute(name, AttributeClass::CLASS());
121  }
122 
124  virtual AttributePtr getAttributeUntyped(const std::string& name, const mira::Class& clazz, bool inherited=true);
125 
127  template <typename AttributeClass>
128  boost::shared_ptr<AttributeClass> getAttribute(const std::string& name, bool inherited=true) {
129  return boost::dynamic_pointer_cast<AttributeClass>(getAttributeUntyped(name, AttributeClass::CLASS(), inherited));
130  }
131 
132  virtual void getAllAttributesUntyped(std::map<std::string, AttributePtr>& oAttributes, const mira::Class& clazz, bool inherited=true);
133 
134  template <typename AttributeClass>
135  std::map<std::string, boost::shared_ptr<AttributeClass>> getAllAttributes(bool inherited=true) {
136  std::map<std::string, AttributePtr> attributes;
137  typedef boost::shared_ptr<AttributeClass> AttributeClassPtr;
138  std::map<std::string, AttributeClassPtr> tattributes;
139 
140  getAllAttributesUntyped(attributes, AttributeClass::CLASS(), inherited);
141  foreach(auto& p, attributes)
142  tattributes[p.first] = boost::dynamic_pointer_cast<AttributeClass>(p.second);
143  return tattributes;
144  }
145 
153  template <typename AttributeClass>
154  boost::shared_ptr<AttributeClass> obtainAttribute(const std::string& name) {
155  boost::shared_ptr<AttributeClass> obj = getAttribute<AttributeClass>(name,false);
156  if(!obj) {
157  obj = boost::make_shared<AttributeClass>();
158  setAttribute(name, obj);
159  }
160  return obj;
161  }
162 
163 
164 protected:
165 
166 
168  {
169  std::string name;
170  std::string clazz;
171 
172  template<typename Reflector>
173  void reflect(Reflector& r)
174  {
175  r.member("name", name, "");
176  r.member("classid", clazz, "");
177  }
178 
179  bool operator<(const AttributeKey& other) const {
180  if(clazz == other.clazz)
181  return name < other.name;
182  return clazz < other.clazz;
183  }
184  };
185  std::map<AttributeKey, AttributePtr> mAttributes;
186 
187 };
188 
190 
191 } } // namespace
std::string mName
Definition: GraphElement.h:85
virtual const GraphElementPtr findElement(const std::string &predicate) const
Definition: GraphElement.h:55
NamedGraphElement(const std::string &name)
Definition: GraphElement.h:65
boost::shared_ptr< AttributeClass > obtainAttribute(const std::string &name)
Similar to getAttribute, but this method will create the attribute, if it does not exist yet...
Definition: GraphElement.h:154
bool operator<(const AttributeKey &other) const
Definition: GraphElement.h:179
virtual void setAttribute(const std::string &name, AttributePtr attribute)
Sets the given object as attribute with the given name.
uint32_t uint32
std::map< std::string, boost::shared_ptr< AttributeClass > > getAllAttributes(bool inherited=true)
Definition: GraphElement.h:135
std::string name
Definition: GraphElement.h:169
boost::shared_ptr< Attribute > AttributePtr
Definition: TopoMapFwd.h:73
virtual void getAllAttributesUntyped(std::map< std::string, AttributePtr > &oAttributes, const mira::Class &clazz, bool inherited=true)
NamedGraphElement(const std::string &prefix, int)
virtual ~GraphElement()
Definition: GraphElement.h:43
AttributedGraphElement(const std::string &prefix, int)
Definition: GraphElement.h:95
void reflect(Reflector &r)
Definition: GraphElement.h:46
virtual AttributePtr getAttributeUntyped(const std::string &name, const mira::Class &clazz, bool inherited=true)
Returns NULL, if no such attribute exsits.
virtual const std::string & getName() const
Definition: GraphElement.h:80
empty base class for all graph elements (Nodes, GatewayNodes and Links)
Definition: GraphElement.h:40
virtual void removeAttribute(const std::string &name, const mira::Class &attrClass)
Removes the attribute with the given name and type.
void reflect(Reflector &r)
Definition: GraphElement.h:173
std::string clazz
Definition: GraphElement.h:170
boost::shared_ptr< GraphElement > GraphElementPtr
Definition: TopoMapFwd.h:80
AttributedGraphElement(const std::string &name)
Definition: GraphElement.h:96
void removeAttribute(const std::string &name)
same as above, but easier interface, where datatype is specified as template parameter ...
Definition: GraphElement.h:119
Definition: MetricCostmapTopoMapPlanner.h:45
std::map< AttributeKey, AttributePtr > mAttributes
Definition: GraphElement.h:185
Forward decls.
virtual GraphElementPtr findElement(const std::string &predicate)
Definition: GraphElement.h:51
virtual void setName(const std::string &name)
Definition: GraphElement.h:76
void reflect(Reflector &r)
Definition: GraphElement.h:101
static volatile uint32 sID
Definition: GraphElement.h:87
Definition: GraphElement.h:60
void reflect(Reflector &r)
Definition: GraphElement.h:68
Definition: GraphElement.h:91
boost::shared_ptr< AttributeClass > getAttribute(const std::string &name, bool inherited=true)
Returns NULL, if no such attribute exsits.
Definition: GraphElement.h:128