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 <any>
21#include <map>
22#include <memory>
23#include <set>
24#include <string>
25#include <utility>
26#include <vector>
27
28#include "sdf/Param.hh"
29#include "sdf/PrintConfig.hh"
30#include "sdf/sdf_config.h"
31#include "sdf/system_util.hh"
32#include "sdf/Types.hh"
33
34#ifdef _WIN32
35// Disable warning C4251 which is triggered by
36// std::enable_shared_from_this
37#pragma warning(push)
38#pragma warning(disable: 4251)
39#endif
40
43namespace sdf
44{
45 // Inline bracket to help doxygen filtering.
46 inline namespace SDF_VERSION_NAMESPACE {
47 //
48
49 class ElementPrivate;
50 class SDFORMAT_VISIBLE Element;
51
54 typedef std::shared_ptr<Element> ElementPtr;
55
58 typedef std::shared_ptr<const Element> ElementConstPtr;
59
62 typedef std::weak_ptr<Element> ElementWeakPtr;
63
66 typedef std::vector<ElementPtr> ElementPtr_V;
67
70
74 public std::enable_shared_from_this<Element>
75 {
77 public: Element();
78
80 public: virtual ~Element();
81
84 public: ElementPtr Clone() const;
85
88 public: void Copy(const ElementPtr _elem);
89
93 public: ElementPtr GetParent() const;
94
97 public: void SetParent(const ElementPtr _parent);
98
101 public: void SetName(const std::string &_name);
102
105 public: const std::string &GetName() const;
106
114 public: void SetRequired(const std::string &_req);
115
119 public: const std::string &GetRequired() const;
120
126 public: void SetExplicitlySetInFile(const bool _value);
127
130 public: bool GetExplicitlySetInFile() const;
131
135 public: void SetCopyChildren(bool _value);
136
140 public: bool GetCopyChildren() const;
141
144 public: void SetReferenceSDF(const std::string &_value);
145
148 public: std::string ReferenceSDF() const;
149
152 public: void PrintDescription(const std::string &_prefix) const;
153
157 public: void PrintValues(std::string _prefix,
158 const PrintConfig &_config = PrintConfig()) const;
159
165 public: void PrintValues(const std::string &_prefix,
166 bool _includeDefaultElements,
167 bool _includeDefaultAttributes,
168 const PrintConfig &_config = PrintConfig()) const;
169
176 public: void PrintDocLeftPane(std::string &_html,
177 int _spacing, int &_index) const;
178
184 public: void PrintDocRightPane(std::string &_html,
185 int _spacing, int &_index) const;
186
191 public: std::string ToString(
192 const std::string &_prefix,
193 const PrintConfig &_config = PrintConfig()) const;
194
204 public: std::string ToString(
205 const std::string &_prefix,
206 bool _includeDefaultElements,
207 bool _includeDefaultAttributes,
208 const PrintConfig &_config = PrintConfig()) const;
209
217 public: void AddAttribute(const std::string &_key,
218 const std::string &_type,
219 const std::string &_defaultvalue,
220 bool _required,
221 const std::string &_description = "");
222
229 public: void AddValue(const std::string &_type,
230 const std::string &_defaultValue, bool _required,
231 const std::string &_description = "");
232
242 public: void AddValue(const std::string &_type,
243 const std::string &_defaultValue, bool _required,
244 const std::string &_minValue,
245 const std::string &_maxValue,
246 const std::string &_description = "");
247
251 public: ParamPtr GetAttribute(const std::string &_key) const;
252
255 public: size_t GetAttributeCount() const;
256
259 public: const Param_V &GetAttributes() const;
260
264 public: ParamPtr GetAttribute(unsigned int _index) const;
265
268 public: size_t GetElementDescriptionCount() const;
269
273 public: ElementPtr GetElementDescription(unsigned int _index) const;
274
278 public: ElementPtr GetElementDescription(const std::string &_key) const;
279
283 public: bool HasElementDescription(const std::string &_name) const;
284
288 public: bool HasAttribute(const std::string &_key) const;
289
293 public: bool GetAttributeSet(const std::string &_key) const;
294
297 public: void RemoveAttribute(const std::string &_key);
298
300 public: void RemoveAllAttributes();
301
304 public: ParamPtr GetValue() const;
305
310 public: std::any GetAny(const std::string &_key = "") const;
311
318 public: template<typename T>
319 T Get(const std::string &_key = "") const;
320
327 public: template<typename T>
328 std::pair<T, bool> Get(const std::string &_key,
329 const T &_defaultValue) const;
330
337 public: template<typename T>
338 bool Get(const std::string &_key,
339 T &_param,
340 const T &_defaultValue) const;
341
345 public: template<typename T>
346 bool Set(const T &_value);
347
351 public: bool HasElement(const std::string &_name) const;
352
357
369 public: ElementPtr GetNextElement(const std::string &_name = "") const;
370
373 public: std::set<std::string> GetElementTypeNames() const;
374
382 public: bool HasUniqueChildNames(const std::string &_type = "") const;
383
395 const std::string &_type,
396 const std::vector<std::string> &_ignoreElements) const;
397
405 public: std::map<std::string, std::size_t>
406 CountNamedElements(const std::string &_type = "") const;
407
418 public: std::map<std::string, std::size_t> CountNamedElements(
419 const std::string &_type,
420 const std::vector<std::string> &_ignoreElements) const;
421
432 public: ElementPtr GetElement(const std::string &_name);
433
443 public: ElementPtr FindElement(const std::string &_name);
444
454 public: ElementConstPtr FindElement(const std::string &_name) const;
455
459 public: ElementPtr AddElement(const std::string &_name);
460
463 public: void InsertElement(ElementPtr _elem);
464
466 public: void RemoveFromParent();
467
470 public: void RemoveChild(ElementPtr _child);
471
473 public: void ClearElements();
474
477 public: void Clear();
478
481 public: void Update();
482
486 public: void Reset();
487
493 public: void SetIncludeElement(sdf::ElementPtr _includeElem);
494
500
503 public: void SetFilePath(const std::string &_path);
504
507 public: const std::string &FilePath() const;
508
511 public: void SetLineNumber(int _lineNumber);
512
516 public: std::optional<int> LineNumber() const;
517
532 public: void SetXmlPath(const std::string &_path);
533
536 public: const std::string &XmlPath() const;
537
540 public: void SetOriginalVersion(const std::string &_version);
541
544 public: const std::string &OriginalVersion() const;
545
548 public: std::string GetDescription() const;
549
552 public: void SetDescription(const std::string &_desc);
553
557
561 public: ElementPtr GetElementImpl(const std::string &_name) const;
562
566 public: static std::vector<std::string> NameUniquenessExceptions();
567
574 private: void ToString(const std::string &_prefix,
575 bool _includeDefaultElements,
576 bool _includeDefaultAttributes,
577 const PrintConfig &_config,
578 std::ostringstream &_out) const;
579
586 private: void PrintValuesImpl(const std::string &_prefix,
587 bool _includeDefaultElements,
588 bool _includeDefaultAttributes,
589 const PrintConfig &_config,
590 std::ostringstream &_out) const;
591
600 private: ParamPtr CreateParam(const std::string &_key,
601 const std::string &_type,
602 const std::string &_defaultValue,
603 bool _required,
604 const std::string &_description = "");
605
606
608 private: std::unique_ptr<ElementPrivate> dataPtr;
609 };
610
614 {
616 public: std::string name;
617
619 public: std::string required;
620
622 public: std::string description;
623
625 public: bool copyChildren;
626
629
630 // Attributes of this element
632
633 // Value of this element
635
636 // The existing child elements
638
639 // The possible child elements
641
663
665 public: std::string referenceSDF;
666
668 public: std::string path;
669
671 public: std::string originalVersion;
672
675
677 public: std::optional<int> lineNumber;
678
680 public: std::string xmlPath;
681 };
682
684 template<typename T>
685 T Element::Get(const std::string &_key) const
686 {
687 T result = T();
688
689 std::pair<T, bool> ret = this->Get<T>(_key, result);
690
691 return ret.first;
692 }
693
695 template<typename T>
696 bool Element::Get(const std::string &_key,
697 T &_param,
698 const T &_defaultValue) const
699 {
700 std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
701 _param = ret.first;
702 return ret.second;
703 }
704
706 template<typename T>
707 std::pair<T, bool> Element::Get(const std::string &_key,
708 const T &_defaultValue) const
709 {
710 std::pair<T, bool> result(_defaultValue, true);
711
712 if (_key.empty() && this->dataPtr->value)
713 {
714 this->dataPtr->value->Get<T>(result.first);
715 }
716 else if (!_key.empty())
717 {
718 ParamPtr param = this->GetAttribute(_key);
719 if (param)
720 {
721 param->Get(result.first);
722 }
723 else if (this->HasElement(_key))
724 {
725 result.first = this->GetElementImpl(_key)->Get<T>();
726 }
727 else if (this->HasElementDescription(_key))
728 {
729 result.first = this->GetElementDescription(_key)->Get<T>();
730 }
731 else
732 {
733 result.second = false;
734 }
735 }
736 else
737 {
738 result.second = false;
739 }
740
741 return result;
742 }
743
745 template<typename T>
746 bool Element::Set(const T &_value)
747 {
748 if (this->dataPtr->value)
749 {
750 this->dataPtr->value->Set(_value);
751 return true;
752 }
753 return false;
754 }
756 }
757}
758
759#ifdef _WIN32
760#pragma warning(pop)
761#endif
762
763#endif
Definition Element.hh:614
bool explicitlySetInFile
True if the element was set in the SDF file.
Definition Element.hh:674
std::string path
Path to file where this element came from.
Definition Element.hh:668
ElementPtr_V elementDescriptions
Definition Element.hh:640
std::string originalVersion
Spec version that this was originally parsed from.
Definition Element.hh:671
std::string description
Element description.
Definition Element.hh:622
std::string name
Element name.
Definition Element.hh:616
std::string required
True if element is required.
Definition Element.hh:619
std::string xmlPath
XML path of this element.
Definition Element.hh:680
bool copyChildren
True if element's children should be copied.
Definition Element.hh:625
std::optional< int > lineNumber
Line number in file where this element came from.
Definition Element.hh:677
ElementPtr includeElement
The element that was used to load this entity.
Definition Element.hh:662
ElementWeakPtr parent
Element's parent.
Definition Element.hh:628
Param_V attributes
Definition Element.hh:631
std::string referenceSDF
Name of reference sdf.
Definition Element.hh:665
ParamPtr value
Definition Element.hh:634
ElementPtr_V elements
Definition Element.hh:637
SDF Element class.
Definition Element.hh:75
ElementPtr GetElementDescription(const std::string &_key) const
Get an element description using a key.
bool HasUniqueChildNames(const std::string &_type, const std::vector< std::string > &_ignoreElements) const
Checks whether any child elements of the specified element type, except those listed in _ignoreElemen...
void RemoveChild(ElementPtr _child)
Remove a child element.
const std::string & XmlPath() const
Get the XML path of this element.
bool HasElementDescription(const std::string &_name) const
Return true if an element description exists.
std::optional< int > LineNumber() const
Get the line number of this element within the SDF document.
ElementPtr FindElement(const std::string &_name)
Return a pointer to the child element with the provided name.
ElementPtr GetElement(const std::string &_name)
Return a pointer to the child element with the provided name.
ElementConstPtr FindElement(const std::string &_name) const
Return a pointer to the child element with the provided name.
void PrintDescription(const std::string &_prefix) const
Output Element's description to stdout.
const std::string & GetName() const
Get the Element's name.
ParamPtr GetAttribute(unsigned int _index) const
Get an attribute using an index.
void SetRequired(const std::string &_req)
Set the requirement type.
bool GetAttributeSet(const std::string &_key) const
Return true if the attribute was set (i.e.
bool HasAttribute(const std::string &_key) const
Return true if an attribute exists.
void RemoveFromParent()
Remove this element from its parent.
std::string ReferenceSDF() const
Get the name of the reference SDF element.
bool HasElement(const std::string &_name) const
Return true if the named element exists.
void PrintDocRightPane(std::string &_html, int _spacing, int &_index) const
Helper function for SDF::PrintDoc.
bool GetExplicitlySetInFile() const
Return if the element was been explicitly set in the file.
void SetDescription(const std::string &_desc)
Set a text description for the element.
void PrintValues(std::string _prefix, const PrintConfig &_config=PrintConfig()) const
Output Element's values to stdout.
void SetParent(const ElementPtr _parent)
Set the parent of this Element.
std::map< std::string, std::size_t > CountNamedElements(const std::string &_type, const std::vector< std::string > &_ignoreElements) const
Count the number of child elements of the specified element type that have the same name attribute va...
std::set< std::string > GetElementTypeNames() const
Get set of child element type names.
bool HasUniqueChildNames(const std::string &_type="") const
Checks whether any child elements of the specified element type have identical name attribute values ...
void SetXmlPath(const std::string &_path)
Private data pointer.
ElementPtr GetFirstElement() const
Get the first child element.
std::string ToString(const std::string &_prefix, const PrintConfig &_config=PrintConfig()) const
Convert the element values to a string representation.
void SetReferenceSDF(const std::string &_value)
Set reference SDF element.
std::string ToString(const std::string &_prefix, bool _includeDefaultElements, bool _includeDefaultAttributes, const PrintConfig &_config=PrintConfig()) const
Convert the element values to a string representation.
Element()
Constructor.
void PrintValues(const std::string &_prefix, bool _includeDefaultElements, bool _includeDefaultAttributes, const PrintConfig &_config=PrintConfig()) const
Output Element's values to stdout.
const std::string & OriginalVersion() const
Get the spec version that this was originally parsed from.
ParamPtr GetValue() const
Get the param of the elements value return A Param pointer to the value of this element.
void AddElementDescription(ElementPtr _elem)
Add a new element description.
ElementPtr AddElement(const std::string &_name)
Add a named element.
ElementPtr GetElementImpl(const std::string &_name) const
Get a pointer to the named element.
void Update()
Call the Update() callback on each element, as well as the embedded Param.
void SetOriginalVersion(const std::string &_version)
Set the spec version that this was originally parsed from.
void Clear()
Remove all child elements and reset file path and original version.
sdf::ElementPtr GetIncludeElement() const
Get the element that was used to load this element.
const Param_V & GetAttributes() const
Get all the attribute params.
std::string GetDescription() const
Get a text description of the element.
void SetFilePath(const std::string &_path)
Set the path to the SDF document where this element came from.
void AddAttribute(const std::string &_key, const std::string &_type, const std::string &_defaultvalue, bool _required, const std::string &_description="")
Add an attribute value.
ElementPtr GetElementDescription(unsigned int _index) const
Get an element description using an index.
const std::string & GetRequired() const
Get the requirement string.
size_t GetAttributeCount() const
Get the number of attributes.
void AddValue(const std::string &_type, const std::string &_defaultValue, bool _required, const std::string &_minValue, const std::string &_maxValue, const std::string &_description="")
Add a value to this Element.
static std::vector< std::string > NameUniquenessExceptions()
List of elements to which exceptions are made when checking for name uniqueness.
ElementPtr GetNextElement(const std::string &_name="") const
Get the next sibling of this element.
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...
void RemoveAllAttributes()
Removes all attributes.
void SetLineNumber(int _lineNumber)
Set the line number of this element within the SDF document.
void SetCopyChildren(bool _value)
Set whether this element should copy its child elements during parsing.
bool GetCopyChildren() const
Return true if this Element's child elements should be copied during parsing.
const std::string & FilePath() const
Get the path to the SDF document where this element came from.
void AddValue(const std::string &_type, const std::string &_defaultValue, bool _required, const std::string &_description="")
Add a value to this Element.
ElementPtr GetParent() const
Get a pointer to this Element's parent.
void RemoveAttribute(const std::string &_key)
Remove an attribute.
std::any GetAny(const std::string &_key="") const
Get the element value/attribute as a std::any.
void InsertElement(ElementPtr _elem)
Add an element object.
ParamPtr GetAttribute(const std::string &_key) const
Get the param of an attribute.
size_t GetElementDescriptionCount() const
Get the number of element descriptions.
void Reset()
Call reset on each element and element description before deleting all of them.
ElementPtr Clone() const
Create a copy of this Element.
virtual ~Element()
Destructor.
void ClearElements()
Remove all child elements.
void SetExplicitlySetInFile(const bool _value)
Set if the element and children where set or default in the original file.
void PrintDocLeftPane(std::string &_html, int _spacing, int &_index) const
Helper function for SDF::PrintDoc.
void Copy(const ElementPtr _elem)
Copy values from an Element.
void SetIncludeElement(sdf::ElementPtr _includeElem)
Set the element that was used to load this element.
void SetName(const std::string &_name)
Set the name of the Element.
This class contains configuration options for printing elements.
Definition PrintConfig.hh:32
std::weak_ptr< Element > ElementWeakPtr
Definition Element.hh:62
std::vector< ElementPtr > ElementPtr_V
Definition Element.hh:66
std::shared_ptr< Element > ElementPtr
Definition Element.hh:54
std::shared_ptr< const Element > ElementConstPtr
Definition Element.hh:58
std::vector< ParamPtr > Param_V
Definition Param.hh:69
std::shared_ptr< Param > ParamPtr
Definition Param.hh:65
namespace for Simulation Description Format parser
Definition Actor.hh:34
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition system_util.hh:41