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: void RemoveAttribute(const std::string &_key);
247 
249  public: void RemoveAllAttributes();
250 
253  public: ParamPtr GetValue() const;
254 
259  public: std::any GetAny(const std::string &_key = "") const;
260 
267  public: template<typename T>
268  T Get(const std::string &_key = "") const;
269 
276  public: template<typename T>
277  std::pair<T, bool> Get(const std::string &_key,
278  const T &_defaultValue) const;
279 
286  public: template<typename T>
287  bool Get(const std::string &_key,
288  T &_param,
289  const T &_defaultValue) const;
290 
294  public: template<typename T>
295  bool Set(const T &_value);
296 
300  public: bool HasElement(const std::string &_name) const;
301 
305  public: ElementPtr GetFirstElement() const;
306 
318  public: ElementPtr GetNextElement(const std::string &_name = "") const;
319 
322  public: std::set<std::string> GetElementTypeNames() const;
323 
331  public: bool HasUniqueChildNames(const std::string &_type = "") const;
332 
340  public: std::map<std::string, std::size_t>
341  CountNamedElements(const std::string &_type = "") const;
342 
352  public: ElementPtr GetElement(const std::string &_name);
353 
357  public: ElementPtr AddElement(const std::string &_name);
358 
361  public: void InsertElement(ElementPtr _elem);
362 
364  public: void RemoveFromParent();
365 
368  public: void RemoveChild(ElementPtr _child);
369 
371  public: void ClearElements();
372 
375  public: void Clear();
376 
379  public: void Update();
380 
384  public: void Reset();
385 
388  public: void SetInclude(const std::string &_filename);
389 
392  public: std::string GetInclude() const;
393 
396  public: void SetFilePath(const std::string &_path);
397 
400  public: const std::string &FilePath() const;
401 
404  public: void SetOriginalVersion(const std::string &_version);
405 
408  public: const std::string &OriginalVersion() const;
409 
412  public: std::string GetDescription() const;
413 
416  public: void SetDescription(const std::string &_desc);
417 
420  public: void AddElementDescription(ElementPtr _elem);
421 
425  public: ElementPtr GetElementImpl(const std::string &_name) const;
426 
430  private: void ToString(const std::string &_prefix,
431  std::ostringstream &_out) const;
432 
436  private: void PrintValuesImpl(const std::string &_prefix,
437  std::ostringstream &_out) const;
438 
447  private: ParamPtr CreateParam(const std::string &_key,
448  const std::string &_type,
449  const std::string &_defaultValue,
450  bool _required,
451  const std::string &_description="");
452 
453 
455  private: std::unique_ptr<ElementPrivate> dataPtr;
456  };
457 
461  {
463  public: std::string name;
464 
466  public: std::string required;
467 
469  public: std::string description;
470 
472  public: bool copyChildren;
473 
476 
477  // Attributes of this element
479 
480  // Value of this element
481  public: ParamPtr value;
482 
483  // The existing child elements
485 
486  // The possible child elements
488 
490  public: std::string includeFilename;
491 
493  public: std::string referenceSDF;
494 
496  public: std::string path;
497 
499  public: std::string originalVersion;
500  };
501 
503  template<typename T>
504  T Element::Get(const std::string &_key) const
505  {
506  T result = T();
507 
508  std::pair<T, bool> ret = this->Get<T>(_key, result);
509 
510  return ret.first;
511  }
512 
514  template<typename T>
515  bool Element::Get(const std::string &_key,
516  T &_param,
517  const T &_defaultValue) const
518  {
519  std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
520  _param = ret.first;
521  return ret.second;
522  }
523 
525  template<typename T>
526  std::pair<T, bool> Element::Get(const std::string &_key,
527  const T &_defaultValue) const
528  {
529  std::pair<T, bool> result(_defaultValue, true);
530 
531  if (_key.empty() && this->dataPtr->value)
532  {
533  this->dataPtr->value->Get<T>(result.first);
534  }
535  else if (!_key.empty())
536  {
537  ParamPtr param = this->GetAttribute(_key);
538  if (param)
539  {
540  param->Get(result.first);
541  }
542  else if (this->HasElement(_key))
543  {
544  result.first = this->GetElementImpl(_key)->Get<T>();
545  }
546  else if (this->HasElementDescription(_key))
547  {
548  result.first = this->GetElementDescription(_key)->Get<T>();
549  }
550  else
551  {
552  result.second = false;
553  }
554  }
555  else
556  {
557  result.second = false;
558  }
559 
560  return result;
561  }
562 
564  template<typename T>
565  bool Element::Set(const T &_value)
566  {
567  if (this->dataPtr->value)
568  {
569  this->dataPtr->value->Set(_value);
570  return true;
571  }
572  return false;
573  }
575  }
576 }
577 
578 #ifdef _WIN32
579 #pragma warning(pop)
580 #endif
581 
582 #endif
sdf::v10::Element
SDF Element class.
Definition: Element.hh:68
sdf
namespace for Simulation Description Format parser
Definition: Actor.hh:32
sdf::v10::Param_V
std::vector< ParamPtr > Param_V
Definition: Param.hh:62
sdf::v10::ElementPrivate::required
std::string required
True if element is required.
Definition: Element.hh:466
sdf::v10::ElementPrivate::attributes
Param_V attributes
Definition: Element.hh:478
sdf::v10::ElementPtr_V
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:61
Types.hh
sdf::v10::ElementPrivate::originalVersion
std::string originalVersion
Spec version that this was originally parsed from.
Definition: Element.hh:499
sdf::v10::ElementPrivate::copyChildren
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:472
sdf::v10::ElementPtr
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:53
SDFORMAT_VISIBLE
#define SDFORMAT_VISIBLE
Definition: system_util.hh:48
sdf::v10::ParamPtr
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:58
sdf::v10::ElementWeakPtr
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:57
Param.hh
sdf::v10::ElementPrivate
Definition: Element.hh:460
sdf::v10::ElementPrivate::includeFilename
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:490
sdf::v10::ElementPrivate::value
ParamPtr value
Definition: Element.hh:481
sdf::v10::ElementPrivate::parent
ElementWeakPtr parent
Element's parent.
Definition: Element.hh:475
sdf::v10::ElementPrivate::elements
ElementPtr_V elements
Definition: Element.hh:484
sdf::v10::ElementPrivate::referenceSDF
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:493
sdf::v10::ElementPrivate::name
std::string name
Element name.
Definition: Element.hh:463
sdf::v10::ElementPrivate::elementDescriptions
ElementPtr_V elementDescriptions
Definition: Element.hh:487
system_util.hh
sdf::v10::ElementPrivate::description
std::string description
Element description.
Definition: Element.hh:469
sdf::v10::ElementPrivate::path
std::string path
Path to file where this element came from.
Definition: Element.hh:496
sdf::v10::Element
class SDFORMAT_VISIBLE Element
Definition: Element.hh:49