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
00047 #ifndef WINSTL_INCL_WINSTL_MEMORY_HPP_SHELL_ALLOCATOR
00048 #define WINSTL_INCL_WINSTL_MEMORY_HPP_SHELL_ALLOCATOR
00049
00050 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00051 # define WINSTL_VER_WINSTL_MEMORY_HPP_SHELL_ALLOCATOR_MAJOR 4
00052 # define WINSTL_VER_WINSTL_MEMORY_HPP_SHELL_ALLOCATOR_MINOR 1
00053 # define WINSTL_VER_WINSTL_MEMORY_HPP_SHELL_ALLOCATOR_REVISION 5
00054 # define WINSTL_VER_WINSTL_MEMORY_HPP_SHELL_ALLOCATOR_EDIT 83
00055 #endif
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #ifndef WINSTL_INCL_WINSTL_H_WINSTL
00070 # include <winstl/winstl.h>
00071 #endif
00072 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE
00073 # include <stlsoft/memory/allocator_base.hpp>
00074 #endif
00075
00076 #ifndef STLSOFT_INCL_STDEXCEPT
00077 # define STLSOFT_INCL_STDEXCEPT
00078 # include <stdexcept>
00079 #endif
00080
00081 #ifndef STLSOFT_INCL_H_SHLOBJ
00082 # define STLSOFT_INCL_H_SHLOBJ
00083 # include <shlobj.h>
00084 #endif
00085
00086
00087
00088
00089
00090 #ifndef _WINSTL_NO_NAMESPACE
00091 # if defined(_STLSOFT_NO_NAMESPACE) || \
00092 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00093
00094 namespace winstl
00095 {
00096 # else
00097
00098
00099 namespace stlsoft
00100 {
00101
00102 namespace winstl_project
00103 {
00104
00105 # endif
00106 #endif
00107
00108
00109
00110
00111
00118 template <ss_typename_param_k T>
00119 class shell_allocator
00120 : public allocator_base<T, shell_allocator<T> >
00121 {
00122 private:
00123 typedef allocator_base<T, shell_allocator<T> > parent_class_type;
00124 public:
00126 typedef shell_allocator<T> class_type;
00128 typedef ss_typename_type_k parent_class_type::value_type value_type;
00130 typedef ss_typename_type_k parent_class_type::pointer pointer;
00132 typedef ss_typename_type_k parent_class_type::const_pointer const_pointer;
00134 typedef ss_typename_type_k parent_class_type::reference reference;
00136 typedef ss_typename_type_k parent_class_type::const_reference const_reference;
00138 typedef ss_typename_type_k parent_class_type::difference_type difference_type;
00140 typedef ss_typename_type_k parent_class_type::size_type size_type;
00141
00142 public:
00143 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00145 template <ss_typename_param_k U>
00146 struct rebind
00147 {
00148 typedef shell_allocator<U> other;
00149 };
00150 #endif
00151
00154 public:
00156 shell_allocator() stlsoft_throw_1(stlsoft_ns_qual_std(runtime_error) )
00157 : m_malloc(get_malloc_())
00158 {}
00160 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00161 template <ss_typename_param_k U>
00162 shell_allocator(shell_allocator<U> const& rhs) stlsoft_throw_0()
00163 : m_malloc(get_malloc_())
00164 {}
00165 #else
00166 shell_allocator(class_type const& rhs) stlsoft_throw_0()
00167 : m_malloc(addref_malloc_(rhs.m_malloc))
00168 {}
00169 #endif
00171 ~shell_allocator() stlsoft_throw_0()
00172 {
00173 if(m_malloc != NULL)
00174 {
00175 m_malloc->Release();
00176 }
00177 }
00178
00182 class_type const& operator =(class_type const& rhs)
00183 {
00184 return *this;
00185 }
00187
00188 private:
00189 friend class allocator_base<T, shell_allocator<T> >;
00190
00191 void *do_allocate(size_type n, void const* hint)
00192 {
00193 STLSOFT_SUPPRESS_UNUSED(hint);
00194
00195 #ifndef STLSOFT_CF_EXCEPTION_SUPPORT
00196 if(NULL != m_malloc)
00197 {
00198 return NULL;
00199 }
00200 #endif
00201
00202 return static_cast<void*>(m_malloc->Alloc(n * sizeof(value_type)));
00203 }
00204 void do_deallocate(void *pv, size_type n)
00205 {
00206 STLSOFT_SUPPRESS_UNUSED(n);
00207
00208 m_malloc->Free(pv);
00209 }
00210 void do_deallocate(void *pv)
00211 {
00212 m_malloc->Free(pv);
00213 }
00214
00215
00216 private:
00217 static LPMALLOC get_malloc_() stlsoft_throw_1(stlsoft_ns_qual_std(runtime_error) )
00218 {
00219 LPMALLOC lpMalloc;
00220 HRESULT hr = ::SHGetMalloc(&lpMalloc);
00221
00222 if(FAILED(hr))
00223 {
00224 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00225 STLSOFT_THROW_X(stlsoft_ns_qual_std(runtime_error)("failed to retrieve the shell allocator"));
00226 #else
00227 lpMalloc = NULL;
00228 #endif
00229 }
00230
00231 return lpMalloc;
00232 }
00233 static LPMALLOC addref_malloc_(LPMALLOC lpMalloc)
00234 {
00235 #ifndef STLSOFT_CF_EXCEPTION_SUPPORT
00236 if(NULL != lpMalloc)
00237 {
00238 return NULL;
00239 }
00240 #endif
00241
00242 lpMalloc->AddRef();
00243
00244 return lpMalloc;
00245 }
00246
00247
00248 private:
00249 LPMALLOC m_malloc;
00250 };
00251
00252 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00253
00254
00255 STLSOFT_TEMPLATE_SPECIALISATION
00256 class shell_allocator<void>
00257 {
00258 public:
00259 typedef void value_type;
00260 typedef shell_allocator<void> class_type;
00261 typedef void* pointer;
00262 typedef void const* const_pointer;
00263 typedef ws_ptrdiff_t difference_type;
00264 typedef ws_size_t size_type;
00265
00266 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00268 template <ss_typename_param_k U>
00269 struct rebind
00270 {
00271 typedef shell_allocator<U> other;
00272 };
00273 #endif
00274 };
00275
00276 #endif
00277
00278 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00279
00280 template <ss_typename_param_k T>
00281 inline ws_bool_t operator ==(const shell_allocator<T> &, const shell_allocator<T> &)
00282 {
00283 return ws_true_v;
00284 }
00285
00286 template <ss_typename_param_k T>
00287 inline ws_bool_t operator !=(const shell_allocator<T> &, const shell_allocator<T> &)
00288 {
00289 return ws_false_v;
00290 }
00291
00292 #endif
00293
00295
00296
00297 #ifdef STLSOFT_UNITTEST
00298 # include "./unittest/shell_allocator_unittest_.h"
00299 #endif
00300
00301
00302
00303 #ifndef _WINSTL_NO_NAMESPACE
00304 # if defined(_STLSOFT_NO_NAMESPACE) || \
00305 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00306 }
00307 # else
00308 }
00309 }
00310 # endif
00311 #endif
00312
00313
00314
00315 #endif
00316
00317