17#ifndef SDF_ELEMENT_HH_
18#define SDF_ELEMENT_HH_
39#pragma warning(disable: 4251)
47 inline namespace SDF_VERSION_NAMESPACE {
75 public std::enable_shared_from_this<Element>
112 public:
void SetName(
const std::string &_name);
169 const std::string &_prefix)
const;
191 bool _includeDefaultElements,
192 bool _includeDefaultAttributes,
202 const std::string &_prefix,
203 bool _includeDefaultElements,
204 bool _includeDefaultAttributes,
214 int _spacing,
int &_index)
const;
222 int _spacing,
int &_index)
const;
229 const std::string &_prefix,
239 const std::string &_prefix,
252 const std::string &_prefix,
253 bool _includeDefaultElements,
254 bool _includeDefaultAttributes,
269 const std::string &_prefix,
270 bool _includeDefaultElements,
271 bool _includeDefaultAttributes,
282 const std::string &_type,
283 const std::string &_defaultvalue,
285 const std::string &_description =
"");
296 const std::string &_type,
297 const std::string &_defaultvalue,
300 const std::string &_description =
"");
309 const std::string &_defaultValue,
bool _required,
310 const std::string &_description =
"");
320 const std::string &_defaultValue,
bool _required,
322 const std::string &_description =
"");
334 const std::string &_defaultValue,
bool _required,
335 const std::string &_minValue,
336 const std::string &_maxValue,
337 const std::string &_description =
"");
350 const std::string &_defaultValue,
bool _required,
351 const std::string &_minValue,
352 const std::string &_maxValue,
354 const std::string &_description =
"");
418 public: std::any
GetAny(
const std::string &_key =
"")
const;
426 const std::string &_key =
"")
const;
435 public:
template<
typename T>
437 const std::string &_key =
"")
const;
445 public:
template<
typename T>
446 T Get(
const std::string &_key =
"")
const;
455 public:
template<
typename T>
457 const std::string &_key,
458 const T &_defaultValue)
const;
465 public:
template<
typename T>
466 std::pair<T, bool> Get(
const std::string &_key,
467 const T &_defaultValue)
const;
475 public:
template<
typename T>
476 bool Get(
const std::string &_key,
478 const T &_defaultValue)
const;
487 public:
template<
typename T>
489 const std::string &_key,
491 const T &_defaultValue)
const;
496 public:
template<
typename T>
497 bool Set(
const T &_value);
503 public:
template<
typename T>
551 const std::string &_type =
"")
const;
564 const std::string &_type,
565 const std::vector<std::string> &_ignoreElements)
const;
580 const std::string &_type,
581 const std::vector<std::string> &_ignoreElements)
const;
590 public: std::map<std::string, std::size_t>
601 public: std::map<std::string, std::size_t>
603 const std::string &_type =
"")
const;
616 const std::string &_type,
617 const std::vector<std::string> &_ignoreElements)
const;
632 const std::string &_type,
633 const std::vector<std::string> &_ignoreElements)
const;
827 std::ostringstream &_out,
828 const std::string &_prefix,
829 bool _includeDefaultElements,
830 bool _includeDefaultAttributes,
840 private:
void PrintValuesImpl(
sdf::Errors &_errors,
841 const std::string &_prefix,
842 bool _includeDefaultElements,
843 bool _includeDefaultAttributes,
845 std::ostringstream &_out)
const;
856 private:
ParamPtr CreateParam(
const std::string &_key,
857 const std::string &_type,
858 const std::string &_defaultValue,
861 const std::string &_description =
"");
864 private: std::unique_ptr<ElementPrivate> dataPtr;
944 std::ostringstream &_out)
const;
952 bool _includeDefaultAttributes,
954 std::ostringstream &_out)
const;
963 std::pair<T, bool> ret = this->Get<T>(_errors, _key, result);
974 std::pair<T, bool> ret = this->Get<T>(_key, result);
982 const std::string &_key,
984 const T &_defaultValue)
const
986 std::pair<T, bool> ret = this->Get<T>(_errors, _key, _defaultValue);
995 const T &_defaultValue)
const
997 std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
1003 template<
typename T>
1005 const T &_defaultValue)
const
1008 std::pair<T, bool> result = this->Get<T>(errors, _key, _defaultValue);
1009 for(
auto& error : errors)
1017 template<
typename T>
1019 const std::string &_key,
1020 const T &_defaultValue)
const
1022 std::pair<T, bool> result(_defaultValue,
true);
1024 if (_key.empty() && this->dataPtr->value)
1026 this->dataPtr->value->Get<T>(result.first, _errors);
1028 else if (!_key.empty())
1033 param->Get(result.first, _errors);
1045 result.second =
false;
1050 result.second =
false;
1057 template<
typename T>
1061 bool result = this->Set<T>(errors, _value);
1062 for(
auto& error : errors)
1070 template<
typename T>
1073 if (this->dataPtr->value)
1075 return this->dataPtr->value->Set(_value, _errors);
Definition: Element.hh:870
bool explicitlySetInFile
True if the element was set in the SDF file.
Definition: Element.hh:930
ElementWeakPtr parent
Element's parent.
Definition: Element.hh:884
std::string description
Element description.
Definition: Element.hh:878
void PrintAttributes(bool _includeDefaultAttributes, const PrintConfig &_config, std::ostringstream &_out) const
Generate the string (XML) for the attributes.
ElementPtr_V elements
Definition: Element.hh:893
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:921
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:881
Param_V attributes
Definition: Element.hh:887
std::string required
True if element is required.
Definition: Element.hh:875
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:890
std::string name
Element name.
Definition: Element.hh:872
ElementPtr_V elementDescriptions
Definition: Element.hh:896
std::string originalVersion
Spec version that this was originally parsed from.
Definition: Element.hh:927
std::string xmlPath
XML path of this element.
Definition: Element.hh:936
std::string path
Path to file where this element came from.
Definition: Element.hh:924
std::optional< int > lineNumber
Line number in file where this element came from.
Definition: Element.hh:933
ElementPtr includeElement
The element that was used to load this entity.
Definition: Element.hh:918
SDF Element class.
Definition: Element.hh:76
void SetOriginalVersion(const std::string &_version)
Set the spec version that this was originally parsed from.
void Copy(const ElementPtr _elem)
Copy values from an Element.
const std::string & XmlPath() const
Get the XML path of this element.
void RemoveChild(ElementPtr _child)
Remove a child element.
std::string GetDescription() const
Get a text description of the element.
const std::string & FilePath() const
Get the path to the SDF document where this element came from.
std::map< std::string, std::size_t > CountNamedElements(sdf::Errors &_errors, const std::string &_type="") const
Count the number of child elements of the specified element type that have the same name attribute va...
size_t GetAttributeCount() const
Get the number of attributes.
const std::string & GetRequired() const
Get the requirement string.
ElementPtr GetNextElement(const std::string &_name="") const
Get the next sibling of this element.
ParamPtr GetValue() const
Get the param of the elements value return A Param pointer to the value of this element.
bool HasElementDescription(const std::string &_name) const
Return true if an element description exists.
void AddElementDescription(ElementPtr _elem)
Add a new element description.
void PrintValues(sdf::Errors &_errors, std::string _prefix, const PrintConfig &_config=PrintConfig()) const
Output Element's values to stdout.
void Clear()
Remove all child elements and reset file path and original version.
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.
void Reset()
Call reset on each element and element description before deleting all of them.
std::string ReferenceSDF() const
Get the name of the reference SDF element.
std::optional< int > LineNumber() const
Get the line number of this element within the SDF document.
bool HasUniqueChildNames(sdf::Errors &_errors, 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 Update()
Call the Update() callback on each element, as well as the embedded Param.
bool HasUniqueChildNames(sdf::Errors &_errors, const std::string &_type="") const
Checks whether any child elements of the specified element type have identical name attribute values ...
void PrintValues(const std::string &_prefix, bool _includeDefaultElements, bool _includeDefaultAttributes, const PrintConfig &_config=PrintConfig()) const
Output Element's values to stdout.
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...
void SetLineNumber(int _lineNumber)
Set the line number of this element within the SDF document.
ElementPtr AddElement(const std::string &_name, sdf::Errors &_errors)
Add a named element.
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.
const std::string & OriginalVersion() const
Get the spec version that this was originally parsed from.
void RemoveAttribute(const std::string &_key)
Remove an attribute.
void SetCopyChildren(bool _value)
Set whether this element should copy its child elements during parsing.
void InsertElement(ElementPtr _elem)
Add an element object.
bool GetAttributeSet(const std::string &_key) const
Return true if the attribute was set (i.e.
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.
const std::string & GetName() const
Get the Element's name.
void Copy(const ElementPtr _elem, sdf::Errors &_errors)
Copy values from an Element.
void SetIncludeElement(sdf::ElementPtr _includeElem)
Set the element that was used to load this element.
void ClearElements()
Remove all child elements.
void Update(sdf::Errors &_errors)
Call the Update() callback on each element, as well as the embedded Param.
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.
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 SetExplicitlySetInFile(const bool _value)
Set if the element and children where set or default in the original file.
void InsertElement(ElementPtr _elem, bool _setParentToSelf)
Add an element object, and optionally set the given element's parent to this object.
ElementPtr GetElementImpl(const std::string &_name) const
Get a pointer to the named element.
ElementPtr GetElementDescription(const std::string &_key) const
Get an element description using a key.
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.
std::any GetAny(sdf::Errors &_errors, const std::string &_key="") const
Get the element value/attribute as a std::any.
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.
ElementPtr GetElementDescription(unsigned int _index) const
Get an element description using an index.
sdf::ElementPtr GetIncludeElement() const
Get the element that was used to load this element.
void SetXmlPath(const std::string &_path)
Private data pointer.
void RemoveFromParent()
Remove this element from its parent.
bool GetExplicitlySetInFile() const
Return if the element was been explicitly set in the file.
std::any GetAny(const std::string &_key="") const
Get the element value/attribute as a std::any.
std::set< std::string > GetElementTypeNames() const
Get set of child element type names.
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.
bool HasAttribute(const std::string &_key) const
Return true if an attribute exists.
bool HasElement(const std::string &_name) const
Return true if the named element exists.
void PrintDocLeftPane(std::string &_html, int _spacing, int &_index) const
Helper function for SDF::PrintDoc.
void SetDescription(const std::string &_desc)
Set a text description for the element.
ElementPtr Clone(sdf::Errors &_errors) const
Create a copy of this Element.
ParamPtr GetAttribute(unsigned int _index) const
Get an attribute using an index.
bool HasUniqueChildNames(const std::string &_type="") const
Checks whether any child elements of the specified element type have identical name attribute values ...
ElementPtr AddElement(const std::string &_name)
Add a named element.
ElementPtr Clone() const
Create a copy of this Element.
std::map< std::string, std::size_t > CountNamedElements(sdf::Errors &_errors, 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::string ToString(const std::string &_prefix, const PrintConfig &_config=PrintConfig()) const
Convert the element values to a string representation.
ElementPtr GetElement(const std::string &_name, sdf::Errors &_errors)
Return a pointer to the child element with the provided name.
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.
void RemoveChild(ElementPtr _child, sdf::Errors &_errors)
Remove a child element.
virtual ~Element()
Destructor.
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 GetFirstElement() const
Get the first child element.
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.
ElementPtr GetParent() const
Get a pointer to this Element's parent.
static std::vector< std::string > NameUniquenessExceptions()
List of elements to which exceptions are made when checking for name uniqueness.
void SetFilePath(const std::string &_path)
Set the path to the SDF document where this element came from.
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
T Get(sdf::Errors &_errors, const std::string &_key="") const
Get the value of a key.
Definition: Element.hh:959
bool Set(const T &_value)
Set the value of this element.
Definition: Element.hh:1058
void GZ_SDFORMAT_VISIBLE throwOrPrintError(sdf::Console::ConsoleStream &_out, const sdf::Error &_error)
Prints the error to _out or throw using SDF_ASSERT depending on the ErrorCode in _error.
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:67
std::shared_ptr< const Element > ElementConstPtr
Definition: Element.hh:59
std::vector< Error > Errors
A vector of Error.
Definition: Types.hh:95
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:63
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:55
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