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_MALLOC_ALLOCATOR
00063 # include "stlsoft_malloc_allocator.h"
00064 #endif
00065 #ifndef STLSOFT_INCL_H_STLSOFT_CHAR_TRAITS
00066 # include "stlsoft_char_traits.h"
00067 #endif
00068
00069
00070
00071
00072
00073 #ifndef _STLSOFT_NO_NAMESPACE
00074 namespace stlsoft
00075 {
00076 #endif
00077
00078
00079
00080
00081
00083 #if defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION) || \
00084 ( ( !defined(__STLSOFT_COMPILER_IS_GCC) || \
00085 __GNUC__ >= 3) && \
00086 ( !defined(__STLSOFT_COMPILER_IS_MSVC) || \
00087 _MSC_VER != 1100))
00088 template< ss_typename_param_k C
00089 , ss_typename_param_k A = malloc_allocator<C>
00090 , ss_typename_param_k T = char_traits<C>
00091 >
00092 struct cstring_maker
00093 {
00094 typedef C char_type;
00095 typedef A allocator_type;
00096 typedef T traits_type;
00097 typedef ss_size_t size_type;
00098 typedef cstring_maker<C, A, T> class_type;
00099
00100
00101 struct block
00102 {
00103 size_type n;
00104 char_type data[1];
00105 };
00106
00107 public:
00113 static char_type *alloc(size_type cch)
00114 {
00115 #if defined(WIN32) || \
00116 defined(_WIN32)
00117 cch *= 2 / sizeof(char_type);
00118 #endif
00119
00120 size_type cb = offsetof(block, data) + sizeof(char_type) * (1 + cch);
00121
00122 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00123 # ifdef STLSOFT_CF_TEMPLATE_QUALIFIER_KEYWORD_SUPPORT
00124 ss_typename_type_k allocator_type::template rebind<ss_byte_t>::other byte_ator;
00125 # else
00126 ss_typename_type_k allocator_type::rebind<ss_byte_t>::other byte_ator;
00127 # endif
00128 #else
00129 malloc_allocator<ss_byte_t> byte_ator;
00130 #endif
00131
00132 cb = (cb + 31) & ~31;
00133
00134 block *pblock = static_cast<block*>(static_cast<void*>(byte_ator.allocate(cb, NULL)));
00135
00136 if(NULL == pblock)
00137 {
00138 return NULL;
00139 }
00140 else
00141 {
00142 pblock->n = cch;
00143 pblock->data[cch] = '\0';
00144
00145 return &pblock->data[0];
00146 }
00147 }
00153 static char_type *dup(char_type const *s)
00154 {
00155 stlsoft_assert(NULL != s);
00156
00157 size_type len = traits_type::length(s);
00158 char_type *s_ = alloc(len);
00159
00160 if(NULL != s_)
00161 {
00162 traits_type::copy(s_, s, 1 + len);
00163 }
00164
00165 return s_;
00166 }
00172 static char_type *dup_null(char_type const *s)
00173 {
00174 return (NULL == s) ? NULL : dup(s);
00175 }
00179 static void free(char_type *s)
00180 {
00181 #ifdef STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
00182 # ifdef STLSOFT_CF_TEMPLATE_QUALIFIER_KEYWORD_SUPPORT
00183 ss_typename_type_k allocator_type::template rebind<ss_byte_t>::other byte_ator;
00184 # else
00185 ss_typename_type_k allocator_type::rebind<ss_byte_t>::other byte_ator;
00186 # endif
00187 #else
00188 malloc_allocator<ss_byte_t> byte_ator;
00189 #endif
00190
00191 if(NULL != s)
00192 {
00193 union
00194 {
00195 ss_byte_t *py;
00196 block *pblock;
00197 } u;
00198
00199 u.py = static_cast<ss_byte_t*>(static_cast<void*>(s)) - offsetof(block, data);
00200
00201 stlsoft_assert(u.pblock->data[u.pblock->n] == '\0');
00202
00203 byte_ator.deallocate(u.py, u.pblock->n);
00204 }
00205 }
00206 };
00207 #else
00208 template< ss_typename_param_k C
00209 >
00210 struct cstring_maker;
00211
00212
00213 STLSOFT_TEMPLATE_SPECIALISATION
00214 struct cstring_maker<char>
00215 {
00216 public:
00217 typedef char char_type;
00218 typedef malloc_allocator<char_type> allocator_type;
00219 typedef char_traits<char_type> traits_type;
00220 typedef ss_size_t size_type;
00221 typedef cstring_maker<char_type> class_type;
00222
00223 public:
00224 static char_type *alloc(size_type cch)
00225 {
00226 return static_cast<char_type*>(malloc((1 + cch) * sizeof(char_type)));
00227 }
00228 static char_type *dup(char_type const *s)
00229 {
00230 return strdup(s);
00231 }
00232 static char_type *dup_null(char_type const *s)
00233 {
00234 return (NULL == s) ? NULL : dup(s);
00235 }
00236 static void free(char_type *s)
00237 {
00238 ::free(s);
00239 }
00240 };
00241
00242
00243 # if !defined(__STLSOFT_COMPILER_IS_GCC)
00244
00245 STLSOFT_TEMPLATE_SPECIALISATION
00246 struct cstring_maker<wchar_t>
00247 {
00248 public:
00249 typedef wchar_t char_type;
00250 typedef malloc_allocator<char_type> allocator_type;
00251 typedef char_traits<char_type> traits_type;
00252 typedef ss_size_t size_type;
00253 typedef cstring_maker<char_type> class_type;
00254
00255 public:
00256 static char_type *alloc(size_type cch)
00257 {
00258 return static_cast<char_type*>(malloc((1 + cch) * sizeof(char_type)));
00259 }
00260 static char_type *dup(char_type const *s)
00261 {
00262 return _wcsdup(s);
00263 }
00264 static char_type *dup_null(char_type const *s)
00265 {
00266 return (NULL == s) ? NULL : dup(s);
00267 }
00268 static void free(char_type *s)
00269 {
00270 ::free(s);
00271 }
00272 };
00273 # endif
00274
00275 #endif
00276
00278
00279
00280 #ifdef STLSOFT_UNITTEST
00281
00282 namespace unittest
00283 {
00284 ss_bool_t test_stlsoft_cstring_maker(unittest_reporter *r)
00285 {
00286 ss_bool_t bSuccess = true;
00287
00288 unittest_initialiser init(r, "STLSoft", "cstring_maker", __FILE__);
00289
00290 typedef cstring_maker<char> string_maker_a_t;
00291 typedef cstring_maker<wchar_t> string_maker_w_t;
00292
00293 char *s1 = string_maker_a_t::dup("String #1");
00294
00295 if(NULL == s1)
00296 {
00297 r->report("dup (ANSI) failed ", __LINE__);
00298 bSuccess = false;
00299 }
00300 else
00301 {
00302
00303 string_maker_a_t::free(s1);
00304 }
00305
00306 return bSuccess;
00307 }
00308
00309 unittest_registrar unittest_atlstl_string_access(test_stlsoft_cstring_maker);
00310
00311 }
00312
00313 #endif
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325