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  public: ElementPtr GetFirstElement() const;
241 
242  public: ElementPtr GetNextElement(const std::string &_name = "") const;
243 
244  public: ElementPtr GetElement(const std::string &_name);
245  public: ElementPtr AddElement(const std::string &_name);
246  public: void InsertElement(ElementPtr _elem);
247 
249  public: void RemoveFromParent();
250 
253  public: void RemoveChild(ElementPtr _child);
254 
256  public: void ClearElements();
257 
258  public: void Update();
259  public: void Reset();
260 
261  public: void SetInclude(const std::string &_filename);
262  public: std::string GetInclude() const;
263 
265  public: std::string GetDescription() const;
266 
268  public: void SetDescription(const std::string &_desc);
269 
271  public: void AddElementDescription(ElementPtr _elem);
272 
273  public: ElementPtr GetElementImpl(const std::string &_name) const;
274 
275  private: void ToString(const std::string &_prefix,
276  std::ostringstream &_out) const;
277 
278 
279  private: ParamPtr CreateParam(const std::string &_key,
280  const std::string &_type, const std::string &_defaultValue,
281  bool _required, const std::string &_description="");
282 
283 
285  private: ElementPrivate *dataPtr;
286  };
287 
291  {
293  public: std::string name;
294 
296  public: std::string required;
297 
299  public: std::string description;
300 
302  public: bool copyChildren;
303 
305  public: ElementWeakPtr parent;
306 
307  // Attributes of this element
309 
310  // Value of this element
311  public: ParamPtr value;
312 
313  // The existing child elements
314  public: ElementPtr_V elements;
315 
316  // The possible child elements
317  public: ElementPtr_V elementDescriptions;
318 
320  public: std::string includeFilename;
321 
323  public: std::string referenceSDF;
324  };
325 
327  template<typename T>
328  T Element::Get(const std::string &_key)
329  {
330  T result = T();
331 
332  if (_key.empty() && this->dataPtr->value)
333  this->dataPtr->value->Get<T>(result);
334  else if (!_key.empty())
335  {
336  ParamPtr param = this->GetAttribute(_key);
337  if (param)
338  param->Get(result);
339  else if (this->HasElement(_key))
340  result = this->GetElementImpl(_key)->Get<T>();
341  else if (this->HasElementDescription(_key))
342  result = this->GetElementDescription(_key)->Get<T>();
343  else
344  sdferr << "Unable to find value for key[" << _key << "]\n";
345  }
346  return result;
347  }
348 
350  template<typename T>
351  std::pair<T, bool> Element::Get(const std::string &_key,
352  const T &_defaultValue)
353  {
354  std::pair<T, bool> result(_defaultValue, true);
355 
356  if (_key.empty() && this->dataPtr->value)
357  this->dataPtr->value->Get<T>(result.first);
358  else if (!_key.empty())
359  {
360  ParamPtr param = this->GetAttribute(_key);
361  if (param)
362  param->Get(result.first);
363  else if (this->HasElement(_key))
364  result.first = this->GetElementImpl(_key)->Get<T>();
365  else if (this->HasElementDescription(_key))
366  result.first = this->GetElementDescription(_key)->Get<T>();
367  else
368  result.second = false;
369  }
370  else
371  {
372  result.second = false;
373  }
374 
375  return result;
376  }
377 
379  template<typename T>
380  bool Element::Set(const T &_value)
381  {
382  if (this->dataPtr->value)
383  {
384  this->dataPtr->value->Set(_value);
385  return true;
386  }
387  return false;
388  }
390 }
391 
392 #ifdef _WIN32
393 #pragma warning(pop)
394 #endif
395 
396 #endif
ElementPtr_V elementDescriptions
Definition: Element.hh:317
bool Set(const T &_value)
Definition: Element.hh:380
Definition: Element.hh:290
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:49
std::string referenceSDF
Name of reference sdf.
Definition: Element.hh:323
class SDFORMAT_VISIBLE Element
Definition: Element.hh:41
std::string name
Element name.
Definition: Element.hh:293
std::vector< ElementPtr > ElementPtr_V
Definition: Element.hh:53
T Get(const std::string &_key="")
Get the value of a key.
Definition: Element.hh:328
SDF Element class.
Definition: Element.hh:60
std::string required
True if element is required.
Definition: Element.hh:296
#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:320
std::string description
Element description.
Definition: Element.hh:299
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:47
std::vector< ParamPtr > Param_V
Definition: Param.hh:51
ElementWeakPtr parent
Element&#39;s parent.
Definition: Element.hh:305
namespace for Simulation Description Format parser
Definition: Console.hh:36
bool copyChildren
True if element&#39;s children should be copied.
Definition: Element.hh:302
Param_V attributes
Definition: Element.hh:308
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:45
ElementPtr_V elements
Definition: Element.hh:314
ParamPtr value
Definition: Element.hh:311