00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00034 #ifndef PROPERTYLIST_H
00035 #define PROPERTYLIST_H
00036
00037 #include <string>
00038 #include <iostream>
00039 #include <map>
00040 #include <utility>
00041
00042 #include <vector>
00043 #include <stdexcept>
00044
00045 #include <simthetic/misc.h>
00046
00047
00048 #ifndef DLLIMPORT
00049 # if defined(__declspec) | defined(IS_WIN32)
00050 # if BUILDING_SIMTHETICBASE_DLL
00051 # define DLLIMPORT __declspec (dllexport)
00052 # else
00053 # define DLLIMPORT __declspec (dllimport)
00054 # endif
00055 # else
00056 # define DLLIMPORT
00057 # endif
00058 #endif
00059 #ifndef EXPIMP_TEMPLATE
00060 # if defined(__declspec) | defined(IS_WIN32)
00061 # if BUILDING_SIMTHETICBASE_DLL
00062 # define EXPIMP_TEMPLATE
00063 # else
00064 # define EXPIMP_TEMPLATE extern
00065 # endif
00066 # else
00067 # define EXPIMP_TEMPLATE
00068 # endif
00069 #endif
00070
00071 #ifndef __GNUC__
00072
00073
00074 # define MSVC_WORKAROUND(x) x
00075 #else
00076 # define MSVC_WORKAROUND(x)
00077 #endif // __GNUC__
00078
00079 namespace simth
00080 {
00081
00082
00106 class DLLIMPORT Property
00107 {
00108 public:
00113 enum p_type {
00115 bool_t,
00117 int_t,
00120 unsigned_t,
00122 double_t,
00124 complex_t,
00126 string_t,
00128 list_string_t,
00136 enum_t,
00140 sequencelength_t,
00144 vector_octal_int_t,
00146 vector_double_t,
00149 octal_int_t
00150 };
00151
00154 typedef std::vector<std::pair<std::string,std::string> > StringPairList;
00157 typedef std::vector<std::string> list_string;
00160 typedef std::vector<int> vector_int;
00163 typedef std::vector<double> vector_double;
00164
00165 private:
00166 std::string name_;
00167 p_type type_;
00168 std::string default_value_;
00169 bool empty_default_enabled_;
00170 std::string description_;
00173 union value_u {
00174 bool bool_v;
00175 int int_v;
00176 unsigned unsigned_v;
00177 double double_v;
00178 simth::Complex *complex_v;
00179 std::string *string_v;
00180 list_string *list_string_v;
00181 vector_int *vector_int_v;
00182 vector_double *vector_double_v;
00183 };
00184 value_u value_;
00185 bool isSet_;
00186 StringPairList possible_values;
00187 bool _obsolete;
00188 std::string _grouping;
00189
00190
00191 void check_type(p_type t) const;
00192 public:
00232 Property(const std::string& name, p_type type,
00233 const std::string& default_value = "",
00234 const std::string& description = "",
00235 const StringPairList& possible_values = StringPairList(),
00236 bool enable_empty_default = false,
00237 bool obsolete = false,
00238 const std::string& grouping = "");
00239
00241 Property(const Property& p);
00242
00245 Property();
00246
00247
00249 ~Property();
00250
00254 const std::string& getName() const {return name_; };
00256 p_type getType() const {return type_;};
00259 const std::string& getDefault_value() const {return default_value_; };
00261 const std::string& getDescription() const {return description_; };
00262
00268 bool hasDefault() const;
00272 bool isSet() const {return isSet_; };
00273
00283 bool hasEmptyDefaultEnabled() const { return empty_default_enabled_; };
00284
00298 const StringPairList& getPossibleValues() const
00299 { return possible_values; };
00300
00305 bool obsolete() const { return _obsolete; };
00306
00309 const std::string& grouping() const { return _grouping; };
00311
00318 template<class T>
00319 void setValue(const T& value);
00320
00329 template<class T>
00330 const T& getValue(MSVC_WORKAROUND(const T& dummyargument = 0)) const;
00332
00336 void setValueStr(const std::string& v);
00337
00344 std::string getValueStr() const;
00345
00350 std::string toString() const;
00352
00353
00356 static const char *p_type_toString(p_type t);
00357
00360 static p_type string_to_p_type(const std::string& t);
00361
00363 static list_string supported_types();
00364
00367 static list_string string_to_list(const std::string &s);
00368
00369 #ifndef __GNUC__
00370
00371 template<> void setValue(const bool& value);
00372 template<> void setValue(const int& value);
00373 template<> void setValue(const unsigned& value);
00374 template<> void setValue(const double& value);
00375 template<> void setValue(const Complex& value);
00376 template<> void setValue(const std::string& value);
00377 template<> void setValue(const list_string& value);
00378 template<> void setValue(const vector_int& value);
00379 template<> void setValue(const vector_double& value);
00380
00381 template<> const bool& getValue(const bool& dummyargument) const;
00382 template<> const int& getValue(const int& dummyargument) const;
00383 template<> const unsigned& getValue(const unsigned& dummyargument) const;
00384 template<> const double& getValue(const double& dummyargument) const;
00385 template<> const Complex& getValue(const Complex& dummyargument) const;
00386 template<> const std::string&
00387 getValue(const std::string& dummyargument) const;
00388 template<> const list_string&
00389 getValue(const list_string& dummyargument) const;
00390 template<> const vector_int&
00391 getValue(const vector_int& dummyargument) const;
00392 template<> const vector_double&
00393 getValue(const vector_double& dummyargument) const;
00394 #endif // ifndef __GNUC__
00395 };
00396
00397 #ifdef __GNUC__
00398
00399
00400
00401
00402
00403
00404
00405 template<> void Property::setValue(const bool& value);
00406 template<> void Property::setValue(const int& value);
00407 template<> void Property::setValue(const unsigned& value);
00408 template<> void Property::setValue(const double& value);
00409 template<> void Property::setValue(const Complex& value);
00410 template<> void Property::setValue(const std::string& value);
00411 template<> void Property::setValue(const list_string& value);
00412 template<> void Property::setValue(const vector_int& value);
00413 template<> void Property::setValue(const vector_double& value);
00414
00415 template<> const bool& Property::getValue<bool>() const;
00416 template<> const int& Property::getValue<int>() const;
00417 template<> const unsigned& Property::getValue<unsigned>() const;
00418 template<> const double& Property::getValue<double>() const;
00419 template<> const Complex& Property::getValue<Complex>() const;
00420 template<> const std::string& Property::getValue<std::string>() const;
00421 template<> const Property::list_string&
00422 Property::getValue<Property::list_string>() const;
00423 template<> const Property::vector_int&
00424 Property::getValue<Property::vector_int>() const;
00425 template<> const Property::vector_double&
00426 Property::getValue<Property::vector_double>() const;
00427
00429 template<class T> const T& Property::getValue() const
00430 {
00431 throw std::runtime_error("Property::getValue: "
00432 "Your type is not (yet) implemented.");
00433 }
00434 #endif // __GNUC__
00435
00437 template<class T> void Property::setValue(const T& value)
00438 {
00439 throw std::runtime_error("Property::setValue: "
00440 "Your type is not (yet) implemented.");
00441 }
00442
00454 class DLLIMPORT PropertyList
00455 {
00456 private:
00458 typedef std::string key_type;
00459
00460 public:
00462 typedef std::map<key_type, Property> map_type;
00463
00466 typedef map_type::value_type value_type;
00468 typedef map_type::iterator iterator;
00470 typedef map_type::const_iterator const_iterator;
00471 private:
00472 map_type list;
00473
00474 public:
00476 PropertyList();
00479 PropertyList(const PropertyList &);
00480
00482 ~PropertyList();
00483
00484
00520 void addProperty(const std::string& name, const std::string& type,
00521 const std::string& default_value = "",
00522 const std::string& description = "",
00523 const Property::StringPairList& possible_values =
00524 Property::StringPairList(),
00525 bool enable_empty_default = false);
00526
00531 void addProperty(const Property& p);
00532
00543 template<class propertyT>
00544 void setProperty(const std::string& name, const propertyT& value)
00545 { get(name).setValue(value); }
00547
00548
00559 template<class propertyT>
00560 const propertyT& getProperty(const std::string &name) const
00561 { return get(name).Property::getValue<propertyT>(); }
00562
00563
00564
00567 Property& get(const std::string& name);
00570 const Property& get(const std::string& name) const;
00571
00577 bool exists(const std::string& name) const;
00579
00580
00584 iterator begin() {return list.begin(); };
00586 const_iterator begin() const {return list.begin(); };
00587
00589 iterator end() {return list.end(); };
00591 const_iterator end() const {return list.end(); };
00592
00594 bool empty() const { return list.empty(); };
00596 size_t size() const { return list.size(); };
00597
00601 bool remove(const std::string& name);
00603
00604
00609 void toStream(std::ostream& os) const;
00610 };
00611
00613 std::ostream& operator<<(std::ostream& os, const PropertyList& pl);
00614
00615
00616 #ifndef __GNUC__
00617
00619 EXPIMP_TEMPLATE template DLLIMPORT Property::list_string;
00620 EXPIMP_TEMPLATE template DLLIMPORT Property::StringPairList;
00621 EXPIMP_TEMPLATE template DLLIMPORT Property::vector_int;
00622 EXPIMP_TEMPLATE template DLLIMPORT Property::vector_double;
00623 EXPIMP_TEMPLATE template DLLIMPORT PropertyList::map_type;
00624 #endif
00625
00626
00644 Property::StringPairList to_stringpairs(const char**a);
00645
00646 }
00647
00648 #endif