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 <boost/shared_ptr.hpp>
23 #include <boost/enable_shared_from_this.hpp>
24 
25 #include "sdf/Param.hh"
26 #include "sdf/system_util.hh"
27 
29 #ifndef _WIN32
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32 #endif
33 #include "sdf/Types.hh"
34 #ifndef _WIN32
35 #pragma GCC diagnostic pop
36 #endif
37 
38 #ifdef _WIN32
39 // Disable warning C4251 which is triggered by
40 // boost::enable_shared_from_this
41 #pragma warning(push)
42 #pragma warning(disable: 4251)
43 #endif
44 
47 namespace sdf
48 {
49  class ElementPrivate;
51 
54  typedef boost::shared_ptr<Element> ElementPtr;
55 
58  typedef boost::weak_ptr<Element> ElementWeakPtr;
59 
62  typedef std::vector<ElementPtr> ElementPtr_V;
63 
66 
70  public boost::enable_shared_from_this<Element>
71  {
73  public: Element();
74 
76  public: virtual ~Element();
77 
80  public: ElementPtr Clone() const;
81 
84  public: void Copy(const ElementPtr _elem);
85 
89  public: ElementPtr GetParent() const;
90 
93  public: void SetParent(const ElementPtr _parent);
94 
97  public: void SetName(const std::string &_name);
98 
101  public: const std::string &GetName() const;
102 
109  public: void SetRequired(const std::string &_req);
110 
114  public: const std::string &GetRequired() const;
115 
119  public: void SetCopyChildren(bool _value);
120 
124  public: bool GetCopyChildren() const;
125 
128  public: void SetReferenceSDF(const std::string &_value);
129 
132  public: std::string ReferenceSDF() const;
133 
136  public: void PrintDescription(const std::string &_prefix);
137 
140  public: void PrintValues(std::string _prefix);
141 
142  public: void PrintWiki(std::string _prefix);
143 
150  public: void PrintDocLeftPane(std::string &_html,
151  int _spacing, int &_index);
152 
158  public: void PrintDocRightPane(std::string &_html,
159  int _spacing, int &_index);
160 
164  public: std::string ToString(const std::string &_prefix) const;
165 
172  public: void AddAttribute(const std::string &_key,
173  const std::string &_type,
174  const std::string &_defaultvalue,
175  bool _required,
176  const std::string &_description="");
177 
183  public: void AddValue(const std::string &_type,
184  const std::string &_defaultValue, bool _required,
185  const std::string &_description="");
186 
190  public: ParamPtr GetAttribute(const std::string &_key);
191 
193  public: size_t GetAttributeCount() const;
194 
196  public: ParamPtr GetAttribute(unsigned int _index) const;
197 
199  public: size_t GetElementDescriptionCount() const;
200 
202  public: ElementPtr GetElementDescription(unsigned int _index) const;
203 
205  public: ElementPtr GetElementDescription(const std::string &_key) const;
206 
208  public: bool HasElementDescription(const std::string &_name);
209 
210  public: bool HasAttribute(const std::string &_key);
211 
213  public: bool GetAttributeSet(const std::string &_key);
214 
216  public: ParamPtr GetValue();
217 
222  public: boost::any GetAny(const std::string &_key = "");
223 
224  public: template<typename T>
225  T Get(const std::string &_key = "");
226 
227  public: template<typename T>
228  bool Set(const T &_value);
229 
230  public: bool HasElement(const std::string &_name) const;
231 
232  public: ElementPtr GetElement(const std::string &_name) const;
233  public: ElementPtr GetFirstElement() const;
234 
235  public: ElementPtr GetNextElement(const std::string &_name = "") const;
236 
237  public: ElementPtr GetElement(const std::string &_name);
238  public: ElementPtr AddElement(const std::string &_name);
239  public: void InsertElement(ElementPtr _elem);
240 
242  public: void RemoveFromParent();
243 
246  public: void RemoveChild(ElementPtr _child);
247 
249  public: void ClearElements();
250 
251  public: void Update();
252  public: void Reset();
253 
254  public: void SetInclude(const std::string &_filename);
255  public: std::string GetInclude() const;
256 
258  public: std::string GetDescription() const;
259 
261  public: void SetDescription(const std::string &_desc);
262 
264  public: void AddElementDescription(ElementPtr _elem);
265 
266  public: ElementPtr GetElementImpl(const std::string &_name) const;
267 
268  private: void ToString(const std::string &_prefix,
269  std::ostringstream &_out) const;
270 
271 
272  private: ParamPtr CreateParam(const std::string &_key,
273  const std::string &_type, const std::string &_defaultValue,
274  bool _required, const std::string &_description="");
275 
276 
278  private: ElementPrivate *dataPtr;
279  };
280 
284  {
286  public: std::string name;
287 
289  public: std::string required;
290 
292  public: std::string description;
293 
295  public: bool copyChildren;
296 
298  public: ElementWeakPtr parent;
299 
300  // Attributes of this element
302 
303  // Value of this element
304  public: ParamPtr value;
305 
306  // The existing child elements
307  public: ElementPtr_V elements;
308 
309  // The possible child elements
310  public: ElementPtr_V elementDescriptions;
311 
313  public: std::string includeFilename;
314 
316  public: std::string referenceSDF;
317  };
318 
320  template<typename T>
321  T Element::Get(const std::string &_key)
322  {
323  T result = T();
324 
325  if (_key.empty() && this->dataPtr->value)
326  this->dataPtr->value->Get<T>(result);
327  else if (!_key.empty())
328  {
329  ParamPtr param = this->GetAttribute(_key);
330  if (param)
331  param->Get(result);
332  else if (this->HasElement(_key))
333  result = this->GetElementImpl(_key)->Get<T>();
334  else if (this->HasElementDescription(_key))
335  result = this->GetElementDescription(_key)->Get<T>();
336  else
337  sdferr << "Unable to find value for key[" << _key << "]\n";
338  }
339  return result;
340  }
341 
343  template<typename T>
344  bool Element::Set(const T &_value)
345  {
346  if (this->dataPtr->value)
347  {
348  this->dataPtr->value->Set(_value);
349  return true;
350  }
351  return false;
352  }
354 }
355 
356 #ifdef _WIN32
357 #pragma warning(pop)
358 #endif
359 
360 #endif
ElementPtr_V elementDescriptions
Definition: Element.hh:310
Element * ElementPtr
Definition: Element.hh:54
bool Set(const T &_value)
Definition: Element.hh:344
Definition: Element.hh:283
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:316
A parameter class.
Definition: Param.hh:67
class SDFORMAT_VISIBLE Element
Definition: Element.hh:50
std::string name
Element name.
Definition: Element.hh:286
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:62
T Get(const std::string &_key="")
Definition: Element.hh:321
SDF Element class.
Definition: Element.hh:69
std::string required
True if element is required.
Definition: Element.hh:289
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:278
#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
boost::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:58
std::string includeFilename
name of the include file that was used to create this element
Definition: Element.hh:313
std::string description
Element description.
Definition: Element.hh:292
std::vector< ParamPtr > Param_V
Definition: Param.hh:60
ElementWeakPtr parent
Element&#39;s parent.
Definition: Element.hh:298
namespace for Simulation Description Format parser
Definition: Console.hh:29
bool copyChildren
True if element&#39;s children should be copied.
Definition: Element.hh:295
Param_V attributes
Definition: Element.hh:301
ElementPtr_V elements
Definition: Element.hh:307
ParamPtr value
Definition: Element.hh:304