MIRA
PropertyManager.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_PROPERTYMANAGER_H_
48 #define _MIRA_PROPERTYMANAGER_H_
49 
50 #include <algorithm>
51 
52 #ifndef Q_MOC_RUN
53 #include <boost/scoped_ptr.hpp>
54 #include <boost/algorithm/string/split.hpp>
55 #endif
56 
57 #include <error/Exceptions.h>
58 #include <error/LoggingCore.h>
59 
60 #include <serialization/adapters/std/list>
62 
63 #include <platform/Typename.h>
64 
65 namespace mira {
66 
68 
83 {
84 public:
85 
87  {}
88 
95  template <typename T>
96  void addObject(const char* name, T& object) {
98  s.reflectProperties(mRoot.get(), name, object);
99  }
100 
101 public:
102 
115  template <typename T>
116  void setProperty(const std::string& name, const T& value)
117  {
118  TypedPropertyNode<T>* node = getPropertyNode<T>(name);
119  if(node==NULL)
120  MIRA_THROW(XLogical, "The property '" << name << "' does not exist");
121  node->set(value);
122  }
123 
136  template <typename T>
137  T getProperty(const std::string& name) const
138  {
139  const TypedPropertyNode<T>* node = getPropertyNode<T>(name);
140  if(node==NULL)
141  MIRA_THROW(XLogical, "The property '" << name << "' does not exist");
142  return node->get();
143  }
144 
145 public:
146 
151  const PropertyNode* getRootNode() const {
152  return mRoot.get();
153  }
154 
159  return mRoot.get();
160  }
161 
172  const PropertyNode* getPropertyNode(const std::string& name) const {
173  return mRoot->findChildNode(name);
174  }
175 
179  PropertyNode* getPropertyNode(const std::string& name) {
180  return mRoot->findChildNode(name);
181  }
182 
194  template<typename T>
195  const TypedPropertyNode<T>* getPropertyNode(const std::string& name) const
196  {
197  const PropertyNode* node = getPropertyNode(name);
198  if (node == NULL)
199  return NULL;
200  if ( node->type() != typeName<T>() )
201  MIRA_THROW(XBadCast, "The requested type '"
202  << typeName<T>() << "' for property '" << name
203  << "' does not match its real type '"
204  << node->type() << "'");
205  return static_cast<const TypedPropertyNode<T>*>(node);
206  }
207 
211  template<typename T>
212  TypedPropertyNode<T>* getPropertyNode(const std::string& name)
213  {
214  const PropertyManager* This = this;
215  return const_cast<TypedPropertyNode<T>*>(This->getPropertyNode<T>(name));
216  }
217 
218 private:
219 
220  boost::scoped_ptr<PropertyNode> mRoot;
221 
222 };
223 
225 
226 } // namespace
227 
228 #endif
Abstract base class for all derived property node classes.
Definition: PropertyNode.h:202
const TypedPropertyNode< T > * getPropertyNode(const std::string &name) const
Returns a typed PropertyNode that represents the specified property.
Definition: PropertyManager.h:195
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
T getProperty(const std::string &name) const
Obtains the value of the specified property.
Definition: PropertyManager.h:137
Get compiler and platform independent typenames.
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:81
TypedPropertyNode< T > * getPropertyNode(const std::string &name)
Returns the PropertyNode that represents the specified property.
Definition: PropertyManager.h:212
virtual void set(const value_type &value)=0
Sets the property to the specified value.
void setProperty(const std::string &name, const T &value)
Modifies the value of the specified property.
Definition: PropertyManager.h:116
PropertyManager()
Definition: PropertyManager.h:86
const Typename & type() const
Returns the type of this property as Typename.
Definition: PropertyNode.h:149
Commonly used exception classes.
Serializer that handles properties and creates PropertyNodes.
void addObject(const char *name, T &object)
Add the specified object to the PropertyManager under the specified name.
Definition: PropertyManager.h:96
const PropertyNode * getPropertyNode(const std::string &name) const
Returns the PropertyNode that represents the specified property.
Definition: PropertyManager.h:172
Core class of the logging library.
A special node that acts only as (empty) root node for a property tree.
Definition: PropertyNode.h:448
A special PropertyReflector that creates a PropertyNode for each reflected property.
Definition: PropertySerializer.h:67
const PropertyNode * getRootNode() const
Returns the root node of the managed property tree.
Definition: PropertyManager.h:151
PropertyNode * getRootNode()
Returns the root node of the managed property tree.
Definition: PropertyManager.h:158
void reflectProperties(PropertyNode *root, const std::string &name, T &object)
Reflects the properties of the specified &#39;object&#39;.
Definition: PropertySerializer.h:219
PropertyNode * getPropertyNode(const std::string &name)
Returns the PropertyNode that represents the specified property.
Definition: PropertyManager.h:179
virtual value_type get() const =0
Returns the value of the property.
Abstract base class for all typed property nodes.
Definition: PropertyNode.h:70
The property manager maintains the properties of objects that are added to the manager.
Definition: PropertyManager.h:82