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 <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include "sdf/Param.hh"
27 #include "sdf/sdf_config.h"
28 #include "sdf/system_util.hh"
29 #include "sdf/Types.hh"
30 
31 #ifdef _WIN32
32 // Disable warning C4251 which is triggered by
33 // std::enable_shared_from_this
34 #pragma warning(push)
35 #pragma warning(disable: 4251)
36 #endif
37 
40 namespace sdf
41 {
42  // Inline bracke to help doxygen filtering.
43  inline namespace SDF_VERSION_NAMESPACE {
44  //
45 
46  class ElementPrivate;
48 
51  typedef std::shared_ptr<Element> ElementPtr;
52 
55  typedef std::weak_ptr<Element> ElementWeakPtr;
56 
59  typedef std::vector<ElementPtr> ElementPtr_V;
60 
63 
67  public std::enable_shared_from_this<Element>
68  {
70  public: Element();
71 
73  public: virtual ~Element();
74 
77  public: ElementPtr Clone() const;
78 
81  public: void Copy(const ElementPtr _elem);
82 
86  public: ElementPtr GetParent() const;
87 
90  public: void SetParent(const ElementPtr _parent);
91 
94  public: void SetName(const std::string &_name);
95 
98  public: const std::string &GetName() const;
99 
106  public: void SetRequired(const std::string &_req);
107 
111  public: const std::string &GetRequired() const;
112 
116  public: void SetCopyChildren(bool _value);
117 
121  public: bool GetCopyChildren() const;
122 
125  public: void SetReferenceSDF(const std::string &_value);
126 
129  public: std::string ReferenceSDF() const;
130 
133  public: void PrintDescription(const std::string &_prefix) const;
134 
137  public: void PrintValues(std::string _prefix) const;
138 
145  public: void PrintDocLeftPane(std::string &_html,
146  int _spacing, int &_index) const;
147 
153  public: void PrintDocRightPane(std::string &_html,
154  int _spacing, int &_index) const;
155 
159  public: std::string ToString(const std::string &_prefix) const;
160 
168  public: void AddAttribute(const std::string &_key,
169  const std::string &_type,
170  const std::string &_defaultvalue,
171  bool _required,
172  const std::string &_description="");
173 
180  public: void AddValue(const std::string &_type,
181  const std::string &_defaultValue, bool _required,
182  const std::string &_description="");
183 
187  public: ParamPtr GetAttribute(const std::string &_key) const;
188 
191  public: size_t GetAttributeCount() const;
192 
196  public: ParamPtr GetAttribute(unsigned int _index) const;
197 
200  public: size_t GetElementDescriptionCount() const;
201 
205  public: ElementPtr GetElementDescription(unsigned int _index) const;
206 
210  public: ElementPtr GetElementDescription(const std::string &_key) const;
211 
215  public: bool HasElementDescription(const std::string &_name) const;
216 
220  public: bool HasAttribute(const std::string &_key) const;
221 
225  public: bool GetAttributeSet(const std::string &_key) const;
226 
229  public: ParamPtr GetValue() const;
230 
235  public: std::any GetAny(const std::string &_key = "") const;
236 
243  public: template<typename T>
244  T Get(const std::string &_key = "") const;
245 
252  public: template<typename T>
253  std::pair<T, bool> Get(const std::string &_key,
254  const T &_defaultValue) const;
255 
262  public: template<typename T>
263  bool Get(const std::string &_key,
264  T &_param,
265  const T &_defaultValue) const;
266 
270  public: template<typename T>
271  bool Set(const T &_value);
272 
276  public: bool HasElement(const std::string &_name) const;
277 
281  public: ElementPtr GetFirstElement() const;
282 
294  public: ElementPtr GetNextElement(const std::string &_name = "") const;
295 
305  public: ElementPtr GetElement(const std::string &_name);
306 
310  public: ElementPtr AddElement(const std::string &_name);
311 
314  public: void InsertElement(ElementPtr _elem);
315 
317  public: void RemoveFromParent();
318 
321  public: void RemoveChild(ElementPtr _child);
322 
324  public: void ClearElements();
325 
328  public: void Update();
329 
333  public: void Reset();
334 
337  public: void SetInclude(const std::string &_filename);
338 
341  public: std::string GetInclude() const;
342 
345  public: std::string GetDescription() const;
346 
349  public: void SetDescription(const std::string &_desc);
350 
353  public: void AddElementDescription(ElementPtr _elem);
354 
358  public: ElementPtr GetElementImpl(const std::string &_name) const;
359 
363  private: void ToString(const std::string &_prefix,
364  std::ostringstream &_out) const;
365 
369  private: void PrintValuesImpl(const std::string &_prefix,
370  std::ostringstream &_out) const;
371 
380  private: ParamPtr CreateParam(const std::string &_key,
381  const std::string &_type,
382  const std::string &_defaultValue,
383  bool _required,
384  const std::string &_description="");
385 
386 
388  private: std::unique_ptr<ElementPrivate> dataPtr;
389  };
390 
394  {
396  public: std::string name;
397 
399  public: std::string required;
400 
402  public: std::string description;
403 
405  public: bool copyChildren;
406 
408  public: ElementWeakPtr parent;
409 
410  // Attributes of this element
412 
413  // Value of this element
414  public: ParamPtr value;
415 
416  // The existing child elements
417  public: ElementPtr_V elements;
418 
419  // The possible child elements
420  public: ElementPtr_V elementDescriptions;
421 
423  public: std::string includeFilename;
424 
426  public: std::string referenceSDF;
427  };
428 
430  template<typename T>
431  T Element::Get(const std::string &_key) const
432  {
433  T result = T();
434 
435  std::pair<T, bool> ret = this->Get<T>(_key, result);
436 
437  return ret.first;
438  }
439 
441  template<typename T>
442  bool Element::Get(const std::string &_key,
443  T &_param,
444  const T &_defaultValue) const
445  {
446  std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
447  _param = ret.first;
448  return ret.second;
449  }
450 
452  template<typename T>
453  std::pair<T, bool> Element::Get(const std::string &_key,
454  const T &_defaultValue) const
455  {
456  std::pair<T, bool> result(_defaultValue, true);
457 
458  if (_key.empty() && this->dataPtr->value)
459  {
460  this->dataPtr->value->Get<T>(result.first);
461  }
462  else if (!_key.empty())
463  {
464  ParamPtr param = this->GetAttribute(_key);
465  if (param)
466  {
467  param->Get(result.first);
468  }
469  else if (this->HasElement(_key))
470  {
471  result.first = this->GetElementImpl(_key)->Get<T>();
472  }
473  else if (this->HasElementDescription(_key))
474  {
475  result.first = this->GetElementDescription(_key)->Get<T>();
476  }
477  else
478  {
479  result.second = false;
480  }
481  }
482  else
483  {
484  result.second = false;
485  }
486 
487  return result;
488  }
489 
491  template<typename T>
492  bool Element::Set(const T &_value)
493  {
494  if (this->dataPtr->value)
495  {
496  this->dataPtr->value->Set(_value);
497  return true;
498  }
499  return false;
500  }
502  }
503 }
504 
505 #ifdef _WIN32
506 #pragma warning(pop)
507 #endif
508 
509 #endif
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:423
std::string required
True if element is required.
Definition: Element.hh:399
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:426
class SDFORMAT_VISIBLE Element
Definition: Element.hh:47
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:59
SDF Element class.
Definition: Element.hh:66
Param_V attributes
Definition: Element.hh:411
ElementPtr_V elements
Definition: Element.hh:417
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:51
std::string name
Element name.
Definition: Element.hh:396
std::string description
Element description.
Definition: Element.hh:402
ElementPtr_V elementDescriptions
Definition: Element.hh:420
ParamPtr value
Definition: Element.hh:414
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
ElementWeakPtr parent
Element&#39;s parent.
Definition: Element.hh:408
bool copyChildren
True if element&#39;s children should be copied.
Definition: Element.hh:405
std::vector< ParamPtr > Param_V
Definition: Param.hh:61
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:55
Definition: Element.hh:393
namespace for Simulation Description Format parser
Definition: AirPressure.hh:25
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:57