All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "sdf/Param.hh"
26 #include "sdf/system_util.hh"
27 #include "sdf/Types.hh"
28 
29 #ifdef _WIN32
30 // Disable warning C4251 which is triggered by
31 // std::enable_shared_from_this
32 #pragma warning(push)
33 #pragma warning(disable: 4251)
34 #endif
35 
38 namespace sdf
39 {
40  class ElementPrivate;
42 
45  typedef std::shared_ptr<Element> ElementPtr;
46 
49  typedef std::weak_ptr<Element> ElementWeakPtr;
50 
53  typedef std::vector<ElementPtr> ElementPtr_V;
54 
57 
61  public std::enable_shared_from_this<Element>
62  {
64  public: Element();
65 
67  public: virtual ~Element();
68 
71  public: ElementPtr Clone() const;
72 
75  public: void Copy(const ElementPtr _elem);
76 
80  public: ElementPtr GetParent() const;
81 
84  public: void SetParent(const ElementPtr _parent);
85 
88  public: void SetName(const std::string &_name);
89 
92  public: const std::string &GetName() const;
93 
100  public: void SetRequired(const std::string &_req);
101 
105  public: const std::string &GetRequired() const;
106 
110  public: void SetCopyChildren(bool _value);
111 
115  public: bool GetCopyChildren() const;
116 
119  public: void SetReferenceSDF(const std::string &_value);
120 
123  public: std::string ReferenceSDF() const;
124 
127  public: void PrintDescription(const std::string &_prefix) const;
128 
131  public: void PrintValues(std::string _prefix) const;
132 
139  public: void PrintDocLeftPane(std::string &_html,
140  int _spacing, int &_index) const;
141 
147  public: void PrintDocRightPane(std::string &_html,
148  int _spacing, int &_index) const;
149 
153  public: std::string ToString(const std::string &_prefix) const;
154 
162  public: void AddAttribute(const std::string &_key,
163  const std::string &_type,
164  const std::string &_defaultvalue,
165  bool _required,
166  const std::string &_description="");
167 
174  public: void AddValue(const std::string &_type,
175  const std::string &_defaultValue, bool _required,
176  const std::string &_description="");
177 
181  public: ParamPtr GetAttribute(const std::string &_key) const;
182 
185  public: size_t GetAttributeCount() const;
186 
190  public: ParamPtr GetAttribute(unsigned int _index) const;
191 
194  public: size_t GetElementDescriptionCount() const;
195 
199  public: ElementPtr GetElementDescription(unsigned int _index) const;
200 
204  public: ElementPtr GetElementDescription(const std::string &_key) const;
205 
209  public: bool HasElementDescription(const std::string &_name) const;
210 
214  public: bool HasAttribute(const std::string &_key) const;
215 
219  public: bool GetAttributeSet(const std::string &_key) const;
220 
223  public: ParamPtr GetValue() const;
224 
229  public: boost::any GetAny(const std::string &_key = "") const;
230 
237  public: template<typename T>
238  T Get(const std::string &_key = "") const;
239 
246  public: template<typename T>
247  std::pair<T, bool> Get(const std::string &_key,
248  const T &_defaultValue) const;
249 
256  public: template<typename T>
257  bool Get(const std::string &_key,
258  T &_param,
259  const T &_defaultValue) const;
260 
264  public: template<typename T>
265  bool Set(const T &_value);
266 
270  public: bool HasElement(const std::string &_name) const;
271 
275  public: ElementPtr GetFirstElement() const;
276 
288  public: ElementPtr GetNextElement(const std::string &_name = "") const;
289 
299  public: ElementPtr GetElement(const std::string &_name);
300 
304  public: ElementPtr AddElement(const std::string &_name);
305 
308  public: void InsertElement(ElementPtr _elem);
309 
311  public: void RemoveFromParent();
312 
315  public: void RemoveChild(ElementPtr _child);
316 
318  public: void ClearElements();
319 
322  public: void Update();
323 
327  public: void Reset();
328 
331  public: void SetInclude(const std::string &_filename);
332 
335  public: std::string GetInclude() const;
336 
339  public: std::string GetDescription() const;
340 
343  public: void SetDescription(const std::string &_desc);
344 
347  public: void AddElementDescription(ElementPtr _elem);
348 
352  public: ElementPtr GetElementImpl(const std::string &_name) const;
353 
357  private: void ToString(const std::string &_prefix,
358  std::ostringstream &_out) const;
359 
363  private: void PrintValuesImpl(const std::string &_prefix,
364  std::ostringstream &_out) const;
365 
374  private: ParamPtr CreateParam(const std::string &_key,
375  const std::string &_type,
376  const std::string &_defaultValue,
377  bool _required,
378  const std::string &_description="");
379 
380 
382  private: std::unique_ptr<ElementPrivate> dataPtr;
383  };
384 
388  {
390  public: std::string name;
391 
393  public: std::string required;
394 
396  public: std::string description;
397 
399  public: bool copyChildren;
400 
403 
404  // Attributes of this element
406 
407  // Value of this element
408  public: ParamPtr value;
409 
410  // The existing child elements
412 
413  // The possible child elements
415 
417  public: std::string includeFilename;
418 
420  public: std::string referenceSDF;
421  };
422 
424  template<typename T>
425  T Element::Get(const std::string &_key) const
426  {
427  T result = T();
428 
429  std::pair<T, bool> ret = this->Get<T>(_key, result);
430 
431  return ret.first;
432  }
433 
435  template<typename T>
436  bool Element::Get(const std::string &_key,
437  T &_param,
438  const T &_defaultValue) const
439  {
440  std::pair<T, bool> ret = this->Get<T>(_key, _defaultValue);
441  _param = ret.first;
442  return ret.second;
443  }
444 
446  template<typename T>
447  std::pair<T, bool> Element::Get(const std::string &_key,
448  const T &_defaultValue) const
449  {
450  std::pair<T, bool> result(_defaultValue, true);
451 
452  if (_key.empty() && this->dataPtr->value)
453  {
454  this->dataPtr->value->Get<T>(result.first);
455  }
456  else if (!_key.empty())
457  {
458  ParamPtr param = this->GetAttribute(_key);
459  if (param)
460  {
461  param->Get(result.first);
462  }
463  else if (this->HasElement(_key))
464  {
465  result.first = this->GetElementImpl(_key)->Get<T>();
466  }
467  else if (this->HasElementDescription(_key))
468  {
469  result.first = this->GetElementDescription(_key)->Get<T>();
470  }
471  else
472  {
473  result.second = false;
474  }
475  }
476  else
477  {
478  result.second = false;
479  }
480 
481  return result;
482  }
483 
485  template<typename T>
486  bool Element::Set(const T &_value)
487  {
488  if (this->dataPtr->value)
489  {
490  this->dataPtr->value->Set(_value);
491  return true;
492  }
493  return false;
494  }
496 }
497 
498 #ifdef _WIN32
499 #pragma warning(pop)
500 #endif
501 
502 #endif
ParamPtr GetAttribute(const std::string &_key) const
Get the param of an attribute.
ElementPtr_V elementDescriptions
Definition: Element.hh:414
bool Set(const T &_value)
Set the value of this element.
Definition: Element.hh:486
Definition: Element.hh:387
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:49
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:420
ElementPtr GetElementImpl(const std::string &_name) const
Get a pointer to the named element.
class SDFORMAT_VISIBLE Element
Definition: Element.hh:41
std::string name
Element name.
Definition: Element.hh:390
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:53
ElementPtr GetElementDescription(unsigned int _index) const
Get an element description using an index.
SDF Element class.
Definition: Element.hh:60
T Get(const std::string &_key="") const
Get the value of a key.
Definition: Element.hh:425
std::string required
True if element is required.
Definition: Element.hh:393
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:417
std::string description
Element description.
Definition: Element.hh:396
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:55
std::vector< ParamPtr > Param_V
Definition: Param.hh:59
ElementWeakPtr parent
Element's parent.
Definition: Element.hh:402
bool HasElement(const std::string &_name) const
Return true if the named element exists.
bool HasElementDescription(const std::string &_name) const
Return true if an element description exists.
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:399
Param_V attributes
Definition: Element.hh:405
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:45
ElementPtr_V elements
Definition: Element.hh:411
ParamPtr value
Definition: Element.hh:408