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 
117  template <typename T>
118  void setProperty(const std::string& name, const T& value,
119  const Duration& timeout = Duration::milliseconds(100))
120  {
121  TypedPropertyNode<T>* node = getPropertyNode<T>(name);
122  if(node==NULL)
123  MIRA_THROW(XLogical, "The property '" << name << "' does not exist");
124  node->set(value, timeout);
125  }
126 
139  template <typename T>
140  T getProperty(const std::string& name) const
141  {
142  const TypedPropertyNode<T>* node = getPropertyNode<T>(name);
143  if(node==NULL)
144  MIRA_THROW(XLogical, "The property '" << name << "' does not exist");
145  return node->get();
146  }
147 
148 public:
149 
154  const PropertyNode* getRootNode() const {
155  return mRoot.get();
156  }
157 
162  return mRoot.get();
163  }
164 
175  const PropertyNode* getPropertyNode(const std::string& name) const {
176  return mRoot->findChildNode(name);
177  }
178 
182  PropertyNode* getPropertyNode(const std::string& name) {
183  return mRoot->findChildNode(name);
184  }
185 
197  template<typename T>
198  const TypedPropertyNode<T>* getPropertyNode(const std::string& name) const
199  {
200  const PropertyNode* node = getPropertyNode(name);
201  if (node == NULL)
202  return NULL;
203  if ( node->type() != typeName<T>() )
204  MIRA_THROW(XBadCast, "The requested type '"
205  << typeName<T>() << "' for property '" << name
206  << "' does not match its real type '"
207  << node->type() << "'");
208  return static_cast<const TypedPropertyNode<T>*>(node);
209  }
210 
214  template<typename T>
215  TypedPropertyNode<T>* getPropertyNode(const std::string& name)
216  {
217  const PropertyManager* This = this;
218  return const_cast<TypedPropertyNode<T>*>(This->getPropertyNode<T>(name));
219  }
220 
221 private:
222 
223  boost::scoped_ptr<PropertyNode> mRoot;
224 
225 };
226 
228 
229 } // namespace
230 
231 #endif
Abstract base class for all derived property node classes.
Definition: PropertyNode.h:212
tick_type milliseconds() const
Returns normalized number of milliseconds (0..999)
Definition: Time.h:285
const TypedPropertyNode< T > * getPropertyNode(const std::string &name) const
Returns a typed PropertyNode that represents the specified property.
Definition: PropertyManager.h:198
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:140
Get compiler and platform independent typenames.
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
TypedPropertyNode< T > * getPropertyNode(const std::string &name)
Returns the PropertyNode that represents the specified property.
Definition: PropertyManager.h:215
PropertyManager()
Definition: PropertyManager.h:86
const Typename & type() const
Returns the type of this property as Typename.
Definition: PropertyNode.h:151
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:175
Core class of the logging library.
Use this class to represent time durations.
Definition: Time.h:106
A special node that acts only as (empty) root node for a property tree.
Definition: PropertyNode.h:463
A special PropertyReflector that creates a PropertyNode for each reflected property.
Definition: PropertySerializer.h:68
const PropertyNode * getRootNode() const
Returns the root node of the managed property tree.
Definition: PropertyManager.h:154
void setProperty(const std::string &name, const T &value, const Duration &timeout=Duration::milliseconds(100))
Modifies the value of the specified property.
Definition: PropertyManager.h:118
PropertyNode * getRootNode()
Returns the root node of the managed property tree.
Definition: PropertyManager.h:161
virtual void set(const value_type &value, const Duration &timeout=Duration::milliseconds(100))=0
Sets the property to the specified value.
void reflectProperties(PropertyNode *root, const std::string &name, T &object)
Reflects the properties of the specified &#39;object&#39;.
Definition: PropertySerializer.h:221
PropertyNode * getPropertyNode(const std::string &name)
Returns the PropertyNode that represents the specified property.
Definition: PropertyManager.h:182
virtual value_type get() const =0
Returns the value of the property.
Abstract base class for all typed property nodes.
Definition: PropertyNode.h:72
The property manager maintains the properties of objects that are added to the manager.
Definition: PropertyManager.h:82