chemically_typed_object.hpp

Go to the documentation of this file.
00001 // -*- mode: c++; indent-tabs-mode: nil; -*-
00002 //
00003 //The Biomolecule Toolkit (BTK) is a C++ library for use in the
00004 //modeling, analysis, and design of biological macromolecules.
00005 //Copyright (C) 2006, Tim Robertson <kid50@users.sourceforge.net>
00006 //
00007 //This program is free software; you can redistribute it and/or modify
00008 //it under the terms of the GNU Lesser General Public License as published
00009 //by the Free Software Foundation; either version 2.1 of the License, or (at
00010 //your option) any later version.
00011 //
00012 //This program is distributed in the hope that it will be useful,  but
00013 //WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 //Lesser General Public License for more details.
00016 //
00017 //You should have received a copy of the GNU Lesser General Public License
00018 //along with this program; if not, write to the Free Software Foundation,
00019 //Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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 } // UTILITY
00156 } // BTK
00157 
00158 #endif //BTK_UTILITY_CHEMICALLY_TYPED_OBJECT_HPP 

Generated on Sun Jul 15 20:46:25 2007 for BTK Core by  doxygen 1.5.1