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 
119  public: void SetCopyChildren(bool _value);
120 
124  public: bool GetCopyChildren() const;
125 
128  public: void SetReferenceSDF(const std::string &_value);
129 
132  public: std::string ReferenceSDF() const;
133 
136  public: void PrintDescription(const std::string &_prefix) const;
137 
140  public: void PrintValues(std::string _prefix) const;
141 
148  public: void PrintDocLeftPane(std::string &_html,
149  int _spacing, int &_index) const;
150 
156  public: void PrintDocRightPane(std::string &_html,
157  int _spacing, int &_index) const;
158 
162  public: std::string ToString(const std::string &_prefix) const;
163 
171  public: void AddAttribute(const std::string &_key,
172  const std::string &_type,
173  const std::string &_defaultvalue,
174  bool _required,
175  const std::string &_description="");
176 
183  public: void AddValue(const std::string &_type,
184  const std::string &_defaultValue, bool _required,
185  const std::string &_description="");
186 
196  public: void AddValue(const std::string &_type,
197  const std::string &_defaultValue, bool _required,
198  const std::string &_minValue,
199  const std::string &_maxValue,
200  const std::string &_description = "");
201 
205  public: ParamPtr GetAttribute(const std::string &_key) const;
206 
209  public: size_t GetAttributeCount() const;
210 
214  public: ParamPtr GetAttribute(unsigned int _index) const;
215 
218  public: size_t GetElementDescriptionCount() const;
219 
223  public: ElementPtr GetElementDescription(unsigned int _index) const;
224 
228  public: ElementPtr GetElementDescription(const std::string &_key) const;
229 
233  public: bool HasElementDescription(const std::string &_name) const;
234 
238  public: bool HasAttribute(const std::string &_key) const;
239 
243  public: bool GetAttributeSet(const std::string &_key) const;
244 
247  public: void RemoveAttribute(const std::string &_key);
248 
250  public: void RemoveAllAttributes();
251 
254  public: ParamPtr GetValue() const;
255 
260  public: std::any GetAny(const std::string &_key = "") const;
261 
268  public: template<typename T>
269  T Get(const std::string &_key = "") const;
270 
277  public: template<typename T>
278  std::pair<T, bool> Get(const std::string &_key,
279  const T &_defaultValue) const;
280 
287  public: template<typename T>
288  bool Get(const std::string &_key,
289  T &_param,
290  const T &_defaultValue) const;
291 
295  public: template<typename T>
296  bool Set(const T &_value);
297 
301  public: bool HasElement(const std::string &_name) const;
302 
306  public: ElementPtr GetFirstElement() const;
307 
319  public: ElementPtr GetNextElement(const std::string &_name = "") const;
320 
323  public: std::set<std::string> GetElementTypeNames() const;
324 
332  public: bool HasUniqueChildNames(const std::string &_type = "") const;
333 
341  public: std::map<std::string, std::size_t>
342  CountNamedElements(const std::string &_type = "") const;
343 
353  public: ElementPtr GetElement(const std::string &_name);
354 
358  public: ElementPtr AddElement(const std::string &_name);
359 
362  public: void InsertElement(ElementPtr _elem);
363 
365  public: void RemoveFromParent();
366 
369  public: void RemoveChild(ElementPtr _child);
370 
372  public: void ClearElements();
373 
376  public: void Clear();
377 
380  public: void Update();
381 
385  public: void Reset();
386 
389  public: void SetInclude(const std::string &_filename) SDF_DEPRECATED(11.0);
390 
393  public: std::string GetInclude() const SDF_DEPRECATED(11.0);
394 
400  public: void SetIncludeElement(sdf::ElementPtr _includeElem);
401 
406  public: sdf::ElementPtr GetIncludeElement() const;
407 
410  public: void SetFilePath(const std::string &_path);
411 
414  public: const std::string &FilePath() const;
415 
418  public: void SetOriginalVersion(const std::string &_version);
419 
422  public: const std::string &OriginalVersion() const;
423 
426  public: std::string GetDescription() const;
427 
430  public: void SetDescription(const std::string &_desc);
431 
434  public: void AddElementDescription(ElementPtr _elem);
435 
439  public: ElementPtr GetElementImpl(const std::string &_name) const;
440 
444  private: void ToString(const std::string &_prefix,
445  std::ostringstream &_out) const;
446 
450  private: void PrintValuesImpl(const std::string &_prefix,
451  std::ostringstream &_out) const;
452 
461  private: ParamPtr CreateParam(const std::string &_key,
462  const std::string &_type,
463  const std::string &_defaultValue,
464  bool _required,
465  const std::string &_description="");
466 
467 
469  private: std::unique_ptr<ElementPrivate> dataPtr;
470  };
471 
475  {
477  public: std::string name;
478 
480  public: std::string required;
481 
483  public: std::string description;
484 
486  public: bool copyChildren;
487 
490 
491  // Attributes of this element
493 
494  // Value of this element
495  public: ParamPtr value;
496 
497  // The existing child elements
499 
500  // The possible child elements
502 
524 
526  public: std::string includeFilename;
527 
529  public: std::string referenceSDF;
530 
532  public: std::string path;
533 
535  public: std::string originalVersion;
536  };
537 
539  template<typename T>
540  T Element::Get(const std::string &_key) const
541  {
542  T result = T();
543 
544  std::pair<T, bool> ret = this->Get<T>(_key, result);
545 
546  return ret.first;
547  }
548 
550  template<typename T>
551  bool Element::Get(const std::string &_key,
552  T &_param,
553  const T &_defaultValue) const
554  {
555  std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
556  _param = ret.first;
557  return ret.second;
558  }
559 
561  template<typename T>
562  std::pair<T, bool> Element::Get(const std::string &_key,
563  const T &_defaultValue) const
564  {
565  std::pair<T, bool> result(_defaultValue, true);
566 
567  if (_key.empty() && this->dataPtr->value)
568  {
569  this->dataPtr->value->Get<T>(result.first);
570  }
571  else if (!_key.empty())
572  {
573  ParamPtr param = this->GetAttribute(_key);
574  if (param)
575  {
576  param->Get(result.first);
577  }
578  else if (this->HasElement(_key))
579  {
580  result.first = this->GetElementImpl(_key)->Get<T>();
581  }
582  else if (this->HasElementDescription(_key))
583  {
584  result.first = this->GetElementDescription(_key)->Get<T>();
585  }
586  else
587  {
588  result.second = false;
589  }
590  }
591  else
592  {
593  result.second = false;
594  }
595 
596  return result;
597  }
598 
600  template<typename T>
601  bool Element::Set(const T &_value)
602  {
603  if (this->dataPtr->value)
604  {
605  this->dataPtr->value->Set(_value);
606  return true;
607  }
608  return false;
609  }
611  }
612 }
613 
614 #ifdef _WIN32
615 #pragma warning(pop)
616 #endif
617 
618 #endif
sdf::v11::ElementPrivate::value
ParamPtr value
Definition: Element.hh:495
sdf::v11::ElementPrivate::includeElement
ElementPtr includeElement
The element that was used to load this entity.
Definition: Element.hh:523
sdf::v11::ElementPrivate::description
std::string description
Element description.
Definition: Element.hh:483
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:498
sdf::v11::ElementPrivate::copyChildren
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:486
sdf::v11::ElementPrivate::originalVersion
std::string originalVersion
Spec version that this was originally parsed from.
Definition: Element.hh:535
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:480
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:529
sdf::v11::ElementPrivate::path
std::string path
Path to file where this element came from.
Definition: Element.hh:532
Param.hh
sdf::v11::ElementPrivate::includeFilename
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:526
sdf::v11::ElementPrivate::parent
ElementWeakPtr parent
Element's parent.
Definition: Element.hh:489
sdf::v11::ElementPrivate::elementDescriptions
ElementPtr_V elementDescriptions
Definition: Element.hh:501
sdf::v11::ElementPrivate::attributes
Param_V attributes
Definition: Element.hh:492
system_util.hh
sdf::v11::ElementPrivate::name
std::string name
Element name.
Definition: Element.hh:477
sdf::v11::Element
class SDFORMAT_VISIBLE Element
Definition: Element.hh:49
sdf::v11::ElementPrivate
Definition: Element.hh:474
sdf::v11::ElementPtr_V
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:61