RcsPySim
A robot control and simulation library
PropertySource.h
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2020, Fabio Muratore, Honda Research Institute Europe GmbH, and
3  Technical University of Darmstadt.
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  3. Neither the name of Fabio Muratore, Honda Research Institute Europe GmbH,
14  or Technical University of Darmstadt, nor the names of its contributors may
15  be used to endorse or promote products derived from this software without
16  specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL FABIO MURATORE, HONDA RESEARCH INSTITUTE EUROPE GMBH,
22  OR TECHNICAL UNIVERSITY OF DARMSTADT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
30 
31 #ifndef _PROPERTYSOURCE_H_
32 #define _PROPERTYSOURCE_H_
33 
34 #include <Rcs_MatNd.h>
35 
36 #include <string>
37 #include <vector>
38 
39 namespace Rcs
40 {
41 
42 /**
43  * Base class for a source of configuration properties.
44  * Basically stores values of different types by name.
45  */
47 {
48 public:
50 
51  virtual ~PropertySource();
52 
53  /**
54  * Check if this property source exists in the underlying storage.
55  */
56  virtual bool exists() = 0;
57 
58  /**
59  * Read a string value.
60  *
61  * @param[out] out storage for read value
62  * @param[in] property name of property to read
63  * @return true if the property was read successfully
64  * @throws std::exception if the property exists, but couldn't be converted
65  */
66  virtual bool getProperty(std::string& out, const char* property) = 0;
67 
68  /**
69  * Read a string list value.
70  *
71  * @param[out] out storage for read value
72  * @param[in] property name of property to read
73  * @return true if the property was read successfully
74  * @throws std::exception if the property exists, but couldn't be converted
75  */
76  virtual bool getProperty(std::vector<std::string>& out, const char* property) = 0;
77 
78  /**
79  * Read a double value.
80  *
81  * @param[out] out storage for read value
82  * @param[in] property name of property to read
83  * @return true if the property was read successfully, false if it doesn't exist
84  * @throws std::exception if the property exists, but couldn't be converted
85  */
86  virtual bool getProperty(double& out, const char* property) = 0;
87 
88  /**
89  * Read an int value.
90  *
91  * @param[out] out storage for read value
92  * @param[in] property name of property to read
93  * @return true if the property was read successfully, false if it doesn't exist
94  * @throws std::exception if the property exists, but couldn't be converted
95  */
96  virtual bool getProperty(int& out, const char* property) = 0;
97 
98  /**
99  * Read a vector/matrix value.
100  * The variable out should be a pointer and will be set to a newly created MatNd* on success.
101  *
102  * @param[out] out storage for read value
103  * @param[in] property name of property to read
104  * @return true if the property was read successfully, false if it doesn't exist
105  * @throws std::exception if the property exists, but couldn't be converted
106  */
107  virtual bool getProperty(MatNd*& out, const char* property) = 0;
108 
109 
110  /**
111  * Read a boolean value, returning a default value if not found.
112  * This interface differs from the others to ease usability
113  *
114  * @param[in] property name of property to read
115  * @param[in] def value to return if property doesn't exist
116  * @return true if the property was read successfully
117  * @throws std::exception if the property exists, but couldn't be converted
118  */
119  virtual bool getPropertyBool(const char* property, bool def = false) = 0;
120 
121  /**
122  * Obtain a child property source.
123  *
124  * The exact meaning of this depends on the implementation. For an Xml document, it could be a child element.
125  * For a python dict, it could be a dict-typed entry.
126  *
127  * The returned object is owned by the parent.
128  */
129  virtual PropertySource* getChild(const char* prefix) = 0;
130 
131  /**
132  * Obtain a list of child property sources. The exact meaning of this depends on the implementation.
133  * For an Xml document, it could be child elements with the same tag name.
134  * For a python dict, it could be a list of dicts.
135  *
136  * The returned objects are owned by the parent.
137  */
138  virtual const std::vector<PropertySource*>& getChildList(const char* prefix) = 0;
139 
140  /**
141  * Create an independent copy of this property source.
142  * @return a copy of this property source. Must take ownership.
143  */
144  virtual PropertySource* clone() const = 0;
145 
146  /**
147  * Save this property source as xml file.
148  * @param[in] fileName name of the file to write
149  * @param[in] rootNodeName name of the xml root node
150  */
151  virtual void saveXML(const char* fileName, const char* rootNodeName) = 0;
152 
153  /**
154  * A singleton property source without entries.
155  */
156  static PropertySource* empty();
157 };
158 
159 } /* namespace Rcs */
160 
161 #endif /* _PROPERTYSOURCE_H_ */
virtual bool getProperty(std::string &out, const char *property)=0
virtual bool getPropertyBool(const char *property, bool def=false)=0
virtual PropertySource * getChild(const char *prefix)=0
virtual const std::vector< PropertySource * > & getChildList(const char *prefix)=0
static PropertySource * empty()
virtual PropertySource * clone() const =0
virtual bool exists()=0
virtual void saveXML(const char *fileName, const char *rootNodeName)=0