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/sdf_config.h"
30 #include "sdf/system_util.hh"
31 #include "sdf/Types.hh"
32 
33 #ifdef _WIN32
34 // Disable warning C4251 which is triggered by
35 // std::enable_shared_from_this
36 #pragma warning(push)
37 #pragma warning(disable: 4251)
38 #endif
39 
42 namespace sdf
43 {
44  // Inline bracket to help doxygen filtering.
45  inline namespace SDF_VERSION_NAMESPACE {
46  //
47 
48  class ElementPrivate;
50 
53  typedef std::shared_ptr<Element> ElementPtr;
54 
57  typedef std::weak_ptr<Element> ElementWeakPtr;
58 
61  typedef std::vector<ElementPtr> ElementPtr_V;
62 
65 
69  public std::enable_shared_from_this<Element>
70  {
72  public: Element();
73 
75  public: virtual ~Element();
76 
79  public: ElementPtr Clone() const;
80 
83  public: void Copy(const ElementPtr _elem);
84 
88  public: ElementPtr GetParent() const;
89 
92  public: void SetParent(const ElementPtr _parent);
93 
96  public: void SetName(const std::string &_name);
97 
100  public: const std::string &GetName() const;
101 
109  public: void SetRequired(const std::string &_req);
110 
114  public: const std::string &GetRequired() const;
115 
121  public: void SetExplicitlySetInFile(const bool _value);
122 
125  public: bool GetExplicitlySetInFile() const;
126 
130  public: void SetCopyChildren(bool _value);
131 
135  public: bool GetCopyChildren() const;
136 
139  public: void SetReferenceSDF(const std::string &_value);
140 
143  public: std::string ReferenceSDF() const;
144 
147  public: void PrintDescription(const std::string &_prefix) const;
148 
151  public: void PrintValues(std::string _prefix) const;
152 
159  public: void PrintDocLeftPane(std::string &_html,
160  int _spacing, int &_index) const;
161 
167  public: void PrintDocRightPane(std::string &_html,
168  int _spacing, int &_index) const;
169 
173  public: std::string ToString(const std::string &_prefix) const;
174 
182  public: void AddAttribute(const std::string &_key,
183  const std::string &_type,
184  const std::string &_defaultvalue,
185  bool _required,
186  const std::string &_description="");
187 
194  public: void AddValue(const std::string &_type,
195  const std::string &_defaultValue, bool _required,
196  const std::string &_description="");
197 
207  public: void AddValue(const std::string &_type,
208  const std::string &_defaultValue, bool _required,
209  const std::string &_minValue,
210  const std::string &_maxValue,
211  const std::string &_description = "");
212 
216  public: ParamPtr GetAttribute(const std::string &_key) const;
217 
220  public: size_t GetAttributeCount() const;
221 
225  public: ParamPtr GetAttribute(unsigned int _index) const;
226 
229  public: size_t GetElementDescriptionCount() const;
230 
234  public: ElementPtr GetElementDescription(unsigned int _index) const;
235 
239  public: ElementPtr GetElementDescription(const std::string &_key) const;
240 
244  public: bool HasElementDescription(const std::string &_name) const;
245 
249  public: bool HasAttribute(const std::string &_key) const;
250 
254  public: bool GetAttributeSet(const std::string &_key) const;
255 
258  public: void RemoveAttribute(const std::string &_key);
259 
261  public: void RemoveAllAttributes();
262 
265  public: ParamPtr GetValue() const;
266 
271  public: std::any GetAny(const std::string &_key = "") const;
272 
279  public: template<typename T>
280  T Get(const std::string &_key = "") const;
281 
288  public: template<typename T>
289  std::pair<T, bool> Get(const std::string &_key,
290  const T &_defaultValue) const;
291 
298  public: template<typename T>
299  bool Get(const std::string &_key,
300  T &_param,
301  const T &_defaultValue) const;
302 
306  public: template<typename T>
307  bool Set(const T &_value);
308 
312  public: bool HasElement(const std::string &_name) const;
313 
317  public: ElementPtr GetFirstElement() const;
318 
330  public: ElementPtr GetNextElement(const std::string &_name = "") const;
331 
334  public: std::set<std::string> GetElementTypeNames() const;
335 
343  public: bool HasUniqueChildNames(const std::string &_type = "") const;
344 
352  public: std::map<std::string, std::size_t>
353  CountNamedElements(const std::string &_type = "") const;
354 
364  public: ElementPtr GetElement(const std::string &_name);
365 
369  public: ElementPtr AddElement(const std::string &_name);
370 
373  public: void InsertElement(ElementPtr _elem);
374 
376  public: void RemoveFromParent();
377 
380  public: void RemoveChild(ElementPtr _child);
381 
383  public: void ClearElements();
384 
387  public: void Clear();
388 
391  public: void Update();
392 
396  public: void Reset();
397 
400  public: void SetInclude(const std::string &_filename) SDF_DEPRECATED(11.0);
401 
404  public: std::string GetInclude() const SDF_DEPRECATED(11.0);
405 
411  public: void SetIncludeElement(sdf::ElementPtr _includeElem);
412 
417  public: sdf::ElementPtr GetIncludeElement() const;
418 
421  public: void SetFilePath(const std::string &_path);
422 
425  public: const std::string &FilePath() const;
426 
429  public: void SetLineNumber(int _lineNumber);
430 
434  public: std::optional<int> LineNumber() const;
435 
450  public: void SetXmlPath(const std::string &_path);
451 
454  public: const std::string &XmlPath() const;
455 
458  public: void SetOriginalVersion(const std::string &_version);
459 
462  public: const std::string &OriginalVersion() const;
463 
466  public: std::string GetDescription() const;
467 
470  public: void SetDescription(const std::string &_desc);
471 
474  public: void AddElementDescription(ElementPtr _elem);
475 
479  public: ElementPtr GetElementImpl(const std::string &_name) const;
480 
484  private: void ToString(const std::string &_prefix,
485  std::ostringstream &_out) const;
486 
490  private: void PrintValuesImpl(const std::string &_prefix,
491  std::ostringstream &_out) const;
492 
501  private: ParamPtr CreateParam(const std::string &_key,
502  const std::string &_type,
503  const std::string &_defaultValue,
504  bool _required,
505  const std::string &_description="");
506 
507 
509  private: std::unique_ptr<ElementPrivate> dataPtr;
510  };
511 
515  {
517  public: std::string name;
518 
520  public: std::string required;
521 
523  public: bool explicitlySetInFile;
524 
526  public: std::string description;
527 
529  public: bool copyChildren;
530 
533 
534  // Attributes of this element
536 
537  // Value of this element
538  public: ParamPtr value;
539 
540  // The existing child elements
542 
543  // The possible child elements
545 
567 
569  public: std::string includeFilename;
570 
572  public: std::string referenceSDF;
573 
575  public: std::string path;
576 
578  public: std::optional<int> lineNumber;
579 
581  public: std::string xmlPath;
582 
584  public: std::string originalVersion;
585  };
586 
588  template<typename T>
589  T Element::Get(const std::string &_key) const
590  {
591  T result = T();
592 
593  std::pair<T, bool> ret = this->Get<T>(_key, result);
594 
595  return ret.first;
596  }
597 
599  template<typename T>
600  bool Element::Get(const std::string &_key,
601  T &_param,
602  const T &_defaultValue) const
603  {
604  std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
605  _param = ret.first;
606  return ret.second;
607  }
608 
610  template<typename T>
611  std::pair<T, bool> Element::Get(const std::string &_key,
612  const T &_defaultValue) const
613  {
614  std::pair<T, bool> result(_defaultValue, true);
615 
616  if (_key.empty() && this->dataPtr->value)
617  {
618  this->dataPtr->value->Get<T>(result.first);
619  }
620  else if (!_key.empty())
621  {
622  ParamPtr param = this->GetAttribute(_key);
623  if (param)
624  {
625  param->Get(result.first);
626  }
627  else if (this->HasElement(_key))
628  {
629  result.first = this->GetElementImpl(_key)->Get<T>();
630  }
631  else if (this->HasElementDescription(_key))
632  {
633  result.first = this->GetElementDescription(_key)->Get<T>();
634  }
635  else
636  {
637  result.second = false;
638  }
639  }
640  else
641  {
642  result.second = false;
643  }
644 
645  return result;
646  }
647 
649  template<typename T>
650  bool Element::Set(const T &_value)
651  {
652  if (this->dataPtr->value)
653  {
654  this->dataPtr->value->Set(_value);
655  return true;
656  }
657  return false;
658  }
660  }
661 }
662 
663 #ifdef _WIN32
664 #pragma warning(pop)
665 #endif
666 
667 #endif
sdf::v11::ElementPrivate::value
ParamPtr value
Definition: Element.hh:538
sdf::v11::ElementPrivate::includeElement
ElementPtr includeElement
The element that was used to load this entity.
Definition: Element.hh:566
sdf::v11::ElementPrivate::description
std::string description
Element description.
Definition: Element.hh:526
sdf::v11::Element
SDF Element class.
Definition: Element.hh:68
sdf::v11::ElementPtr
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:53
sdf::v11::ElementWeakPtr
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:57
sdf::v11::ElementPrivate::elements
ElementPtr_V elements
Definition: Element.hh:541
sdf::v11::ElementPrivate::copyChildren
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:529
sdf::v11::ElementPrivate::originalVersion
std::string originalVersion
Spec version that this was originally parsed from.
Definition: Element.hh:584
sdf::v11::ParamPtr
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:60
sdf
namespace for Simulation Description Format parser
Definition: Actor.hh:33
sdf::v11::ElementPrivate::required
std::string required
True if element is required.
Definition: Element.hh:520
SDF_DEPRECATED
#define SDF_DEPRECATED(version)
Definition: Types.hh:39
Types.hh
sdf::v11::Param_V
std::vector< ParamPtr > Param_V
Definition: Param.hh:64
SDFORMAT_VISIBLE
#define SDFORMAT_VISIBLE
Definition: system_util.hh:41
sdf::v11::ElementPrivate::referenceSDF
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:572
sdf::v11::ElementPrivate::path
std::string path
Path to file where this element came from.
Definition: Element.hh:575
sdf::v11::ElementPrivate::xmlPath
std::string xmlPath
XML path of this element.
Definition: Element.hh:581
Param.hh
sdf::v11::ElementPrivate::explicitlySetInFile
bool explicitlySetInFile
True if the element was set in the SDF file.
Definition: Element.hh:523
sdf::v11::ElementPrivate::includeFilename
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:569
sdf::v11::ElementPrivate::parent
ElementWeakPtr parent
Element's parent.
Definition: Element.hh:532
sdf::v11::ElementPrivate::elementDescriptions
ElementPtr_V elementDescriptions
Definition: Element.hh:544
sdf::v11::ElementPrivate::lineNumber
std::optional< int > lineNumber
Line number in file where this element came from.
Definition: Element.hh:578
sdf::v11::ElementPrivate::attributes
Param_V attributes
Definition: Element.hh:535
system_util.hh
sdf::v11::ElementPrivate::name
std::string name
Element name.
Definition: Element.hh:517
sdf::v11::Element
class SDFORMAT_VISIBLE Element
Definition: Element.hh:49
sdf::v11::ElementPrivate
Definition: Element.hh:514
sdf::v11::ElementPtr_V
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:61