CppUnit project page CppUnit home page

_stlimpl.h

Go to the documentation of this file.
00001 #ifndef CPPTL__STLIMPL_H_INCLUDED
00002 # define CPPTL__STLIMPL_H_INCLUDED
00003 
00004 # include "config.h"
00005 # include <stdlib.h>
00006 
00007 /*
00008  Notes: we need to prefix some (all) functions with cpptl_ because some STL implementations
00009  are bugged:
00010  namespace A { 
00011     void copy();
00012 
00013     struct X { 
00014        X( int x ) : x_( x ) {} 
00015        int x_; 
00016     };
00017  }
00018  std::vector<X> a;   // failed to compile because std::vector template find A::copy()
00019                      // with ADL instead of std::copy().
00020  */
00021 
00022 
00023 
00024 // Make exception related code optional
00025 # ifndef CPPTL_NO_EXCEPTION_SUPPORT
00026 #  define CPPTL_TRY_BEGIN
00027 #  define CPPTL_TRY_END_CLEANUP( expression )
00028 # else
00029 #  define CPPTL_TRY_BEGIN try
00030 #  define CPPTL_TRY_END_CLEANUP( expression ) catch ( ... ) { expression; throw; }
00031 # endif
00032 
00033 
00034 namespace CppTL { namespace Impl {
00035 
00036 // iterator stuffs
00037 
00038    template<class T>
00039    struct ConstIteratorTraits
00040    {
00041       typedef T &reference;
00042       typedef T *pointer;
00043    };
00044 
00045    template<class T>
00046    struct NonConstIteratorTraits
00047    {
00048       typedef T &reference;
00049       typedef T *pointer;
00050    };
00051 
00052 // algorithm for use when implementing containers
00053 // algorithms
00054 
00055 template<class T>
00056 inline void construct( T &object, const T &value )
00057 {
00058    new (&object) T(value);
00059 }
00060 
00061 template<class T>
00062 inline void destruct( T &object )
00063 {
00064    object.~T();
00065 }
00066 
00067 template<class T,class OutputIt>
00068 inline void destruct_range( OutputIt first, OutputIt last )
00069 {
00070    for ( ; first != last; ++first )
00071       ::CppTL::Impl::destruct( *first );
00072 }
00073 
00074 template<class InputIt,class OutputIt>
00075 void
00076 cpptl_copy( InputIt first, InputIt last, OutputIt firstDestIt )
00077 {
00078    for ( ; first != last; ++first, ++firstDestIt )
00079       *first = *firstDestIt;
00080 }
00081 
00082 template<class InputIt,class OutputIt>
00083 void
00084 copy_backwards( InputIt first, InputIt last, OutputIt endDestIt )
00085 {
00086    if ( first != last )
00087    {
00088       do
00089       {
00090          --last;
00091          --endDestIt;
00092          *endDestIt = *last;
00093       }
00094       while ( first != last );
00095    }
00096 }
00097 
00098 template<class InputIt,class OutputIt>
00099 void
00100 copy_with_construct( InputIt first, InputIt last, OutputIt firstDestIt )
00101 {
00102    for ( ; first != last; ++first, ++firstDestIt )
00103    {
00104       CPPTL_TYPENAME InputIt::reference old = *first;
00105       ::CppTL::Impl::construct(*firstDestIt, *first );
00106    }
00107 }
00108 
00109 template<class OutputIt, class ValueType>
00110 void
00111 copy_with_construct_value( OutputIt firstDestIt, unsigned int count, const ValueType &value )
00112 {
00113    while ( count-- > 0 )
00114    {
00115       construct( *firstDestIt, value );
00116       ++firstDestIt;
00117    }
00118 }
00119 
00120 
00121 template<class InputIt,class OutputIt>
00122 void
00123 copy_and_destroy( InputIt first, InputIt last, OutputIt firstDestIt )
00124 {
00125    for ( ; first != last; ++first, ++firstDestIt )
00126    {
00127       CPPTL_TYPENAME InputIt::reference old = *first;
00128       ::CppTL::Impl::construct(*firstDestIt, old );
00129       ::CppTL::Impl::destruct( old );
00130    }
00131 }
00132 
00133 template<class InputIt,class OutputIt>
00134 void
00135 copy_backwards_and_destroy( InputIt first, InputIt last, OutputIt endDestIt )
00136 {
00137    if ( first != last )
00138    {
00139       do
00140       {
00141          --last;
00142          --endDestIt;
00143          CPPTL_TYPENAME InputIt::reference old = *last;
00144          ::CppTL::Impl::construct( *endDestIt, old );
00145          ::CppTL::Impl::destruct( old );
00146       }
00147       while ( first != last );
00148    }
00149 }
00150 
00151 
00152 } } // namespace CppTL { namespace Impl {
00153 
00154 
00155 
00156 namespace CppTL { 
00157 
00158 // Common predicates
00159 template<class T>
00160 struct LessPred
00161 {
00162    bool operator()( const T &a, const T &b ) const
00163    {
00164       return a < b;
00165    }
00166 };
00167 
00168 // Pair
00169 template<class A, class B>
00170 class Pair
00171 {
00172 public:
00173    typedef A first_type;
00174    typedef B second_type;
00175 
00176    Pair()
00177    {
00178    }
00179 
00180    Pair( const first_type &a, const second_type &b )
00181       : first( a )
00182       , second( b )
00183    {
00184    }
00185 
00186    first_type first;
00187    second_type second;
00188 };
00189 
00190 
00191 // Common allocators
00192 
00193 template<class T>
00194 class MallocAllocator
00195 {
00196 public:
00197    typedef unsigned int size_t;
00198 
00199    T *allocate()
00200    {
00201       return static_cast<T*>( malloc( sizeof(T) ) );
00202    }
00203 
00204    void release( T *object )
00205    {
00206       free( object );
00207    }
00208 
00209    T *allocateArray( size_t count )
00210    {
00211       return static_cast<T*>( malloc( sizeof(T) * count ) );
00212    }
00213 
00214    void releaseArray( T *array, size_t allocatedSize )
00215    {
00216       free( array );
00217    }
00218 
00219    void swap( MallocAllocator &other )
00220    {
00221    }
00222 };
00223 
00224 
00225 } // namespace CppTL
00226 
00227 
00228 #endif // CPPTL__STLIMPL_H_INCLUDED

SourceForge Logo hosts this site. Send comments to:
CppUnit Developers