ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
characters.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_characters of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_CHARACTERS_CHARACTERS
9#define HPP_ALIB_CHARACTERS_CHARACTERS 1
10
11#if !defined(HPP_ALIB) && !defined(ALIB_DOX)
12# include "alib/alib.hpp"
13#endif
14
15ALIB_ASSERT_MODULE(CHARACTERS)
16
17#if !defined(HPP_ALIB_LANG_INTEGERS)
18# include "alib/lang/integers.hpp"
19#endif
20
21
22#if !defined (_GLIBCXX_STRING) && !defined(_STRING_)
23# include <string>
24#endif
25
26
27namespace alib { namespace characters {
28
29
30// #################################################################################################
31// Narrow type: nchar
32// #################################################################################################
33
34/**
35 * This type represents a narrow character in \alib. This is an alias for built-in C++
36 * type \c char.
37 *
38 * \see
39 * For details, see chapter \ref alib_characters_chars "2. Character Types" of the
40 * Programmer's Manual of module \alib_characters_nl.
41 */
42using nchar= char;
43
44// #################################################################################################
45// Wide types: wchar, xchar
46// #################################################################################################
47#if defined(ALIB_CHARACTERS_NATIVE_WCHAR)
48# error "Preprocessor symbol ALIB_CHARACTERS_NATIVE_WCHAR must not be passed to the compiler. It is deduced in ALib headers."
49#endif
50
51
52
53#if defined(ALIB_DOX)
54/**
55 * This type represents a wide character in \alib.
56 * Usually this is an alias for built-in C++ type \c wchar_t.
57 * As the width of the latter is compiler-specific (the width may vary with different compilers even
58 * on the same platform), compiler symobls \ref ALIB_CHARACTERS_SIZEOF_WCHAR may be used to
59 * manipulate its width.
60 *
61 *
62 * \see
63 * For details, see chapter \ref alib_characters_chars "2. Character Types" of the
64 * Programmer's Manual of module \alib_characters_nl.
65 */
66using wchar= PLATFORM_SPECIFIC;
67
68/**
69 * This type represents a second wide character type which has a width complementary to
70 * that of type \alib{characters;wchar}: If \b %wchar is 2 bytes wide, then this type aliases
71 * \c char32_t and if its width is 4 bytes, this type aliases \c char16_t.
72 *
73 * While together with types \ref alib::nchar and \ref alib::wchar it forms
74 * the group of "explicit character types", it is always identical to logical type
75 * \ref alib::strangeChar.
76 *
77 * \see
78 * For details, see chapter \ref alib_characters_chars "2. Character Types" of the
79 * Programmer's Manual of module \alib_characters_nl.
80 */
81using xchar = PLATFORM_SPECIFIC;
82
83#else // !defined(ALIB_DOX)
84
85 #define A_NCHAR(STR) STR
86
87
88 #if ALIB_CHARACTERS_SIZEOF_WCHAR == ALIB_SIZEOF_WCHAR_T
89
90 # define ALIB_CHARACTERS_NATIVE_WCHAR 1
91
92 # if ALIB_CHARACTERS_SIZEOF_WCHAR == 2
93 using wchar = wchar_t;
94 using xchar = char32_t;
95 #define A_WCHAR(STR) L ## STR
96 #define A_XCHAR(STR) U ## STR
97 #define A_SCHAR(STR) U ## STR
98 # else
99 using wchar = wchar_t;
100 using xchar = char16_t;
101 #define A_WCHAR(STR) L ## STR
102 #define A_XCHAR(STR) u ## STR
103 #define A_SCHAR(STR) u ## STR
104 # endif
105
106 #else
107
108 # define ALIB_CHARACTERS_NATIVE_WCHAR 0
109
110 # if ALIB_CHARACTERS_SIZEOF_WCHAR == 2
111 using wchar = char16_t;
112 using xchar = wchar_t;
113 #define A_WCHAR(STR) u ## STR
114 #define A_XCHAR(STR) L ## STR
115 #define A_SCHAR(STR) L ## STR
116 # else
117 using wchar = char32_t;
118 using xchar = wchar_t;
119 #define A_WCHAR(STR) U ## STR
120 #define A_XCHAR(STR) L ## STR
121 #define A_SCHAR(STR) L ## STR
122 # endif
123
124 #endif
125
126
127#endif // !defined(ALIB_DOX)
128
129
130// #################################################################################################
131// Logical types: character, complementChar and strangeChar
132// #################################################################################################
133
134#if defined(ALIB_DOX)
135/**
136 * This type represents a standard \alib character.
137 * The width (size) of a character is dependent on the platform and compilation flags.
138 *
139 * This is why this type is called a "logical" type. The two other logical character types are
140 * #complementChar and #strangeChar.
141 *
142 * \see
143 * For details, see chapter \ref alib_characters_chars "2. Character Types" of the
144 * Programmer's Manual of module \alib_characters_nl.
145 */
146using character= PLATFORM_SPECIFIC;
147
148/**
149 * This type represents a non-standard \alib character.
150 * If \ref alib::character is defined to implement a narrow character type, then this type
151 * implements a wide character and vice versa.
152 * Note, that the width of a wide character is not defined as well. It might be \c 2 or \c 4
153 * bytes wide.
154 *
155 * This type is called a "logical" type. The two other logical character types are
156 * #character and #strangeChar.
157 *
158 * \see
159 * For details, see chapter \ref alib_characters_chars "2. Character Types" of the
160 * Programmer's Manual of module \alib_characters_nl.
161 */
162using complementChar= PLATFORM_SPECIFIC;
163
164/**
165 * Besides types \ref alib::character and \ref alib::complementChar, this is the third
166 * logical character type defined by \alib.<br>
167 * Independent from compiler defaults and optional compiler symbols provided, this type always
168 * is equivalent to type \alib{characters;xchar}
169 *
170 * \see
171 * For details, see chapter \ref alib_characters_chars "2. Character Types" of the
172 * Programmer's Manual of module \alib_characters_nl.
173 */
174using strangeChar= PLATFORM_SPECIFIC;
175
176#elif !ALIB_CHARACTERS_WIDE
177 using character= nchar;
178 using complementChar= wchar;
179 using strangeChar= xchar;
180
181 #define A_CHAR(STR) STR
182 #define A_CCHAR(STR) A_WCHAR(STR)
183#else
184 using character= wchar;
185 using complementChar= nchar;
186 using strangeChar= xchar;
187
188 #define A_CHAR(STR) A_WCHAR(STR)
189 #define A_CCHAR(STR) STR
190
191#endif
192
193// #################################################################################################
194// T_CharArray and T_ZTCharArray
195// #################################################################################################
196
197/**
198 * Enumeration of possible values for field \b %Access used with traits types
199 * \alib{characters;T_CharArray} and \alib{characters;T_ZTCharArray}.
200 * The elements of this enumeration indicate if and how the data of an
201 * (non-zero terminated, respectively zero terminated) array-like type may be accessed.
202 */
203enum class AccessType
204{
205 /**
206 * Data may not be received. This value usually indicates that a custom type does not implement
207 * a character array at all. Hence, this is the default value of the non-specialized versions of
208 * \alib{characters;T_CharArray} and \alib{characters;T_ZTCharArray}.
209 *
210 */
211 NONE,
212
213 /** Allows implicit (and explicit) access of the character array data from mutable or constant
214 * objects. */
215 Implicit,
216
217 /** Allows explicit access of the character array data from mutable or constant objects. */
219
220 /** Allows explicit access of the character array data from mutable objects. */
222};
223
224/**
225 * Enumeration of possible values for field \b %Construction used with traits types
226 * \alib{characters;T_CharArray} and \alib{characters;T_ZTCharArray}.
227 * The elements of this enumeration indicate if and how values an array-like type may be
228 * constructed from a (non-zero terminated, respectively zero terminated) character array.
229 */
231{
232 /**
233 * The custom type may not be constructed from character arrays.
234 * This is the default value of the non-specialized versions of \alib{characters;T_CharArray}
235 * and \alib{characters;T_ZTCharArray} but may very well be used with specializations as well.
236 */
237 NONE,
238
239 /** Allows implicit (and explicit) construction of objects from character array data. */
240 Implicit,
241
242 /** Allows explicit construction of objects from character array data. */
244
245};
246
247/** ************************************************************************************************
248 * This type provides type traits for character arrays. Specializations of this struct for
249 * a custom type \p{TString}, expose information about that type representing a character array
250 * and how the array data may be accessed.<br>
251 * In addition, information about how the type may be constructed from character array data
252 * may be provided with specializations of this type.
253 *
254 * \see
255 * For detailed information, see chapter \ref alib_characters_arrays "4. Character Arrays" of
256 * the Programmer's Manual of module \alib_characters.
257 *
258 * @tparam TString The type for which specializations of this struct provide array type traits.
259 * @tparam TChar The character type of the character array that specializations provide
260 * type traits for.
261 * @tparam TEnableIf Optional TMP parameter to allow templated and/or selective specializations.
262 * ************************************************************************************************/
263template<typename TString, typename TChar, typename TEnableIf= void>
265{
266 /** Provides information about how the character array data of instances of type \p{TString}
267 * may be accessed */
269
270 /** Provides information about if and how instances of type \p{TString} may be created from
271 * character array data.*/
273
274 #if defined(ALIB_DOX)
275 /** ********************************************************************************************
276 * In specialized versions, this method has to be provided in case that field #Access is
277 * not equal to \alib{characters;AccessType::NONE}.
278 * In addition to this static method, sibling method #Length has to be provoided.
279 *
280 * For example, in a specialization for standard C++ class \c std::string, this method returns
281 * the result of method <c>std::string::data()</c>.
282 *
283 * Note, that in the case of access type \alib{characters;AccessType::MutableOnly}, the
284 * signature of this method needs to be slightly different with the specialization: argument
285 * \p{src} in this case must be of type <c>TString&</c>, hence must not be \c const.
286 *
287 * @param src The source object of external type \p{TString}.
288 * @returns Specializatins have to return a pointer to the start of the character array
289 * represented by the given object \p{src} of type \p{TString}.
290 **********************************************************************************************/
291 static
292 const TChar* Buffer( const TString& src );
293
294 /** ********************************************************************************************
295 * In specialized versions, this method has to be provided in case that field #Access is
296 * not equal to \alib{characters;AccessType::NONE}.
297 * In addition to this static method, sibling method #Buffer has to be provoided.
298 *
299 * For example, in a specialization for standard C++ class \c std::string, this method returns
300 * the result of method <c>std::string::size()</c>.
301 *
302 * Note, that in the case of access type \alib{characters;AccessType::MutableOnly}, the
303 * signature of this method needs to be slightly different with the specialization: argument
304 * \p{src} in this case must be of type <c>TString&</c>, hence must not be \c const.
305 *
306 * @param src The source object of external type \p{TString}.
307 * @returns Specializatins have to return the length of the character array
308 * represented by the given object \p{src} of type \p{TString}.
309 **********************************************************************************************/
310 static
311 integer Length( const TString& src );
312
313 /** ********************************************************************************************
314 * In specialized versions, this method has to be provided in case that field #Construction is
315 * not equal to \alib{characters;ConstructionType::NONE}.
316 *
317 * If so, this method needs to construct and return an instance of type \p{TString}, created
318 * from the character array specified by arguments \p{array} and \p{length}
319 *
320 * @param array The external array to be used to create the return value.
321 * @param length The length of the external array.
322 * @returns A new instance (value) of type \p{TString}.
323 **********************************************************************************************/
324 static
325 TString Construct( const TChar* array, integer length );
326#endif
327};
328
329/** ************************************************************************************************
330 * This type traits struct is in all aspects equivalent to \alib{characters;T_CharArray}, only that
331 * this struct provides traits on zero-terminated character arrays, while \b % T_CharArray is
332 * about non-zero terminated arrays.
333 *
334 * Please, therefore consult the documentation of type \alib{characters;T_CharArray}.
335 *
336 * \see
337 * For detailed information, see chapter \ref alib_characters_arrays "4. Character Arrays" of
338 * the Programmer's Manual of module \alib_characters.
339 *
340 * @tparam TString The type for which specializations of this struct provide array type traits.
341 * @tparam TChar The character type of the character array that specializations provide
342 * type traits for.
343 * @tparam TEnableIf Optional TMP parameter to allow templated and/or selective specializations.
344 * ************************************************************************************************/
345template<typename TString, typename TChar, typename TEnableIf= void>
347{
348 #if !defined(ALIB_DOX)
349 static constexpr AccessType Access = AccessType::NONE;
350 static constexpr ConstructionType Construction= ConstructionType::NONE;
351 #else
352 /** ********************************************************************************************
353 * Same as corresponding method \alib{characters::T_CharArray;Buffer} of sibling struct
354 * \b T_CharArray.
355 *
356 * @param src The source object of external type \p{TString}.
357 * @returns Specializatins have to return a pointer to the start of the zero-terminated
358 * character array represented by the given object \p{src} of type \p{TString}.
359 **********************************************************************************************/
360 static
361 const TChar* Buffer( const TString& src );
362
363 /** ********************************************************************************************
364 * Same as corresponding method \alib{characters::T_CharArray;Length} of sibling struct
365 * \b T_CharArray.
366 *
367 * @param src The source object of external type \p{TString}.
368 * @returns Specializatins have to return the length of the character array
369 * represented by the given object \p{src} of type \p{TString}.
370 **********************************************************************************************/
371 static
372 integer Length( const TString& src );
373
374 /** ********************************************************************************************
375 * Same as corresponding method \alib{characters::T_CharArray;Construct} of sibling struct
376 * \b T_CharArray.
377 *
378 * @param array The external zero-terminated array to be used to create the return value.
379 * @param length The length of the external array.
380 * @returns A new instance (value) of type \p{TString}.
381 **********************************************************************************************/
382 static
383 TString Construct( const TChar* array, integer length );
384#endif
385
386};
387
388
389/** ************************************************************************************************
390 * Helper TMP struct that determines if a specialization of TMP struct \alib{characters;T_CharArray}
391 * exists for type \p{TString}, and if so, for which character type such specialization was
392 * made.
393 *
394 * If a specialization exists, the class inherits <c>std::true_type</c>, otherwise
395 * <c>std::false_type</c>.
396 *
397 * \see
398 * For details see Programmer's Manual chapter \ref alib_characters_arrays_traits_tool_arraytype.
399 * A sibling helper struct exists with \alib{characters;TT_ZTCharArrayType}.
400 *
401 * @tparam TString The custom type to test.
402 * @tparam TEnableIf Helper parameter used for templated specializations of this struct.
403 **************************************************************************************************/
404template<typename TString, typename TEnableIf= void>
405struct TT_CharArrayType : std::false_type
406{
407 /** The character type of the character array that is implemented with type \p{TString}. */
408 using TChar= void;
409};
410
411/** ************************************************************************************************
412 * Helper TMP struct that determines if a specialization of TMP struct \alib{characters;T_ZTCharArray}
413 * exists for type \p{TString}, and if so, for which character type such specialization was
414 * made.
415 *
416 * If a specialization exists, the class inherits <c>std::true_type</c>, otherwise
417 * <c>std::false_type</c>.
418 *
419 * \see
420 * For details see Programmer's Manual chapter \ref alib_characters_arrays_traits_tool_arraytype.
421 * A sibling helper struct exists with \alib{characters;TT_CharArrayType}.
422 *
423 * @tparam TString The custom type to test.
424 * @tparam EnableIf Helper parameter used for templated specializations of this struct.
425 **************************************************************************************************/
426template<typename TString, typename EnableIf= void>
427struct TT_ZTCharArrayType : std::false_type
428{
429 /** The character type of the zero-terminated character array that is implemented with type
430 * \p{TString}. */
431 using TChar= void;
432};
433
434#if !defined(ALIB_DOX)
435template<typename T> struct TT_CharArrayType <T, typename std::enable_if<characters::T_CharArray <T,nchar>::Access != AccessType::NONE>::type>: std::true_type { using TChar= nchar; };
436template<typename T> struct TT_CharArrayType <T, typename std::enable_if<characters::T_CharArray <T,wchar>::Access != AccessType::NONE>::type>: std::true_type { using TChar= wchar; };
437template<typename T> struct TT_CharArrayType <T, typename std::enable_if<characters::T_CharArray <T,xchar>::Access != AccessType::NONE>::type>: std::true_type { using TChar= xchar; };
438template<typename T> struct TT_ZTCharArrayType<T, typename std::enable_if<characters::T_ZTCharArray<T,nchar>::Access != AccessType::NONE>::type>: std::true_type { using TChar= nchar; };
439template<typename T> struct TT_ZTCharArrayType<T, typename std::enable_if<characters::T_ZTCharArray<T,wchar>::Access != AccessType::NONE>::type>: std::true_type { using TChar= wchar; };
440template<typename T> struct TT_ZTCharArrayType<T, typename std::enable_if<characters::T_ZTCharArray<T,xchar>::Access != AccessType::NONE>::type>: std::true_type { using TChar= xchar; };
441#endif
442
443
444// #################################################################################################
445// Helper macros to specialize T_CharArray/T_ZTCharArray
446// #################################################################################################
447
448#if !defined(ALIB_DOX)
449
450#define ALIB_CHARACTER_ARRAY_internal( C_ZTC, TString, TChar, Const, pAccess, pConstruction) \
451template<> struct T_ ## C_ZTC ## harArray<TString, TChar> \
452{ \
453 static constexpr AccessType Access= AccessType:: pAccess; \
454 static constexpr ConstructionType Construction= ConstructionType:: pConstruction; \
455 \
456 static inline const TChar* Buffer( TString Const & src ); \
457 static inline integer Length( TString Const & src ); \
458 \
459 static inline TString Construct( const TChar* array, integer length ); \
460};
461
462#endif
463
464
465#define ALIB_CHARACTER_ARRAY( TString, TChar, Access , Construction ) \
466ALIB_CHARACTER_ARRAY_internal( C , TString, TChar, const, Access , Construction )
467
468#define ALIB_CHARACTER_ARRAY_MUTABLE( TString, TChar, Construction ) \
469ALIB_CHARACTER_ARRAY_internal( C , TString, TChar, , MutableOnly, Construction )
470
471#define ALIB_CHARACTER_ZT_ARRAY( TString, TChar, Access , Construction ) \
472ALIB_CHARACTER_ARRAY_internal( ZTC, TString, TChar, const, Access , Construction )
473
474#define ALIB_CHARACTER_ZT_ARRAY_MUTABLE( TString, TChar, Construction ) \
475ALIB_CHARACTER_ARRAY_internal( ZTC, TString, TChar, , MutableOnly, Construction )
476
477
478
479#define ALIB_CHARACTER_ARRAY_IMPL_BUFFER( TString, TChar, ... ) \
480const TChar* T_CharArray<TString,TChar>::Buffer(TString const& src ) { __VA_ARGS__ }
481
482#define ALIB_CHARACTER_ARRAY_IMPL_LENGTH( TString, TChar, ... ) \
483integer T_CharArray<TString,TChar>::Length(TString const& src ) { __VA_ARGS__ }
484
485#define ALIB_CHARACTER_ARRAY_IMPL_BUFFER_MUTABLE( TString, TChar, ... ) \
486const TChar* T_CharArray<TString,TChar>::Buffer(TString & src ) { __VA_ARGS__ }
487
488#define ALIB_CHARACTER_ARRAY_IMPL_LENGTH_MUTABLE( TString, TChar, ... ) \
489integer T_CharArray<TString,TChar>::Length(TString & src ) { __VA_ARGS__ }
490
491#define ALIB_CHARACTER_ARRAY_IMPL_CONSTRUCT( TString, TChar, ... ) \
492 TString T_CharArray <TString,TChar>::Construct( const TChar* array, integer length ) { __VA_ARGS__ }
493
494#define ALIB_CHARACTER_ZT_ARRAY_IMPL_BUFFER( TString, TChar, ... ) \
495const TChar* T_ZTCharArray<TString,TChar>::Buffer( TString const& src ) { __VA_ARGS__ }
496
497#define ALIB_CHARACTER_ZT_ARRAY_IMPL_LENGTH( TString, TChar, ... ) \
498integer T_ZTCharArray<TString,TChar>::Length( TString const& src ) { __VA_ARGS__ }
499
500#define ALIB_CHARACTER_ZT_ARRAY_IMPL_BUFFER_MUTABLE(TString, TChar, ... ) \
501const TChar* T_ZTCharArray<TString,TChar>::Buffer( TString & src ) { __VA_ARGS__ }
502
503#define ALIB_CHARACTER_ZT_ARRAY_IMPL_LENGTH_MUTABLE(TString, TChar, ... ) \
504integer T_ZTCharArray<TString,TChar>::Length( TString & src ) { __VA_ARGS__ }
505
506#define ALIB_CHARACTER_ZT_ARRAY_IMPL_CONSTRUCT( TString, TChar, ... ) \
507 TString T_ZTCharArray<TString,TChar>::Construct( const TChar* array, integer length ) { __VA_ARGS__ }
508
509
510// #################################################################################################
511// Specializations of T_CharArray and T_ZTCharArray for const and non-const character pointer types
512// #################################################################################################
513
514#if !defined(ALIB_DOX)
515// Fixed length arrays
516template<size_t TCapacity, typename TChar>
517struct T_CharArray<TChar[TCapacity], TChar>
518{
519 static constexpr AccessType Access = AccessType::Implicit;
520 static constexpr const TChar* Buffer( TChar const (&src) [TCapacity] ) { return src; }
521 static constexpr integer Length( TChar const (& ) [TCapacity] ) { return TCapacity -1; }
522};
523
524
525template<size_t TCapacity, typename TChar>
526struct T_ZTCharArray<TChar[TCapacity], TChar>
527{
528 static constexpr AccessType Access = AccessType::Implicit;
529 static constexpr const TChar* Buffer( TChar const (&src) [TCapacity] ) { return src; }
530 static constexpr integer Length( TChar const (& ) [TCapacity] ) { return TCapacity -1; }
531};
532
533// constant character pointers
534template<typename TChar> struct T_CharArray<TChar const*, TChar>
535{
536 static constexpr AccessType Access = AccessType::Implicit;
537 static constexpr ConstructionType Construction = ConstructionType::ExplicitOnly;
538 static constexpr const TChar* Buffer(const TChar* const & src ) { return src; }
539 static constexpr integer Length(const TChar* const & src ) { return src ? static_cast<integer>( std::char_traits<TChar>::length(src) ) : 0; }
540 static constexpr const TChar* Construct(const TChar* array, integer ) { return array; }
541};
542
543template<typename TChar> struct T_ZTCharArray<TChar const*, TChar>
544{
545 static constexpr AccessType Access = AccessType::Implicit;
546 static constexpr ConstructionType Construction = ConstructionType::Implicit;
547 static constexpr const TChar* Buffer(const TChar* const & src ) { return src; }
548 static constexpr integer Length(const TChar* const & src ) { return src ? static_cast<integer>( std::char_traits<TChar>::length(src) ) : 0; }
549 static constexpr const TChar* Construct(const TChar* array, integer ) { return array; }
550};
551
552// mutable character pointers:
553template<typename TChar> struct T_CharArray<TChar*, TChar>
554{
555 static constexpr AccessType Access = AccessType::ExplicitOnly;
556 static constexpr ConstructionType Construction = ConstructionType::ExplicitOnly;
557 static constexpr const TChar* Buffer( TChar* const & src ) { return src; }
558 static constexpr integer Length( TChar* const & src ) { return src ? static_cast<integer>( std::char_traits<TChar>::length(src) ) : 0; }
559 static constexpr TChar* Construct(const TChar* array, integer ) { return const_cast<TChar*>( array ); }
560};
561
562// mutable character pointers:
563template<typename TChar> struct T_ZTCharArray<TChar*, TChar>
564{
565 static constexpr AccessType Access = AccessType::ExplicitOnly;
566 static constexpr ConstructionType Construction = ConstructionType::ExplicitOnly;
567 static constexpr const TChar* Buffer( TChar* const & src ) { return src; }
568 static constexpr integer Length( TChar* const & src ) { return src ? static_cast<integer>( std::char_traits<TChar>::length(src) ) : 0; }
569 static constexpr TChar* Construct(const TChar* array, integer ) { return const_cast<TChar*>( array ); }
570};
571
572#endif
573
574// #################################################################################################
575// TMP tool structs
576// #################################################################################################
577
578/**
579 * This simple template struct provides inner type definition <c>type</c>, which in the two
580 * specializations for types \ref alib::nchar and \ref alib::wchar aliases the respective other
581 * type.
582 *
583 * The struct may be used to generalize code that is templated in respect to the character type to
584 * work on and that needs to refer to the character type that is complementary to the one currently
585 * compiled.
586 *
587 * The struct may be used in code via shortcut macro \ref ATMP_CHAR_COMPLEMENT.
588 */
589template<typename> struct TT_ComplementChar
590{
591 using type= void; ///< Default is void
592};
593
594#if !defined(ALIB_DOX)
595template<> struct TT_ComplementChar<nchar> { using type= wchar; };
596template<> struct TT_ComplementChar<wchar> { using type= nchar; };
597
598
599#define ATMP_CHAR_COMPLEMENT( TChar ) typename TT_ComplementChar<TChar>::type
600
601#endif
602
603
604
605/**
606 * This helper struct is used to determine the corresponding \alib character type, in cases that
607 * a fixed size of characters is required. Specializations for size values \c 1, \c 2 and \c 4
608 * exists.
609 *
610 * As an example, the \https{QT Class Library,www.qt.io} uses a 2-byte character width,
611 * independent from compiler and platform. Therefore, to convert a \b QT character value to
612 * an \alib character value, the destination type is:
613 *
614 * typename TT_CharOfSize<2>::type
615 *
616 * @tparam TCharSize The size of the required character type in bytes.
617 */
618template<int TCharSize> struct TT_CharOfSize : public std::false_type
619{
620 #if defined(ALIB_DOX)
621 /** One of the \alib character types, \alib{characters;nchar}, \alib{characters;wchar} or
622 * \alib{characters;xchar}. */
623 using type= void;
624 #endif
625};
626
627#if !defined(ALIB_DOX)
628template<> struct TT_CharOfSize<sizeof(nchar)> : public std::true_type { using type= nchar; };
629template<> struct TT_CharOfSize<sizeof(wchar)> : public std::true_type { using type= wchar; };
630template<> struct TT_CharOfSize<sizeof(xchar)> : public std::true_type { using type= xchar; };
631#endif
632
633/**
634 * Helper struct that in its non-specialized version inherits from \c std::false_type.
635 * For the three \alib character types \alib{characters;nchar}, \alib{characters;wchar} and
636 * \alib{characters;xchar} (respectively \alib{characters;character},
637 * \alib{characters;complementChar} and \alib{characters;strangeChar}), a specialization exists
638 * that inherits \c std::true_type.
639 *
640 * With that, the class may be used to decide if template type \p{TChar} is an \alib character type.
641 *
642 * @tparam TChar The type to test for being an \alib character type.
643 */
644template<typename TChar> struct TT_IsChar : public std::false_type {};
645
646#if !defined(ALIB_DOX)
647template<> struct TT_IsChar<nchar> : public std::true_type {};
648template<> struct TT_IsChar<wchar> : public std::true_type {};
649template<> struct TT_IsChar<xchar> : public std::true_type {};
650#endif
651
652} // namespace alib[::character]
653
654
655/// Type alias in namespace \b alib.
657
658/// Type alias in namespace \b alib.
660
661/// Type alias in namespace \b alib.
663
664/// Type alias in namespace \b alib.
666
667/// Type alias in namespace \b alib.
669
670/// Type alias in namespace \b alib.
672
673} // namespace [alib]
674
675
676
677
678
679#endif // HPP_ALIB_CHARACTERS_CHARACTERS
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:190
PLATFORM_SPECIFIC xchar
PLATFORM_SPECIFIC strangeChar
PLATFORM_SPECIFIC complementChar
PLATFORM_SPECIFIC character
PLATFORM_SPECIFIC wchar
Definition alib.cpp:57
characters::complementChar complementChar
Type alias in namespace alib.
characters::wchar wchar
Type alias in namespace alib.
characters::xchar xchar
Type alias in namespace alib.
characters::strangeChar strangeChar
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
characters::nchar nchar
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
static const TChar * Buffer(const TString &src)
static constexpr ConstructionType Construction
static TString Construct(const TChar *array, integer length)
static constexpr AccessType Access
static integer Length(const TString &src)
static const TChar * Buffer(const TString &src)
static TString Construct(const TChar *array, integer length)
static integer Length(const TString &src)