Repository navigation
Expand file tree
/
Copy pathcstring_maker.hpp
More file actions
435 lines (369 loc) · 12.2 KB
/
Copy pathcstring_maker.hpp
File metadata and controls
435 lines (369 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* /////////////////////////////////////////////////////////////////////////
* File: stlsoft/string/cstring_maker.hpp
*
* Purpose: Simple utility class for creating (constant) C-strings.
*
* Created: 14th May 2004
* Updated: 15th October 2026
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2019-2026, Matthew Wilson and Synesis Information Systems
* Copyright (c) 2004-2019, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Information Systems
* nor the names of any contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file stlsoft/string/cstring_maker.hpp
*
* \brief [C++] Definition of the stlsoft::string_maker class template
* (\ref group__library__String "String" Library).
*/
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER
#define STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_MAKER_MAJOR 5
# define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_MAKER_MINOR 0
# define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_MAKER_REVISION 6
# define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_MAKER_EDIT 69
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* includes
*/
#ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
# include <stlsoft/stlsoft.h>
#endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
#ifdef STLSOFT_TRACE_INCLUDE
# pragma message(__FILE__)
#endif /* STLSOFT_TRACE_INCLUDE */
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS
# include <stlsoft/string/char_traits.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS */
#ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE
# include <stlsoft/memory/allocator_base.hpp> // for STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
#endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE */
#ifndef STLSOFT_INCL_STLSOFT_MEMORY_UTIL_HPP_ALLOCATOR_SELECTOR
# include <stlsoft/memory/util/allocator_selector.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_MEMORY_UTIL_HPP_ALLOCATOR_SELECTOR */
#ifndef STLSOFT_INCL_STLSOFT_API_external_h_memfns
# include <stlsoft/api/external/memfns.h>
#endif /* !STLSOFT_INCL_STLSOFT_API_external_h_memfns */
/* /////////////////////////////////////////////////////////////////////////
* compatibility
*/
#if 0 || \
( defined(STLSOFT_COMPILER_IS_GCC) && \
__GNUC__ < 4) || \
( defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER < 1200) || \
0
# ifndef STLSOFT_cstring_maker_REQUIRE_PER_COMPILER_
# define STLSOFT_cstring_maker_REQUIRE_PER_COMPILER_
# endif /* !STLSOFT_cstring_maker_REQUIRE_PER_COMPILER_ */
#endif
/* /////////////////////////////////////////////////////////////////////////
* namespace
*/
#ifndef STLSOFT_NO_NAMESPACE
namespace stlsoft
{
#endif /* STLSOFT_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* helpers
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
STLSOFT_OPEN_WORKER_NS_(ximpl_cstring_maker_)
template <ss_typename_param_k C>
struct char_to_byte__traits_
{
static ss_size_t byte_size_(ss_size_t cch)
{
return cch * sizeof(C);
}
};
STLSOFT_TEMPLATE_SPECIALISATION
struct char_to_byte__traits_<ss_char_a_t>
{
static ss_size_t byte_size_(ss_size_t cch)
{
return cch;
}
};
STLSOFT_CLOSE_WORKER_NS_(ximpl_cstring_maker_)
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* classes
*/
/** Utility class that creates/destroys fixed-size C-strings for
* other classes.
*
* \ingroup group__library__String
*/
#if defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) || \
!defined(STLSOFT_cstring_maker_REQUIRE_PER_COMPILER_)
template<
ss_typename_param_k C
, ss_typename_param_k A = ss_typename_type_def_k allocator_selector<C>::allocator_type
, ss_typename_param_k T = stlsoft_char_traits<C>
>
struct cstring_maker
{
public: // types
/// The character type
typedef C char_type;
/// The allocator type
typedef A allocator_type;
/// The traits type
typedef T traits_type;
/// The size type
typedef ss_size_t size_type;
/// This type
typedef cstring_maker<C, A, T> class_type;
private:
typedef long count_type;
public:
/// Shared block type
struct block
{
size_type n;
count_type rc;
char_type data[1];
};
private: // constants
enum
{
// Must be power of 2
AllocationGranularity = 32
};
public:
/// Allocates storage for writing into a string, optionally initialising
///
/// \note Includes space for a NUL character character.
///
/// \param s The optional string to be copied into the block. May be
/// \c null; if not, must be of at least \c cch characters long
/// \param cch The number of characters to allocate
///
/// \return the allocated block
/// \retval nullptr Only if space cannot be allocated AND the current
/// compilation mode does not support exceptions
static
block*
alloc(
char_type const* s
, size_type cch
) STLSOFT_NOEXCEPT
{
#if defined(WIN32) || \
defined(_WIN32)
cch = STLSOFT_WORKER_NS_QUAL_(ximpl_cstring_maker_, char_to_byte__traits_)<char_type>::byte_size_(cch);
#endif /* WIN32 || _WIN32 */
size_type cb = offsetof(block, data) + sizeof(char_type) * (1 + cch);
#ifdef STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
ss_typename_type_k allocator_type::ss_template_qual_k rebind<ss_byte_t>::other byte_ator;
#else /* ? STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
allocator_selector<ss_byte_t>::allocator_type byte_ator;
#endif /* STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
cb = (cb + (AllocationGranularity - 1)) & ~(AllocationGranularity - 1);
#ifdef STLSOFT_LF_ALLOCATOR_ALLOCATE_HAS_HINT
block* const pblock = static_cast<block*>(static_cast<void*>(byte_ator.allocate(cb, NULL)));
#else /* ? STLSOFT_LF_ALLOCATOR_ALLOCATE_HAS_HINT */
block* const pblock = static_cast<block*>(static_cast<void*>(byte_ator.allocate(cb)));
#endif /* STLSOFT_LF_ALLOCATOR_ALLOCATE_HAS_HINT */
if (NULL == pblock)
{
return NULL;
}
else
{
pblock->n = cch;
pblock->rc = 1;
pblock->data[cch] = '\0';
if (NULL != s)
{
traits_type::copy(&pblock->data[0], s, cch);
}
return pblock;
}
}
/// Allocates storage for writing into a string, optionally
/// initialising, or returns \c nullptr if \c cch is 0
///
/// \note Includes space for a NUL character character.
///
/// \param s The optional string to be copied into the block. May be
/// \c null; if not, must be of at least \c cch characters long
/// \param cch The number of characters to allocate
static
block*
alloc_null(
char_type const* s
, size_type cch
) STLSOFT_NOEXCEPT
{
return (0 == cch) ? NULL : alloc(s, cch);
}
/// Shares the (reference to the) block
static
block*
share(
block* pblock
) STLSOFT_NOEXCEPT
{
if (NULL != pblock)
{
++pblock->rc;
}
return pblock;
}
/// Releases the storage associated with the string
///
/// \param s The string to free
static
void
free(
block* pblock
) STLSOFT_NOEXCEPT
{
#ifdef STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
ss_typename_type_k allocator_type::ss_template_qual_k rebind<ss_byte_t>::other byte_ator;
#else /* ? STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
allocator_selector<ss_byte_t>::allocator_type byte_ator;
#endif /* STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
if (NULL != pblock)
{
if (0 == --pblock->rc)
{
STLSOFT_MESSAGE_ASSERT("block is overwritten!", pblock->data[pblock->n] == '\0');
byte_ator.deallocate(static_cast<ss_byte_t*>(static_cast<void*>(pblock)), 0);
}
}
}
};
#else /* ? per-compiler */
template<
ss_typename_param_k C
>
struct cstring_maker
{
public:
typedef C char_type;
typedef ss_size_t size_type;
typedef cstring_maker<char_type> class_type;
private:
typedef long count_type;
public:
/// Shared block type
struct block
{
size_type n;
count_type rc;
char_type data[1];
};
private: // constants
enum
{
// Must be power of 2
AllocationGranularity = 32
};
public:
static
block*
alloc(
char_type const* s
, size_type cch
)
{
size_type cb = offsetof(block, data) + sizeof(char_type) * (1 + cch);
cb = (cb + (AllocationGranularity - 1)) & ~(AllocationGranularity - 1);
block* const pblock = static_cast<block*>(malloc(cb));
if (NULL == pblock)
{
return NULL;
}
else
{
pblock->n = cch;
pblock->rc = 1;
pblock->data[cch] = '\0';
if (NULL != s)
{
STLSOFT_API_EXTERNAL_memfns_memcpy(&pblock->data[0], s, cch * sizeof(char_type));
}
return pblock;
}
}
static
block*
alloc_null(
char_type const* s
, size_type cch
)
{
return (0 == cch) ? NULL : alloc(s, cch);
}
static
block*
share(
block* pblock
)
{
if (NULL == pblock)
{
++pblock->rc;
}
return pblock;
}
static
void
free(
block* pblock
)
{
if (NULL != pblock)
{
if (0 == --pblock->rc)
{
::free(pblock);
}
}
}
};
#endif /* compiler */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef STLSOFT_NO_NAMESPACE
} // namespace stlsoft
#endif /* STLSOFT_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* inclusion control
*/
#ifdef STLSOFT_CF_PRAGMA_ONCE_SUPPORT
# pragma once
#endif /* STLSOFT_CF_PRAGMA_ONCE_SUPPORT */
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER */
/* ///////////////////////////// end of file //////////////////////////// */