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 
236  public: ElementPtr GetFirstElement() const;
237 
248  public: ElementPtr GetNextElement(const std::string &_name = "") const;
249 
259  public: ElementPtr GetElement(const std::string &_name);
260 
261  public: ElementPtr AddElement(const std::string &_name);
262  public: void InsertElement(ElementPtr _elem);
263 
265  public: void RemoveFromParent();
266 
269  public: void RemoveChild(ElementPtr _child);
270 
272  public: void ClearElements();
273 
274  public: void Update();
275  public: void Reset();
276 
277  public: void SetInclude(const std::string &_filename);
278  public: std::string GetInclude() const;
279 
281  public: std::string GetDescription() const;
282 
284  public: void SetDescription(const std::string &_desc);
285 
287  public: void AddElementDescription(ElementPtr _elem);
288 
289  public: ElementPtr GetElementImpl(const std::string &_name) const;
290 
291  private: void ToString(const std::string &_prefix,
292  std::ostringstream &_out) const;
293 
294 
295  private: ParamPtr CreateParam(const std::string &_key,
296  const std::string &_type, const std::string &_defaultValue,
297  bool _required, const std::string &_description="");
298 
299 
301  private: ElementPrivate *dataPtr;
302  };
303 
307  {
309  public: std::string name;
310 
312  public: std::string required;
313 
315  public: std::string description;
316 
318  public: bool copyChildren;
319 
321  public: ElementWeakPtr parent;
322 
323  // Attributes of this element
325 
326  // Value of this element
327  public: ParamPtr value;
328 
329  // The existing child elements
330  public: ElementPtr_V elements;
331 
332  // The possible child elements
333  public: ElementPtr_V elementDescriptions;
334 
336  public: std::string includeFilename;
337 
339  public: std::string referenceSDF;
340  };
341 
343  template<typename T>
344  T Element::Get(const std::string &_key)
345  {
346  T result = T();
347 
348  if (_key.empty() && this->dataPtr->value)
349  this->dataPtr->value->Get<T>(result);
350  else if (!_key.empty())
351  {
352  ParamPtr param = this->GetAttribute(_key);
353  if (param)
354  param->Get(result);
355  else if (this->HasElement(_key))
356  result = this->GetElementImpl(_key)->Get<T>();
357  else if (this->HasElementDescription(_key))
358  result = this->GetElementDescription(_key)->Get<T>();
359  else
360  sdferr << "Unable to find value for key[" << _key << "]\n";
361  }
362  return result;
363  }
364 
366  template<typename T>
367  bool Element::Set(const T &_value)
368  {
369  if (this->dataPtr->value)
370  {
371  this->dataPtr->value->Set(_value);
372  return true;
373  }
374  return false;
375  }
377 }
378 
379 #ifdef _WIN32
380 #pragma warning(pop)
381 #endif
382 
383 #endif
ElementPtr_V elementDescriptions
Definition: Element.hh:333
bool Set(const T &_value)
Definition: Element.hh:367
Definition: Element.hh:306
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:57
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:339
class SDFORMAT_VISIBLE Element
Definition: Element.hh:49
std::string name
Element name.
Definition: Element.hh:309
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:61
T Get(const std::string &_key="")
Definition: Element.hh:344
SDF Element class.
Definition: Element.hh:68
std::string required
True if element is required.
Definition: Element.hh:312
#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:336
std::string description
Element description.
Definition: Element.hh:315
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:56
std::vector< ParamPtr > Param_V
Definition: Param.hh:60
ElementWeakPtr parent
Element&#39;s parent.
Definition: Element.hh:321
namespace for Simulation Description Format parser
Definition: Console.hh:36
bool copyChildren
True if element&#39;s children should be copied.
Definition: Element.hh:318
Param_V attributes
Definition: Element.hh:324
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:53
ElementPtr_V elements
Definition: Element.hh:330
ParamPtr value
Definition: Element.hh:327