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 <optional>
24#include <set>
25#include <string>
26#include <unordered_set>
27#include <utility>
28#include <vector>
29
30#include "sdf/Error.hh"
31#include "sdf/Param.hh"
32#include "sdf/PrintConfig.hh"
33#include "sdf/config.hh"
34#include "sdf/system_util.hh"
35#include "sdf/Types.hh"
36
37#ifdef _WIN32
38// Disable warning C4251 which is triggered by
39// std::enable_shared_from_this
40#pragma warning(push)
41#pragma warning(disable: 4251)
42#endif
43
46namespace sdf
47{
48 // Inline bracket to help doxygen filtering.
49 inline namespace SDF_VERSION_NAMESPACE {
50 //
51
52 class ElementPrivate;
54
57 typedef std::shared_ptr<Element> ElementPtr;
58
61 typedef std::shared_ptr<const Element> ElementConstPtr;
62
65 typedef std::weak_ptr<Element> ElementWeakPtr;
66
69 typedef std::vector<ElementPtr> ElementPtr_V;
70
73
77 public std::enable_shared_from_this<Element>
78 {
80 public: Element();
81
83 public: virtual ~Element();
84
87 public: ElementPtr Clone() const;
88
92 public: ElementPtr Clone(sdf::Errors &_errors) const;
93
96 public: void Copy(const ElementPtr _elem);
97
101 public: void Copy(const ElementPtr _elem, sdf::Errors &_errors);
102
106 public: ElementPtr GetParent() const;
107
110 public: void SetParent(const ElementPtr _parent);
111
114 public: void SetName(const std::string &_name);
115
118 public: const std::string &GetName() const;
119
127 public: void SetRequired(const std::string &_req);
128
132 public: const std::string &GetRequired() const;
133
139 public: void SetExplicitlySetInFile(const bool _value);
140
143 public: bool GetExplicitlySetInFile() const;
144
148 public: void SetCopyChildren(bool _value);
149
153 public: bool GetCopyChildren() const;
154
157 public: void SetReferenceSDF(const std::string &_value);
158
161 public: std::string ReferenceSDF() const;
162
165 public: void PrintDescription(const std::string &_prefix) const;
166
170 public: void PrintDescription(sdf::Errors &_errors,
171 const std::string &_prefix) const;
172
176 public: void PrintValues(std::string _prefix,
177 const PrintConfig &_config = PrintConfig()) const;
178
183 public: void PrintValues(sdf::Errors &_errors,
184 std::string _prefix,
185 const PrintConfig &_config = PrintConfig()) const;
186
192 public: void PrintValues(const std::string &_prefix,
193 bool _includeDefaultElements,
194 bool _includeDefaultAttributes,
195 const PrintConfig &_config = PrintConfig()) const;
196
203 public: void PrintValues(sdf::Errors &_errors,
204 const std::string &_prefix,
205 bool _includeDefaultElements,
206 bool _includeDefaultAttributes,
207 const PrintConfig &_config = PrintConfig()) const;
208
215 public: void PrintDocLeftPane(std::string &_html,
216 int _spacing, int &_index) const;
217
223 public: void PrintDocRightPane(std::string &_html,
224 int _spacing, int &_index) const;
225
230 public: std::string ToString(
231 const std::string &_prefix,
232 const PrintConfig &_config = PrintConfig()) const;
233
239 public: std::string ToString(
240 sdf::Errors &_errors,
241 const std::string &_prefix,
242 const PrintConfig &_config = PrintConfig()) const;
243
253 public: std::string ToString(
254 const std::string &_prefix,
255 bool _includeDefaultElements,
256 bool _includeDefaultAttributes,
257 const PrintConfig &_config = PrintConfig()) const;
258
269 public: std::string ToString(
270 sdf::Errors &_errors,
271 const std::string &_prefix,
272 bool _includeDefaultElements,
273 bool _includeDefaultAttributes,
274 const PrintConfig &_config = PrintConfig()) const;
275
283 public: void AddAttribute(const std::string &_key,
284 const std::string &_type,
285 const std::string &_defaultvalue,
286 bool _required,
287 const std::string &_description = "");
288
297 public: void AddAttribute(const std::string &_key,
298 const std::string &_type,
299 const std::string &_defaultvalue,
300 bool _required,
301 sdf::Errors &_errors,
302 const std::string &_description = "");
303
310 public: void AddValue(const std::string &_type,
311 const std::string &_defaultValue, bool _required,
312 const std::string &_description = "");
313
321 public: void AddValue(const std::string &_type,
322 const std::string &_defaultValue, bool _required,
323 sdf::Errors &_errors,
324 const std::string &_description = "");
325
335 public: void AddValue(const std::string &_type,
336 const std::string &_defaultValue, bool _required,
337 const std::string &_minValue,
338 const std::string &_maxValue,
339 const std::string &_description = "");
340
351 public: void AddValue(const std::string &_type,
352 const std::string &_defaultValue, bool _required,
353 const std::string &_minValue,
354 const std::string &_maxValue,
355 sdf::Errors &_errors,
356 const std::string &_description = "");
357
361 public: ParamPtr GetAttribute(const std::string &_key) const;
362
365 public: size_t GetAttributeCount() const;
366
369 public: const Param_V &GetAttributes() const;
370
374 public: ParamPtr GetAttribute(unsigned int _index) const;
375
378 public: size_t GetElementDescriptionCount() const;
379
384 public:
386 ElementPtr GetElementDescription(unsigned int _index) const;
387
392 public:
393 GZ_DEPRECATED(16)
394 ElementPtr GetElementDescription(const std::string &_key) const;
395
400 public: ElementConstPtr ElementDescription(unsigned int _index) const;
401
406 public: ElementConstPtr ElementDescription(const std::string &_key) const;
407
411 public: ElementPtr MutableElementDescription(unsigned int _index);
412
416 public: ElementPtr MutableElementDescription(const std::string &_key);
417
421 public: bool HasElementDescription(const std::string &_name) const;
422
426 public: bool HasAttribute(const std::string &_key) const;
427
431 public: bool GetAttributeSet(const std::string &_key) const;
432
435 public: void RemoveAttribute(const std::string &_key);
436
438 public: void RemoveAllAttributes();
439
442 public: ParamPtr GetValue() const;
443
448 public: std::any GetAny(const std::string &_key = "") const;
449
455 public: std::any GetAny(sdf::Errors &_errors,
456 const std::string &_key = "") const;
457
465 public: template<typename T>
466 T Get(sdf::Errors &_errors,
467 const std::string &_key = "") const;
468
475 public: template<typename T>
476 T Get(const std::string &_key = "") const;
477
485 public: template<typename T>
486 std::pair<T, bool> Get(sdf::Errors &_errors,
487 const std::string &_key,
488 const T &_defaultValue) const;
495 public: template<typename T>
496 std::pair<T, bool> Get(const std::string &_key,
497 const T &_defaultValue) const;
498
505 public: template<typename T>
506 bool Get(const std::string &_key,
507 T &_param,
508 const T &_defaultValue) const;
509
517 public: template<typename T>
518 bool Get(sdf::Errors &_errors,
519 const std::string &_key,
520 T &_param,
521 const T &_defaultValue) const;
522
526 public: template<typename T>
527 bool Set(const T &_value);
528
533 public: template<typename T>
534 bool Set(sdf::Errors &_errors, const T &_value);
535
539 public: bool HasElement(const std::string &_name) const;
540
544 public: ElementPtr GetFirstElement() const;
545
557 public: ElementPtr GetNextElement(const std::string &_name = "") const;
558
561 public: std::set<std::string> GetElementTypeNames() const;
562
570 public: bool HasUniqueChildNames(const std::string &_type = "") const;
571
580 public: bool HasUniqueChildNames(sdf::Errors &_errors,
581 const std::string &_type = "") const;
582
593 public: bool HasUniqueChildNames(
594 const std::string &_type,
595 const std::vector<std::string> &_ignoreElements) const;
596
608 public: bool HasUniqueChildNames(
609 sdf::Errors &_errors,
610 const std::string &_type,
611 const std::vector<std::string> &_ignoreElements) const;
612
620 public: std::map<std::string, std::size_t>
621 CountNamedElements(const std::string &_type = "") const;
622
631 public: std::map<std::string, std::size_t>
632 CountNamedElements(sdf::Errors &_errors,
633 const std::string &_type = "") const;
634
645 public: std::map<std::string, std::size_t> CountNamedElements(
646 const std::string &_type,
647 const std::vector<std::string> &_ignoreElements) const;
648
660 public: std::map<std::string, std::size_t> CountNamedElements(
661 sdf::Errors &_errors,
662 const std::string &_type,
663 const std::vector<std::string> &_ignoreElements) const;
664
675 public: ElementPtr GetElement(const std::string &_name);
676
688 public: ElementPtr GetElement(const std::string &_name,
689 sdf::Errors &_errors);
690
700 public: ElementPtr FindElement(const std::string &_name);
701
711 public: ElementConstPtr FindElement(const std::string &_name) const;
712
716 public: ElementPtr AddElement(const std::string &_name);
717
722 public: ElementPtr AddElement(const std::string &_name,
723 sdf::Errors &_errors);
724
727 public: void InsertElement(ElementPtr _elem);
728
734 public: void InsertElement(ElementPtr _elem, bool _setParentToSelf);
735
737 public: void RemoveFromParent();
738
741 public: void RemoveChild(ElementPtr _child);
742
746 public: void RemoveChild(ElementPtr _child, sdf::Errors &_errors);
747
749 public: void ClearElements();
750
753 public: void Clear();
754
757 public: void Update();
758
762 public: void Update(sdf::Errors &_errors);
763
767 public: void Reset();
768
774 public: void SetIncludeElement(sdf::ElementPtr _includeElem);
775
780 public: sdf::ElementPtr GetIncludeElement() const;
781
784 public: void SetFilePath(const std::string &_path);
785
788 public: const std::string &FilePath() const;
789
792 public: void SetLineNumber(int _lineNumber);
793
797 public: std::optional<int> LineNumber() const;
798
813 public: void SetXmlPath(const std::string &_path);
814
817 public: const std::string &XmlPath() const;
818
821 public: void SetOriginalVersion(const std::string &_version);
822
825 public: const std::string &OriginalVersion() const;
826
829 public: std::string GetDescription() const;
830
833 public: void SetDescription(const std::string &_desc);
834
837 public: void AddElementDescription(ElementPtr _elem);
838
842 public: ElementPtr GetElementImpl(const std::string &_name) const;
843
847 public: static std::vector<std::string> NameUniquenessExceptions();
848
856 private: void ToString(sdf::Errors &_errors,
857 std::ostringstream &_out,
858 const std::string &_prefix,
859 bool _includeDefaultElements,
860 bool _includeDefaultAttributes,
861 const PrintConfig &_config) const;
862
870 private: void PrintValuesImpl(sdf::Errors &_errors,
871 const std::string &_prefix,
872 bool _includeDefaultElements,
873 bool _includeDefaultAttributes,
874 const PrintConfig &_config,
875 std::ostringstream &_out) const;
876
886 private: ParamPtr CreateParam(const std::string &_key,
887 const std::string &_type,
888 const std::string &_defaultValue,
889 bool _required,
890 sdf::Errors &_errors,
891 const std::string &_description = "");
892
894 private: std::unique_ptr<ElementPrivate> dataPtr;
895 };
896
900 {
902 public: std::string name;
903
905 public: std::string required;
906
908 public: std::string description;
909
911 public: bool copyChildren;
912
915
916 // Attributes of this element
918
919 // Value of this element
921
922 // The existing child elements
924
925 // The possible child elements
927
949
951 public: std::string referenceSDF;
952
954 public: std::string path;
955
957 public: std::string originalVersion;
958
961
963 public: std::optional<int> lineNumber;
964
966 public: std::string xmlPath;
967
970 public: std::unordered_set<ElementConstPtr> clonedElementDescriptions;
971
976 public: void PrintAttributes(bool _includeDefaultAttributes,
977 const PrintConfig &_config,
978 std::ostringstream &_out) const;
979
985 public: void PrintAttributes(sdf::Errors &_errors,
986 bool _includeDefaultAttributes,
987 const PrintConfig &_config,
988 std::ostringstream &_out) const;
989
994 public:
995 std::optional<unsigned int> ElementDescriptionIndex(
996 const std::string &_key);
997 };
998
1000 template<typename T>
1001 T Element::Get(sdf::Errors &_errors, const std::string &_key) const
1002 {
1003 T result = T();
1004
1005 std::pair<T, bool> ret = this->Get<T>(_errors, _key, result);
1006
1007 return ret.first;
1008 }
1009
1011 template<typename T>
1012 T Element::Get(const std::string &_key) const
1013 {
1014 T result = T();
1015
1016 std::pair<T, bool> ret = this->Get<T>(_key, result);
1017
1018 return ret.first;
1019 }
1020
1022 template<typename T>
1023 bool Element::Get(sdf::Errors &_errors,
1024 const std::string &_key,
1025 T &_param,
1026 const T &_defaultValue) const
1027 {
1028 std::pair<T, bool> ret = this->Get<T>(_errors, _key, _defaultValue);
1029 _param = ret.first;
1030 return ret.second;
1031 }
1032
1034 template<typename T>
1035 bool Element::Get(const std::string &_key,
1036 T &_param,
1037 const T &_defaultValue) const
1038 {
1039 std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
1040 _param = ret.first;
1041 return ret.second;
1042 }
1043
1045 template<typename T>
1046 std::pair<T, bool> Element::Get(const std::string &_key,
1047 const T &_defaultValue) const
1048 {
1049 sdf::Errors errors;
1050 std::pair<T, bool> result = this->Get<T>(errors, _key, _defaultValue);
1051 for(auto& error : errors)
1052 {
1053 internal::throwOrPrintError(sdferr, error);
1054 }
1055 return result;
1056 }
1057
1059 template<typename T>
1060 std::pair<T, bool> Element::Get(sdf::Errors &_errors,
1061 const std::string &_key,
1062 const T &_defaultValue) const
1063 {
1064 std::pair<T, bool> result(_defaultValue, true);
1065
1066 if (_key.empty() && this->dataPtr->value)
1067 {
1068 this->dataPtr->value->Get<T>(result.first, _errors);
1069 }
1070 else if (!_key.empty())
1071 {
1072 ParamPtr param = this->GetAttribute(_key);
1073 if (param)
1074 {
1075 param->Get(result.first, _errors);
1076 }
1077 else if (this->HasElement(_key))
1078 {
1079 result.first = this->GetElementImpl(_key)->Get<T>(_errors);
1080 }
1081 else if (this->HasElementDescription(_key))
1082 {
1083 result.first = this->ElementDescription(_key)->Get<T>(_errors);
1084 }
1085 else
1086 {
1087 result.second = false;
1088 }
1089 }
1090 else
1091 {
1092 result.second = false;
1093 }
1094
1095 return result;
1096 }
1097
1099 template<typename T>
1100 bool Element::Set(const T &_value)
1101 {
1102 sdf::Errors errors;
1103 bool result = this->Set<T>(errors, _value);
1104 for(auto& error : errors)
1105 {
1106 internal::throwOrPrintError(sdferr, error);
1107 }
1108 return result;
1109 }
1110
1112 template<typename T>
1113 bool Element::Set(sdf::Errors &_errors, const T &_value)
1114 {
1115 if (this->dataPtr->value)
1116 {
1117 return this->dataPtr->value->Set(_value, _errors);
1118 }
1119 return false;
1120 }
1122 }
1123}
1124
1125#ifdef _WIN32
1126#pragma warning(pop)
1127#endif
1128
1129#endif
bool explicitlySetInFile
True if the element was set in the SDF file.
Definition Element.hh:960
ElementWeakPtr parent
Element's parent.
Definition Element.hh:914
std::string description
Element description.
Definition Element.hh:908
void PrintAttributes(bool _includeDefaultAttributes, const PrintConfig &_config, std::ostringstream &_out) const
Generate the string (XML) for the attributes.
ElementPtr_V elements
Definition Element.hh:923
std::unordered_set< ElementConstPtr > clonedElementDescriptions
Used to keep track of which element descriptions we have cloned in order to provide a mutable pointer...
Definition Element.hh:970
std::string referenceSDF
Name of reference sdf.
Definition Element.hh:951
bool copyChildren
True if element's children should be copied.
Definition Element.hh:911
Param_V attributes
Definition Element.hh:917
std::string required
True if element is required.
Definition Element.hh:905
void PrintAttributes(sdf::Errors &_errors, bool _includeDefaultAttributes, const PrintConfig &_config, std::ostringstream &_out) const
Generate the string (XML) for the attributes.
ParamPtr value
Definition Element.hh:920
std::string name
Element name.
Definition Element.hh:902
std::optional< unsigned int > ElementDescriptionIndex(const std::string &_key)
Helper function to get the index of an element description identified by a given key.
ElementPtr_V elementDescriptions
Definition Element.hh:926
std::string originalVersion
Spec version that this was originally parsed from.
Definition Element.hh:957
std::string xmlPath
XML path of this element.
Definition Element.hh:966
std::string path
Path to file where this element came from.
Definition Element.hh:954
std::optional< int > lineNumber
Line number in file where this element came from.
Definition Element.hh:963
ElementPtr includeElement
The element that was used to load this entity.
Definition Element.hh:948
SDF Element class.
Definition Element.hh:78
void Copy(const ElementPtr _elem)
Copy values from an Element.
size_t GetAttributeCount() const
Get the number of attributes.
const std::string & GetRequired() const
Get the requirement string.
void PrintValues(sdf::Errors &_errors, std::string _prefix, const PrintConfig &_config=PrintConfig()) const
Output Element's values to stdout.
void PrintDescription(sdf::Errors &_errors, const std::string &_prefix) const
Output Element's description to stdout.
std::string ToString(sdf::Errors &_errors, const std::string &_prefix, const PrintConfig &_config=PrintConfig()) const
Convert the element values to a string representation.
std::string ReferenceSDF() const
Get the name of the reference SDF element.
void PrintValues(const std::string &_prefix, bool _includeDefaultElements, bool _includeDefaultAttributes, const PrintConfig &_config=PrintConfig()) const
Output Element's values to stdout.
void SetRequired(const std::string &_req)
Set the requirement type.
std::string ToString(const std::string &_prefix, bool _includeDefaultElements, bool _includeDefaultAttributes, const PrintConfig &_config=PrintConfig()) const
Convert the element values to a string representation.
void SetCopyChildren(bool _value)
Set whether this element should copy its child elements during parsing.
const std::string & GetName() const
Get the Element's name.
void Copy(const ElementPtr _elem, sdf::Errors &_errors)
Copy values from an Element.
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.
void SetExplicitlySetInFile(const bool _value)
Set if the element and children where set or default in the original file.
void AddAttribute(const std::string &_key, const std::string &_type, const std::string &_defaultvalue, bool _required, sdf::Errors &_errors, const std::string &_description="")
Add an attribute value.
void SetParent(const ElementPtr _parent)
Set the parent of this Element.
void PrintDocRightPane(std::string &_html, int _spacing, int &_index) const
Helper function for SDF::PrintDoc.
GZ_DEPRECATED(16) ElementPtr GetElementDescription(unsigned int _index) const
Get an element description using an index.
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 GetExplicitlySetInFile() const
Return if the element was been explicitly set in the file.
void AddValue(const std::string &_type, const std::string &_defaultValue, bool _required, const std::string &_description="")
Add a value to this Element.
void PrintDescription(const std::string &_prefix) const
Output Element's description to stdout.
void PrintValues(std::string _prefix, const PrintConfig &_config=PrintConfig()) 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 AddValue(const std::string &_type, const std::string &_defaultValue, bool _required, sdf::Errors &_errors, const std::string &_description="")
Add a value to this Element.
void PrintDocLeftPane(std::string &_html, int _spacing, int &_index) const
Helper function for SDF::PrintDoc.
ElementPtr Clone(sdf::Errors &_errors) const
Create a copy of this Element.
ParamPtr GetAttribute(unsigned int _index) const
Get an attribute using an index.
ElementPtr Clone() const
Create a copy of this 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.
void PrintValues(sdf::Errors &_errors, const std::string &_prefix, bool _includeDefaultElements, bool _includeDefaultAttributes, const PrintConfig &_config=PrintConfig()) const
Output Element's values to stdout.
size_t GetElementDescriptionCount() const
Get the number of element descriptions.
void AddValue(const std::string &_type, const std::string &_defaultValue, bool _required, const std::string &_minValue, const std::string &_maxValue, sdf::Errors &_errors, const std::string &_description="")
Add a value to this Element.
const Param_V & GetAttributes() const
Get all the attribute params.
ElementPtr GetParent() const
Get a pointer to this Element's parent.
void SetName(const std::string &_name)
Set the name of the Element.
std::string ToString(sdf::Errors &_errors, const std::string &_prefix, bool _includeDefaultElements, bool _includeDefaultAttributes, const PrintConfig &_config=PrintConfig()) const
Convert the element values to a string representation.
This class contains configuration options for printing elements.
Definition PrintConfig.hh:33
#define sdferr
Output an error message.
Definition Console.hh:57
std::shared_ptr< Param > ParamPtr
Definition Param.hh:70
std::vector< ParamPtr > Param_V
Definition Param.hh:74
std::vector< ElementPtr > ElementPtr_V
Definition Element.hh:69
std::shared_ptr< const Element > ElementConstPtr
Definition Element.hh:61
std::vector< Error > Errors
A vector of Error.
Definition Types.hh:80
std::weak_ptr< Element > ElementWeakPtr
Definition Element.hh:65
std::shared_ptr< Element > ElementPtr
Definition Element.hh:57
namespace for Simulation Description Format parser
Definition Actor.hh:35
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition system_util.hh:25