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