Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

propertylist.h

Go to the documentation of this file.
00001 /*-*-c++-*-*****************************************************************
00002                           propertylist.h  -  description
00003                              -------------------
00004     begin                : June 2003
00005     copyright            : (C) 2003 by Christian Stimming
00006     email                : stimming@tuhh.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00024  *   MA  02111-1307  USA                                                   *
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 //#include <list>
00042 #include <vector>
00043 #include <stdexcept>
00044 
00045 #include <simthetic/misc.h>
00046 
00047 // Needed for windows DLLs
00048 #ifndef DLLIMPORT
00049 #  if defined(__declspec) | defined(IS_WIN32)
00050 #    if BUILDING_SIMTHETICBASE_DLL
00051 #      define DLLIMPORT __declspec (dllexport)
00052 #    else     /* Not BUILDING_SIMTHETICBASE_DLL */
00053 #      define DLLIMPORT __declspec (dllimport)
00054 #    endif    /* Not BUILDING_SIMTHETICBASE_DLL */
00055 #  else
00056 #    define DLLIMPORT
00057 #  endif    /* __declspec */
00058 #endif  /* DLLIMPORT */
00059 #ifndef EXPIMP_TEMPLATE
00060 #  if defined(__declspec) | defined(IS_WIN32)
00061 #    if BUILDING_SIMTHETICBASE_DLL
00062 #      define EXPIMP_TEMPLATE
00063 #    else     /* Not BUILDING_SIMTHETICBASE_DLL */
00064 #      define EXPIMP_TEMPLATE extern
00065 #    endif    /* Not BUILDING_SIMTHETICBASE_DLL */
00066 #  else
00067 #    define EXPIMP_TEMPLATE
00068 #  endif    /* __declspec */
00069 #endif  /* DLLIMPORT */
00070 
00071 #ifndef __GNUC__
00072 // This macro enables a workaround which is necessary for Microsoft
00073 // Visual C++. See below at Property::getValue().
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     // Checks whether this is the given type.
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     //Property& operator=(const Property& p);
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     // These are used by MSVC but not by GCC. See below.
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   // These are used by GCC only because MSVC doesn't accept these
00399   // specialisations outside of the class declaration, and GCC doesn't
00400   // accept the specialisations inside the class declaration.
00401 
00402 /* Declarations of available specializations. These don't
00403  * necessarily have to appear here, but since we provide a default
00404  * template implementation below, we have to use these here. */
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     // I have ZERO idea why getValue needs the extra qualifier
00563     // prefix. Really.
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 } // namespace
00647 
00648 #endif

Generated on Mon Apr 24 21:19:19 2006 for simthetic by  doxygen 1.4.1