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 
24 #include "sdf/Param.hh"
25 #include "sdf/system_util.hh"
26 
28 #ifndef _WIN32
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
31 #endif
32 #include "sdf/Types.hh"
33 #ifndef _WIN32
34 #pragma GCC diagnostic pop
35 #endif
36 
37 #ifdef _WIN32
38 // Disable warning C4251 which is triggered by
39 // std::enable_shared_from_this
40 #pragma warning(push)
41 #pragma warning(disable: 4251)
42 #endif
43 
46 namespace sdf
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);
136 
139  public: void PrintValues(std::string _prefix);
140 
141  public: void PrintWiki(std::string _prefix);
142 
149  public: void PrintDocLeftPane(std::string &_html,
150  int _spacing, int &_index);
151 
157  public: void PrintDocRightPane(std::string &_html,
158  int _spacing, int &_index);
159 
163  public: std::string ToString(const std::string &_prefix) const;
164 
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 
182  public: void AddValue(const std::string &_type,
183  const std::string &_defaultValue, bool _required,
184  const std::string &_description="");
185 
189  public: ParamPtr GetAttribute(const std::string &_key);
190 
192  public: size_t GetAttributeCount() const;
193 
195  public: ParamPtr GetAttribute(unsigned int _index) const;
196 
198  public: size_t GetElementDescriptionCount() const;
199 
201  public: ElementPtr GetElementDescription(unsigned int _index) const;
202 
204  public: ElementPtr GetElementDescription(const std::string &_key) const;
205 
207  public: bool HasElementDescription(const std::string &_name);
208 
209  public: bool HasAttribute(const std::string &_key);
210 
212  public: bool GetAttributeSet(const std::string &_key);
213 
215  public: ParamPtr GetValue();
216 
221  public: boost::any GetAny(const std::string &_key = "");
222 
223  public: template<typename T>
224  T Get(const std::string &_key = "");
225 
226  public: template<typename T>
227  bool Set(const T &_value);
228 
229  public: bool HasElement(const std::string &_name) const;
230 
231  public: ElementPtr GetElement(const std::string &_name) const;
232  public: ElementPtr GetFirstElement() const;
233 
234  public: ElementPtr GetNextElement(const std::string &_name = "") const;
235 
236  public: ElementPtr GetElement(const std::string &_name);
237  public: ElementPtr AddElement(const std::string &_name);
238  public: void InsertElement(ElementPtr _elem);
239 
241  public: void RemoveFromParent();
242 
245  public: void RemoveChild(ElementPtr _child);
246 
248  public: void ClearElements();
249 
250  public: void Update();
251  public: void Reset();
252 
253  public: void SetInclude(const std::string &_filename);
254  public: std::string GetInclude() const;
255 
257  public: std::string GetDescription() const;
258 
260  public: void SetDescription(const std::string &_desc);
261 
263  public: void AddElementDescription(ElementPtr _elem);
264 
265  public: ElementPtr GetElementImpl(const std::string &_name) const;
266 
267  private: void ToString(const std::string &_prefix,
268  std::ostringstream &_out) const;
269 
270 
271  private: ParamPtr CreateParam(const std::string &_key,
272  const std::string &_type, const std::string &_defaultValue,
273  bool _required, const std::string &_description="");
274 
275 
277  private: ElementPrivate *dataPtr;
278  };
279 
283  {
285  public: std::string name;
286 
288  public: std::string required;
289 
291  public: std::string description;
292 
294  public: bool copyChildren;
295 
298 
299  // Attributes of this element
301 
302  // Value of this element
303  public: ParamPtr value;
304 
305  // The existing child elements
307 
308  // The possible child elements
310 
312  public: std::string includeFilename;
313 
315  public: std::string referenceSDF;
316  };
317 
319  template<typename T>
320  T Element::Get(const std::string &_key)
321  {
322  T result = T();
323 
324  if (_key.empty() && this->dataPtr->value)
325  this->dataPtr->value->Get<T>(result);
326  else if (!_key.empty())
327  {
328  ParamPtr param = this->GetAttribute(_key);
329  if (param)
330  param->Get(result);
331  else if (this->HasElement(_key))
332  result = this->GetElementImpl(_key)->Get<T>();
333  else if (this->HasElementDescription(_key))
334  result = this->GetElementDescription(_key)->Get<T>();
335  else
336  sdferr << "Unable to find value for key[" << _key << "]\n";
337  }
338  return result;
339  }
340 
342  template<typename T>
343  bool Element::Set(const T &_value)
344  {
345  if (this->dataPtr->value)
346  {
347  this->dataPtr->value->Set(_value);
348  return true;
349  }
350  return false;
351  }
353 }
354 
355 #ifdef _WIN32
356 #pragma warning(pop)
357 #endif
358 
359 #endif
ElementPtr_V elementDescriptions
Definition: Element.hh:309
bool Set(const T &_value)
Definition: Element.hh:343
Definition: Element.hh:282
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:57
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:315
ElementPtr GetElementImpl(const std::string &_name) const
class SDFORMAT_VISIBLE Element
Definition: Element.hh:49
bool HasElementDescription(const std::string &_name)
Return true if an element description exists.
std::string name
Element name.
Definition: Element.hh:285
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:61
T Get(const std::string &_key="")
Definition: Element.hh:320
ElementPtr GetElementDescription(unsigned int _index) const
Get an element description using an index.
SDF Element class.
Definition: Element.hh:68
std::string required
True if element is required.
Definition: Element.hh:288
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
#define sdferr
Output an error message.
Definition: Console.hh:47
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:312
std::string description
Element description.
Definition: Element.hh:291
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:297
bool HasElement(const std::string &_name) const
bool copyChildren
True if element's children should be copied.
Definition: Element.hh:294
Param_V attributes
Definition: Element.hh:300
ParamPtr GetAttribute(const std::string &_key)
Get the param of an attribute.
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:53
ElementPtr_V elements
Definition: Element.hh:306
ParamPtr value
Definition: Element.hh:303