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/Error.hh"
29 #include "sdf/Param.hh"
30 #include "sdf/PrintConfig.hh"
31 #include "sdf/sdf_config.h"
32 #include "sdf/system_util.hh"
33 #include "sdf/Types.hh"
34 
35 #ifdef _WIN32
36 // Disable warning C4251 which is triggered by
37 // std::enable_shared_from_this
38 #pragma warning(push)
39 #pragma warning(disable: 4251)
40 #endif
41 
44 namespace sdf
45 {
46  // Inline bracket to help doxygen filtering.
47  inline namespace SDF_VERSION_NAMESPACE {
48  //
49 
50  class ElementPrivate;
52 
55  typedef std::shared_ptr<Element> ElementPtr;
56 
59  typedef std::shared_ptr<const Element> ElementConstPtr;
60 
63  typedef std::weak_ptr<Element> ElementWeakPtr;
64 
67  typedef std::vector<ElementPtr> ElementPtr_V;
68 
71 
75  public std::enable_shared_from_this<Element>
76  {
78  public: Element();
79 
81  public: virtual ~Element();
82 
85  public: ElementPtr Clone() const;
86 
90  public: ElementPtr Clone(sdf::Errors &_errors) const;
91 
94  public: void Copy(const ElementPtr _elem);
95 
99  public: void Copy(const ElementPtr _elem, sdf::Errors &_errors);
100 
104  public: ElementPtr GetParent() const;
105 
108  public: void SetParent(const ElementPtr _parent);
109 
112  public: void SetName(const std::string &_name);
113 
116  public: const std::string &GetName() const;
117 
125  public: void SetRequired(const std::string &_req);
126 
130  public: const std::string &GetRequired() const;
131 
137  public: void SetExplicitlySetInFile(const bool _value);
138 
141  public: bool GetExplicitlySetInFile() const;
142 
146  public: void SetCopyChildren(bool _value);
147 
151  public: bool GetCopyChildren() const;
152 
155  public: void SetReferenceSDF(const std::string &_value);
156 
159  public: std::string ReferenceSDF() const;
160 
163  public: void PrintDescription(const std::string &_prefix) const;
164 
168  public: void PrintDescription(sdf::Errors &_errors,
169  const std::string &_prefix) const;
170 
174  public: void PrintValues(std::string _prefix,
175  const PrintConfig &_config = PrintConfig()) const;
176 
181  public: void PrintValues(sdf::Errors &_errors,
182  std::string _prefix,
183  const PrintConfig &_config = PrintConfig()) const;
184 
190  public: void PrintValues(const std::string &_prefix,
191  bool _includeDefaultElements,
192  bool _includeDefaultAttributes,
193  const PrintConfig &_config = PrintConfig()) const;
194 
201  public: void PrintValues(sdf::Errors &_errors,
202  const std::string &_prefix,
203  bool _includeDefaultElements,
204  bool _includeDefaultAttributes,
205  const PrintConfig &_config = PrintConfig()) const;
206 
213  public: void PrintDocLeftPane(std::string &_html,
214  int _spacing, int &_index) const;
215 
221  public: void PrintDocRightPane(std::string &_html,
222  int _spacing, int &_index) const;
223 
228  public: std::string ToString(
229  const std::string &_prefix,
230  const PrintConfig &_config = PrintConfig()) const;
231 
237  public: std::string ToString(
238  sdf::Errors &_errors,
239  const std::string &_prefix,
240  const PrintConfig &_config = PrintConfig()) const;
241 
251  public: std::string ToString(
252  const std::string &_prefix,
253  bool _includeDefaultElements,
254  bool _includeDefaultAttributes,
255  const PrintConfig &_config = PrintConfig()) const;
256 
267  public: std::string ToString(
268  sdf::Errors &_errors,
269  const std::string &_prefix,
270  bool _includeDefaultElements,
271  bool _includeDefaultAttributes,
272  const PrintConfig &_config = PrintConfig()) const;
273 
281  public: void AddAttribute(const std::string &_key,
282  const std::string &_type,
283  const std::string &_defaultvalue,
284  bool _required,
285  const std::string &_description = "");
286 
295  public: void AddAttribute(const std::string &_key,
296  const std::string &_type,
297  const std::string &_defaultvalue,
298  bool _required,
299  sdf::Errors &_errors,
300  const std::string &_description = "");
301 
308  public: void AddValue(const std::string &_type,
309  const std::string &_defaultValue, bool _required,
310  const std::string &_description = "");
311 
319  public: void AddValue(const std::string &_type,
320  const std::string &_defaultValue, bool _required,
321  sdf::Errors &_errors,
322  const std::string &_description = "");
323 
333  public: void AddValue(const std::string &_type,
334  const std::string &_defaultValue, bool _required,
335  const std::string &_minValue,
336  const std::string &_maxValue,
337  const std::string &_description = "");
338 
349  public: void AddValue(const std::string &_type,
350  const std::string &_defaultValue, bool _required,
351  const std::string &_minValue,
352  const std::string &_maxValue,
353  sdf::Errors &_errors,
354  const std::string &_description = "");
355 
359  public: ParamPtr GetAttribute(const std::string &_key) const;
360 
363  public: size_t GetAttributeCount() const;
364 
367  public: const Param_V &GetAttributes() const;
368 
372  public: ParamPtr GetAttribute(unsigned int _index) const;
373 
376  public: size_t GetElementDescriptionCount() const;
377 
381  public: ElementPtr GetElementDescription(unsigned int _index) const;
382 
386  public: ElementPtr GetElementDescription(const std::string &_key) const;
387 
391  public: bool HasElementDescription(const std::string &_name) const;
392 
396  public: bool HasAttribute(const std::string &_key) const;
397 
401  public: bool GetAttributeSet(const std::string &_key) const;
402 
405  public: void RemoveAttribute(const std::string &_key);
406 
408  public: void RemoveAllAttributes();
409 
412  public: ParamPtr GetValue() const;
413 
418  public: std::any GetAny(const std::string &_key = "") const;
419 
425  public: std::any GetAny(sdf::Errors &_errors,
426  const std::string &_key = "") const;
427 
435  public: template<typename T>
436  T Get(sdf::Errors &_errors,
437  const std::string &_key = "") const;
438 
445  public: template<typename T>
446  T Get(const std::string &_key = "") const;
447 
455  public: template<typename T>
456  std::pair<T, bool> Get(sdf::Errors &_errors,
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;
468 
475  public: template<typename T>
476  bool Get(const std::string &_key,
477  T &_param,
478  const T &_defaultValue) const;
479 
487  public: template<typename T>
488  bool Get(sdf::Errors &_errors,
489  const std::string &_key,
490  T &_param,
491  const T &_defaultValue) const;
492 
496  public: template<typename T>
497  bool Set(const T &_value);
498 
503  public: template<typename T>
504  bool Set(sdf::Errors &_errors, const T &_value);
505 
509  public: bool HasElement(const std::string &_name) const;
510 
514  public: ElementPtr GetFirstElement() const;
515 
527  public: ElementPtr GetNextElement(const std::string &_name = "") const;
528 
531  public: std::set<std::string> GetElementTypeNames() const;
532 
540  public: bool HasUniqueChildNames(const std::string &_type = "") const;
541 
550  public: bool HasUniqueChildNames(sdf::Errors &_errors,
551  const std::string &_type = "") const;
552 
563  public: bool HasUniqueChildNames(
564  const std::string &_type,
565  const std::vector<std::string> &_ignoreElements) const;
566 
578  public: bool HasUniqueChildNames(
579  sdf::Errors &_errors,
580  const std::string &_type,
581  const std::vector<std::string> &_ignoreElements) const;
582 
590  public: std::map<std::string, std::size_t>
591  CountNamedElements(const std::string &_type = "") const;
592 
601  public: std::map<std::string, std::size_t>
602  CountNamedElements(sdf::Errors &_errors,
603  const std::string &_type = "") const;
604 
615  public: std::map<std::string, std::size_t> CountNamedElements(
616  const std::string &_type,
617  const std::vector<std::string> &_ignoreElements) const;
618 
630  public: std::map<std::string, std::size_t> CountNamedElements(
631  sdf::Errors &_errors,
632  const std::string &_type,
633  const std::vector<std::string> &_ignoreElements) const;
634 
645  public: ElementPtr GetElement(const std::string &_name);
646 
658  public: ElementPtr GetElement(const std::string &_name,
659  sdf::Errors &_errors);
660 
670  public: ElementPtr FindElement(const std::string &_name);
671 
681  public: ElementConstPtr FindElement(const std::string &_name) const;
682 
686  public: ElementPtr AddElement(const std::string &_name);
687 
692  public: ElementPtr AddElement(const std::string &_name,
693  sdf::Errors &_errors);
694 
697  public: void InsertElement(ElementPtr _elem);
698 
704  public: void InsertElement(ElementPtr _elem, bool _setParentToSelf);
705 
707  public: void RemoveFromParent();
708 
711  public: void RemoveChild(ElementPtr _child);
712 
716  public: void RemoveChild(ElementPtr _child, sdf::Errors &_errors);
717 
719  public: void ClearElements();
720 
723  public: void Clear();
724 
727  public: void Update();
728 
732  public: void Update(sdf::Errors &_errors);
733 
737  public: void Reset();
738 
744  public: void SetIncludeElement(sdf::ElementPtr _includeElem);
745 
750  public: sdf::ElementPtr GetIncludeElement() const;
751 
754  public: void SetFilePath(const std::string &_path);
755 
758  public: const std::string &FilePath() const;
759 
762  public: void SetLineNumber(int _lineNumber);
763 
767  public: std::optional<int> LineNumber() const;
768 
783  public: void SetXmlPath(const std::string &_path);
784 
787  public: const std::string &XmlPath() const;
788 
791  public: void SetOriginalVersion(const std::string &_version);
792 
795  public: const std::string &OriginalVersion() const;
796 
799  public: std::string GetDescription() const;
800 
803  public: void SetDescription(const std::string &_desc);
804 
807  public: void AddElementDescription(ElementPtr _elem);
808 
812  public: ElementPtr GetElementImpl(const std::string &_name) const;
813 
817  public: static std::vector<std::string> NameUniquenessExceptions();
818 
826  private: void ToString(sdf::Errors &_errors,
827  std::ostringstream &_out,
828  const std::string &_prefix,
829  bool _includeDefaultElements,
830  bool _includeDefaultAttributes,
831  const PrintConfig &_config) const;
832 
840  private: void PrintValuesImpl(sdf::Errors &_errors,
841  const std::string &_prefix,
842  bool _includeDefaultElements,
843  bool _includeDefaultAttributes,
844  const PrintConfig &_config,
845  std::ostringstream &_out) const;
846 
856  private: ParamPtr CreateParam(const std::string &_key,
857  const std::string &_type,
858  const std::string &_defaultValue,
859  bool _required,
860  sdf::Errors &_errors,
861  const std::string &_description = "");
862 
864  private: std::unique_ptr<ElementPrivate> dataPtr;
865  };
866 
870  {
872  public: std::string name;
873 
875  public: std::string required;
876 
878  public: std::string description;
879 
881  public: bool copyChildren;
882 
885 
886  // Attributes of this element
888 
889  // Value of this element
890  public: ParamPtr value;
891 
892  // The existing child elements
894 
895  // The possible child elements
897 
919 
921  public: std::string referenceSDF;
922 
924  public: std::string path;
925 
927  public: std::string originalVersion;
928 
930  public: bool explicitlySetInFile;
931 
933  public: std::optional<int> lineNumber;
934 
936  public: std::string xmlPath;
937 
942  public: void PrintAttributes(bool _includeDefaultAttributes,
943  const PrintConfig &_config,
944  std::ostringstream &_out) const;
945 
951  public: void PrintAttributes(sdf::Errors &_errors,
952  bool _includeDefaultAttributes,
953  const PrintConfig &_config,
954  std::ostringstream &_out) const;
955  };
956 
958  template<typename T>
959  T Element::Get(sdf::Errors &_errors, const std::string &_key) const
960  {
961  T result = T();
962 
963  std::pair<T, bool> ret = this->Get<T>(_errors, _key, result);
964 
965  return ret.first;
966  }
967 
969  template<typename T>
970  T Element::Get(const std::string &_key) const
971  {
972  T result = T();
973 
974  std::pair<T, bool> ret = this->Get<T>(_key, result);
975 
976  return ret.first;
977  }
978 
980  template<typename T>
981  bool Element::Get(sdf::Errors &_errors,
982  const std::string &_key,
983  T &_param,
984  const T &_defaultValue) const
985  {
986  std::pair<T, bool> ret = this->Get<T>(_errors, _key, _defaultValue);
987  _param = ret.first;
988  return ret.second;
989  }
990 
992  template<typename T>
993  bool Element::Get(const std::string &_key,
994  T &_param,
995  const T &_defaultValue) const
996  {
997  std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
998  _param = ret.first;
999  return ret.second;
1000  }
1001 
1003  template<typename T>
1004  std::pair<T, bool> Element::Get(const std::string &_key,
1005  const T &_defaultValue) const
1006  {
1007  sdf::Errors errors;
1008  std::pair<T, bool> result = this->Get<T>(errors, _key, _defaultValue);
1009  for(auto& error : errors)
1010  {
1012  }
1013  return result;
1014  }
1015 
1017  template<typename T>
1018  std::pair<T, bool> Element::Get(sdf::Errors &_errors,
1019  const std::string &_key,
1020  const T &_defaultValue) const
1021  {
1022  std::pair<T, bool> result(_defaultValue, true);
1023 
1024  if (_key.empty() && this->dataPtr->value)
1025  {
1026  this->dataPtr->value->Get<T>(result.first, _errors);
1027  }
1028  else if (!_key.empty())
1029  {
1030  ParamPtr param = this->GetAttribute(_key);
1031  if (param)
1032  {
1033  param->Get(result.first, _errors);
1034  }
1035  else if (this->HasElement(_key))
1036  {
1037  result.first = this->GetElementImpl(_key)->Get<T>(_errors);
1038  }
1039  else if (this->HasElementDescription(_key))
1040  {
1041  result.first = this->GetElementDescription(_key)->Get<T>(_errors);
1042  }
1043  else
1044  {
1045  result.second = false;
1046  }
1047  }
1048  else
1049  {
1050  result.second = false;
1051  }
1052 
1053  return result;
1054  }
1055 
1057  template<typename T>
1058  bool Element::Set(const T &_value)
1059  {
1060  sdf::Errors errors;
1061  bool result = this->Set<T>(errors, _value);
1062  for(auto& error : errors)
1063  {
1065  }
1066  return result;
1067  }
1068 
1070  template<typename T>
1071  bool Element::Set(sdf::Errors &_errors, const T &_value)
1072  {
1073  if (this->dataPtr->value)
1074  {
1075  return this->dataPtr->value->Set(_value, _errors);
1076  }
1077  return false;
1078  }
1080  }
1081 }
1082 
1083 #ifdef _WIN32
1084 #pragma warning(pop)
1085 #endif
1086 
1087 #endif
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::elements
ElementPtr_V elements
Definition: Element.hh:893
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::path
std::string path
Path to file where this element came from.
Definition: Element.hh:924
Error.hh
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::includeElement
ElementPtr includeElement
The element that was used to load this entity.
Definition: Element.hh:918
sdf::SDF_VERSION_NAMESPACE::Element
SDF Element class.
Definition: Element.hh:74
sdf::SDF_VERSION_NAMESPACE::Element::GetAttribute
ParamPtr GetAttribute(const std::string &_key) const
Get the param of an attribute.
sdf
namespace for Simulation Description Format parser
Definition: Actor.hh:34
PrintConfig.hh
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::lineNumber
std::optional< int > lineNumber
Line number in file where this element came from.
Definition: Element.hh:933
sdf::SDF_VERSION_NAMESPACE::Element::Get
T Get(sdf::Errors &_errors, const std::string &_key="") const
Get the value of a key.
Definition: Element.hh:959
sdf::SDF_VERSION_NAMESPACE::PrintConfig
This class contains configuration options for printing elements.
Definition: PrintConfig.hh:32
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::referenceSDF
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:921
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::originalVersion
std::string originalVersion
Spec version that this was originally parsed from.
Definition: Element.hh:927
sdf::SDF_VERSION_NAMESPACE::ElementConstPtr
std::shared_ptr< const Element > ElementConstPtr
Definition: Element.hh:59
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::name
std::string name
Element name.
Definition: Element.hh:872
Types.hh
sdf_config.h
sdf::SDF_VERSION_NAMESPACE::ParamPtr
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:70
SDFORMAT_VISIBLE
#define SDFORMAT_VISIBLE
Definition: system_util.hh:25
sdf::SDF_VERSION_NAMESPACE::Element::HasElement
bool HasElement(const std::string &_name) const
Return true if the named element exists.
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::description
std::string description
Element description.
Definition: Element.hh:878
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::value
ParamPtr value
Definition: Element.hh:890
Param.hh
sdf::SDF_VERSION_NAMESPACE::Element::GetElementDescription
ElementPtr GetElementDescription(unsigned int _index) const
Get an element description using an index.
sdf::SDF_VERSION_NAMESPACE::Element::GetElementImpl
ElementPtr GetElementImpl(const std::string &_name) const
Get a pointer to the named element.
sdf::SDF_VERSION_NAMESPACE::Element::HasElementDescription
bool HasElementDescription(const std::string &_name) const
Return true if an element description exists.
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::elementDescriptions
ElementPtr_V elementDescriptions
Definition: Element.hh:896
sdf::SDF_VERSION_NAMESPACE::Element
class GZ_SDFORMAT_VISIBLE Element
Definition: Element.hh:51
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::copyChildren
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:881
sdf::SDF_VERSION_NAMESPACE::ElementPrivate
Definition: Element.hh:869
sdf::SDF_VERSION_NAMESPACE::Errors
std::vector< Error > Errors
A vector of Error.
Definition: Types.hh:80
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::xmlPath
std::string xmlPath
XML path of this element.
Definition: Element.hh:936
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::required
std::string required
True if element is required.
Definition: Element.hh:875
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::attributes
Param_V attributes
Definition: Element.hh:887
sdf::SDF_VERSION_NAMESPACE::ElementPtr_V
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:67
sdf::SDF_VERSION_NAMESPACE::ElementWeakPtr
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:63
sdf::SDF_VERSION_NAMESPACE::Element::Set
bool Set(const T &_value)
Set the value of this element.
Definition: Element.hh:1058
system_util.hh
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::parent
ElementWeakPtr parent
Element's parent.
Definition: Element.hh:884
sdferr
#define sdferr
Output an error message.
Definition: Console.hh:57
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::PrintAttributes
void PrintAttributes(bool _includeDefaultAttributes, const PrintConfig &_config, std::ostringstream &_out) const
Generate the string (XML) for the attributes.
sdf::SDF_VERSION_NAMESPACE::ElementPtr
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:55
sdf::SDF_VERSION_NAMESPACE::ElementPrivate::explicitlySetInFile
bool explicitlySetInFile
True if the element was set in the SDF file.
Definition: Element.hh:930
sdf::SDF_VERSION_NAMESPACE::internal::throwOrPrintError
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.
sdf::SDF_VERSION_NAMESPACE::Param_V
std::vector< ParamPtr > Param_V
Definition: Param.hh:74