All Classes Namespaces Files Functions Variables Typedefs Friends Macros Modules
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 #include "sdf/system_util.hh"
28 
31 namespace sdf
32 {
35 
38  typedef boost::shared_ptr<SDF> SDFPtr;
39 
42  typedef boost::shared_ptr<Element> ElementPtr;
43 
46  typedef std::vector< ElementPtr > ElementPtr_V;
47 
50 
58  std::string findFile(const std::string &_filename,
59  bool _searchLocalPath = true,
60  bool _useCallback = false);
61 
67  void addURIPath(const std::string &_uri, const std::string &_path);
68 
74  void setFindCallback(boost::function<std::string (const std::string &)> _cb);
75 
79  public boost::enable_shared_from_this<Element>
80  {
82  public: Element();
83 
85  public: virtual ~Element();
86 
89  public: boost::shared_ptr<Element> Clone() const;
90 
93  public: void Copy(const ElementPtr _elem);
94 
98  public: ElementPtr GetParent() const;
99 
102  public: void SetParent(const ElementPtr _parent);
103 
106  public: void SetName(const std::string &_name);
107 
110  public: const std::string &GetName() const;
111 
118  public: void SetRequired(const std::string &_req);
119 
123  public: const std::string &GetRequired() const;
124 
128  public: void SetCopyChildren(bool _value);
129 
133  public: bool GetCopyChildren() const;
134 
137  public: void PrintDescription(const std::string &_prefix);
138 
141  public: void PrintValues(std::string _prefix);
142 
143  public: void PrintWiki(std::string _prefix);
144 
151  public: void PrintDocLeftPane(std::string &_html,
152  int _spacing, int &_index);
153 
159  public: void PrintDocRightPane(std::string &_html,
160  int _spacing, int &_index);
161 
165  public: std::string ToString(const std::string &_prefix) const;
166 
173  public: void AddAttribute(const std::string &_key,
174  const std::string &_type,
175  const std::string &_defaultvalue,
176  bool _required,
177  const std::string &_description="");
178 
184  public: void AddValue(const std::string &_type,
185  const std::string &_defaultValue, bool _required,
186  const std::string &_description="");
187 
191  public: ParamPtr GetAttribute(const std::string &_key);
192 
194  public: unsigned int GetAttributeCount() const;
195 
197  public: ParamPtr GetAttribute(unsigned int _index) const;
198 
200  public: unsigned int GetElementDescriptionCount() const;
201 
203  public: ElementPtr GetElementDescription(unsigned int _index) const;
204 
206  public: ElementPtr GetElementDescription(const std::string &_key) const;
207 
209  public: bool HasElementDescription(const std::string &_name);
210 
211  public: bool HasAttribute(const std::string &_key);
212 
214  public: bool GetAttributeSet(const std::string &_key);
215 
217  public: ParamPtr GetValue();
218 
219  public: bool GetValueBool(
220  const std::string &_key = "") SDF_DEPRECATED(1.4);
221  public: int GetValueInt(
222  const std::string &_key = "") SDF_DEPRECATED(1.4);
223  public: float GetValueFloat(
224  const std::string &_key = "") SDF_DEPRECATED(1.4);
225  public: double GetValueDouble(
226  const std::string &_key = "") SDF_DEPRECATED(1.4);
227  public: unsigned int GetValueUInt(
228  const std::string &_key = "") SDF_DEPRECATED(1.4);
229  public: char GetValueChar(
230  const std::string &_key = "") SDF_DEPRECATED(1.4);
231  public: std::string GetValueString(
232  const std::string &_key = "") SDF_DEPRECATED(1.4);
233  public: sdf::Vector3 GetValueVector3(
234  const std::string &_key = "") SDF_DEPRECATED(1.4);
235  public: sdf::Vector2d GetValueVector2d(
236  const std::string &_key = "") SDF_DEPRECATED(1.4);
237  public: sdf::Quaternion GetValueQuaternion(
238  const std::string &_key = "") SDF_DEPRECATED(1.4);
239  public: sdf::Pose GetValuePose(
240  const std::string &_key = "") SDF_DEPRECATED(1.4);
241  public: sdf::Color GetValueColor(
242  const std::string &_key = "") SDF_DEPRECATED(1.4);
243  public: sdf::Time GetValueTime(
244  const std::string &_key = "") SDF_DEPRECATED(1.4);
245 
246  public: template<typename T>
247  T Get(const std::string &_key = "")
248  {
249  T result = T();
250 
251  if (_key.empty() && this->value)
252  this->value->Get<T>(result);
253  else if (!_key.empty())
254  {
255  ParamPtr param = this->GetAttribute(_key);
256  if (param)
257  param->Get(result);
258  else if (this->HasElement(_key))
259  result = this->GetElementImpl(_key)->Get<T>();
260  else if (this->HasElementDescription(_key))
261  result = this->GetElementDescription(_key)->Get<T>();
262  else
263  sdferr << "Unable to find value for key[" << _key << "]\n";
264  }
265  return result;
266  }
267 
268  public: template<typename T>
269  bool Set(const T &_value)
270  {
271  if (this->value)
272  {
273  this->value->Set(_value);
274  return true;
275  }
276  return false;
277  }
278 
279  public: bool HasElement(const std::string &_name) const;
280 
281  public: ElementPtr GetElement(const std::string &_name) const;
282  public: ElementPtr GetFirstElement() const;
283 
284  public: ElementPtr GetNextElement(const std::string &_name = "") const;
285 
286  public: ElementPtr GetElement(const std::string &_name);
287  public: ElementPtr AddElement(const std::string &_name);
288  public: void InsertElement(ElementPtr _elem);
289 
291  public: void RemoveFromParent();
292 
295  public: void RemoveChild(ElementPtr _child);
296 
298  public: void ClearElements();
299 
300  public: void Update();
301  public: void Reset();
302 
303  public: void SetInclude(const std::string &_filename);
304  public: std::string GetInclude() const;
305 
307  public: std::string GetDescription() const;
308 
310  public: void SetDescription(const std::string &_desc);
311 
313  public: void AddElementDescription(ElementPtr _elem);
314 
315  private: void ToString(const std::string &_prefix,
316  std::ostringstream &_out) const;
317 
318 
319  private: boost::shared_ptr<Param> CreateParam(const std::string &_key,
320  const std::string &_type, const std::string &_defaultValue,
321  bool _required, const std::string &_description="");
322 
323  public: ElementPtr GetElementImpl(const std::string &_name) const;
324 
325  private: std::string name;
326  private: std::string required;
327  private: std::string description;
328  private: bool copyChildren;
329 
330  private: ElementPtr parent;
331 
332  // Attributes of this element
333  private: Param_V attributes;
334 
335  // Value of this element
336  private: ParamPtr value;
337 
338  // The existing child elements
339  private: ElementPtr_V elements;
340 
341  // The possible child elements
342  private: ElementPtr_V elementDescriptions;
343 
345  private: std::string includeFilename;
346  };
347 
348 
351  {
352  public: SDF();
353  public: void PrintDescription();
354  public: void PrintValues();
355  public: void PrintWiki();
356  public: void PrintDoc();
357  public: void Write(const std::string &_filename);
358  public: std::string ToString() const;
359 
361  public: void SetFromString(const std::string &_sdfData);
362 
363  public: ElementPtr root;
364 
365  public: static std::string version;
366  };
368 }
369 #endif
Generic double x, y vector.
Definition: Types.hh:157
Base SDF class.
Definition: SDFImpl.hh:350
Element * ElementPtr
Definition: SDFImpl.hh:42
bool Get(T &_value)
Get the value of the parameter.
Definition: Param.hh:147
A parameter class.
Definition: Param.hh:53
static std::string version
Definition: SDFImpl.hh:365
SDF * SDFPtr
Definition: SDFImpl.hh:38
bool Set(const T &_value)
Definition: SDFImpl.hh:269
class SDFORMAT_VISIBLE Element
Definition: SDFImpl.hh:34
A quaternion class.
Definition: Types.hh:310
SDFORMAT_VISIBLE void setFindCallback(boost::function< std::string(const std::string &)> _cb)
Set the callback to use when SDF can't find a file.
class SDFORMAT_VISIBLE SDF
Definition: SDFImpl.hh:33
SDF Element class.
Definition: SDFImpl.hh:78
SDFORMAT_VISIBLE std::string findFile(const std::string &_filename, bool _searchLocalPath=true, bool _useCallback=false)
Find the absolute path of a file.
ElementPtr root
Definition: SDFImpl.hh:363
#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::vector< ElementPtr > ElementPtr_V
Definition: SDFImpl.hh:46
std::vector< ParamPtr > Param_V
Definition: Param.hh:49
Defines a color.
Definition: Types.hh:51
namespace for Simulation Description Format parser
Definition: Console.hh:29
T Get(const std::string &_key="")
Definition: SDFImpl.hh:247
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:681
SDFORMAT_VISIBLE void copyChildren(ElementPtr _sdf, TiXmlElement *_xml)
SDFORMAT_VISIBLE void addURIPath(const std::string &_uri, const std::string &_path)
Associate paths to a URI.
Encapsulates a position and rotation in three space.
Definition: Types.hh:582
#define SDF_DEPRECATED(version)
Definition: Types.hh:33
The Vector3 class represents the generic vector containing 3 elements.
Definition: Types.hh:209