Loading...
Searching...
No Matches
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 <map>
21#include <memory>
22#include <set>
23#include <string>
24#include <utility>
25#include <vector>
26
27#include "sdf/Param.hh"
28#include "sdf/system_util.hh"
29#include "sdf/Types.hh"
30
31#ifdef _WIN32
32// Disable warning C4251 which is triggered by
33// std::enable_shared_from_this
34#pragma warning(push)
35#pragma warning(disable: 4251)
36#endif
37
40namespace sdf
41{
42 class ElementPrivate;
44
47 typedef std::shared_ptr<Element> ElementPtr;
48
51 typedef std::weak_ptr<Element> ElementWeakPtr;
52
55 typedef std::vector<ElementPtr> ElementPtr_V;
56
59
63 public std::enable_shared_from_this<Element>
64 {
66 public: Element();
67
69 public: virtual ~Element();
70
73 public: ElementPtr Clone() const;
74
77 public: void Copy(const ElementPtr _elem);
78
82 public: ElementPtr GetParent() const;
83
86 public: void SetParent(const ElementPtr _parent);
87
90 public: void SetName(const std::string &_name);
91
94 public: const std::string &GetName() const;
95
102 public: void SetRequired(const std::string &_req);
103
107 public: const std::string &GetRequired() const;
108
112 public: void SetCopyChildren(bool _value);
113
117 public: bool GetCopyChildren() const;
118
121 public: void SetReferenceSDF(const std::string &_value);
122
125 public: std::string ReferenceSDF() const;
126
129 public: void PrintDescription(const std::string &_prefix) const;
130
133 public: void PrintValues(std::string _prefix) const;
134
141 public: void PrintDocLeftPane(std::string &_html,
142 int _spacing, int &_index) const;
143
149 public: void PrintDocRightPane(std::string &_html,
150 int _spacing, int &_index) const;
151
155 public: std::string ToString(const std::string &_prefix) const;
156
164 public: void AddAttribute(const std::string &_key,
165 const std::string &_type,
166 const std::string &_defaultvalue,
167 bool _required,
168 const std::string &_description="");
169
176 public: void AddValue(const std::string &_type,
177 const std::string &_defaultValue, bool _required,
178 const std::string &_description="");
179
183 public: ParamPtr GetAttribute(const std::string &_key) const;
184
187 public: size_t GetAttributeCount() const;
188
192 public: ParamPtr GetAttribute(unsigned int _index) const;
193
196 public: size_t GetElementDescriptionCount() const;
197
201 public: ElementPtr GetElementDescription(unsigned int _index) const;
202
206 public: ElementPtr GetElementDescription(const std::string &_key) const;
207
211 public: bool HasElementDescription(const std::string &_name) const;
212
216 public: bool HasAttribute(const std::string &_key) const;
217
221 public: bool GetAttributeSet(const std::string &_key) const;
222
225 public: ParamPtr GetValue() const;
226
231 public: boost::any GetAny(const std::string &_key = "") const;
232
239 public: template<typename T>
240 T Get(const std::string &_key = "") const;
241
248 public: template<typename T>
249 std::pair<T, bool> Get(const std::string &_key,
250 const T &_defaultValue) const;
251
258 public: template<typename T>
259 bool Get(const std::string &_key,
260 T &_param,
261 const T &_defaultValue) const;
262
266 public: template<typename T>
267 bool Set(const T &_value);
268
272 public: bool HasElement(const std::string &_name) const;
273
278
290 public: ElementPtr GetNextElement(const std::string &_name = "") const;
291
294 public: std::set<std::string> GetElementTypeNames() const;
295
303 public: bool HasUniqueChildNames(const std::string &_type = "") const;
304
312 public: std::map<std::string, std::size_t>
313 CountNamedElements(const std::string &_type = "") const;
314
324 public: ElementPtr GetElement(const std::string &_name);
325
329 public: ElementPtr AddElement(const std::string &_name);
330
333 public: void InsertElement(ElementPtr _elem);
334
336 public: void RemoveFromParent();
337
340 public: void RemoveChild(ElementPtr _child);
341
343 public: void ClearElements();
344
347 public: void Update();
348
352 public: void Reset();
353
356 public: void SetInclude(const std::string &_filename);
357
360 public: std::string GetInclude() const;
361
364 public: std::string GetDescription() const;
365
368 public: void SetDescription(const std::string &_desc);
369
373
377 public: ElementPtr GetElementImpl(const std::string &_name) const;
378
382 private: void ToString(const std::string &_prefix,
383 std::ostringstream &_out) const;
384
388 private: void PrintValuesImpl(const std::string &_prefix,
389 std::ostringstream &_out) const;
390
399 private: ParamPtr CreateParam(const std::string &_key,
400 const std::string &_type,
401 const std::string &_defaultValue,
402 bool _required,
403 const std::string &_description="");
404
405
407 private: std::unique_ptr<ElementPrivate> dataPtr;
408 };
409
413 {
415 public: std::string name;
416
418 public: std::string required;
419
421 public: std::string description;
422
424 public: bool copyChildren;
425
428
429 // Attributes of this element
431
432 // Value of this element
434
435 // The existing child elements
437
438 // The possible child elements
440
442 public: std::string includeFilename;
443
445 public: std::string referenceSDF;
446 };
447
449 template<typename T>
450 T Element::Get(const std::string &_key) const
451 {
452 T result = T();
453
454 std::pair<T, bool> ret = this->Get<T>(_key, result);
455
456 return ret.first;
457 }
458
460 template<typename T>
461 bool Element::Get(const std::string &_key,
462 T &_param,
463 const T &_defaultValue) const
464 {
465 std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
466 _param = ret.first;
467 return ret.second;
468 }
469
471 template<typename T>
472 std::pair<T, bool> Element::Get(const std::string &_key,
473 const T &_defaultValue) const
474 {
475 std::pair<T, bool> result(_defaultValue, true);
476
477 if (_key.empty() && this->dataPtr->value)
478 {
479 this->dataPtr->value->Get<T>(result.first);
480 }
481 else if (!_key.empty())
482 {
483 ParamPtr param = this->GetAttribute(_key);
484 if (param)
485 {
486 param->Get(result.first);
487 }
488 else if (this->HasElement(_key))
489 {
490 result.first = this->GetElementImpl(_key)->Get<T>();
491 }
492 else if (this->HasElementDescription(_key))
493 {
494 result.first = this->GetElementDescription(_key)->Get<T>();
495 }
496 else
497 {
498 result.second = false;
499 }
500 }
501 else
502 {
503 result.second = false;
504 }
505
506 return result;
507 }
508
510 template<typename T>
511 bool Element::Set(const T &_value)
512 {
513 if (this->dataPtr->value)
514 {
515 this->dataPtr->value->Set(_value);
516 return true;
517 }
518 return false;
519 }
520
521}
522
523#ifdef _WIN32
524#pragma warning(pop)
525#endif
526
527#endif
Definition Element.hh:413
std::string required
True if element is required.
Definition Element.hh:418
ElementPtr_V elements
Definition Element.hh:436
Param_V attributes
Definition Element.hh:430
std::string name
Element name.
Definition Element.hh:415
std::string referenceSDF
Name of reference sdf.
Definition Element.hh:445
std::string includeFilename
name of the include file that was used to create this element
Definition Element.hh:442
ElementPtr_V elementDescriptions
Definition Element.hh:439
ElementWeakPtr parent
Element's parent.
Definition Element.hh:427
bool copyChildren
True if element's children should be copied.
Definition Element.hh:424
std::string description
Element description.
Definition Element.hh:421
ParamPtr value
Definition Element.hh:433
SDF Element class.
Definition Element.hh:64
void SetRequired(const std::string &_req)
Set the requirement type.
void Reset()
Call reset on each element and element description before deleting all of them.
bool HasAttribute(const std::string &_key) const
Return true if an attribute exists.
ElementPtr GetFirstElement() const
Get the first child element.
Element()
Constructor.
ElementPtr GetParent() const
Get a pointer to this Element's parent.
void SetInclude(const std::string &_filename)
Set the include filename to the passed in filename.
void InsertElement(ElementPtr _elem)
Add an element object.
void SetDescription(const std::string &_desc)
Set a text description for the element.
bool HasElement(const std::string &_name) const
Return true if the named element exists.
void Copy(const ElementPtr _elem)
Copy values from an Element.
bool HasElementDescription(const std::string &_name) const
Return true if an element description exists.
void RemoveChild(ElementPtr _child)
Remove a child element.
virtual ~Element()
Destructor.
std::string ReferenceSDF() const
Get the name of the reference SDF element.
void SetName(const std::string &_name)
Set the name of the Element.
bool HasUniqueChildNames(const std::string &_type="") const
Checks whether any child elements of the specified element type have identical name attribute values ...
ElementPtr GetElement(const std::string &_name)
Return a pointer to the child element with the provided name.
const std::string & GetName() const
Get the Element's name.
std::map< std::string, std::size_t > CountNamedElements(const std::string &_type="") const
Count the number of child elements of the specified element type that have the same name attribute va...
ElementPtr AddElement(const std::string &_name)
Add a named element.
void PrintDocLeftPane(std::string &_html, int _spacing, int &_index) const
Helper function for SDF::PrintDoc.
void PrintDescription(const std::string &_prefix) const
Output Element's description to stdout.
void PrintValues(std::string _prefix) const
Output Element's values to stdout.
void AddAttribute(const std::string &_key, const std::string &_type, const std::string &_defaultvalue, bool _required, const std::string &_description="")
Add an attribute value.
void SetParent(const ElementPtr _parent)
Set the parent of this Element.
ParamPtr GetAttribute(unsigned int _index) const
Get an attribute using an index.
void AddValue(const std::string &_type, const std::string &_defaultValue, bool _required, const std::string &_description="")
Add a value to this Element.
std::string GetInclude() const
Get the include filename.
void AddElementDescription(ElementPtr _elem)
Add a new element description.
std::string ToString(const std::string &_prefix) const
Convert the element values to a string representation.
void SetReferenceSDF(const std::string &_value)
Set reference SDF element.
boost::any GetAny(const std::string &_key="") const
Get the element value/attribute as a boost::any.
void SetCopyChildren(bool _value)
Set whether this element should copy its child elements during parsing.
size_t GetElementDescriptionCount() const
Get the number of element descriptions.
ParamPtr GetValue() const
Get the param of the elements value return A Param pointer to the value of this element.
void ClearElements()
Remove all child elements.
ElementPtr GetNextElement(const std::string &_name="") const
Get the next sibling of this element.
ElementPtr GetElementDescription(unsigned int _index) const
Get an element description using an index.
std::set< std::string > GetElementTypeNames() const
Get set of child element type names.
void Update()
Call the Update() callback on each element, as well as the embedded Param.
std::string GetDescription() const
Get a text description of the element.
const std::string & GetRequired() const
Get the requirement string.
ElementPtr GetElementImpl(const std::string &_name) const
Get a pointer to the named element.
size_t GetAttributeCount() const
Get the number of attributes.
ElementPtr Clone() const
Create a copy of this Element.
void RemoveFromParent()
Remove this element from its parent.
ParamPtr GetAttribute(const std::string &_key) const
Get the param of an attribute.
bool GetCopyChildren() const
Return true if this Element's child elements should be copied during parsing.
bool GetAttributeSet(const std::string &_key) const
Return true if the attribute was set (i.e.
ElementPtr GetElementDescription(const std::string &_key) const
Get an element description using a key.
void PrintDocRightPane(std::string &_html, int _spacing, int &_index) const
Helper function for SDF::PrintDoc.
T Get(const std::string &_key="") const
Get the value of a key.
Definition Element.hh:450
bool Set(const T &_value)
Set the value of this element.
Definition Element.hh:511
namespace for Simulation Description Format parser
Definition Console.hh:36
std::vector< ElementPtr > ElementPtr_V
Definition Element.hh:55
std::vector< ParamPtr > Param_V
Definition Param.hh:64
std::shared_ptr< Element > ElementPtr
Definition Element.hh:47
std::shared_ptr< Param > ParamPtr
Definition Param.hh:60
std::weak_ptr< Element > ElementWeakPtr
Definition Element.hh:51
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition system_util.hh:48