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 
112  public: void SetRequired(const std::string &_req);
113 
117  public: const std::string &GetRequired() const;
118 
124  public: void SetExplicitlySetInFile(const bool _value);
125 
128  public: bool GetExplicitlySetInFile() const;
129 
133  public: void SetCopyChildren(bool _value);
134 
138  public: bool GetCopyChildren() const;
139 
142  public: void SetReferenceSDF(const std::string &_value);
143 
146  public: std::string ReferenceSDF() const;
147 
150  public: void PrintDescription(const std::string &_prefix) const;
151 
154  public: void PrintValues(std::string _prefix) const;
155 
160  public: void PrintValues(const std::string &_prefix,
161  bool _includeDefaultElements,
162  bool _includeDefaultAttributes) const;
163 
170  public: void PrintDocLeftPane(std::string &_html,
171  int _spacing, int &_index) const;
172 
178  public: void PrintDocRightPane(std::string &_html,
179  int _spacing, int &_index) const;
180 
184  public: std::string ToString(const std::string &_prefix) const;
185 
194  public: std::string ToString(const std::string &_prefix,
195  bool _includeDefaultElements,
196  bool _includeDefaultAttributes) const;
197 
205  public: void AddAttribute(const std::string &_key,
206  const std::string &_type,
207  const std::string &_defaultvalue,
208  bool _required,
209  const std::string &_description = "");
210 
217  public: void AddValue(const std::string &_type,
218  const std::string &_defaultValue, bool _required,
219  const std::string &_description = "");
220 
230  public: void AddValue(const std::string &_type,
231  const std::string &_defaultValue, bool _required,
232  const std::string &_minValue,
233  const std::string &_maxValue,
234  const std::string &_description = "");
235 
239  public: ParamPtr GetAttribute(const std::string &_key) const;
240 
243  public: size_t GetAttributeCount() const;
244 
248  public: ParamPtr GetAttribute(unsigned int _index) const;
249 
252  public: size_t GetElementDescriptionCount() const;
253 
257  public: ElementPtr GetElementDescription(unsigned int _index) const;
258 
262  public: ElementPtr GetElementDescription(const std::string &_key) const;
263 
267  public: bool HasElementDescription(const std::string &_name) const;
268 
272  public: bool HasAttribute(const std::string &_key) const;
273 
277  public: bool GetAttributeSet(const std::string &_key) const;
278 
281  public: void RemoveAttribute(const std::string &_key);
282 
284  public: void RemoveAllAttributes();
285 
288  public: ParamPtr GetValue() const;
289 
294  public: std::any GetAny(const std::string &_key = "") const;
295 
302  public: template<typename T>
303  T Get(const std::string &_key = "") const;
304 
311  public: template<typename T>
312  std::pair<T, bool> Get(const std::string &_key,
313  const T &_defaultValue) const;
314 
321  public: template<typename T>
322  bool Get(const std::string &_key,
323  T &_param,
324  const T &_defaultValue) const;
325 
329  public: template<typename T>
330  bool Set(const T &_value);
331 
335  public: bool HasElement(const std::string &_name) const;
336 
340  public: ElementPtr GetFirstElement() const;
341 
353  public: ElementPtr GetNextElement(const std::string &_name = "") const;
354 
357  public: std::set<std::string> GetElementTypeNames() const;
358 
366  public: bool HasUniqueChildNames(const std::string &_type = "") const;
367 
378  public: bool HasUniqueChildNames(
379  const std::string &_type,
380  const std::vector<std::string> &_ignoreElements) const;
381 
389  public: std::map<std::string, std::size_t>
390  CountNamedElements(const std::string &_type = "") const;
391 
402  public: std::map<std::string, std::size_t> CountNamedElements(
403  const std::string &_type,
404  const std::vector<std::string> &_ignoreElements) const;
405 
416  public: ElementPtr GetElement(const std::string &_name);
417 
427  public: ElementPtr FindElement(const std::string &_name);
428 
438  public: ElementConstPtr FindElement(const std::string &_name) const;
439 
443  public: ElementPtr AddElement(const std::string &_name);
444 
447  public: void InsertElement(ElementPtr _elem);
448 
450  public: void RemoveFromParent();
451 
454  public: void RemoveChild(ElementPtr _child);
455 
457  public: void ClearElements();
458 
461  public: void Clear();
462 
465  public: void Update();
466 
470  public: void Reset();
471 
474  public: void SetInclude(const std::string &_filename);
475 
478  public: std::string GetInclude() const;
479 
482  public: void SetFilePath(const std::string &_path);
483 
486  public: const std::string &FilePath() const;
487 
490  public: void SetOriginalVersion(const std::string &_version);
491 
494  public: const std::string &OriginalVersion() const;
495 
498  public: std::string GetDescription() const;
499 
502  public: void SetDescription(const std::string &_desc);
503 
506  public: void AddElementDescription(ElementPtr _elem);
507 
511  public: ElementPtr GetElementImpl(const std::string &_name) const;
512 
516  public: static std::vector<std::string> NameUniquenessExceptions();
517 
523  private: void ToString(const std::string &_prefix,
524  bool _includeDefaultElements,
525  bool _includeDefaultAttributes,
526  std::ostringstream &_out) const;
527 
533  private: void PrintValuesImpl(const std::string &_prefix,
534  bool _includeDefaultElements,
535  bool _includeDefaultAttributes,
536  std::ostringstream &_out) const;
537 
546  private: ParamPtr CreateParam(const std::string &_key,
547  const std::string &_type,
548  const std::string &_defaultValue,
549  bool _required,
550  const std::string &_description = "");
551 
552 
554  private: std::unique_ptr<ElementPrivate> dataPtr;
555  };
556 
560  {
562  public: std::string name;
563 
565  public: std::string required;
566 
568  public: std::string description;
569 
571  public: bool copyChildren;
572 
574  public: ElementWeakPtr parent;
575 
576  // Attributes of this element
578 
579  // Value of this element
580  public: ParamPtr value;
581 
582  // The existing child elements
583  public: ElementPtr_V elements;
584 
585  // The possible child elements
586  public: ElementPtr_V elementDescriptions;
587 
589  public: std::string includeFilename;
590 
592  public: std::string referenceSDF;
593 
595  public: std::string path;
596 
598  public: std::string originalVersion;
599 
601  public: bool explicitlySetInFile;
602  };
603 
605  template<typename T>
606  T Element::Get(const std::string &_key) const
607  {
608  T result = T();
609 
610  std::pair<T, bool> ret = this->Get<T>(_key, result);
611 
612  return ret.first;
613  }
614 
616  template<typename T>
617  bool Element::Get(const std::string &_key,
618  T &_param,
619  const T &_defaultValue) const
620  {
621  std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
622  _param = ret.first;
623  return ret.second;
624  }
625 
627  template<typename T>
628  std::pair<T, bool> Element::Get(const std::string &_key,
629  const T &_defaultValue) const
630  {
631  std::pair<T, bool> result(_defaultValue, true);
632 
633  if (_key.empty() && this->dataPtr->value)
634  {
635  this->dataPtr->value->Get<T>(result.first);
636  }
637  else if (!_key.empty())
638  {
639  ParamPtr param = this->GetAttribute(_key);
640  if (param)
641  {
642  param->Get(result.first);
643  }
644  else if (this->HasElement(_key))
645  {
646  result.first = this->GetElementImpl(_key)->Get<T>();
647  }
648  else if (this->HasElementDescription(_key))
649  {
650  result.first = this->GetElementDescription(_key)->Get<T>();
651  }
652  else
653  {
654  result.second = false;
655  }
656  }
657  else
658  {
659  result.second = false;
660  }
661 
662  return result;
663  }
664 
666  template<typename T>
667  bool Element::Set(const T &_value)
668  {
669  if (this->dataPtr->value)
670  {
671  this->dataPtr->value->Set(_value);
672  return true;
673  }
674  return false;
675  }
677  }
678 }
679 
680 #ifdef _WIN32
681 #pragma warning(pop)
682 #endif
683 
684 #endif
std::string required
True if element is required.
Definition: Element.hh:565
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:589
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:58
std::string name
Element name.
Definition: Element.hh:562
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:65
std::vector< ParamPtr > Param_V
Definition: Param.hh:62
Param_V attributes
Definition: Element.hh:577
ParamPtr value
Definition: Element.hh:580
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:53
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:61
std::string path
Path to file where this element came from.
Definition: Element.hh:595
ElementPtr_V elementDescriptions
Definition: Element.hh:586
bool Set(const T &_value)
Set the value of this element.
Definition: Element.hh:667
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:592
std::string description
Element description.
Definition: Element.hh:568
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:25
bool copyChildren
True if element&#39;s children should be copied.
Definition: Element.hh:571
class SDFORMAT_VISIBLE Element
Definition: Element.hh:49
std::string originalVersion
Spec version that this was originally parsed from.
Definition: Element.hh:598
ElementWeakPtr parent
Element&#39;s parent.
Definition: Element.hh:574
T Get(const std::string &_key="") const
Get the value of a key.
Definition: Element.hh:606
SDF Element class.
Definition: Element.hh:72
namespace for Simulation Description Format parser
Definition: Actor.hh:32
ElementPtr_V elements
Definition: Element.hh:583
bool explicitlySetInFile
True if the element was set in the SDF file.
Definition: Element.hh:601
std::shared_ptr< const Element > ElementConstPtr
Definition: Element.hh:57