MachineIntelligenceCore:Toolchain
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Property.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_CONFIGURATION_PROPERTY_HPP_
24 #define SRC_CONFIGURATION_PROPERTY_HPP_
25 
26 #include <string>
27 
28 #include <boost/lexical_cast.hpp>
29 #include <boost/function.hpp>
30 
31 #include <boost/preprocessor/list.hpp>
32 #include <boost/preprocessor/tuple/to_list.hpp>
33 
34 #include <typeinfo>
35 #include <map>
36 #include <sstream>
37 
38 #include <iostream>
39 
45 namespace mic {
46 
52 namespace configuration {
53 
58 template<typename T>
60 public:
61  static std::string toStr(const T & val) {
62  try {
63  return boost::lexical_cast<std::string>(val);
64  } catch(...) {
65  std::stringstream ss;
66  ss << val;
67  return ss.str();
68  }
69  return "";
70  }
71 
72  static T fromStr(const std::string & str) {
73  try {
74  return boost::lexical_cast<T>(str);
75  } catch (...) {
76  std::stringstream ss(str);
77  T ret;
78  ss >> ret;
79  return ret;
80  }
81  return T();
82  }
83 };
84 
85 
91 public:
92 
93  PropertyInterface(const std::string & name_) :
94  property_name(name_) {
95  }
96 
97  virtual ~PropertyInterface() {}
98 
103  const std::string & name() const {
104  return property_name;
105  }
106 
111  virtual void setValue(const std::string & str) = 0;
112 
118  virtual std::string getValue() = 0;
119 
120 private:
122  std::string property_name;
123 
124 };
125 
126 
127 
132 template<class T, class Translator = LexicalTranslator<T> >
133 class Property : public PropertyInterface {
134 public:
141  Property(const std::string& name_, const T & initializer_ = T(),
142  std::string type_ = typeid(T).name()) : PropertyInterface(name_),
143  property_value(initializer_), property_type(type_) {
144  }
145 
149  T operator()() const {
150  return property_value;
151  }
152 
158  T operator()(T const & value_) {
159  property_value = value_;
160  return property_value;
161  }
162 
167  operator T() const {
168  return property_value;
169  }
170 
176  Property<T>& operator=(T const & value_) {
177  property_value = value_;
178  return *this;
179  }
180 
186  bool operator==(T const & value_) {
187  return property_value == value_;
188  }
189 
195  bool operator!=(T const & value_) {
196  return property_value != value_;
197  }
198 
199 
204  virtual void setValue(const std::string & str) {
205  property_value = Translator::fromStr(str);
206  }
207 
213  virtual std::string getValue() {
214  return Translator::toStr(property_value);
215  }
216 
221  std::string type() const {
222  return property_type;
223  }
224 
225 
232  friend std::ostream & operator<<(std::ostream & os, const Property & prop) {
233  os << prop.property_value;
234  return os;
235  }
236 
237 protected:
240 
242  std::string property_type;
243 
244 };
245 
246 
251 typedef std::pair<std::string, PropertyInterface *> PropertyPair;
252 
253 } /* namespace configuration */
254 } /* namespace mic */
255 
256 
257 
258 #endif /* SRC_CONFIGURATION_PROPERTY_HPP_ */
T property_value
Actual property value.
Definition: Property.hpp:239
std::string type() const
Definition: Property.hpp:221
friend std::ostream & operator<<(std::ostream &os, const Property &prop)
Definition: Property.hpp:232
static T fromStr(const std::string &str)
Definition: Property.hpp:72
bool operator==(T const &value_)
Definition: Property.hpp:186
std::string property_name
Name of the property.
Definition: Property.hpp:122
Template class used for lexical casting between string and other types. Used by Property class...
Definition: Property.hpp:59
Basic interface property - used during registration etc.
Definition: Property.hpp:90
virtual std::string getValue()=0
std::pair< std::string, PropertyInterface * > PropertyPair
Type representing a pair consisting of name-property.
Definition: Property.hpp:251
bool operator!=(T const &value_)
Definition: Property.hpp:195
Template class for storing properties.
Definition: Property.hpp:133
Property(const std::string &name_, const T &initializer_=T(), std::string type_=typeid(T).name())
Definition: Property.hpp:141
virtual void setValue(const std::string &str)
Definition: Property.hpp:204
PropertyInterface(const std::string &name_)
Definition: Property.hpp:93
Property< T > & operator=(T const &value_)
Definition: Property.hpp:176
virtual std::string getValue()
Definition: Property.hpp:213
virtual void setValue(const std::string &str)=0
T operator()(T const &value_)
Definition: Property.hpp:158
std::string property_type
Type of the property.
Definition: Property.hpp:242
const std::string & name() const
Definition: Property.hpp:103
static std::string toStr(const T &val)
Definition: Property.hpp:61