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 <memory>
21#include <string>
22#include <utility>
23#include <vector>
24
25#include "sdf/Param.hh"
26#include "sdf/system_util.hh"
27#include "sdf/Types.hh"
28
29#ifdef _WIN32
30// Disable warning C4251 which is triggered by
31// std::enable_shared_from_this
32#pragma warning(push)
33#pragma warning(disable: 4251)
34#endif
35
38namespace sdf
39{
40 class ElementPrivate;
42
45 typedef std::shared_ptr<Element> ElementPtr;
46
49 typedef std::weak_ptr<Element> ElementWeakPtr;
50
53 typedef std::vector<ElementPtr> ElementPtr_V;
54
57
61 public std::enable_shared_from_this<Element>
62 {
64 public: Element();
65
67 public: virtual ~Element();
68
71 public: ElementPtr Clone() const;
72
75 public: void Copy(const ElementPtr _elem);
76
80 public: ElementPtr GetParent() const;
81
84 public: void SetParent(const ElementPtr _parent);
85
88 public: void SetName(const std::string &_name);
89
92 public: const std::string &GetName() const;
93
100 public: void SetRequired(const std::string &_req);
101
105 public: const std::string &GetRequired() const;
106
110 public: void SetCopyChildren(bool _value);
111
115 public: bool GetCopyChildren() const;
116
119 public: void SetReferenceSDF(const std::string &_value);
120
123 public: std::string ReferenceSDF() const;
124
127 public: void PrintDescription(const std::string &_prefix) const;
128
131 public: void PrintValues(std::string _prefix) const;
132
139 public: void PrintDocLeftPane(std::string &_html,
140 int _spacing, int &_index) const;
141
147 public: void PrintDocRightPane(std::string &_html,
148 int _spacing, int &_index) const;
149
153 public: std::string ToString(const std::string &_prefix) const;
154
162 public: void AddAttribute(const std::string &_key,
163 const std::string &_type,
164 const std::string &_defaultvalue,
165 bool _required,
166 const std::string &_description="");
167
174 public: void AddValue(const std::string &_type,
175 const std::string &_defaultValue, bool _required,
176 const std::string &_description="");
177
181 public: ParamPtr GetAttribute(const std::string &_key) const;
182
185 public: size_t GetAttributeCount() const;
186
190 public: ParamPtr GetAttribute(unsigned int _index) const;
191
194 public: size_t GetElementDescriptionCount() const;
195
199 public: ElementPtr GetElementDescription(unsigned int _index) const;
200
204 public: ElementPtr GetElementDescription(const std::string &_key) const;
205
209 public: bool HasElementDescription(const std::string &_name) const;
210
214 public: bool HasAttribute(const std::string &_key) const;
215
219 public: bool GetAttributeSet(const std::string &_key) const;
220
223 public: ParamPtr GetValue() const;
224
229 public: boost::any GetAny(const std::string &_key = "") const;
230
237 public: template<typename T>
238 T Get(const std::string &_key = "") const;
239
246 public: template<typename T>
247 std::pair<T, bool> Get(const std::string &_key,
248 const T &_defaultValue) const;
249
256 public: template<typename T>
257 bool Get(const std::string &_key,
258 T &_param,
259 const T &_defaultValue) const;
260
264 public: template<typename T>
265 bool Set(const T &_value);
266
270 public: bool HasElement(const std::string &_name) const;
271
276
288 public: ElementPtr GetNextElement(const std::string &_name = "") const;
289
299 public: ElementPtr GetElement(const std::string &_name);
300
304 public: ElementPtr AddElement(const std::string &_name);
305
308 public: void InsertElement(ElementPtr _elem);
309
311 public: void RemoveFromParent();
312
315 public: void RemoveChild(ElementPtr _child);
316
318 public: void ClearElements();
319
322 public: void Update();
323
327 public: void Reset();
328
331 public: void SetInclude(const std::string &_filename);
332
335 public: std::string GetInclude() const;
336
339 public: std::string GetDescription() const;
340
343 public: void SetDescription(const std::string &_desc);
344
348
352 public: ElementPtr GetElementImpl(const std::string &_name) const;
353
357 private: void ToString(const std::string &_prefix,
358 std::ostringstream &_out) const;
359
363 private: void PrintValuesImpl(const std::string &_prefix,
364 std::ostringstream &_out) const;
365
374 private: ParamPtr CreateParam(const std::string &_key,
375 const std::string &_type,
376 const std::string &_defaultValue,
377 bool _required,
378 const std::string &_description="");
379
380
382 private: std::unique_ptr<ElementPrivate> dataPtr;
383 };
384
388 {
390 public: std::string name;
391
393 public: std::string required;
394
396 public: std::string description;
397
399 public: bool copyChildren;
400
403
404 // Attributes of this element
406
407 // Value of this element
409
410 // The existing child elements
412
413 // The possible child elements
415
417 public: std::string includeFilename;
418
420 public: std::string referenceSDF;
421 };
422
424 template<typename T>
425 T Element::Get(const std::string &_key) const
426 {
427 T result = T();
428
429 std::pair<T, bool> ret = this->Get<T>(_key, result);
430
431 return ret.first;
432 }
433
435 template<typename T>
436 bool Element::Get(const std::string &_key,
437 T &_param,
438 const T &_defaultValue) const
439 {
440 std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
441 _param = ret.first;
442 return ret.second;
443 }
444
446 template<typename T>
447 std::pair<T, bool> Element::Get(const std::string &_key,
448 const T &_defaultValue) const
449 {
450 std::pair<T, bool> result(_defaultValue, true);
451
452 if (_key.empty() && this->dataPtr->value)
453 {
454 this->dataPtr->value->Get<T>(result.first);
455 }
456 else if (!_key.empty())
457 {
458 ParamPtr param = this->GetAttribute(_key);
459 if (param)
460 {
461 param->Get(result.first);
462 }
463 else if (this->HasElement(_key))
464 {
465 result.first = this->GetElementImpl(_key)->Get<T>();
466 }
467 else if (this->HasElementDescription(_key))
468 {
469 result.first = this->GetElementDescription(_key)->Get<T>();
470 }
471 else
472 {
473 result.second = false;
474 }
475 }
476 else
477 {
478 result.second = false;
479 }
480
481 return result;
482 }
483
485 template<typename T>
486 bool Element::Set(const T &_value)
487 {
488 if (this->dataPtr->value)
489 {
490 this->dataPtr->value->Set(_value);
491 return true;
492 }
493 return false;
494 }
496}
497
498#ifdef _WIN32
499#pragma warning(pop)
500#endif
501
502#endif
Definition: Element.hh:388
std::string required
True if element is required.
Definition: Element.hh:393
ElementPtr_V elements
Definition: Element.hh:411
Param_V attributes
Definition: Element.hh:405
std::string name
Element name.
Definition: Element.hh:390
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:420
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:417
ElementPtr_V elementDescriptions
Definition: Element.hh:414
ElementWeakPtr parent
Element's parent.
Definition: Element.hh:402
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:399
std::string description
Element description.
Definition: Element.hh:396
ParamPtr value
Definition: Element.hh:408
SDF Element class.
Definition: Element.hh:62
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.
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.
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.
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:425
bool Set(const T &_value)
Set the value of this element.
Definition: Element.hh:486
namespace for Simulation Description Format parser
Definition: Console.hh:36
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:53
std::vector< ParamPtr > Param_V
Definition: Param.hh:59
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:45
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:55
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:49
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48