g2o
Loading...
Searching...
No Matches
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 <map>
31#include <sstream>
32#include <string>
33
34#include "g2o_stuff_api.h"
35#include "string_tools.h"
36
37namespace g2o {
38
40 public:
41 BaseProperty(const std::string& name_);
42 virtual ~BaseProperty();
43 const std::string& name() { return _name; }
44 virtual std::string toString() const = 0;
45 virtual bool fromString(const std::string& s) = 0;
46
47 protected:
48 std::string _name;
49};
50
51template <typename T>
52class Property : public BaseProperty {
53 public:
54 typedef T ValueType;
55 Property(const std::string& name_) : BaseProperty(name_) {}
56 Property(const std::string& name_, const T& v)
57 : BaseProperty(name_), _value(v) {}
58 void setValue(const T& v) { _value = v; }
59 const T& value() const { return _value; }
60 virtual std::string toString() const {
61 std::stringstream sstr;
62 sstr << _value;
63 return sstr.str();
64 }
65 virtual bool fromString(const std::string& s) {
66 bool status = convertString(s, _value);
67 return status;
68 }
69
70 protected:
72};
73
78 : protected std::map<std::string, BaseProperty*> {
79 public:
80 typedef std::map<std::string, BaseProperty*> BaseClass;
81 typedef BaseClass::iterator PropertyMapIterator;
82 typedef BaseClass::const_iterator PropertyMapConstIterator;
83
85
89 bool addProperty(BaseProperty* p);
90
94 bool eraseProperty(const std::string& name_);
95
99 template <typename P>
100 P* getProperty(const std::string& name_) {
101 PropertyMapIterator it = find(name_);
102 if (it == end()) return nullptr;
103 return dynamic_cast<P*>(it->second);
104 }
105 template <typename P>
106 const P* getProperty(const std::string& name_) const {
107 PropertyMapConstIterator it = find(name_);
108 if (it == end()) return nullptr;
109 return dynamic_cast<P*>(it->second);
110 }
111
115 template <typename P>
116 P* makeProperty(const std::string& name_, const typename P::ValueType& v) {
117 PropertyMapIterator it = find(name_);
118 if (it == end()) {
119 P* p = new P(name_, v);
120 addProperty(p);
121 return p;
122 } else
123 return dynamic_cast<P*>(it->second);
124 }
125
130 bool updatePropertyFromString(const std::string& name,
131 const std::string& value);
132
137 bool updateMapFromString(const std::string& values);
138
139 void writeToCSV(std::ostream& os) const;
140
141 using BaseClass::begin;
142 using BaseClass::const_iterator;
143 using BaseClass::end;
144 using BaseClass::iterator;
145 using BaseClass::size;
146};
147
153
154} // namespace g2o
155#endif
std::string _name
Definition property.h:48
virtual std::string toString() const =0
const std::string & name()
Definition property.h:43
virtual bool fromString(const std::string &s)=0
a collection of properties mapping from name to the property itself
Definition property.h:78
const P * getProperty(const std::string &name_) const
Definition property.h:106
P * makeProperty(const std::string &name_, const typename P::ValueType &v)
Definition property.h:116
P * getProperty(const std::string &name_)
Definition property.h:100
std::map< std::string, BaseProperty * > BaseClass
Definition property.h:80
BaseClass::const_iterator PropertyMapConstIterator
Definition property.h:82
BaseClass::iterator PropertyMapIterator
Definition property.h:81
Property(const std::string &name_)
Definition property.h:55
void setValue(const T &v)
Definition property.h:58
virtual bool fromString(const std::string &s)
Definition property.h:65
Property(const std::string &name_, const T &v)
Definition property.h:56
virtual std::string toString() const
Definition property.h:60
const T & value() const
Definition property.h:59
#define G2O_STUFF_API
Property< bool > BoolProperty
Definition property.h:149
Property< std::string > StringProperty
Definition property.h:152
bool convertString(const std::string &s, T &x, bool failIfLeftoverChars=true)
Property< double > DoubleProperty
Definition property.h:151
Property< int > IntProperty
Definition property.h:148
Property< float > FloatProperty
Definition property.h:150