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