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 
224  public: ParamPtr GetAttribute(const std::string &_key) const;
225 
228  public: size_t GetAttributeCount() const;
229 
233  public: ParamPtr GetAttribute(unsigned int _index) const;
234 
237  public: size_t GetElementDescriptionCount() const;
238 
242  public: ElementPtr GetElementDescription(unsigned int _index) const;
243 
247  public: ElementPtr GetElementDescription(const std::string &_key) const;
248 
252  public: bool HasElementDescription(const std::string &_name) const;
253 
257  public: bool HasAttribute(const std::string &_key) const;
258 
262  public: bool GetAttributeSet(const std::string &_key) const;
263 
266  public: void RemoveAttribute(const std::string &_key);
267 
269  public: void RemoveAllAttributes();
270 
273  public: ParamPtr GetValue() const;
274 
279  public: std::any GetAny(const std::string &_key = "") const;
280 
287  public: template<typename T>
288  T Get(const std::string &_key = "") const;
289 
296  public: template<typename T>
297  std::pair<T, bool> Get(const std::string &_key,
298  const T &_defaultValue) const;
299 
306  public: template<typename T>
307  bool Get(const std::string &_key,
308  T &_param,
309  const T &_defaultValue) const;
310 
314  public: template<typename T>
315  bool Set(const T &_value);
316 
320  public: bool HasElement(const std::string &_name) const;
321 
325  public: ElementPtr GetFirstElement() const;
326 
338  public: ElementPtr GetNextElement(const std::string &_name = "") const;
339 
342  public: std::set<std::string> GetElementTypeNames() const;
343 
351  public: bool HasUniqueChildNames(const std::string &_type = "") const;
352 
363  public: bool HasUniqueChildNames(
364  const std::string &_type,
365  const std::vector<std::string> &_ignoreElements) const;
366 
374  public: std::map<std::string, std::size_t>
375  CountNamedElements(const std::string &_type = "") const;
376 
387  public: std::map<std::string, std::size_t> CountNamedElements(
388  const std::string &_type,
389  const std::vector<std::string> &_ignoreElements) const;
390 
401  public: ElementPtr GetElement(const std::string &_name);
402 
412  public: ElementPtr FindElement(const std::string &_name);
413 
423  public: ElementConstPtr FindElement(const std::string &_name) const;
424 
428  public: ElementPtr AddElement(const std::string &_name);
429 
432  public: void InsertElement(ElementPtr _elem);
433 
435  public: void RemoveFromParent();
436 
439  public: void RemoveChild(ElementPtr _child);
440 
442  public: void ClearElements();
443 
446  public: void Clear();
447 
450  public: void Update();
451 
455  public: void Reset();
456 
459  public: void SetInclude(const std::string &_filename);
460 
463  public: std::string GetInclude() const;
464 
467  public: void SetFilePath(const std::string &_path);
468 
471  public: const std::string &FilePath() const;
472 
475  public: void SetOriginalVersion(const std::string &_version);
476 
479  public: const std::string &OriginalVersion() const;
480 
483  public: std::string GetDescription() const;
484 
487  public: void SetDescription(const std::string &_desc);
488 
491  public: void AddElementDescription(ElementPtr _elem);
492 
496  public: ElementPtr GetElementImpl(const std::string &_name) const;
497 
501  public: static std::vector<std::string> NameUniquenessExceptions();
502 
508  private: void ToString(const std::string &_prefix,
509  bool _includeDefaultElements,
510  bool _includeDefaultAttributes,
511  std::ostringstream &_out) const;
512 
518  private: void PrintValuesImpl(const std::string &_prefix,
519  bool _includeDefaultElements,
520  bool _includeDefaultAttributes,
521  std::ostringstream &_out) const;
522 
531  private: ParamPtr CreateParam(const std::string &_key,
532  const std::string &_type,
533  const std::string &_defaultValue,
534  bool _required,
535  const std::string &_description = "");
536 
537 
539  private: std::unique_ptr<ElementPrivate> dataPtr;
540  };
541 
545  {
547  public: std::string name;
548 
550  public: std::string required;
551 
553  public: std::string description;
554 
556  public: bool copyChildren;
557 
560 
561  // Attributes of this element
563 
564  // Value of this element
565  public: ParamPtr value;
566 
567  // The existing child elements
569 
570  // The possible child elements
572 
574  public: std::string includeFilename;
575 
577  public: std::string referenceSDF;
578 
580  public: std::string path;
581 
583  public: std::string originalVersion;
584 
586  public: bool explicitlySetInFile;
587  };
588 
590  template<typename T>
591  T Element::Get(const std::string &_key) const
592  {
593  T result = T();
594 
595  std::pair<T, bool> ret = this->Get<T>(_key, result);
596 
597  return ret.first;
598  }
599 
601  template<typename T>
602  bool Element::Get(const std::string &_key,
603  T &_param,
604  const T &_defaultValue) const
605  {
606  std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
607  _param = ret.first;
608  return ret.second;
609  }
610 
612  template<typename T>
613  std::pair<T, bool> Element::Get(const std::string &_key,
614  const T &_defaultValue) const
615  {
616  std::pair<T, bool> result(_defaultValue, true);
617 
618  if (_key.empty() && this->dataPtr->value)
619  {
620  this->dataPtr->value->Get<T>(result.first);
621  }
622  else if (!_key.empty())
623  {
624  ParamPtr param = this->GetAttribute(_key);
625  if (param)
626  {
627  param->Get(result.first);
628  }
629  else if (this->HasElement(_key))
630  {
631  result.first = this->GetElementImpl(_key)->Get<T>();
632  }
633  else if (this->HasElementDescription(_key))
634  {
635  result.first = this->GetElementDescription(_key)->Get<T>();
636  }
637  else
638  {
639  result.second = false;
640  }
641  }
642  else
643  {
644  result.second = false;
645  }
646 
647  return result;
648  }
649 
651  template<typename T>
652  bool Element::Set(const T &_value)
653  {
654  if (this->dataPtr->value)
655  {
656  return this->dataPtr->value->Set(_value);
657  }
658  return false;
659  }
661  }
662 }
663 
664 #ifdef _WIN32
665 #pragma warning(pop)
666 #endif
667 
668 #endif
sdf::v9::ElementPrivate::elements
ElementPtr_V elements
Definition: Element.hh:568
sdf::v9::ElementConstPtr
std::shared_ptr< const Element > ElementConstPtr
Definition: Element.hh:57
sdf::v9::ElementPrivate::name
std::string name
Element name.
Definition: Element.hh:547
sdf::v9::ElementPrivate::originalVersion
std::string originalVersion
Spec version that this was originally parsed from.
Definition: Element.hh:583
sdf
namespace for Simulation Description Format parser
Definition: Actor.hh:32
sdf::v9::ElementPrivate::explicitlySetInFile
bool explicitlySetInFile
True if the element was set in the SDF file.
Definition: Element.hh:586
sdf::v9::ElementWeakPtr
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:61
sdf::v9::ElementPrivate::value
ParamPtr value
Definition: Element.hh:565
Types.hh
sdf::v9::ElementPrivate
Definition: Element.hh:544
sdf::v9::ParamPtr
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:57
SDFORMAT_VISIBLE
#define SDFORMAT_VISIBLE
Definition: system_util.hh:48
sdf::v9::ElementPrivate::copyChildren
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:556
sdf::v9::ElementPrivate::path
std::string path
Path to file where this element came from.
Definition: Element.hh:580
sdf::v9::Param_V
std::vector< ParamPtr > Param_V
Definition: Param.hh:61
Param.hh
sdf::v9::ElementPrivate::parent
ElementWeakPtr parent
Element's parent.
Definition: Element.hh:559
sdf::v9::ElementPrivate::required
std::string required
True if element is required.
Definition: Element.hh:550
sdf::v9::ElementPrivate::description
std::string description
Element description.
Definition: Element.hh:553
sdf::v9::Element
SDF Element class.
Definition: Element.hh:72
sdf::v9::ElementPrivate::attributes
Param_V attributes
Definition: Element.hh:562
sdf::v9::ElementPrivate::referenceSDF
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:577
sdf::v9::ElementPtr
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:53
sdf::v9::ElementPrivate::includeFilename
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:574
sdf::v9::ElementPrivate::elementDescriptions
ElementPtr_V elementDescriptions
Definition: Element.hh:571
system_util.hh
sdf::v9::Element
class SDFORMAT_VISIBLE Element
Definition: Element.hh:49
sdf::v9::ElementPtr_V
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:65