00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifndef STLSOFT_INCL_H_STLSOFT
00060 # include "stlsoft.h"
00061 #endif
00062 #ifndef STLSOFT_INCL_H_STLSOFT_ALLOCATOR_BASE
00063 # include "stlsoft_allocator_base.h"
00064 #endif
00065 #ifndef STLSOFT_INCL_H_STLSOFT_SAP_CAST
00066 # include "stlsoft_sap_cast.h"
00067 #endif
00068
00069
00070
00071
00072
00073 #ifndef _STLSOFT_NO_NAMESPACE
00074 namespace stlsoft
00075 {
00076 #endif
00077
00078
00079
00082
00086
00091
00092
00093
00094
00095
00096 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00097
00098 template <ss_typename_param_k T>
00099 class null_allocator;
00100
00101
00102 STLSOFT_TEMPLATE_SPECIALISATION
00103 class null_allocator<void>
00104 {
00105 public:
00106 typedef void value_type;
00107 typedef null_allocator<void> class_type;
00108 typedef void *pointer;
00109 typedef void const *const_pointer;
00110 typedef ptrdiff_t difference_type;
00111 typedef ss_size_t size_type;
00112
00113 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00114
00115 template <ss_typename_param_k U>
00116 struct rebind
00117 {
00118 typedef null_allocator<U> other;
00119 };
00120 #endif
00121 };
00122
00123 #endif
00124
00128 template <ss_typename_param_k T>
00129 class null_allocator
00130 {
00131 public:
00133 typedef T value_type;
00135 typedef null_allocator<value_type> class_type;
00137 typedef value_type *pointer;
00139 typedef value_type const *const_pointer;
00141 typedef value_type &reference;
00143 typedef value_type const &const_reference;
00145 typedef ptrdiff_t difference_type;
00147 typedef ss_size_t size_type;
00149 #ifdef __STLSOFT_CF_ALLOCATOR_TYPED_DEALLOCATE_POINTER
00150 typedef pointer deallocate_pointer;
00151 #else
00152 typedef void *deallocate_pointer;
00153 #endif
00154
00155 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00156
00157 template <ss_typename_param_k U>
00158 struct rebind
00159 {
00160 typedef null_allocator<U> other;
00161 };
00162 #endif
00163
00164
00165 public:
00167 null_allocator() stlsoft_throw_0()
00168 {}
00170 #ifndef __STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
00171 null_allocator(const null_allocator &) stlsoft_throw_0()
00172 {}
00173 #else
00174 template <ss_typename_param_k U>
00175 null_allocator(const null_allocator<U> &) stlsoft_throw_0()
00176 {}
00177 #endif
00178
00179 ~null_allocator() stlsoft_throw_0()
00180 {}
00181
00182
00183 public:
00185 size_type max_size() const stlsoft_throw_0()
00186 {
00187 return static_cast<size_type>(-1) / sizeof(value_type);
00188 }
00189
00190
00191 public:
00195 pointer address(reference x) const stlsoft_throw_0()
00196 {
00197 return &x;
00198 }
00202 const_pointer address(const_reference x) const stlsoft_throw_0()
00203 {
00204 return &x;
00205 }
00206
00207
00208 public:
00214 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00215 static
00216 #endif
00217 pointer allocate(size_type n, null_allocator<void>::const_pointer pv = NULL)
00218 {
00219 STLSOFT_SUPPRESS_UNUSED(n);
00220 STLSOFT_SUPPRESS_UNUSED(pv);
00221
00222 null_allocator<void>::pointer p = NULL;
00223
00224 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00225 if(p == NULL)
00226 {
00227 throw stlsoft_ns_qual_std(bad_alloc)();
00228 }
00229 #endif
00230
00231 return static_cast<pointer>(p);
00232 }
00233
00234 #ifdef __STLSOFT_CF_ALLOCATOR_CHARALLOC_METHOD
00235
00236 char *_Charalloc(size_type n)
00237 {
00238 return sap_cast<char*>(allocate(n, NULL));
00239 }
00240 #endif
00241
00246 #ifdef __STLSOFT_CF_ALLOCATOR_STATIC_ALLOCATE_METHODS
00247 static
00248 #endif
00249 void deallocate(pointer p, size_type n)
00250 {
00251 STLSOFT_SUPPRESS_UNUSED(p);
00252 STLSOFT_SUPPRESS_UNUSED(n);
00253 }
00254
00258 void deallocate(pointer p)
00259 {
00260 STLSOFT_SUPPRESS_UNUSED(p);
00261 }
00262
00263
00264 public:
00269 void construct(pointer p, value_type const &x)
00270 {
00271 stlsoft_assert(p != NULL);
00272
00273 new(p) value_type(x);
00274 }
00275
00279 void construct(pointer p)
00280 {
00281 stlsoft_assert(p != NULL);
00282
00283 new(p) value_type();
00284 }
00285
00289 void destroy(pointer p) stlsoft_throw_0()
00290 {
00291 stlsoft_assert(p != NULL);
00292
00293 stlsoft_destroy_instance(T, value_type, p);
00294 }
00295
00296
00297 private:
00298 class_type const &operator =(class_type const &rhs);
00299 };
00300
00301 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00302
00303 template <ss_typename_param_k T>
00304 inline ss_bool_t operator ==(const null_allocator<T> &, const null_allocator<T> &)
00305 {
00306 return ss_true_v;
00307 }
00308
00309 template <ss_typename_param_k T>
00310 inline ss_bool_t operator !=(const null_allocator<T> &, const null_allocator<T> &)
00311 {
00312 return ss_false_v;
00313 }
00314
00315 #endif
00316
00318
00319
00320 #ifdef STLSOFT_UNITTEST
00321
00322 namespace unittest
00323 {
00324 ss_bool_t test_stlsoft_null_allocator(unittest_reporter *r)
00325 {
00326 ss_bool_t bSuccess = true;
00327
00328 unittest_initialiser init(r, "STLSoft", "null_allocator", __FILE__);
00329
00330 typedef null_allocator<int> int_allocator_t;
00331
00332 int i;
00333 int_allocator_t ator1;
00334
00335 ator1.construct(&i, 1968);
00336
00337 if(1968 != i)
00338 {
00339 r->report("failed to construct ", __LINE__);
00340 bSuccess = false;
00341 }
00342
00343 #ifdef __STLSOFT_CF_THROW_BAD_ALLOC
00344 try
00345 {
00346 ator1.allocate(1);
00347
00348 r->report("null_allocator provided a non-NULL allocation ", __LINE__);
00349 bSuccess = false;
00350 }
00351 catch(std::bad_alloc &)
00352 {
00353 }
00354 #else
00355 if(ator1.allocate(1) != NULL)
00356 {
00357 r->report("null_allocator provided a non-NULL allocation ", __LINE__);
00358 bSuccess = false;
00359 }
00360 #endif
00361
00362 return bSuccess;
00363 }
00364
00365 unittest_registrar unittest_stlsoft_null_allocator(test_stlsoft_null_allocator);
00366
00367 }
00368
00369 #endif
00370
00371
00372
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385