00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00022
00023 #ifndef BTK_UTILITY_CHEMICALLY_TYPED_OBJECT_HPP
00024 #define BTK_UTILITY_CHEMICALLY_TYPED_OBJECT_HPP
00025
00026 #include <string>
00027 #include <algorithm>
00028
00029 #include <boost/operators.hpp>
00030
00031 #include <btk/core/concepts/type_system_concept.hpp>
00032
00033 namespace BTK {
00034 namespace UTILITY {
00035
00043 template <typename ChemicalTypeSystemType,
00044 typename DictionaryType>
00045 class ChemicallyTypedObject :
00046 protected boost::less_than_comparable<ChemicallyTypedObject<ChemicalTypeSystemType,
00047 DictionaryType> >
00048 {
00049 BOOST_CLASS_REQUIRE(ChemicalTypeSystemType,BTK::CONCEPTS,TypeSystemConcept);
00050 typedef ChemicallyTypedObject<ChemicalTypeSystemType,
00051 DictionaryType> self_type;
00052 public:
00053 typedef ChemicalTypeSystemType chemical_type_system;
00054 typedef DictionaryType dictionary;
00055 typedef typename dictionary::id_type id_type;
00056
00057 ChemicallyTypedObject(chemical_type_system const & ts = chemical_type_system(),
00058 id_type t = id_type()):
00059 _type_system(ts), _type(t) {}
00060
00061 ChemicallyTypedObject(self_type const & src) :
00062 _type_system(src._type_system), _type(src._type) {}
00063
00064 virtual ~ChemicallyTypedObject() {}
00065
00067 id_type type() const { return _type; }
00068
00073 std::string name() const
00074 {
00075 typename dictionary::const_iterator i = get_dictionary().find(_type);
00076
00077 if (i != get_dictionary().end()) return i->second;
00078 return "";
00079 }
00080
00082 chemical_type_system const & get_chemical_type_system() const
00083 {
00084 return _type_system;
00085 }
00086
00088 chemical_type_system & get_chemical_type_system()
00089 {
00090 return _type_system;
00091 }
00092
00093 virtual dictionary const & get_dictionary() const = 0;
00094 virtual dictionary & get_dictionary() = 0;
00095
00096 protected:
00097
00099 virtual void set_type(id_type t) { _type = t; }
00100
00102 virtual void set_chemical_type_system(chemical_type_system const & ts)
00103 {
00104 _type_system = ts;
00105 }
00106
00107 self_type const & operator=(self_type const & src)
00108 {
00109 if (this == &src) return *this;
00110 _type_system = src._type_system;
00111 _type = src._type;
00112 return *this;
00113 }
00114
00115 void swap(self_type & rhs)
00116 {
00117 std::swap(_type_system,rhs._type_system);
00118 std::swap(_type,rhs._type);
00119 }
00120
00126
00127 bool operator==(self_type const & rhs) const
00128 {
00129 return (_type_system == rhs._type_system &&
00130 _type == rhs._type);
00131 }
00132
00133 bool operator!=(self_type const & rhs) const
00134 {
00135 return !(*this == rhs);
00136 }
00137
00138 bool operator<(self_type const & rhs) const
00139 {
00140 return (_type_system == rhs._type_system &&
00141 _type < rhs._type);
00142 }
00144
00145 private:
00146 chemical_type_system _type_system;
00147 id_type _type;
00148 };
00149
00150 #define IMPORT_CHEMICALLY_TYPED_OBJECT_TYPES(CTOType) \
00151 typedef typename CTOType::chemical_type_system chemical_type_system; \
00152 typedef typename CTOType::dictionary dictionary; \
00153 typedef typename CTOType::id_type id_type;
00154
00155 }
00156 }
00157
00158 #endif //BTK_UTILITY_CHEMICALLY_TYPED_OBJECT_HPP