All Classes Namespaces Files Functions Variables Typedefs 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 <string>
21 #include <vector>
22 #include <memory>
23 #include <utility>
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);
128 
131  public: void PrintValues(std::string _prefix);
132 
133  public: void PrintWiki(std::string _prefix);
134 
141  public: void PrintDocLeftPane(std::string &_html,
142  int _spacing, int &_index);
143 
149  public: void PrintDocRightPane(std::string &_html,
150  int _spacing, int &_index);
151 
155  public: std::string ToString(const std::string &_prefix) const;
156 
163  public: void AddAttribute(const std::string &_key,
164  const std::string &_type,
165  const std::string &_defaultvalue,
166  bool _required,
167  const std::string &_description="");
168 
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);
182 
184  public: size_t GetAttributeCount() const;
185 
187  public: ParamPtr GetAttribute(unsigned int _index) const;
188 
190  public: size_t GetElementDescriptionCount() const;
191 
193  public: ElementPtr GetElementDescription(unsigned int _index) const;
194 
196  public: ElementPtr GetElementDescription(const std::string &_key) const;
197 
199  public: bool HasElementDescription(const std::string &_name);
200 
201  public: bool HasAttribute(const std::string &_key);
202 
204  public: bool GetAttributeSet(const std::string &_key);
205 
207  public: ParamPtr GetValue();
208 
213  public: boost::any GetAny(const std::string &_key = "");
214 
221  public: template<typename T>
222  T Get(const std::string &_key = "");
223 
230  public: template<typename T>
231  std::pair<T, bool> Get(const std::string &_key,
232  const T &_defaultValue);
233 
234  public: template<typename T>
235  bool Set(const T &_value);
236 
237  public: bool HasElement(const std::string &_name) const;
238 
239  public: ElementPtr GetElement(const std::string &_name) const;
240 
244  public: ElementPtr GetFirstElement() const;
245 
256  public: ElementPtr GetNextElement(const std::string &_name = "") const;
257 
267  public: ElementPtr GetElement(const std::string &_name);
268 
269  public: ElementPtr AddElement(const std::string &_name);
270  public: void InsertElement(ElementPtr _elem);
271 
273  public: void RemoveFromParent();
274 
277  public: void RemoveChild(ElementPtr _child);
278 
280  public: void ClearElements();
281 
282  public: void Update();
283  public: void Reset();
284 
285  public: void SetInclude(const std::string &_filename);
286  public: std::string GetInclude() const;
287 
289  public: std::string GetDescription() const;
290 
292  public: void SetDescription(const std::string &_desc);
293 
295  public: void AddElementDescription(ElementPtr _elem);
296 
297  public: ElementPtr GetElementImpl(const std::string &_name) const;
298 
299  private: void ToString(const std::string &_prefix,
300  std::ostringstream &_out) const;
301 
302 
303  private: ParamPtr CreateParam(const std::string &_key,
304  const std::string &_type, const std::string &_defaultValue,
305  bool _required, const std::string &_description="");
306 
307 
309  private: ElementPrivate *dataPtr;
310  };
311 
315  {
317  public: std::string name;
318 
320  public: std::string required;
321 
323  public: std::string description;
324 
326  public: bool copyChildren;
327 
330 
331  // Attributes of this element
333 
334  // Value of this element
335  public: ParamPtr value;
336 
337  // The existing child elements
339 
340  // The possible child elements
342 
344  public: std::string includeFilename;
345 
347  public: std::string referenceSDF;
348  };
349 
351  template<typename T>
352  T Element::Get(const std::string &_key)
353  {
354  T result = T();
355 
356  if (_key.empty() && this->dataPtr->value)
357  this->dataPtr->value->Get<T>(result);
358  else if (!_key.empty())
359  {
360  ParamPtr param = this->GetAttribute(_key);
361  if (param)
362  param->Get(result);
363  else if (this->HasElement(_key))
364  result = this->GetElementImpl(_key)->Get<T>();
365  else if (this->HasElementDescription(_key))
366  result = this->GetElementDescription(_key)->Get<T>();
367  else
368  sdferr << "Unable to find value for key[" << _key << "]\n";
369  }
370  return result;
371  }
372 
374  template<typename T>
375  std::pair<T, bool> Element::Get(const std::string &_key,
376  const T &_defaultValue)
377  {
378  std::pair<T, bool> result(_defaultValue, true);
379 
380  if (_key.empty() && this->dataPtr->value)
381  this->dataPtr->value->Get<T>(result.first);
382  else if (!_key.empty())
383  {
384  ParamPtr param = this->GetAttribute(_key);
385  if (param)
386  param->Get(result.first);
387  else if (this->HasElement(_key))
388  result.first = this->GetElementImpl(_key)->Get<T>();
389  else if (this->HasElementDescription(_key))
390  result.first = this->GetElementDescription(_key)->Get<T>();
391  else
392  result.second = false;
393  }
394  else
395  {
396  result.second = false;
397  }
398 
399  return result;
400  }
401 
403  template<typename T>
404  bool Element::Set(const T &_value)
405  {
406  if (this->dataPtr->value)
407  {
408  this->dataPtr->value->Set(_value);
409  return true;
410  }
411  return false;
412  }
414 }
415 
416 #ifdef _WIN32
417 #pragma warning(pop)
418 #endif
419 
420 #endif
ElementPtr_V elementDescriptions
Definition: Element.hh:341
bool Set(const T &_value)
Definition: Element.hh:404
Definition: Element.hh:314
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:49
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:347
ElementPtr GetElementImpl(const std::string &_name) const
class SDFORMAT_VISIBLE Element
Definition: Element.hh:41
bool HasElementDescription(const std::string &_name)
Return true if an element description exists.
std::string name
Element name.
Definition: Element.hh:317
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:53
T Get(const std::string &_key="")
Get the value of a key.
Definition: Element.hh:352
ElementPtr GetElementDescription(unsigned int _index) const
Get an element description using an index.
SDF Element class.
Definition: Element.hh:60
std::string required
True if element is required.
Definition: Element.hh:320
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
#define sdferr
Output an error message.
Definition: Console.hh:54
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:344
std::string description
Element description.
Definition: Element.hh:323
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:47
std::vector< ParamPtr > Param_V
Definition: Param.hh:51
ElementWeakPtr parent
Element's parent.
Definition: Element.hh:329
bool HasElement(const std::string &_name) const
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:326
Param_V attributes
Definition: Element.hh:332
ParamPtr GetAttribute(const std::string &_key)
Get the param of an attribute.
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:45
ElementPtr_V elements
Definition: Element.hh:338
ParamPtr value
Definition: Element.hh:335