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 
108  public: void SetRequired(const std::string &_req);
109 
113  public: const std::string &GetRequired() const;
114 
118  public: void SetCopyChildren(bool _value);
119 
123  public: bool GetCopyChildren() const;
124 
127  public: void SetReferenceSDF(const std::string &_value);
128 
131  public: std::string ReferenceSDF() const;
132 
135  public: void PrintDescription(const std::string &_prefix) const;
136 
139  public: void PrintValues(std::string _prefix) const;
140 
147  public: void PrintDocLeftPane(std::string &_html,
148  int _spacing, int &_index) const;
149 
155  public: void PrintDocRightPane(std::string &_html,
156  int _spacing, int &_index) const;
157 
161  public: std::string ToString(const std::string &_prefix) const;
162 
170  public: void AddAttribute(const std::string &_key,
171  const std::string &_type,
172  const std::string &_defaultvalue,
173  bool _required,
174  const std::string &_description="");
175 
182  public: void AddValue(const std::string &_type,
183  const std::string &_defaultValue, bool _required,
184  const std::string &_description="");
185 
195  public: void AddValue(const std::string &_type,
196  const std::string &_defaultValue, bool _required,
197  const std::string &_minValue,
198  const std::string &_maxValue,
199  const std::string &_description = "");
200 
204  public: ParamPtr GetAttribute(const std::string &_key) const;
205 
208  public: size_t GetAttributeCount() const;
209 
213  public: ParamPtr GetAttribute(unsigned int _index) const;
214 
217  public: size_t GetElementDescriptionCount() const;
218 
222  public: ElementPtr GetElementDescription(unsigned int _index) const;
223 
227  public: ElementPtr GetElementDescription(const std::string &_key) const;
228 
232  public: bool HasElementDescription(const std::string &_name) const;
233 
237  public: bool HasAttribute(const std::string &_key) const;
238 
242  public: bool GetAttributeSet(const std::string &_key) const;
243 
246  public: ParamPtr GetValue() const;
247 
252  public: std::any GetAny(const std::string &_key = "") const;
253 
260  public: template<typename T>
261  T Get(const std::string &_key = "") const;
262 
269  public: template<typename T>
270  std::pair<T, bool> Get(const std::string &_key,
271  const T &_defaultValue) const;
272 
279  public: template<typename T>
280  bool Get(const std::string &_key,
281  T &_param,
282  const T &_defaultValue) const;
283 
287  public: template<typename T>
288  bool Set(const T &_value);
289 
293  public: bool HasElement(const std::string &_name) const;
294 
298  public: ElementPtr GetFirstElement() const;
299 
311  public: ElementPtr GetNextElement(const std::string &_name = "") const;
312 
315  public: std::set<std::string> GetElementTypeNames() const;
316 
324  public: bool HasUniqueChildNames(const std::string &_type = "") const;
325 
333  public: std::map<std::string, std::size_t>
334  CountNamedElements(const std::string &_type = "") const;
335 
345  public: ElementPtr GetElement(const std::string &_name);
346 
350  public: ElementPtr AddElement(const std::string &_name);
351 
354  public: void InsertElement(ElementPtr _elem);
355 
357  public: void RemoveFromParent();
358 
361  public: void RemoveChild(ElementPtr _child);
362 
364  public: void ClearElements();
365 
368  public: void Clear();
369 
372  public: void Update();
373 
377  public: void Reset();
378 
381  public: void SetInclude(const std::string &_filename) SDF_DEPRECATED(11.0);
382 
385  public: std::string GetInclude() const SDF_DEPRECATED(11.0);
386 
392  public: void SetIncludeElement(sdf::ElementPtr _includeElem);
393 
398  public: sdf::ElementPtr GetIncludeElement() const;
399 
402  public: void SetFilePath(const std::string &_path);
403 
406  public: const std::string &FilePath() const;
407 
410  public: void SetOriginalVersion(const std::string &_version);
411 
414  public: const std::string &OriginalVersion() const;
415 
418  public: std::string GetDescription() const;
419 
422  public: void SetDescription(const std::string &_desc);
423 
426  public: void AddElementDescription(ElementPtr _elem);
427 
431  public: ElementPtr GetElementImpl(const std::string &_name) const;
432 
436  private: void ToString(const std::string &_prefix,
437  std::ostringstream &_out) const;
438 
442  private: void PrintValuesImpl(const std::string &_prefix,
443  std::ostringstream &_out) const;
444 
453  private: ParamPtr CreateParam(const std::string &_key,
454  const std::string &_type,
455  const std::string &_defaultValue,
456  bool _required,
457  const std::string &_description="");
458 
459 
461  private: std::unique_ptr<ElementPrivate> dataPtr;
462  };
463 
467  {
469  public: std::string name;
470 
472  public: std::string required;
473 
475  public: std::string description;
476 
478  public: bool copyChildren;
479 
481  public: ElementWeakPtr parent;
482 
483  // Attributes of this element
485 
486  // Value of this element
487  public: ParamPtr value;
488 
489  // The existing child elements
490  public: ElementPtr_V elements;
491 
492  // The possible child elements
493  public: ElementPtr_V elementDescriptions;
494 
515  public: ElementPtr includeElement;
516 
518  public: std::string includeFilename;
519 
521  public: std::string referenceSDF;
522 
524  public: std::string path;
525 
527  public: std::string originalVersion;
528  };
529 
531  template<typename T>
532  T Element::Get(const std::string &_key) const
533  {
534  T result = T();
535 
536  std::pair<T, bool> ret = this->Get<T>(_key, result);
537 
538  return ret.first;
539  }
540 
542  template<typename T>
543  bool Element::Get(const std::string &_key,
544  T &_param,
545  const T &_defaultValue) const
546  {
547  std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
548  _param = ret.first;
549  return ret.second;
550  }
551 
553  template<typename T>
554  std::pair<T, bool> Element::Get(const std::string &_key,
555  const T &_defaultValue) const
556  {
557  std::pair<T, bool> result(_defaultValue, true);
558 
559  if (_key.empty() && this->dataPtr->value)
560  {
561  this->dataPtr->value->Get<T>(result.first);
562  }
563  else if (!_key.empty())
564  {
565  ParamPtr param = this->GetAttribute(_key);
566  if (param)
567  {
568  param->Get(result.first);
569  }
570  else if (this->HasElement(_key))
571  {
572  result.first = this->GetElementImpl(_key)->Get<T>();
573  }
574  else if (this->HasElementDescription(_key))
575  {
576  result.first = this->GetElementDescription(_key)->Get<T>();
577  }
578  else
579  {
580  result.second = false;
581  }
582  }
583  else
584  {
585  result.second = false;
586  }
587 
588  return result;
589  }
590 
592  template<typename T>
593  bool Element::Set(const T &_value)
594  {
595  if (this->dataPtr->value)
596  {
597  this->dataPtr->value->Set(_value);
598  return true;
599  }
600  return false;
601  }
603  }
604 }
605 
606 #ifdef _WIN32
607 #pragma warning(pop)
608 #endif
609 
610 #endif
std::string name
Element name.
Definition: Element.hh:469
ParamPtr value
Definition: Element.hh:487
SDF Element class.
Definition: Element.hh:68
std::string originalVersion
Spec version that this was originally parsed from.
Definition: Element.hh:527
std::string description
Element description.
Definition: Element.hh:475
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:518
ElementPtr includeElement
The element that was used to load this entity.
Definition: Element.hh:515
bool copyChildren
True if element&#39;s children should be copied.
Definition: Element.hh:478
ElementPtr_V elements
Definition: Element.hh:490
std::string required
True if element is required.
Definition: Element.hh:472
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:61
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:521
std::string path
Path to file where this element came from.
Definition: Element.hh:524
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:41
Definition: Element.hh:466
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:53
class SDFORMAT_VISIBLE Element
Definition: Element.hh:49
ElementWeakPtr parent
Element&#39;s parent.
Definition: Element.hh:481
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
ElementPtr_V elementDescriptions
Definition: Element.hh:493
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:57
#define SDF_DEPRECATED(version)
Definition: Types.hh:39
Param_V attributes
Definition: Element.hh:484