btk_container.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) 2005-2006, Christopher Saunders <ctsa@users.sourceforge.net>,
00006 //                         Tim Robertson <kid50@users.sourceforge.net>
00007 //
00008 //This program is free software; you can redistribute it and/or modify
00009 //it under the terms of the GNU Lesser General Public License as published
00010 //by the Free Software Foundation; either version 2.1 of the License, or (at
00011 //your option) any later version.
00012 //
00013 //This program is distributed in the hope that it will be useful,  but
00014 //WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //Lesser General Public License for more details.
00017 //
00018 //You should have received a copy of the GNU Lesser General Public License
00019 //along with this program; if not, write to the Free Software Foundation,
00020 //Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 
00024 
00025 #ifndef BTK_UTILITY_BTK_CONTAINER_HPP
00026 #define BTK_UTILITY_BTK_CONTAINER_HPP
00027 
00028 #include <iterator>
00029 #include <vector>
00030 
00031 #include <boost/concept_check.hpp>
00032 
00033 namespace BTK {
00034 namespace UTILITY {
00035 
00036 #ifndef DOXYGEN_SHOULD_IGNORE_THIS
00037 namespace internal {
00038 
00039 template <typename C>
00040 typename C::reference
00041 get_element(C & container, unsigned n) 
00042 {
00043   typename C::iterator i = container.begin();
00044   std::advance(i,n);
00045   return *i;
00046 } 
00047 
00048 template <typename V>
00049 typename std::vector<V>::reference
00050 get_element(std::vector<V> & container, unsigned n)
00051 {
00052   return container[n];
00053 }
00054 
00055 template <typename C>
00056 typename C::const_reference
00057 get_element(C const & container, unsigned n) 
00058 {
00059   typename C::const_iterator i = container.begin();
00060   std::advance(i,n);
00061   return *i;
00062 } 
00063 
00064 template <typename V>
00065 typename std::vector<V>::const_reference
00066 get_element(std::vector<V> const & container, unsigned n)
00067 {
00068   return container[n];
00069 }
00070 
00071 } // BTK::UTILITY::internal
00072 #endif // DOXYGEN_SHOULD_IGNORE_THIS
00073 
00074 template <typename T, typename StorageStrategy>
00075 class BTKContainer
00076 {
00077   BOOST_CLASS_REQUIRE(StorageStrategy,boost,Mutable_ReversibleContainerConcept);
00078 
00079 public:
00080   typedef BTKContainer<T,StorageStrategy> self_type;
00081 
00082   typedef T value_type;
00083   typedef typename StorageStrategy::iterator iterator;
00084   typedef typename StorageStrategy::const_iterator const_iterator;
00085   typedef typename StorageStrategy::reverse_iterator reverse_iterator;
00086   typedef typename StorageStrategy::const_reverse_iterator const_reverse_iterator;
00087   typedef typename StorageStrategy::reference reference;
00088   typedef typename StorageStrategy::const_reference const_reference;
00089   typedef typename StorageStrategy::pointer pointer;
00090   typedef typename StorageStrategy::const_pointer const_pointer;
00091   typedef typename StorageStrategy::difference_type difference_type;
00092   typedef typename StorageStrategy::size_type size_type;
00093 
00095 
00096   BTKContainer(self_type const & source) : _storage(source._storage) {}
00097   virtual ~BTKContainer() {}
00098 
00099   iterator begin() { return _storage.begin(); }
00100   const_iterator begin() const { return _storage.begin(); }
00101   
00102   iterator end() { return _storage.end(); }
00103   const_iterator end() const { return _storage.end(); }
00104 
00105   reverse_iterator rbegin() { return _storage.rbegin(); }
00106   const_reverse_iterator rbegin() const { return _storage.rbegin(); }
00107   
00108   reverse_iterator rend() { return _storage.rend(); }
00109   const_reverse_iterator rend() const { return _storage.rend(); }
00110   
00111   size_type size() const { return _storage.size(); }
00112   size_type max_size() const { return _storage.max_size(); }
00113 
00114   bool empty() const { return _storage.empty(); }
00116 
00117   reference operator[](size_type n) 
00118   {
00119     return internal::get_element(_storage,n);
00120   }
00121 
00122   const_reference operator[](size_type n) const
00123   {
00124     return internal::get_element(_storage,n);
00125   }
00126 
00127 protected:
00128   
00132 
00133   void swap(self_type & b) { _storage.swap(b._storage); }
00134   
00135   self_type & operator=(self_type const & rhs) 
00136   { 
00137     _storage = rhs._storage; 
00138     return *this; 
00139   }
00140 
00141   bool operator==(self_type const & rhs) const
00142   {
00143     return _storage == rhs._storage;
00144   }
00145 
00146   bool operator!=(self_type const & rhs) const
00147   {
00148     return _storage != rhs._storage;
00149   }
00150 
00151   bool operator<(self_type const & rhs) const
00152   {
00153     return _storage < rhs._storage;
00154   }
00156   
00157 #define DOXYGEN_SHOULD_IGNORE_THIS
00158   // These are constructors that are needed for the STL Sequence concept and
00159   // the BTKSequence class, but aren't part of the STL Reversible Container concept.
00160   BTKContainer(size_type n = 0, const_reference t = value_type()) : _storage(n,t) {}
00161   
00162   template <typename InputIterator>
00163   BTKContainer(InputIterator i, InputIterator j) : _storage(i,j) 
00164   {
00165     boost::function_requires<boost::InputIteratorConcept<InputIterator> >();
00166   }
00167 #undef DOXYGEN_SHOULD_IGNORE_THIS
00168 
00169   StorageStrategy _storage;
00170 };
00171 
00172 #define IMPORT_BTK_CONTAINER_TYPES(Container)                           \
00173   typedef typename Container::value_type value_type;                    \
00174   typedef typename Container::iterator iterator;                        \
00175   typedef typename Container::const_iterator const_iterator;            \
00176   typedef typename Container::reverse_iterator reverse_iterator;        \
00177   typedef typename Container::const_reverse_iterator const_reverse_iterator; \
00178   typedef typename Container::reference reference;                      \
00179   typedef typename Container::const_reference const_reference;          \
00180   typedef typename Container::pointer pointer;                          \
00181   typedef typename Container::const_pointer const_pointer;              \
00182   typedef typename Container::difference_type difference_type;          \
00183   typedef typename Container::size_type size_type;
00184 
00185 #define IMPORT_BTK_CONTAINER_METHODS(Container)                 \
00186   using Container::begin;                                       \
00187   using Container::end;                                         \
00188   using Container::rbegin;                                      \
00189   using Container::rend;                                        \
00190   using Container::size;                                        \
00191   using Container::max_size;                                    \
00192   using Container::empty;                                       \
00193   using Container::operator[];                                  
00194 
00195 } // namesapce UTILITY
00196 } // namespace BTK
00197 
00198 #endif // BTK_UTILITY_BTK_CONTAINER_HPP

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