All Classes Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
Element.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef _SDF_ELEMENT_HH_
18 #define _SDF_ELEMENT_HH_
19 
20 #include <string>
21 #include <vector>
22 #include <boost/shared_ptr.hpp>
23 #include <boost/enable_shared_from_this.hpp>
24 
25 #include "sdf/Param.hh"
26 #include "sdf/system_util.hh"
27 
29 #ifndef _WIN32
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32 #endif
33 #include "sdf/Types.hh"
34 #ifndef _WIN32
35 #pragma GCC diagnostic pop
36 #endif
37 
38 #ifdef _WIN32
39 // Disable warning C4251 which is triggered by
40 // boost::enable_shared_from_this
41 #pragma warning(push)
42 #pragma warning(disable: 4251)
43 #endif
44 
47 namespace sdf
48 {
49  class ElementPrivate;
51 
54  typedef boost::shared_ptr<Element> ElementPtr;
55 
58  typedef std::vector< ElementPtr > ElementPtr_V;
59 
62 
66  public boost::enable_shared_from_this<Element>
67  {
69  public: Element();
70 
72  public: virtual ~Element();
73 
76  public: boost::shared_ptr<Element> Clone() const;
77 
80  public: void Copy(const ElementPtr _elem);
81 
85  public: ElementPtr GetParent() const;
86 
89  public: void SetParent(const ElementPtr _parent);
90 
93  public: void SetName(const std::string &_name);
94 
97  public: const std::string &GetName() const;
98 
105  public: void SetRequired(const std::string &_req);
106 
110  public: const std::string &GetRequired() const;
111 
115  public: void SetCopyChildren(bool _value);
116 
120  public: bool GetCopyChildren() const;
121 
124  public: void PrintDescription(const std::string &_prefix);
125 
128  public: void PrintValues(std::string _prefix);
129 
130  public: void PrintWiki(std::string _prefix);
131 
138  public: void PrintDocLeftPane(std::string &_html,
139  int _spacing, int &_index);
140 
146  public: void PrintDocRightPane(std::string &_html,
147  int _spacing, int &_index);
148 
152  public: std::string ToString(const std::string &_prefix) const;
153 
160  public: void AddAttribute(const std::string &_key,
161  const std::string &_type,
162  const std::string &_defaultvalue,
163  bool _required,
164  const std::string &_description="");
165 
171  public: void AddValue(const std::string &_type,
172  const std::string &_defaultValue, bool _required,
173  const std::string &_description="");
174 
178  public: ParamPtr GetAttribute(const std::string &_key);
179 
181  public: size_t GetAttributeCount() const;
182 
184  public: ParamPtr GetAttribute(unsigned int _index) const;
185 
187  public: size_t GetElementDescriptionCount() const;
188 
190  public: ElementPtr GetElementDescription(unsigned int _index) const;
191 
193  public: ElementPtr GetElementDescription(const std::string &_key) const;
194 
196  public: bool HasElementDescription(const std::string &_name);
197 
198  public: bool HasAttribute(const std::string &_key);
199 
201  public: bool GetAttributeSet(const std::string &_key);
202 
204  public: ParamPtr GetValue();
205 
210  public: boost::any GetAny(const std::string &_key = "");
211 
212  public: template<typename T>
213  T Get(const std::string &_key = "");
214 
215  public: template<typename T>
216  bool Set(const T &_value);
217 
218  public: bool HasElement(const std::string &_name) const;
219 
220  public: ElementPtr GetElement(const std::string &_name) const;
221  public: ElementPtr GetFirstElement() const;
222 
223  public: ElementPtr GetNextElement(const std::string &_name = "") const;
224 
225  public: ElementPtr GetElement(const std::string &_name);
226  public: ElementPtr AddElement(const std::string &_name);
227  public: void InsertElement(ElementPtr _elem);
228 
230  public: void RemoveFromParent();
231 
234  public: void RemoveChild(ElementPtr _child);
235 
237  public: void ClearElements();
238 
239  public: void Update();
240  public: void Reset();
241 
242  public: void SetInclude(const std::string &_filename);
243  public: std::string GetInclude() const;
244 
246  public: std::string GetDescription() const;
247 
249  public: void SetDescription(const std::string &_desc);
250 
252  public: void AddElementDescription(ElementPtr _elem);
253 
254  public: ElementPtr GetElementImpl(const std::string &_name) const;
255 
256  private: void ToString(const std::string &_prefix,
257  std::ostringstream &_out) const;
258 
259 
260  private: boost::shared_ptr<Param> CreateParam(const std::string &_key,
261  const std::string &_type, const std::string &_defaultValue,
262  bool _required, const std::string &_description="");
263 
264 
266  private: ElementPrivate *dataPtr;
267  };
268 
272  {
274  public: std::string name;
275 
277  public: std::string required;
278 
280  public: std::string description;
281 
283  public: bool copyChildren;
284 
287 
288  // Attributes of this element
290 
291  // Value of this element
292  public: ParamPtr value;
293 
294  // The existing child elements
296 
297  // The possible child elements
299 
301  public: std::string includeFilename;
302  };
303 
305  template<typename T>
306  T Element::Get(const std::string &_key)
307  {
308  T result = T();
309 
310  if (_key.empty() && this->dataPtr->value)
311  this->dataPtr->value->Get<T>(result);
312  else if (!_key.empty())
313  {
314  ParamPtr param = this->GetAttribute(_key);
315  if (param)
316  param->Get(result);
317  else if (this->HasElement(_key))
318  result = this->GetElementImpl(_key)->Get<T>();
319  else if (this->HasElementDescription(_key))
320  result = this->GetElementDescription(_key)->Get<T>();
321  else
322  sdferr << "Unable to find value for key[" << _key << "]\n";
323  }
324  return result;
325  }
326 
328  template<typename T>
329  bool Element::Set(const T &_value)
330  {
331  if (this->dataPtr->value)
332  {
333  this->dataPtr->value->Set(_value);
334  return true;
335  }
336  return false;
337  }
339 }
340 
341 #ifdef _WIN32
342 #pragma warning(pop)
343 #endif
344 
345 #endif
ElementPtr_V elementDescriptions
Definition: Element.hh:298
Element * ElementPtr
Definition: Element.hh:54
bool Set(const T &_value)
Definition: Element.hh:329
Definition: Element.hh:271
A parameter class.
Definition: Param.hh:67
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:258
ElementPtr GetElementImpl(const std::string &_name) const
class SDFORMAT_VISIBLE Element
Definition: Element.hh:50
bool HasElementDescription(const std::string &_name)
Return true if an element description exists.
std::string name
Element name.
Definition: Element.hh:274
T Get(const std::string &_key="")
Definition: Element.hh:306
ElementPtr GetElementDescription(unsigned int _index) const
Get an element description using an index.
SDF Element class.
Definition: Element.hh:65
ElementPtr parent
Element's parent.
Definition: Element.hh:286
std::string required
True if element is required.
Definition: Element.hh:277
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:278
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
#define sdferr
Output an error message.
Definition: Console.hh:47
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:301
std::string description
Element description.
Definition: Element.hh:280
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:58
std::vector< ParamPtr > Param_V
Definition: Param.hh:60
bool HasElement(const std::string &_name) const
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:283
Param_V attributes
Definition: Element.hh:289
ParamPtr GetAttribute(const std::string &_key)
Get the param of an attribute.
ElementPtr_V elements
Definition: Element.hh:295
ParamPtr value
Definition: Element.hh:292