property.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef G2O_PROPERTY_H_
28 #define G2O_PROPERTY_H_
29 
30 #include <string>
31 #include <map>
32 #include <sstream>
33 
34 #include "string_tools.h"
35 
36 namespace g2o {
37 
38  class BaseProperty {
39  public:
40  BaseProperty(const std::string name_);
41  virtual ~BaseProperty();
42  const std::string& name() {return _name;}
43  virtual std::string toString() const = 0;
44  virtual bool fromString(const std::string& s) = 0;
45  protected:
46  std::string _name;
47  };
48 
49  template <typename T>
50  class Property: public BaseProperty {
51  public:
52  typedef T ValueType;
53  Property(const std::string& name_): BaseProperty(name_){}
54  Property(const std::string& name_, const T& v): BaseProperty(name_), _value(v){}
55  void setValue(const T& v) {_value = v; }
56  const T& value() const {return _value;}
57  virtual std::string toString() const
58  {
59  std::stringstream sstr;
60  sstr << _value;
61  return sstr.str();
62  }
63  virtual bool fromString(const std::string& s)
64  {
65  bool status = convertString(s, _value);
66  return status;
67  }
68  protected:
69  T _value;
70  };
71 
75  class PropertyMap : protected std::map<std::string, BaseProperty*>
76  {
77  public:
78  typedef std::map<std::string, BaseProperty*> BaseClass;
79  typedef BaseClass::iterator PropertyMapIterator;
80  typedef BaseClass::const_iterator PropertyMapConstIterator;
81 
82  ~PropertyMap();
83 
87  bool addProperty(BaseProperty* p);
88 
92  bool eraseProperty(const std::string& name_);
93 
97  template <typename P>
98  P* getProperty(const std::string& name_)
99  {
100  PropertyMapIterator it=find(name_);
101  if (it==end())
102  return 0;
103  return dynamic_cast<P*>(it->second);
104  }
105  template <typename P>
106  const P* getProperty(const std::string& name_) const
107  {
108  PropertyMapConstIterator it=find(name_);
109  if (it==end())
110  return 0;
111  return dynamic_cast<P*>(it->second);
112  }
113 
117  template <typename P>
118  P* makeProperty(const std::string& name_, const typename P::ValueType& v)
119  {
120  PropertyMapIterator it=find(name_);
121  if (it==end()){
122  P* p=new P(name_, v);
123  addProperty(p);
124  return p;
125  } else
126  return dynamic_cast<P*>(it->second);
127  }
128 
133  bool updatePropertyFromString(const std::string& name, const std::string& value);
134 
139  bool updateMapFromString(const std::string& values);
140 
141  void writeToCSV(std::ostream& os) const;
142 
143  using BaseClass::size;
144  using BaseClass::begin;
145  using BaseClass::end;
146  using BaseClass::iterator;
147  using BaseClass::const_iterator;
148 
149  };
150 
156 
157 } // end namespace
158 #endif
BaseClass::iterator PropertyMapIterator
Definition: property.h:79
BaseClass::const_iterator PropertyMapConstIterator
Definition: property.h:80
virtual std::string toString() const
Definition: property.h:57
std::vector< double > values
Property(const std::string &name_, const T &v)
Definition: property.h:54
Property< double > DoubleProperty
Definition: property.h:154
std::string _name
Definition: property.h:46
XmlRpcServer s
Property(const std::string &name_)
Definition: property.h:53
virtual ~BaseProperty()
Definition: property.cpp:42
P * makeProperty(const std::string &name_, const typename P::ValueType &v)
Definition: property.h:118
virtual bool fromString(const std::string &s)=0
void setValue(const T &v)
Definition: property.h:55
std::map< std::string, BaseProperty * > BaseClass
Definition: property.h:78
Property< std::string > StringProperty
Definition: property.h:155
bool convertString(const std::string &s, T &x, bool failIfLeftoverChars=true)
Definition: string_tools.h:124
Property< int > IntProperty
Definition: property.h:151
virtual std::string toString() const =0
virtual bool fromString(const std::string &s)
Definition: property.h:63
BaseProperty(const std::string name_)
Definition: property.cpp:39
const T & value() const
Definition: property.h:56
const P * getProperty(const std::string &name_) const
Definition: property.h:106
Property< float > FloatProperty
Definition: property.h:153
a collection of properties mapping from name to the property itself
Definition: property.h:75
const std::string & name()
Definition: property.h:42
Property< bool > BoolProperty
Definition: property.h:152
P * getProperty(const std::string &name_)
Definition: property.h:98


orb_slam2_ros
Author(s):
autogenerated on Wed Apr 21 2021 02:53:05