00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef OPSPACE_PARAMETER_HPP
00023 #define OPSPACE_PARAMETER_HPP
00024
00025 #include <jspace/Status.hpp>
00026 #include <jspace/wrap_eigen.hpp>
00027 #include <boost/shared_ptr.hpp>
00028 #include <map>
00029
00030
00031 namespace opspace {
00032
00033
00034 using jspace::Status;
00035 using jspace::Vector;
00036 using jspace::Matrix;
00037
00038
00042 typedef enum {
00043 PARAMETER_TYPE_VOID,
00044 PARAMETER_TYPE_STRING,
00045 PARAMETER_TYPE_INTEGER,
00046 PARAMETER_TYPE_REAL,
00047 PARAMETER_TYPE_VECTOR,
00048 PARAMETER_TYPE_MATRIX
00049 } parameter_type_t;
00050
00051
00052 typedef enum {
00053 PARAMETER_FLAG_DEFAULT = 0,
00054 PARAMETER_FLAG_NOLOG = 1,
00055 PARAMETER_FLAG_READONLY = 2
00056 } parameter_flags_t;
00057
00058
00059 class ParameterReflection;
00060
00061
00073 class Parameter
00074 {
00075 public:
00076 std::string const name_;
00077 parameter_type_t const type_;
00078 parameter_flags_t const flags_;
00079 ParameterReflection const * checker_;
00080
00081 Parameter(std::string const & name,
00082 parameter_type_t type,
00083 parameter_flags_t flags,
00084 ParameterReflection const * checker);
00085
00086 virtual ~Parameter();
00087
00088 virtual int const * getInteger() const;
00089 virtual std::string const * getString() const;
00090 virtual double const * getReal() const;
00091 virtual Vector const * getVector() const;
00092 virtual Matrix const * getMatrix() const;
00093
00094 virtual Status set(int value);
00095 virtual Status set(std::string const & value);
00096 virtual Status set(double value);
00097 virtual Status set(Vector const & value);
00098 virtual Status set(Matrix const & value);
00099
00100 virtual void dump(std::ostream & os, std::string const & prefix) const;
00101 };
00102
00103
00105 class IntegerParameter : public Parameter {
00106 public:
00107 IntegerParameter(std::string const & name,
00108 parameter_flags_t flags,
00109 ParameterReflection const * checker,
00110 int * instance);
00111 virtual int const * getInteger() const { return integer_; }
00112 virtual Status set(int integer);
00113 virtual void dump(std::ostream & os, std::string const & prefix) const;
00114 protected:
00115 int * integer_;
00116 };
00117
00118
00120 class StringParameter : public Parameter {
00121 public:
00122 StringParameter(std::string const & name,
00123 parameter_flags_t flags,
00124 ParameterReflection const * checker,
00125 std::string * instance);
00126 virtual std::string const * getString() const { return string_; }
00127 virtual Status set(std::string const & value);
00128 virtual void dump(std::ostream & os, std::string const & prefix) const;
00129 protected:
00130 std::string * string_;
00131 };
00132
00133
00135 class RealParameter : public Parameter {
00136 public:
00137 RealParameter(std::string const & name,
00138 parameter_flags_t flags,
00139 ParameterReflection const * checker,
00140 double * real);
00141 virtual double const * getReal() const { return real_; }
00142 virtual Status set(double real);
00143 virtual void dump(std::ostream & os, std::string const & prefix) const;
00144 protected:
00145 double * real_;
00146 };
00147
00148
00150 class VectorParameter : public Parameter {
00151 public:
00152 VectorParameter(std::string const & name,
00153 parameter_flags_t flags,
00154 ParameterReflection const * checker,
00155 Vector * vector);
00156 virtual Vector const * getVector() const { return vector_; }
00157 virtual Status set(Vector const & vector);
00158 virtual void dump(std::ostream & os, std::string const & prefix) const;
00159 protected:
00160 Vector * vector_;
00161 };
00162
00163
00165 class MatrixParameter : public Parameter {
00166 public:
00167 MatrixParameter(std::string const & name,
00168 parameter_flags_t flags,
00169 ParameterReflection const * checker,
00170 Matrix * matrix);
00171 virtual Matrix const * getMatrix() const { return matrix_; }
00172 virtual Status set(Matrix const & matrix);
00173 virtual void dump(std::ostream & os, std::string const & prefix) const;
00174 protected:
00175 Matrix * matrix_;
00176 };
00177
00178
00179 typedef std::map<std::string, Parameter *> parameter_lookup_t;
00180
00181
00187 class ParameterReflection
00188 {
00189 public:
00190 ParameterReflection(std::string const & type_name,
00191 std::string const & instance_name);
00192
00193 virtual ~ParameterReflection();
00194
00196 virtual Status check(int const * param, int value) const;
00197
00199 virtual Status check(std::string const * param, std::string const & value) const;
00200
00202 virtual Status check(double const * param, double value) const;
00203
00205 virtual Status check(Vector const * param, Vector const & value) const;
00206
00208 virtual Status check(Matrix const * param, Matrix const & value) const;
00209
00210 inline std::string const & getName() const { return instance_name_; }
00211 inline std::string const & getTypeName() const { return type_name_; }
00212
00219 Parameter * lookupParameter(std::string const & name);
00220
00227 Parameter const * lookupParameter(std::string const & name) const;
00228
00235 Parameter * lookupParameter(std::string const & name, parameter_type_t type);
00236
00243 Parameter const * lookupParameter(std::string const & name, parameter_type_t type) const;
00244
00245 parameter_lookup_t const & getParameterTable() const { return parameter_lookup_; }
00246
00247 virtual void dump(std::ostream & os,
00248 std::string const & title,
00249 std::string const & prefix) const;
00250
00251 protected:
00252 std::string const type_name_;
00253 std::string const instance_name_;
00254
00266 IntegerParameter * declareParameter(std::string const & name,
00267 int * integer,
00268 parameter_flags_t flags = PARAMETER_FLAG_DEFAULT);
00269
00271 StringParameter * declareParameter(std::string const & name,
00272 std::string * instance,
00273 parameter_flags_t flags = PARAMETER_FLAG_DEFAULT);
00274
00276 RealParameter * declareParameter(std::string const & name,
00277 double * real,
00278 parameter_flags_t flags = PARAMETER_FLAG_DEFAULT);
00279
00281 VectorParameter * declareParameter(std::string const & name,
00282 Vector * vector,
00283 parameter_flags_t flags = PARAMETER_FLAG_DEFAULT);
00284
00286 MatrixParameter * declareParameter(std::string const & name,
00287 Matrix * matrix,
00288 parameter_flags_t flags = PARAMETER_FLAG_DEFAULT);
00289
00290 private:
00291 parameter_lookup_t parameter_lookup_;
00292 };
00293
00294
00295 class ReflectionRegistry
00296 {
00297 public:
00298 struct enumeration_entry_s {
00299 std::string type_name, instance_name, parameter_name;
00300 Parameter * parameter;
00301 };
00302 typedef std::vector<enumeration_entry_s> enumeration_t;
00303
00304
00305 void add(boost::shared_ptr<ParameterReflection> instance);
00306
00307 boost::shared_ptr<ParameterReflection> find(std::string const & type_name,
00308 std::string const & instance_name);
00309
00310 void enumerate(enumeration_t & enumeration);
00311
00312 Parameter * lookupParameter(std::string const & type_name,
00313 std::string const & instance_name,
00314 std::string const & parameter_name);
00315
00316 Parameter const * lookupParameter(std::string const & type_name,
00317 std::string const & instance_name,
00318 std::string const & parameter_name) const;
00319
00320 Parameter * lookupParameter(std::string const & type_name,
00321 std::string const & instance_name,
00322 std::string const & parameter_name,
00323 parameter_type_t parameter_type);
00324
00325 Parameter const * lookupParameter(std::string const & type_name,
00326 std::string const & instance_name,
00327 std::string const & parameter_name,
00328 parameter_type_t parameter_type) const;
00329
00330 private:
00331 typedef std::map<std::string, boost::shared_ptr<ParameterReflection> > instance_map_t;
00332 typedef std::map<std::string, instance_map_t> type_map_t;
00333 type_map_t type_map_;
00334 };
00335
00336
00337 class ParameterLog
00338 {
00339 public:
00340 template<typename parameter_t, typename storage_t>
00341 struct log_s {
00342 explicit log_s(parameter_t const * pp): parameter(pp) {}
00343 parameter_t const * parameter;
00344 std::vector<storage_t> log;
00345 };
00346
00347 ParameterLog(std::string const & name, parameter_lookup_t const & parameter_lookup);
00348
00349 void update(long long timestamp);
00350 void writeFiles(std::string const & prefix, std::ostream * progress) const;
00351
00352 std::string const name;
00353 std::vector<long long> timestamp;
00354 std::vector<log_s<IntegerParameter, int> > intlog;
00355 std::vector<log_s<StringParameter, std::string> > strlog;
00356 std::vector<log_s<RealParameter, double> > reallog;
00357 std::vector<log_s<VectorParameter, Vector> > veclog;
00358 std::vector<log_s<MatrixParameter, Matrix> > mxlog;
00359 };
00360
00361 }
00362
00363 #endif // OPSPACE_PARAMETER_HPP