All Classes Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
SDFImpl.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2012 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 _SDFIMPL_HH_
18 #define _SDFIMPL_HH_
19 
20 #include <vector>
21 #include <string>
22 #include <boost/shared_ptr.hpp>
23 #include <boost/enable_shared_from_this.hpp>
24 
25 #include "sdf/Types.hh"
26 #include "sdf/Param.hh"
27 
30 namespace sdf
31 {
32  class SDF;
33  class Element;
34 
37  typedef boost::shared_ptr<SDF> SDFPtr;
38 
41  typedef boost::shared_ptr<Element> ElementPtr;
42 
45  typedef std::vector< ElementPtr > ElementPtr_V;
46 
49 
56  std::string findFile(const std::string &_filename,
57  bool _searchLocalPath = true,
58  bool _useCallback = false);
59 
64  void addURIPath(const std::string &_uri, const std::string &_path);
65 
70  void setFindCallback(boost::function<std::string (const std::string &)> _cb);
71 
74  class Element : public boost::enable_shared_from_this<Element>
75  {
77  public: Element();
78 
80  public: virtual ~Element();
81 
84  public: boost::shared_ptr<Element> Clone() const;
85 
88  public: void Copy(const ElementPtr _elem);
89 
93  public: ElementPtr GetParent() const;
94 
97  public: void SetParent(const ElementPtr _parent);
98 
101  public: void SetName(const std::string &_name);
102 
105  public: const std::string &GetName() const;
106 
113  public: void SetRequired(const std::string &_req);
114 
118  public: const std::string &GetRequired() const;
119 
123  public: void SetCopyChildren(bool _value);
124 
128  public: bool GetCopyChildren() const;
129 
132  public: void PrintDescription(const std::string &_prefix);
133 
136  public: void PrintValues(std::string _prefix);
137 
138  public: void PrintWiki(std::string _prefix);
139 
146  public: void PrintDocLeftPane(std::string &_html,
147  int _spacing, int &_index);
148 
154  public: void PrintDocRightPane(std::string &_html,
155  int _spacing, int &_index);
156 
160  public: std::string ToString(const std::string &_prefix) const;
161 
168  public: void AddAttribute(const std::string &_key,
169  const std::string &_type,
170  const std::string &_defaultvalue,
171  bool _required,
172  const std::string &_description="");
173 
179  public: void AddValue(const std::string &_type,
180  const std::string &_defaultValue, bool _required,
181  const std::string &_description="");
182 
186  public: ParamPtr GetAttribute(const std::string &_key);
187 
189  public: unsigned int GetAttributeCount() const;
190 
192  public: ParamPtr GetAttribute(unsigned int _index) const;
193 
195  public: unsigned int GetElementDescriptionCount() const;
196 
198  public: ElementPtr GetElementDescription(unsigned int _index) const;
199 
201  public: ElementPtr GetElementDescription(const std::string &_key) const;
202 
204  public: bool HasElementDescription(const std::string &_name);
205 
206  public: bool HasAttribute(const std::string &_key);
207 
209  public: bool GetAttributeSet(const std::string &_key);
210 
212  public: ParamPtr GetValue();
213 
214  public: bool GetValueBool(
215  const std::string &_key = "") SDF_DEPRECATED(1.4);
216  public: int GetValueInt(
217  const std::string &_key = "") SDF_DEPRECATED(1.4);
218  public: float GetValueFloat(
219  const std::string &_key = "") SDF_DEPRECATED(1.4);
220  public: double GetValueDouble(
221  const std::string &_key = "") SDF_DEPRECATED(1.4);
222  public: unsigned int GetValueUInt(
223  const std::string &_key = "") SDF_DEPRECATED(1.4);
224  public: char GetValueChar(
225  const std::string &_key = "") SDF_DEPRECATED(1.4);
226  public: std::string GetValueString(
227  const std::string &_key = "") SDF_DEPRECATED(1.4);
229  const std::string &_key = "") SDF_DEPRECATED(1.4);
231  const std::string &_key = "") SDF_DEPRECATED(1.4);
233  const std::string &_key = "") SDF_DEPRECATED(1.4);
234  public: sdf::Pose GetValuePose(
235  const std::string &_key = "") SDF_DEPRECATED(1.4);
236  public: sdf::Color GetValueColor(
237  const std::string &_key = "") SDF_DEPRECATED(1.4);
238  public: sdf::Time GetValueTime(
239  const std::string &_key = "") SDF_DEPRECATED(1.4);
240 
241  public: template<typename T>
242  T Get(const std::string &_key = "")
243  {
244  T result = T();
245 
246  if (_key.empty() && this->value)
247  this->value->Get<T>(result);
248  else if (!_key.empty())
249  {
250  ParamPtr param = this->GetAttribute(_key);
251  if (param)
252  param->Get(result);
253  else if (this->HasElement(_key))
254  result = this->GetElementImpl(_key)->Get<T>();
255  else if (this->HasElementDescription(_key))
256  result = this->GetElementDescription(_key)->Get<T>();
257  else
258  sdferr << "Unable to find value for key[" << _key << "]\n";
259  }
260  return result;
261  }
262 
263  public: template<typename T>
264  bool Set(const T &_value)
265  {
266  if (this->value)
267  {
268  this->value->Set(_value);
269  return true;
270  }
271  return false;
272  }
273 
274  public: bool HasElement(const std::string &_name) const;
275 
276  public: ElementPtr GetElement(const std::string &_name) const;
277  public: ElementPtr GetFirstElement() const;
278 
279  public: ElementPtr GetNextElement(const std::string &_name = "") const;
280 
281  public: ElementPtr GetElement(const std::string &_name);
282  public: ElementPtr AddElement(const std::string &_name);
283  public: void InsertElement(ElementPtr _elem);
284 
286  public: void RemoveFromParent();
287 
290  public: void RemoveChild(ElementPtr _child);
291 
293  public: void ClearElements();
294 
295  public: void Update();
296  public: void Reset();
297 
298  public: void SetInclude(const std::string &_filename);
299  public: std::string GetInclude() const;
300 
302  public: std::string GetDescription() const;
303 
305  public: void SetDescription(const std::string &_desc);
306 
308  public: void AddElementDescription(ElementPtr _elem);
309 
310  private: void ToString(const std::string &_prefix,
311  std::ostringstream &_out) const;
312 
313 
314  private: boost::shared_ptr<Param> CreateParam(const std::string &_key,
315  const std::string &_type, const std::string &_defaultValue,
316  bool _required, const std::string &_description="");
317 
318  public: ElementPtr GetElementImpl(const std::string &_name) const;
319 
320  private: std::string name;
321  private: std::string required;
322  private: std::string description;
323  private: bool copyChildren;
324 
325  private: ElementPtr parent;
326 
327  // Attributes of this element
328  private: Param_V attributes;
329 
330  // Value of this element
331  private: ParamPtr value;
332 
333  // The existing child elements
334  private: ElementPtr_V elements;
335 
336  // The possible child elements
337  private: ElementPtr_V elementDescriptions;
338 
340  private: std::string includeFilename;
341  };
342 
343 
345  class SDF
346  {
347  public: SDF();
348  public: void PrintDescription();
349  public: void PrintValues();
350  public: void PrintWiki();
351  public: void PrintDoc();
352  public: void Write(const std::string &_filename);
353  public: std::string ToString() const;
354 
356  public: void SetFromString(const std::string &_sdfData);
357 
358  public: ElementPtr root;
359 
360  public: static std::string version;
361  };
363 }
364 #endif
void InsertElement(ElementPtr _elem)
void SetDescription(const std::string &_desc)
Set a text description for the element.
Generic double x, y vector.
Definition: Types.hh:155
bool GetCopyChildren() const
Return true if this Element's child elements should be copied during parsing.
float GetValueFloat(const std::string &_key="") SDF_DEPRECATED(1.4)
Element * Clone() const
Create a copy of this Element.
Base SDF class.
Definition: SDFImpl.hh:345
ElementPtr GetElement(const std::string &_name) const
virtual ~Element()
Destructor.
Element * ElementPtr
Definition: SDFImpl.hh:41
std::string GetValueString(const std::string &_key="") SDF_DEPRECATED(1.4)
void SetFromString(const std::string &_sdfData)
Set SDF values from a string.
void SetCopyChildren(bool _value)
Set whether this element should copy its child elements during parsing.
bool GetAttributeSet(const std::string &_key)
Return true if the attribute was set (i.e. not default value)
void addURIPath(const std::string &_uri, const std::string &_path)
Associate paths to a URI.
bool Get(T &_value)
Get the value of the parameter.
Definition: Param.hh:146
A parameter class.
Definition: Param.hh:52
double GetValueDouble(const std::string &_key="") SDF_DEPRECATED(1.4)
static std::string version
Definition: SDFImpl.hh:360
std::string ToString() const
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:124
SDF * SDFPtr
Definition: SDFImpl.hh:37
void PrintDoc()
bool Set(const T &_value)
Definition: SDFImpl.hh:264
ElementPtr GetNextElement(const std::string &_name="") const
bool GetValueBool(const std::string &_key="") SDF_DEPRECATED(1.4)
ParamPtr GetValue()
Get the param of the elements value.
ElementPtr GetElementImpl(const std::string &_name) const
void AddValue(const std::string &_type, const std::string &_defaultValue, bool _required, const std::string &_description="")
Add a value to this Element.
bool HasElementDescription(const std::string &_name)
Return true if an element description exists.
sdf::Quaternion GetValueQuaternion(const std::string &_key="") SDF_DEPRECATED(1.4)
std::string GetInclude() const
void ClearElements()
Remove all child elements.
A quaternion class.
Definition: Types.hh:308
ElementPtr GetParent() const
Get a pointer to this Element's parent.
int GetValueInt(const std::string &_key="") SDF_DEPRECATED(1.4)
void Write(const std::string &_filename)
sdf::Time GetValueTime(const std::string &_key="") SDF_DEPRECATED(1.4)
std::string findFile(const std::string &_filename, bool _searchLocalPath=true, bool _useCallback=false)
Find the absolute path of a file.
sdf::Vector3 GetValueVector3(const std::string &_key="") SDF_DEPRECATED(1.4)
unsigned int GetAttributeCount() const
Get the number of attributes.
void setFindCallback(boost::function< std::string(const std::string &)> _cb)
Set the callback to use when SDF can't find a file.
void PrintWiki(std::string _prefix)
char GetValueChar(const std::string &_key="") SDF_DEPRECATED(1.4)
void RemoveFromParent()
Remove this element from its parent.
void PrintDescription()
void SetParent(const ElementPtr _parent)
Set the parent of this Element.
ElementPtr GetElementDescription(unsigned int _index) const
Get an element description using an index.
SDF Element class.
Definition: SDFImpl.hh:74
void PrintDescription(const std::string &_prefix)
Output Element's description to stdout.
void PrintWiki()
void PrintDocRightPane(std::string &_html, int _spacing, int &_index)
Helper function for SDF::PrintDoc.
ElementPtr GetFirstElement() const
Element()
Constructor.
sdf::Color GetValueColor(const std::string &_key="") SDF_DEPRECATED(1.4)
void SetRequired(const std::string &_req)
Set the requirement type.
void SetName(const std::string &_name)
Set the name of the Element.
ElementPtr root
Definition: SDFImpl.hh:358
std::string ToString(const std::string &_prefix) const
Convert the element values to a string representation.
#define sdferr
Output an error message.
Definition: Console.hh:45
std::vector< ElementPtr > ElementPtr_V
Definition: SDFImpl.hh:45
void AddElementDescription(ElementPtr _elem)
Add a new element description.
unsigned int GetElementDescriptionCount() const
Get the number of element descriptions.
std::vector< ParamPtr > Param_V
Definition: Param.hh:48
void RemoveChild(ElementPtr _child)
Remove a child element.
void PrintValues()
sdf::Pose GetValuePose(const std::string &_key="") SDF_DEPRECATED(1.4)
void SetInclude(const std::string &_filename)
Defines a color.
Definition: Types.hh:49
bool HasElement(const std::string &_name) const
void AddAttribute(const std::string &_key, const std::string &_type, const std::string &_defaultvalue, bool _required, const std::string &_description="")
Add an attribute value.
T Get(const std::string &_key="")
Definition: SDFImpl.hh:242
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:679
bool HasAttribute(const std::string &_key)
void Copy(const ElementPtr _elem)
Copy values from an Element.
const std::string & GetName() const
Get the Element's name.
sdf::Vector2d GetValueVector2d(const std::string &_key="") SDF_DEPRECATED(1.4)
ElementPtr AddElement(const std::string &_name)
Encapsulates a position and rotation in three space.
Definition: Types.hh:580
ParamPtr GetAttribute(const std::string &_key)
Get the param of an attribute.
void PrintDocLeftPane(std::string &_html, int _spacing, int &_index)
Helper function for SDF::PrintDoc.
#define SDF_DEPRECATED(version)
Definition: Types.hh:31
The Vector3 class represents the generic vector containing 3 elements.
Definition: Types.hh:207
const std::string & GetRequired() const
Get the requirement string.
void PrintValues(std::string _prefix)
Output Element's values to stdout.
std::string GetDescription() const
Get a text description of the element.
unsigned int GetValueUInt(const std::string &_key="") SDF_DEPRECATED(1.4)