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::shared_ptr<const Element> ElementConstPtr;
58 
61  typedef std::weak_ptr<Element> ElementWeakPtr;
62 
65  typedef std::vector<ElementPtr> ElementPtr_V;
66 
69 
73  public std::enable_shared_from_this<Element>
74  {
76  public: Element();
77 
79  public: virtual ~Element();
80 
83  public: ElementPtr Clone() const;
84 
87  public: void Copy(const ElementPtr _elem);
88 
92  public: ElementPtr GetParent() const;
93 
96  public: void SetParent(const ElementPtr _parent);
97 
100  public: void SetName(const std::string &_name);
101 
104  public: const std::string &GetName() const;
105 
113  public: void SetRequired(const std::string &_req);
114 
118  public: const std::string &GetRequired() const;
119 
125  public: void SetExplicitlySetInFile(const bool _value);
126 
129  public: bool GetExplicitlySetInFile() const;
130 
134  public: void SetCopyChildren(bool _value);
135 
139  public: bool GetCopyChildren() const;
140 
143  public: void SetReferenceSDF(const std::string &_value);
144 
147  public: std::string ReferenceSDF() const;
148 
151  public: void PrintDescription(const std::string &_prefix) const;
152 
155  public: void PrintValues(std::string _prefix) const;
156 
161  public: void PrintValues(const std::string &_prefix,
162  bool _includeDefaultElements,
163  bool _includeDefaultAttributes) const;
164 
171  public: void PrintDocLeftPane(std::string &_html,
172  int _spacing, int &_index) const;
173 
179  public: void PrintDocRightPane(std::string &_html,
180  int _spacing, int &_index) const;
181 
185  public: std::string ToString(const std::string &_prefix) const;
186 
195  public: std::string ToString(const std::string &_prefix,
196  bool _includeDefaultElements,
197  bool _includeDefaultAttributes) const;
198 
206  public: void AddAttribute(const std::string &_key,
207  const std::string &_type,
208  const std::string &_defaultvalue,
209  bool _required,
210  const std::string &_description = "");
211 
218  public: void AddValue(const std::string &_type,
219  const std::string &_defaultValue, bool _required,
220  const std::string &_description = "");
221 
231  public: void AddValue(const std::string &_type,
232  const std::string &_defaultValue, bool _required,
233  const std::string &_minValue,
234  const std::string &_maxValue,
235  const std::string &_description = "");
236 
240  public: ParamPtr GetAttribute(const std::string &_key) const;
241 
244  public: size_t GetAttributeCount() const;
245 
249  public: ParamPtr GetAttribute(unsigned int _index) const;
250 
253  public: size_t GetElementDescriptionCount() const;
254 
258  public: ElementPtr GetElementDescription(unsigned int _index) const;
259 
263  public: ElementPtr GetElementDescription(const std::string &_key) const;
264 
268  public: bool HasElementDescription(const std::string &_name) const;
269 
273  public: bool HasAttribute(const std::string &_key) const;
274 
278  public: bool GetAttributeSet(const std::string &_key) const;
279 
282  public: void RemoveAttribute(const std::string &_key);
283 
285  public: void RemoveAllAttributes();
286 
289  public: ParamPtr GetValue() const;
290 
295  public: std::any GetAny(const std::string &_key = "") const;
296 
303  public: template<typename T>
304  T Get(const std::string &_key = "") const;
305 
312  public: template<typename T>
313  std::pair<T, bool> Get(const std::string &_key,
314  const T &_defaultValue) const;
315 
322  public: template<typename T>
323  bool Get(const std::string &_key,
324  T &_param,
325  const T &_defaultValue) const;
326 
330  public: template<typename T>
331  bool Set(const T &_value);
332 
336  public: bool HasElement(const std::string &_name) const;
337 
341  public: ElementPtr GetFirstElement() const;
342 
354  public: ElementPtr GetNextElement(const std::string &_name = "") const;
355 
358  public: std::set<std::string> GetElementTypeNames() const;
359 
367  public: bool HasUniqueChildNames(const std::string &_type = "") const;
368 
376  public: std::map<std::string, std::size_t>
377  CountNamedElements(const std::string &_type = "") const;
378 
389  public: ElementPtr GetElement(const std::string &_name);
390 
400  public: ElementPtr FindElement(const std::string &_name);
401 
411  public: ElementConstPtr FindElement(const std::string &_name) const;
412 
416  public: ElementPtr AddElement(const std::string &_name);
417 
420  public: void InsertElement(ElementPtr _elem);
421 
423  public: void RemoveFromParent();
424 
427  public: void RemoveChild(ElementPtr _child);
428 
430  public: void ClearElements();
431 
434  public: void Clear();
435 
438  public: void Update();
439 
443  public: void Reset();
444 
447  public: void SetInclude(const std::string &_filename) SDF_DEPRECATED(11.0);
448 
451  public: std::string GetInclude() const SDF_DEPRECATED(11.0);
452 
458  public: void SetIncludeElement(sdf::ElementPtr _includeElem);
459 
464  public: sdf::ElementPtr GetIncludeElement() const;
465 
468  public: void SetFilePath(const std::string &_path);
469 
472  public: const std::string &FilePath() const;
473 
476  public: void SetLineNumber(int _lineNumber);
477 
481  public: std::optional<int> LineNumber() const;
482 
497  public: void SetXmlPath(const std::string &_path);
498 
501  public: const std::string &XmlPath() const;
502 
505  public: void SetOriginalVersion(const std::string &_version);
506 
509  public: const std::string &OriginalVersion() const;
510 
513  public: std::string GetDescription() const;
514 
517  public: void SetDescription(const std::string &_desc);
518 
521  public: void AddElementDescription(ElementPtr _elem);
522 
526  public: ElementPtr GetElementImpl(const std::string &_name) const;
527 
533  private: void ToString(const std::string &_prefix,
534  bool _includeDefaultElements,
535  bool _includeDefaultAttributes,
536  std::ostringstream &_out) const;
537 
543  private: void PrintValuesImpl(const std::string &_prefix,
544  bool _includeDefaultElements,
545  bool _includeDefaultAttributes,
546  std::ostringstream &_out) const;
547 
556  private: ParamPtr CreateParam(const std::string &_key,
557  const std::string &_type,
558  const std::string &_defaultValue,
559  bool _required,
560  const std::string &_description = "");
561 
562 
564  private: std::unique_ptr<ElementPrivate> dataPtr;
565  };
566 
570  {
572  public: std::string name;
573 
575  public: std::string required;
576 
578  public: std::string description;
579 
581  public: bool copyChildren;
582 
584  public: ElementWeakPtr parent;
585 
586  // Attributes of this element
588 
589  // Value of this element
590  public: ParamPtr value;
591 
592  // The existing child elements
593  public: ElementPtr_V elements;
594 
595  // The possible child elements
596  public: ElementPtr_V elementDescriptions;
597 
618  public: ElementPtr includeElement;
619 
621  public: std::string includeFilename;
622 
624  public: std::string referenceSDF;
625 
627  public: std::string path;
628 
630  public: std::string originalVersion;
631 
633  public: bool explicitlySetInFile;
634 
636  public: std::optional<int> lineNumber;
637 
639  public: std::string xmlPath;
640  };
641 
643  template<typename T>
644  T Element::Get(const std::string &_key) const
645  {
646  T result = T();
647 
648  std::pair<T, bool> ret = this->Get<T>(_key, result);
649 
650  return ret.first;
651  }
652 
654  template<typename T>
655  bool Element::Get(const std::string &_key,
656  T &_param,
657  const T &_defaultValue) const
658  {
659  std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
660  _param = ret.first;
661  return ret.second;
662  }
663 
665  template<typename T>
666  std::pair<T, bool> Element::Get(const std::string &_key,
667  const T &_defaultValue) const
668  {
669  std::pair<T, bool> result(_defaultValue, true);
670 
671  if (_key.empty() && this->dataPtr->value)
672  {
673  this->dataPtr->value->Get<T>(result.first);
674  }
675  else if (!_key.empty())
676  {
677  ParamPtr param = this->GetAttribute(_key);
678  if (param)
679  {
680  param->Get(result.first);
681  }
682  else if (this->HasElement(_key))
683  {
684  result.first = this->GetElementImpl(_key)->Get<T>();
685  }
686  else if (this->HasElementDescription(_key))
687  {
688  result.first = this->GetElementDescription(_key)->Get<T>();
689  }
690  else
691  {
692  result.second = false;
693  }
694  }
695  else
696  {
697  result.second = false;
698  }
699 
700  return result;
701  }
702 
704  template<typename T>
705  bool Element::Set(const T &_value)
706  {
707  if (this->dataPtr->value)
708  {
709  this->dataPtr->value->Set(_value);
710  return true;
711  }
712  return false;
713  }
715  }
716 }
717 
718 #ifdef _WIN32
719 #pragma warning(pop)
720 #endif
721 
722 #endif
std::shared_ptr< const Element > ElementConstPtr
Definition: Element.hh:57
std::string name
Element name.
Definition: Element.hh:572
ParamPtr value
Definition: Element.hh:590
SDF Element class.
Definition: Element.hh:72
std::string originalVersion
Spec version that this was originally parsed from.
Definition: Element.hh:630
std::string description
Element description.
Definition: Element.hh:578
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:621
ElementPtr includeElement
The element that was used to load this entity.
Definition: Element.hh:618
bool copyChildren
True if element&#39;s children should be copied.
Definition: Element.hh:581
ElementPtr_V elements
Definition: Element.hh:593
std::string required
True if element is required.
Definition: Element.hh:575
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:65
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:624
std::string path
Path to file where this element came from.
Definition: Element.hh:627
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:41
Definition: Element.hh:569
std::string xmlPath
XML path of this element.
Definition: Element.hh:639
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:53
bool explicitlySetInFile
True if the element was set in the SDF file.
Definition: Element.hh:633
class SDFORMAT_VISIBLE Element
Definition: Element.hh:49
ElementWeakPtr parent
Element&#39;s parent.
Definition: Element.hh:584
namespace for Simulation Description Format parser
Definition: Actor.hh:33
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:60
std::vector< ParamPtr > Param_V
Definition: Param.hh:64
std::optional< int > lineNumber
Line number in file where this element came from.
Definition: Element.hh:636
ElementPtr_V elementDescriptions
Definition: Element.hh:596
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:61
#define SDF_DEPRECATED(version)
Definition: Types.hh:39
Param_V attributes
Definition: Element.hh:587