Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnProperties.h
1 /******************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnProperties.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 ** DESCRIPTION:
11 **
12 *****************************************************************************/
13 
14 #pragma once
15 
16 #include <string>
17 #include <string.h>
18 #include <vector>
19 #include <map>
20 #include <list>
21 
22 #include "Eigen/Core"
23 
24 #include "drwnSmartPointer.h"
25 #include "drwnXMLParser.h"
26 
27 using namespace std;
28 
29 // drwnPropertyType -------------------------------------------------------
30 
31 typedef enum _drwnPropertyType {
32  DRWN_INVALID_PROPERTY, DRWN_BOOLEAN_PROPERTY, DRWN_INTEGER_PROPERTY,
33  DRWN_DOUBLE_PROPERTY, DRWN_STRING_PROPERTY, DRWN_FILENAME_PROPERTY,
34  DRWN_DIRECTORY_PROPERTY, DRWN_LIST_PROPERTY, DRWN_SELECTION_PROPERTY,
35  DRWN_VECTOR_PROPERTY, DRWN_MATRIX_PROPERTY, DRWN_USER_PROPERTY
36 } drwnPropertyType;
37 
38 // drwnPropertyInterface --------------------------------------------------
39 
41  protected:
42  bool _bReadOnly; // read-only property
43  bool _bSerialize; // serializeable property
44 
45  drwnPropertyInterface() : _bReadOnly(false), _bSerialize(true) { };
46  drwnPropertyInterface(bool bReadOnly, bool bSerialize = true) :
47  _bReadOnly(bReadOnly), _bSerialize(bSerialize) { }
48 
49  public:
50  virtual ~drwnPropertyInterface() { }
51 
52  // type info
53  virtual drwnPropertyType type() const = 0;
54  virtual string asString() const = 0;
55 
56  inline bool isReadOnly() const { return _bReadOnly; }
57  inline bool isSerializeable() const { return _bSerialize; }
58 
59  // set/cast functions
60  virtual bool setProperty(bool value);
61  virtual bool setProperty(int value);
62  virtual bool setProperty(double value);
63  virtual bool setProperty(const string& value);
64  virtual bool setProperty(const char *value);
65  virtual bool setProperty(const Eigen::VectorXd& value);
66  virtual bool setProperty(const Eigen::MatrixXd& value);
67 
68  // i/o
69  virtual void read(drwnXMLNode& xml);
70  virtual void write(drwnXMLNode& xml) const;
71  virtual drwnPropertyInterface* clone() const = 0;
72  inline drwnPropertyInterface* clone(bool bReadOnly, bool bSerialize) const {
73  drwnPropertyInterface *iface = this->clone();
74  iface->_bReadOnly |= bReadOnly;
75  iface->_bSerialize = bSerialize;
76  return iface;
77  }
78 };
79 
80 // drwnStoragePropertyInterface -------------------------------------------
81 
82 template <typename T>
84  protected:
85  T* _storage;
86 
87  public:
88  drwnStoragePropertyInterface(T *storage, bool bReadOnly = false) :
89  drwnPropertyInterface(bReadOnly), _storage(storage) { }
90 
91  inline const T& getValue() const { return *_storage; }
92  inline bool setValue(const T& value) { *_storage = value; return true; }
93 
94  //virtual bool setProperty(const T& value) { *_storage = value; return true; }
95 };
96 
97 // standard drwnProperty types --------------------------------------------
98 
100 {
101  public:
102  drwnBooleanProperty(bool *storage, bool bReadOnly = false) :
103  drwnStoragePropertyInterface<bool>(storage, bReadOnly) { }
104 
105  drwnPropertyType type() const { return DRWN_BOOLEAN_PROPERTY; }
106  string asString() const;
107  drwnPropertyInterface* clone() const { return new drwnBooleanProperty(*this); }
108 
109  bool setProperty(bool value);
110  bool setProperty(int value);
111  bool setProperty(double value);
112  bool setProperty(const string& value);
113  bool setProperty(const Eigen::VectorXd& value);
114  bool setProperty(const Eigen::MatrixXd& value);
115 };
116 
118 {
119  public:
120  drwnIntegerProperty(int *storage, bool bReadOnly = false) :
121  drwnStoragePropertyInterface<int>(storage, bReadOnly) { }
122 
123  drwnPropertyType type() const { return DRWN_INTEGER_PROPERTY; }
124  string asString() const;
125  drwnPropertyInterface* clone() const { return new drwnIntegerProperty(*this); }
126 
127  bool setProperty(bool value);
128  bool setProperty(int value);
129  bool setProperty(double value);
130  bool setProperty(const string& value);
131  bool setProperty(const Eigen::VectorXd& value);
132  bool setProperty(const Eigen::MatrixXd& value);
133 };
134 
136 {
137  protected:
138  int _lowerBound;
139  int _upperBound;
140 
141  public:
142  drwnRangeProperty(int *storage, bool bReadOnly = false) :
143  drwnIntegerProperty(storage, bReadOnly), _lowerBound(0), _upperBound(100) { }
144  drwnRangeProperty(int *storage, int lb, int ub, bool bReadOnly = false) :
145  drwnIntegerProperty(storage, bReadOnly), _lowerBound(lb), _upperBound(ub) { }
146 
147  drwnPropertyInterface* clone() const { return new drwnRangeProperty(*this); }
148 
149  bool setProperty(int value);
150 };
151 
153 {
154  public:
155  drwnDoubleProperty(double *storage, bool bReadOnly = false) :
156  drwnStoragePropertyInterface<double>(storage, bReadOnly) { }
157 
158  drwnPropertyType type() const { return DRWN_DOUBLE_PROPERTY; }
159  string asString() const;
160  drwnPropertyInterface* clone() const { return new drwnDoubleProperty(*this); }
161 
162  bool setProperty(bool value);
163  bool setProperty(int value);
164  bool setProperty(double value);
165  bool setProperty(const string& value);
166  bool setProperty(const Eigen::VectorXd& value);
167  bool setProperty(const Eigen::MatrixXd& value);
168 };
169 
171 {
172  protected:
173  double _lowerBound;
174  double _upperBound;
175 
176  public:
177  drwnDoubleRangeProperty(double *storage, bool bReadOnly = false) :
178  drwnDoubleProperty(storage, bReadOnly), _lowerBound(0.0), _upperBound(1.0) { }
179  drwnDoubleRangeProperty(double *storage, double lb, double ub, bool bReadOnly = false) :
180  drwnDoubleProperty(storage, bReadOnly), _lowerBound(lb), _upperBound(ub) { }
181 
182  drwnPropertyInterface* clone() const { return new drwnDoubleRangeProperty(*this); }
183 
184  bool setProperty(double value);
185 };
186 
188 {
189  public:
190  drwnStringProperty(string *storage, bool bReadOnly = false) :
191  drwnStoragePropertyInterface<string>(storage, bReadOnly) { }
192 
193  drwnPropertyType type() const { return DRWN_STRING_PROPERTY; }
194  string asString() const;
195 
196  drwnPropertyInterface* clone() const { return new drwnStringProperty(*this); }
197 
198  bool setProperty(bool value);
199  bool setProperty(int value);
200  bool setProperty(double value);
201  bool setProperty(const string& value);
202  bool setProperty(const Eigen::VectorXd& value);
203  bool setProperty(const Eigen::MatrixXd& value);
204 };
205 
207 {
208  public:
209  drwnFilenameProperty(string *storage, bool bReadOnly = false) :
210  drwnStringProperty(storage, bReadOnly) { }
211  drwnPropertyType type() const { return DRWN_FILENAME_PROPERTY; }
212  drwnPropertyInterface* clone() const { return new drwnFilenameProperty(*this); }
213 };
214 
216 {
217  public:
218  drwnDirectoryProperty(string *storage, bool bReadOnly = false) :
219  drwnStringProperty(storage, bReadOnly) { }
220  drwnPropertyType type() const { return DRWN_DIRECTORY_PROPERTY; }
221  drwnPropertyInterface* clone() const { return new drwnDirectoryProperty(*this); }
222 };
223 
224 class drwnListProperty : public drwnStoragePropertyInterface<list<string> >
225 {
226  public:
227  drwnListProperty(list<string> *storage, bool bReadOnly = false) :
228  drwnStoragePropertyInterface<list<string> >(storage, bReadOnly) { }
229 
230  drwnPropertyType type() const { return DRWN_LIST_PROPERTY; }
231  string asString() const;
232 
233  drwnPropertyInterface* clone() const { return new drwnListProperty(*this); }
234 
235  bool setProperty(bool value);
236  bool setProperty(int value);
237  bool setProperty(double value);
238  bool setProperty(const string& value);
239  bool setProperty(const Eigen::VectorXd& value);
240  bool setProperty(const Eigen::MatrixXd& value);
241 };
242 
244  protected:
245  const vector<string> *_choices;
246 
247  public:
248  drwnSelectionProperty(int *storage, const vector<string>* choices,
249  bool bReadOnly = false) : drwnStoragePropertyInterface<int>(storage, bReadOnly),
250  _choices(choices) { }
251 
252  drwnPropertyType type() const { return DRWN_SELECTION_PROPERTY; }
253  string asString() const;
254  drwnPropertyInterface* clone() const { return new drwnSelectionProperty(*this); }
255 
256  const vector<string> *getChoices() const { return _choices; }
257 
258  bool setProperty(bool value);
259  bool setProperty(int value);
260  bool setProperty(double value);
261  bool setProperty(const string& value);
262 
263  // i/o
264  void read(drwnXMLNode& xml);
265  void write(drwnXMLNode& xml) const;
266 };
267 
268 class drwnVectorProperty : public drwnStoragePropertyInterface<Eigen::VectorXd>
269 {
270  public:
271  drwnVectorProperty(Eigen::VectorXd *storage, bool bReadOnly = false) :
272  drwnStoragePropertyInterface<Eigen::VectorXd>(storage, bReadOnly) { }
273 
274  drwnPropertyType type() const { return DRWN_VECTOR_PROPERTY; }
275  string asString() const;
276  drwnPropertyInterface* clone() const { return new drwnVectorProperty(*this); }
277 
278  bool setProperty(bool value);
279  bool setProperty(int value);
280  bool setProperty(double value);
281  bool setProperty(const string& value);
282  bool setProperty(const Eigen::VectorXd& value);
283  bool setProperty(const Eigen::MatrixXd& value);
284 
285  // i/o
286  void read(drwnXMLNode& xml);
287  void write(drwnXMLNode& xml) const;
288 };
289 
290 class drwnMatrixProperty : public drwnStoragePropertyInterface<Eigen::MatrixXd>
291 {
292  public:
293  drwnMatrixProperty(Eigen::MatrixXd *storage, bool bReadOnly = false) :
294  drwnStoragePropertyInterface<Eigen::MatrixXd>(storage, bReadOnly) { }
295 
296  drwnPropertyType type() const { return DRWN_MATRIX_PROPERTY; }
297  string asString() const;
298  drwnPropertyInterface* clone() const { return new drwnMatrixProperty(*this); }
299 
300  bool setProperty(bool value);
301  bool setProperty(int value);
302  bool setProperty(double value);
303  bool setProperty(const string& value);
304  bool setProperty(const Eigen::VectorXd& value);
305  bool setProperty(const Eigen::MatrixXd& value);
306 
307  // i/o
308  void read(drwnXMLNode& xml);
309  void write(drwnXMLNode& xml) const;
310 };
311 
312 // drwnProperties -------------------------------------------------------------
313 
339  private:
340  // Properties. This data member is private, it cannot be accessed directly
341  // from derived classes. Instead derived classes should use the access
342  // functions: declareProperty and undeclareProperty. All properties need
343  // to be declared before they can be set. This is usually done in the
344  // derived class's constructor.
345 
346  // TODO: replace below with templated drwnOrderedMap class with:
347  // vector<pair<map<string, unsigned>::iterator, T> > _entries;
348  // map<string, unsigned> _index;
349  //typedef drwnSmartPointer<drwnPropertyInterface> drwnPropertyInterfacePtr;
351  vector<pair<string, drwnPropertyInterfacePtr> > _properties;
352  map<string, unsigned> _propertiesIndex;
353 
354  public:
355  drwnProperties() { /* do nothing */ }
356  virtual ~drwnProperties();
357 
358  // Returns the number of properties and mapping from property
359  // names to index.
360  unsigned numProperties() const { return _properties.size(); }
361  bool hasProperty(const string& name) const;
362  bool hasProperty(const char *name) const { return hasProperty(string(name)); }
363  unsigned findProperty(const string& name) const;
364  unsigned findProperty(const char *name) const { return findProperty(string(name)); }
365 
366  // Called by owner of the derived class to set properties. Incorrect
367  // types are cast into the appropriate form.
368  void setProperty(unsigned indx, bool value);
369  void setProperty(unsigned indx, int value);
370  void setProperty(unsigned indx, double value);
371  void setProperty(unsigned indx, const string& value);
372  void setProperty(unsigned indx, const char *value);
373  void setProperty(unsigned indx, const Eigen::VectorXd& value);
374  void setProperty(unsigned indx, const Eigen::MatrixXd& value);
375 
376  void setProperty(const char *name, bool value);
377  void setProperty(const char *name, int value);
378  void setProperty(const char *name, double value);
379  void setProperty(const char *name, const string& value);
380  void setProperty(const char *name, const char *value);
381  void setProperty(const char *name, const Eigen::VectorXd& value);
382  void setProperty(const char *name, const Eigen::MatrixXd& value);
383 
384  // Used to access property settings.
385  string getPropertyAsString(unsigned indx) const;
386  drwnPropertyType getPropertyType(unsigned indx) const;
387  bool isReadOnly(unsigned indx) const;
388 
389  const drwnPropertyInterface *getProperty(unsigned indx) const;
390  const drwnPropertyInterface *getProperty(const char *name) const;
391  bool getBoolProperty(unsigned indx) const;
392  int getIntProperty(unsigned indx) const;
393  double getDoubleProperty(unsigned indx) const;
394  const string& getStringProperty(unsigned indx) const;
395  const list<string>& getListProperty(unsigned indx) const;
396  int getSelectionProperty(unsigned indx) const;
397  const Eigen::VectorXd& getVectorProperty(unsigned indx) const;
398  const Eigen::MatrixXd& getMatrixProperty(unsigned indx) const;
399 
400  // Return list of all property names.
401  const string& getPropertyName(unsigned indx) const;
402  vector<string> getPropertyNames() const;
403 
404  // Serialization/deserialization.
405  void readProperties(drwnXMLNode& xml, const char *tag = "property");
406  void writeProperties(drwnXMLNode& xml, const char *tag = "property") const;
407  void printProperties(ostream &os) const;
408 
409  protected:
410  // Declare properties in the constructor for the class. An undeclare
411  // method is provided to be able to remove properties in derived classes.
412  // Ownership of the drwnPropertyInterface object is transferred to this
413  // class and will be destoryed when no longer needed.
414  void declareProperty(const string& name, drwnPropertyInterface *optif);
415 
416  // Remove an property declared in a base class. Allows class to remove
417  // exposure to certain base class properties.
418  void undeclareProperty(const string& name);
419 
420  // Mirror properties of a contained class. Since the property interface points
421  // to contained data members, this should only be used with static data
422  // members.
423  void exposeProperties(drwnProperties *opts, const string& prefix = string(""),
424  bool bSerializable = false);
425 
426  // Called whenever an property changes (i.e. setProperty or setProperties
427  // is called). Should be overridden by derived classed. Can be used to
428  // validate properties or setup internal datastructures. The default
429  // behaviour is to do nothing. This is a simpler interface than defining
430  // a new drwnPropertyInterface type, but is only called *after* the property
431  // is set, which may be problematic in some cases.
432  virtual void propertyChanged(const string& name) {
433  // do nothing
434  }
435 
436  private:
437  // properties cannot be copied because they point to member variables
438  drwnProperties(const drwnProperties& o) { DRWN_ASSERT(false); }
439 };
440 
441 // drwnPropertiesCopy ------------------------------------------------------
442 
444  protected:
445  // copy of property value and modification flag
446  vector<pair<void *, bool> > _data;
447 
448  public:
451 
452  void copyBack(drwnProperties *o) const;
453 
454  protected:
455  void propertyChanged(const string& name);
456 };
457 
Provides an abstract interface for dynamic properties.
Definition: drwnProperties.h:338
Definition: drwnProperties.h:152
Definition: drwnProperties.h:290
Definition: drwnProperties.h:243
Definition: drwnProperties.h:135
Definition: drwnProperties.h:99
Definition: drwnProperties.h:40
Definition: drwnProperties.h:215
Definition: drwnProperties.h:443
Definition: drwnProperties.h:206
Definition: drwnProperties.h:187
Definition: drwnProperties.h:224
Definition: drwnProperties.h:83
Provides XML parsing functionality for serializing and deserializing objects and containers of object...
Definition: drwnProperties.h:117
Definition: drwnProperties.h:268
Definition: drwnProperties.h:170