home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _alloc_old.h < prev    next >
C/C++ Source or Header  |  2001-12-05  |  10KB  |  304 lines

  1. template<class _Tp, class _Alloc>
  2. class __simple_alloc {
  3.   typedef _Alloc __alloc_type;
  4. public:
  5.   typedef typename _Alloc::value_type __alloc_value_type;
  6.   typedef _Tp value_type;
  7.   static size_t  _STLP_CALL __chunk(size_t __n) { 
  8.     return (sizeof(__alloc_value_type)==sizeof(value_type)) ? __n : 
  9.         ((__n*sizeof(value_type)+sizeof(__alloc_value_type)-1)/sizeof(__alloc_value_type));
  10.   }
  11.   static _Tp*  _STLP_CALL allocate(size_t __n) { return 0 == __n ? 0 : (_Tp*) __alloc_type::allocate(__chunk(__n)); }
  12.   static void  _STLP_CALL deallocate(_Tp * __p, size_t __n) { 
  13.     __alloc_type::deallocate((__alloc_value_type*)__p, __chunk(__n)); }
  14. };
  15.  
  16. // Allocator adaptor to turn an SGI-style allocator (e.g. alloc, malloc_alloc)
  17. // into a standard-conforming allocator.   Note that this adaptor does
  18. // *not* assume that all objects of the underlying alloc class are
  19. // identical, nor does it assume that all of the underlying alloc's
  20. // member functions are static member functions.  Note, also, that 
  21. // __allocator<_Tp, alloc> is essentially the same thing as allocator<_Tp>.
  22.  
  23. template <class _Tp, class _Alloc>
  24. struct __allocator : public _Alloc {
  25.   typedef _Alloc __underlying_alloc;
  26.  
  27.   typedef size_t    size_type;
  28.   typedef ptrdiff_t difference_type;
  29.   typedef _Tp*       pointer;
  30.   typedef const _Tp* const_pointer;
  31.   typedef _Tp&       reference;
  32.   typedef const _Tp& const_reference;
  33.   typedef _Tp        value_type;
  34.  
  35. # if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
  36.   template <class _Tp1> struct rebind {
  37.     typedef __allocator<_Tp1, _Alloc> other;
  38.   };
  39. # endif
  40.   __allocator() _STLP_NOTHROW {}
  41.   __allocator(const _Alloc& ) _STLP_NOTHROW {}
  42.   __allocator(const __allocator<_Tp, _Alloc>& __a) _STLP_NOTHROW
  43.     : _Alloc(__a) {}
  44. # if defined (_STLP_MEMBER_TEMPLATES) && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  45.   template <class _Tp1> 
  46.   __allocator(const __allocator<_Tp1, _Alloc>& __a) _STLP_NOTHROW
  47.     : _Alloc(__a) {}
  48. # endif
  49. # ifdef _STLP_TRIVIAL_DESTRUCTOR_BUG
  50.   ~__allocator() _STLP_NOTHROW {}
  51. # endif
  52.   pointer address(reference __x) const { return &__x; }
  53.  
  54. # if !defined (__WATCOM_CPLUSPLUS__)
  55.   const_pointer address(const_reference __x) const { return &__x; }
  56. # endif
  57.  
  58.   // __n is permitted to be 0.
  59.   _Tp* allocate(size_type __n, const void* = 0) {
  60.     return __n != 0 
  61.         ? __STATIC_CAST(_Tp*,__underlying_alloc::allocate(__n * sizeof(_Tp))) 
  62.         : 0;
  63.   }
  64.  
  65.   // __p is not permitted to be a null pointer.
  66.   void deallocate(pointer __p, size_type __n)
  67.     { if (__p) __underlying_alloc::deallocate(__p, __n * sizeof(_Tp)); }
  68.  
  69.   size_type max_size() const _STLP_NOTHROW 
  70.     { return size_t(-1) / sizeof(_Tp); }
  71.  
  72.   void construct(pointer __p, const _Tp& __val) { _STLP_STD::_Construct(__p, __val); }
  73.   void destroy(pointer __p) { _STLP_STD::_Destroy(__p); }
  74.  
  75.   const __underlying_alloc& __get_underlying_alloc() const { return *this; }
  76. };
  77.  
  78. #ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
  79. template <class _Alloc>
  80. class __allocator<void, _Alloc> {
  81.   typedef size_t      size_type;
  82.   typedef ptrdiff_t   difference_type;
  83.   typedef void*       pointer;
  84.   typedef const void* const_pointer;
  85.   typedef void        value_type;
  86. #ifdef _STLP_MEMBER_TEMPLATE_CLASSES
  87.   template <class _Tp1> struct rebind {
  88.     typedef __allocator<_Tp1, _Alloc> other;
  89.   };
  90. #endif
  91. };
  92. #endif
  93.  
  94. template <class _Tp, class _Alloc>
  95. inline bool  _STLP_CALL operator==(const __allocator<_Tp, _Alloc>& __a1,
  96.                                    const __allocator<_Tp, _Alloc>& __a2)
  97. {
  98.   return __a1.__get_underlying_alloc() == __a2.__get_underlying_alloc();
  99. }
  100.  
  101. #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
  102. template <class _Tp, class _Alloc>
  103. inline bool  _STLP_CALL operator!=(const __allocator<_Tp, _Alloc>& __a1,
  104.                                    const __allocator<_Tp, _Alloc>& __a2)
  105. {
  106.   return __a1.__get_underlying_alloc() != __a2.__get_underlying_alloc();
  107. }
  108. #endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
  109.  
  110.  
  111. // Comparison operators for all of the predifined SGI-style allocators.
  112. // This ensures that __allocator<malloc_alloc> (for example) will
  113. // work correctly.
  114.  
  115. #ifndef _STLP_NON_TYPE_TMPL_PARAM_BUG
  116. template <int inst>
  117. inline bool  _STLP_CALL operator==(const __malloc_alloc<inst>&,
  118.                                    const __malloc_alloc<inst>&)
  119. {
  120.   return true;
  121. }
  122.  
  123. #ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
  124. template <int __inst>
  125. inline bool  _STLP_CALL operator!=(const __malloc_alloc<__inst>&,
  126.                                    const __malloc_alloc<__inst>&)
  127. {
  128.   return false;
  129. }
  130. #endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
  131.  
  132. inline bool _STLP_CALL operator==(const __new_alloc&, const __new_alloc&) { return true; }
  133.  
  134. # ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
  135. inline bool _STLP_CALL operator!=(const __new_alloc&, const __new_alloc&) { return false; }
  136. # endif
  137.  
  138.  
  139. template <bool __threads, int __inst>
  140. inline bool  _STLP_CALL operator==(const __node_alloc<__threads, __inst>&,
  141.                                    const __node_alloc<__threads, __inst>&)
  142. {
  143.   return true;
  144. }
  145.  
  146. #if defined( _STLP_FUNCTION_TMPL_PARTIAL_ORDER )
  147.  
  148. template <bool __threads, int __inst>
  149. inline bool  _STLP_CALL operator!=(const __node_alloc<__threads, __inst>&,
  150.                                    const __node_alloc<__threads, __inst>&)
  151. {
  152.   return false;
  153. }
  154. #endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
  155.  
  156. #endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
  157.  
  158. template <class _Alloc>
  159. inline bool  _STLP_CALL operator==(const __debug_alloc<_Alloc>&, const __debug_alloc<_Alloc>&) {  return true; }
  160. # ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
  161. template <class _Alloc>
  162. inline bool  _STLP_CALL operator!=(const __debug_alloc<_Alloc>&, const __debug_alloc<_Alloc>&) {  return false; }
  163. # endif
  164.  
  165. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  166.  
  167. // Versions for the predefined SGI-style allocators.
  168. template <class _Tp, int __inst>
  169. struct _Alloc_traits<_Tp, __malloc_alloc<__inst> > {
  170.   typedef __allocator<_Tp, __malloc_alloc<__inst> > allocator_type;
  171. };
  172.  
  173.  
  174. template <class _Tp, bool __threads, int __inst>
  175. struct _Alloc_traits<_Tp, __node_alloc<__threads, __inst> > {
  176.   typedef __allocator<_Tp, __node_alloc<__threads, __inst> > 
  177.           allocator_type;
  178. };
  179.  
  180. template <class _Tp, class _Alloc>
  181. struct _Alloc_traits<_Tp, __debug_alloc<_Alloc> > {
  182.   typedef __allocator<_Tp, __debug_alloc<_Alloc> > allocator_type;
  183. };
  184.  
  185. // Versions for the __allocator adaptor used with the predefined
  186. // SGI-style allocators.
  187.  
  188. template <class _Tp, class _Tp1, class _Alloc>
  189. struct _Alloc_traits<_Tp, __allocator<_Tp1, _Alloc > > {
  190.   typedef __allocator<_Tp, _Alloc > allocator_type;
  191. };
  192.  
  193. #endif
  194.  
  195. #if !defined (_STLP_MEMBER_TEMPLATE_CLASSES) 
  196.  
  197. // Versions for the predefined SGI-style allocators.
  198.  
  199.  
  200. #  if defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
  201.  
  202. typedef __malloc_alloc<0> __malloc_alloc_dfl;
  203. typedef __node_alloc<false, 0> __single_client_node_alloc;
  204. typedef __node_alloc<true, 0>  __multithreaded_node_alloc;
  205.  
  206. template <class _Tp>
  207. inline __allocator<_Tp, __malloc_alloc_dfl >& _STLP_CALL
  208. __stl_alloc_rebind(__malloc_alloc_dfl& __a, const _Tp*) {
  209.   return (__allocator<_Tp, __malloc_alloc_dfl >&)__a;
  210. }
  211.  
  212. template <class _Tp>
  213. inline __allocator<_Tp, __single_client_node_alloc >& _STLP_CALL
  214. __stl_alloc_rebind(__single_client_node_alloc& __a, const _Tp*) {
  215.   return (__allocator<_Tp, __single_client_node_alloc >&)__a;
  216. }
  217.  
  218. template <class _Tp>
  219. inline __allocator<_Tp, __multithreaded_node_alloc >& _STLP_CALL
  220. __stl_alloc_rebind(__multithreaded_node_alloc& __a, const _Tp*) {
  221.   return (__allocator<_Tp, __multithreaded_node_alloc >&)__a;
  222. }
  223.  
  224. template <class _Tp>
  225. inline __allocator<_Tp, __malloc_alloc_dfl > _STLP_CALL
  226. __stl_alloc_create(const __malloc_alloc_dfl&, const _Tp*) {
  227.   return __allocator<_Tp, __malloc_alloc_dfl > ();
  228. }
  229.  
  230. template <class _Tp>
  231. inline __allocator<_Tp, __single_client_node_alloc > _STLP_CALL
  232. __stl_alloc_create(const __single_client_node_alloc&, const _Tp*) {
  233.   return __allocator<_Tp, __single_client_node_alloc >();
  234. }
  235.  
  236. template <class _Tp>
  237. inline __allocator<_Tp, __multithreaded_node_alloc > _STLP_CALL
  238. __stl_alloc_create(const __multithreaded_node_alloc&, const _Tp*) {
  239.   return __allocator<_Tp, __multithreaded_node_alloc >();
  240. }
  241.  
  242. #  else
  243.  
  244. template <class _Tp, int __inst>
  245. inline __allocator<_Tp, __malloc_alloc<__inst> >& _STLP_CALL
  246. __stl_alloc_rebind(__malloc_alloc<__inst>& __a, const _Tp*) {
  247.   return (__allocator<_Tp, __malloc_alloc<__inst> >&)__a;
  248. }
  249.  
  250. template <class _Tp, bool __threads, int __inst>
  251. inline __allocator<_Tp, __node_alloc<__threads, __inst> >& _STLP_CALL
  252. __stl_alloc_rebind(__node_alloc<__threads, __inst>& __a, const _Tp*) {
  253.   return (__allocator<_Tp, __node_alloc<__threads, __inst> >&)__a;
  254. }
  255.  
  256. template <class _Tp, int __inst>
  257. inline __allocator<_Tp, __malloc_alloc<__inst> > _STLP_CALL
  258. __stl_alloc_create(const __malloc_alloc<__inst>&, const _Tp*) {
  259.   return __allocator<_Tp, __malloc_alloc<__inst> >();
  260. }
  261.  
  262. template <class _Tp, bool __threads, int __inst>
  263. inline __allocator<_Tp, __node_alloc<__threads, __inst> > _STLP_CALL
  264. __stl_alloc_create(const __node_alloc<__threads, __inst>&, const _Tp*) {
  265.   return __allocator<_Tp, __node_alloc<__threads, __inst> >();
  266. }
  267.  
  268. #  endif
  269.  
  270. template <class _Tp, class _Alloc>
  271. inline __allocator<_Tp, __debug_alloc<_Alloc> > _STLP_CALL
  272. __stl_alloc_create(const __debug_alloc<_Alloc>&, const _Tp*) {
  273.   return __allocator<_Tp, __debug_alloc<_Alloc> >();
  274. }
  275. template <class _Tp, class _Alloc>
  276. inline __allocator<_Tp, __debug_alloc<_Alloc> >& _STLP_CALL
  277. __stl_alloc_rebind(__debug_alloc<_Alloc>& __a, const _Tp*) {
  278.   return (__allocator<_Tp, __debug_alloc<_Alloc> >&)__a;
  279. }
  280.  
  281. template <class _Tp>
  282. inline __allocator<_Tp, __new_alloc > _STLP_CALL
  283. __stl_alloc_create(const __new_alloc&, const _Tp*) {
  284.   return __allocator<_Tp, __new_alloc >();
  285. }
  286. template <class _Tp>
  287. inline __allocator<_Tp, __new_alloc >&  _STLP_CALL
  288. __stl_alloc_rebind(__new_alloc& __a, const _Tp*) {
  289.   return (__allocator<_Tp, __new_alloc >&)__a;
  290. }
  291.  
  292. template <class _Tp1, class _Alloc, class _Tp2>
  293. inline __allocator<_Tp2, _Alloc>& _STLP_CALL
  294. __stl_alloc_rebind(__allocator<_Tp1, _Alloc>& __a, const _Tp2*) {
  295.   return (__allocator<_Tp2, _Alloc>&)__a;
  296. }
  297.  
  298. template <class _Tp1, class _Alloc, class _Tp2>
  299. inline __allocator<_Tp2, _Alloc> _STLP_CALL
  300. __stl_alloc_create(const __allocator<_Tp1, _Alloc>&, const _Tp2*) {
  301.   return __allocator<_Tp2, _Alloc>();
  302. }
  303. #endif
  304.