Documentation


container_traits.h

Go to the documentation of this file.
00001 #if !defined(____________animal_container_traits_h_____________)
00002 #define ____________animal_container_traits_h_____________ 1
00003 
00004 // -------------------------------------------------------------------------- 
00005 
00006 #include <vector>  // just for std::size_t
00007 
00008 // -------------------------------------------------------------------------- 
00009 
00010 namespace animal
00011 {
00012 
00013   // --- a general version of container traits ------------------------------ 
00014 
00016   template <typename Cont>
00017     struct container_traits
00018     {
00019       typedef typename Cont::iterator  iterator;  
00020       typedef iterator iter_type; 
00021       typedef typename Cont::size_type size_type; 
00022       typedef typename Cont::value_type value_type;  
00023       typedef typename Cont::reference reference; 
00024       //typedef typename Cont::pointer pointer;
00026       static iterator begin(Cont &cont) { return cont.begin(); }
00028       static iterator end(Cont &cont) { return cont.end(); }
00030       static size_type size(Cont &cont) { return cont.size(); }
00031     };
00032 
00033   // --- a version of container traits for constant constainer --------------
00034 
00036   template <typename Cont>
00037     struct container_traits<Cont const>
00038     {
00039       typedef typename Cont::const_iterator iterator;  
00040       typedef iterator iter_type; 
00041       typedef typename Cont::size_type size_type; 
00042       typedef typename Cont::value_type value_type;  
00043       typedef typename Cont::const_reference reference; 
00044       //typedef typename Cont::const_pointer pointer;
00046       static iterator begin(Cont const &cont) { return cont.begin(); }
00048       static iterator end(Cont const &cont) { return cont.end(); }
00050       static size_type size(Cont const &cont) { return cont.size(); }
00051     };
00052 
00053   // --- a special version for non-const built-in arrays -------------------- 
00054 
00056   template <typename T, std::size_t sz>
00057     struct container_traits<T[sz]>
00058     {
00059       typedef T* iterator;   
00060       typedef iterator iter_type; 
00061       typedef T value_type; 
00062       typedef value_type& reference; 
00063       typedef std::size_t size_type; 
00064 
00066       static iterator begin(T (&array)[sz]) { return array; }
00068       static iterator end(T (&array)[sz]) { return array + sz; }
00070       static size_type size(T (&)[sz]) { return sz; }
00071     };
00072 
00073   // --- a special version for const built-in arrays ------------------------ 
00074 
00076   template <typename T, std::size_t sz>
00077     struct container_traits<T const[sz]>
00078     {
00079       typedef T const* iterator;   
00080       typedef iterator iter_type; 
00081       typedef std::size_t size_type; 
00082       typedef T const value_type; 
00083       typedef value_type& reference; 
00084       //typedef value_type* pointer;
00086       static iterator begin(T const (&array)[sz]) { return array; }
00088       static iterator end(T const (&array)[sz]) { return array + sz; }
00090       static size_type size(T const (&array)[sz]) { return sz; }
00091     };
00092 
00093 //  template <typename T, int sz>
00094 //  inline char (&sizer(T (&)[sz]))[sz];
00095   
00096   
00097   // --- general version of the global accessor functions --------------------- 
00098 
00100   template <typename Cont>
00101     inline typename container_traits<Cont>::iterator
00102     begin(Cont &cont) { return container_traits<Cont>::begin(cont); }
00103 
00105   template <typename Cont>
00106     inline typename container_traits<Cont>::iterator
00107     end(Cont &cont) { return container_traits<Cont>::end(cont); }
00108 
00110   template <typename Cont>
00111     inline typename container_traits<Cont>::size_type
00112     size(Cont &cont) { return container_traits<Cont>::size(cont); }
00113 
00114   // --- Actually the above should be sufficient but compilers seem -----------
00115   // --- to welcome some help. So here we go:
00116 
00118   template <typename T, std::size_t sz>
00119     inline typename container_traits<T[sz]>::iterator
00120     begin(T (&a)[sz]) { return container_traits<T[sz]>::begin(a); }
00121   
00123   template <typename T, std::size_t sz>
00124     inline typename container_traits<T[sz]>::iterator
00125     end(T (&a)[sz]) { return container_traits<T[sz]>::end(a); }
00126   
00128   template <typename T, std::size_t sz>
00129     inline typename container_traits<T[sz]>::size_type
00130     size(T (&a)[sz]) { return container_traits<T[sz]>::size(a); }
00131   
00132   // --- Apparently the compilers also need some specific help, ---------------
00133 
00134   // --- EDG-2.39 wants to pass around pointers in some contexts --------------
00135 #ifdef __EDG__
00136   template <typename T>
00137     struct container_traits<T*>
00138     {
00139       typedef T*     iterator;
00140       typedef iterator iter_type; // just for backward compatibility
00141       typedef std::size_t size_type;
00142     };
00143 #endif
00144 
00145   // --- egcs-1998-11-22 apparently likes an extra const version: -------------
00146 #ifdef __GNUG__
00147 
00148   template <typename T, std::size_t sz>
00149     inline typename container_traits<T const[sz]>::iterator
00150     begin(T const(&a)[sz]) { return container_traits<T const[sz]>::begin(a); }
00151   
00153   template <typename T, std::size_t sz>
00154     inline typename container_traits<T const[sz]>::iterator
00155     end(T const(&a)[sz]) { return container_traits<T const[sz]>::end(a); }
00156   
00158   template <typename T, std::size_t sz>
00159     inline typename container_traits<T const[sz]>::size_type
00160     size(T const (&a)[sz]) { return container_traits<T const[sz]>::size(a); }
00161 #endif
00162 
00163 
00164 // -----------------------------------------------------------------------------
00165 // Elementary type operators
00166 
00167 template< typename T >
00168 struct value_type {
00169     typedef typename T::value_type type;
00170 }                                     ;
00171 
00172 
00173   
00174 }//animal
00175 
00176 // -----------------------------------------------------------------------------
00177 
00178 #endif

Generated on Thu Dec 23 13:52:24 2004 by doxygen 1.3.6