ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
chartraits.inl
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-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
9
10/// Enumeration of possible values for fields \b %Access and \b %Construction of traits-types
11/// \alib{characters;ArrayTraits} and \alib{characters;ZTArrayTraits}.
12/// The elements of this enumeration indicate if and how the data of a "char-array-like type"
13/// (which may be non-zero- or zero-terminated) may be accessed and, reversely, how such types
14/// may be constructed from character arrays.
15///
16/// This is best explained when looking at class \alib{strings;TString} of higher-level
17/// module \alib_strings.
18/// That classes' constructors use this policy to decide if an instance it may be constructed
19/// implicitly or explicitly from a third-party type.
20/// In the other direction, the type provides cast methods, which let instances of \b TString
21/// implicitly or explicitly convert into third-party types.
22enum class Policy
23{
24 /// Data may not be received, respectively the custom type may not be constructed from
25 /// a (character-array-) type.
26 /// This value usually indicates that a custom type does not represent a character array at all.
27 /// Hence, this is the default value exposed by the non-specialized version of type-traits
28 /// structs \alib{characters;ArrayTraits} and \alib{characters;ZTArrayTraits}.
30
31 /// Allows implicit (and explicit) access of the character array data, respectively allows
32 /// implicit and explicit construction of string-like custom types, from mutable or constant
33 /// objects.
35
36 /// Allows explicit access of the character array data respectively allows
37 /// explicit construction of string-like custom types, from mutable or constant objects.
39
40 /// Allows explicit access of the character array data from mutable objects only.<br>
41 /// With field \b %Construction of traits-types \alib{characters;ArrayTraits} and
42 /// \alib{characters;ZTArrayTraits}, this enumeration element is not used.
44};
45
46//==================================================================================================
47/// This type provides type-traits for character arrays. Specializations of this struct for
48/// a custom type \p{TStringSource}, expose information about that type representing a character
49/// array and how the array data may be accessed.<br>
50/// In addition, information about how the type may be constructed from character array data
51/// may be provided with specializations of this type.
52///
53/// \see
54/// For detailed information, see chapter \ref alib_characters_arrays "4. Character Arrays" of
55/// the Programmer's Manual of module \alib_characters.
56///
57/// @tparam TStringSource The type for which specializations of this struct provide array type-traits.
58/// @tparam TChar The character type of the character array that specializations provide
59/// type-traits for.
60//==================================================================================================
61template<typename TStringSource, typename TChar>
63{
64 /// Provides information about how the character array data of instances of type
65 /// \p{TStringSource} may be accessed
66 static constexpr Policy Access = Policy::NONE;
67
68 /// Provides information about if and how instances of type \p{TStringSource} may be created
69 /// from character array data.
70 static constexpr Policy Construction = Policy::NONE;
71
72 #if DOXYGEN
73 /// In specialized versions, this method has to be provided in case that field #Access is
74 /// not equal to \alib{characters;Policy::NONE}.
75 /// In addition to this static method, sibling method #Length has to be provided.
76 ///
77 /// For example, in a specialization for standard C++ class \c std::string, this method returns
78 /// the result of method <c>std::string::data()</c>.
79 ///
80 /// Note that in the case of access type \alib{characters;Policy::MutableOnly}, the
81 /// signature of this method needs to be slightly different with the specialization: argument
82 /// \p{src} in this case must be of type <c>TStringSource&</c>, hence must not be \c const.
83 ///
84 /// @param src The source object of external type \p{TStringSource}.
85 /// @returns Specializations have to return a pointer to the start of the character array
86 /// represented by the given object \p{src} of type \p{TStringSource}.
87 static
88 const TChar* Buffer( const TStringSource& src );
89
90 /// In specialized versions, this method has to be provided in case that field #Access is
91 /// not equal to \alib{characters;Policy::NONE}.
92 /// In addition to this static method, sibling method #Buffer has to be provoided.
93 ///
94 /// For example, in a specialization for standard C++ class \c std::string, this method returns
95 /// the result of method <c>std::string::size()</c>.
96 ///
97 /// Note, that in the case of access type \alib{characters;Policy::MutableOnly}, the
98 /// signature of this method needs to be slightly different with the specialization: argument
99 /// \p{src} in this case must be of type <c>TStringSource&</c>, hence must not be \c const.
100 ///
101 /// @param src The source object of external type \p{TStringSource}.
102 /// @returns Specializations have to return the length of the character array
103 /// represented by the given object \p{src} of type \p{TStringSource}.
104 static
105 integer Length( const TStringSource& src );
106
107 /// In specialized versions, this method has to be provided in case that field #Construction is
108 /// not equal to \alib{characters;Policy::NONE}.
109 ///
110 /// If so, this method needs to construct and return an instance of type \p{TStringSource}, created
111 /// from the character array specified by arguments \p{array} and \p{length}
112 ///
113 /// @param array The external array to be used to create the return value.
114 /// @param length The length of the external array.
115 /// @returns A new instance (value) of type \p{TStringSource}.
116 static
117 TStringSource Construct( const TChar* array, integer length );
118 #endif
119};
120
121//==================================================================================================
122/// This type trait is in all aspects equivalent to \alib{characters;ArrayTraits}, only that
123/// this struct provides traits on zero-terminated character arrays, while \b %ArrayTraits is
124/// about non-zero-terminated arrays.
125///
126/// Please, therefore consult the documentation of type \alib{characters;ArrayTraits}.
127///
128/// \see
129/// For detailed information, see chapter \ref alib_characters_arrays "4. Character Arrays" of
130/// the Programmer's Manual of module \alib_characters.
131///
132/// @tparam TStringSource The type for which specializations of this struct provide array type-traits.
133/// @tparam TChar The character type of the character array that specializations provide
134/// type-traits for.
135//==================================================================================================
136template<typename TStringSource, typename TChar>
138{
139 /// Provides information about how the zero-terminated character arrays of instances of
140 /// type \p{TStringSource} may be accessed.
141 static constexpr Policy Access = Policy::NONE;
142
143 /// Provides information about if and how instances of type \p{TStringSource} may be created
144 /// from zero-terminated character arrays.
145 static constexpr Policy Construction = Policy::NONE;
146
147 #if DOXYGEN
148 /// Same as corresponding method \alib{characters::ArrayTraits;Buffer} of sibling struct
149 /// \b ArrayTraits.
150 ///
151 /// @param src The source object of external type \p{TStringSource}.
152 /// @returns Specializations have to return a pointer to the start of the zero-terminated
153 /// character array represented by the given object \p{src} of type \p{TStringSource}.
154 static
155 const TChar* Buffer( const TStringSource& src );
156
157 /// Same as corresponding method \alib{characters::ArrayTraits;Length} of sibling struct
158 /// \b ArrayTraits.
159 ///
160 /// @param src The source object of external type \p{TStringSource}.
161 /// @returns Specializations have to return the length of the character array
162 /// represented by the given object \p{src} of type \p{TStringSource}.
163 static
164 integer Length( const TStringSource& src );
165
166 /// Same as corresponding method \alib{characters::ArrayTraits;Construct} of sibling struct
167 /// \b ArrayTraits.
168 ///
169 /// @param array The external zero-terminated array to be used to create the return value.
170 /// @param length The length of the external array.
171 /// @returns A new instance (value) of type \p{TStringSource}.
172 static
173 TStringSource Construct( const TChar* array, integer length );
174 #endif
175}; // struct ZTArrayTraits
176
177
179//==================================================================================================
180/// Concept which is satisfied for given template type \p{T} in case a specialization of
181/// the type trait \alib{characters;ArrayTraits;ArrayTraits<T, TChar>} exists.
182///
183/// \see
184/// - For details see chapter \ref alib_characters_arrays_traits_tool_arraytype of
185/// the Programmer's Manual of module \alib_characters_nl.
186/// - Type alias {characters;CharType}.
187/// - A sibling concept exists with concept \alib{characters;IsZTArray}.
188///
189/// @tparam T The custom type to test for being or incorporating a character array.
190/// @tparam TChar The character type of the array to test for.
191//==================================================================================================
192template<typename T, typename TChar>
194
195//==================================================================================================
196/// Determines the character type of the array that the given template type \p{T} represents.
197/// In case no a specialization of the type trait
198/// \alib{characters;ArrayTraits;ArrayTraits<T, TChar>} exists, this evaluates to \c void.
199/// Otherwise, this alias evaluates to one of \alib{characters;nchar}, \alib{characters;wchar}
200/// or \alib{characters;xchar}.
201///
202/// \see
203/// - For details see chapter \ref alib_characters_arrays_traits_tool_arraytype of
204/// the Programmer's Manual of module \alib_characters_nl.
205/// - Concept \alib{characters;IsArray}.
206/// - A sibling alias exists with \alib{characters;ZTType}.
207///
208/// @tparam T The custom type to get the character type for.
209//==================================================================================================
210template<typename T>
211using Type= std::conditional_t<ArrayTraits<T,nchar>::Access != Policy::NONE, nchar,
212 std::conditional_t<ArrayTraits<T,wchar>::Access != Policy::NONE, wchar,
213 std::conditional_t<ArrayTraits<T,xchar>::Access != Policy::NONE, xchar,
214 void > > >;
215
216//==================================================================================================
217/// Concept which is satisfied for given template type \p{T} in case a specialization of
218/// the type trait \alib{characters;ZTArrayTraits;ZTArrayTraits<T, TChar>} exists.
219///
220/// \see
221/// - For details see chapter \ref alib_characters_arrays_traits_tool_arraytype of
222/// the Programmer's Manual of module \alib_characters_nl.
223/// - Type alias \alib{characters;ZTType}.
224/// - A sibling concept exists with concept \alib{characters;IsZTArray}.
225///
226/// @tparam T The custom type to test for being or incorporating a zero-terminated character
227/// array.
228/// @tparam TChar The character type of the array to test for.
229//==================================================================================================
230template<typename T, typename TChar>
232
233/// Determines the character type of the zero-terminated array that the given template type
234/// \p{T} represents.
235/// In case no a specialization of the type trait
236/// \alib{characters;ArrayTraits;ZTArrayTraits<T, TChar>} exists, this evaluates to \c void.
237/// Otherwise, this alias evaluates to one of \alib{characters;nchar}, \alib{characters;wchar}
238/// or \alib{characters;xchar}.
239///
240/// \see
241/// - For details see chapter \ref alib_characters_arrays_traits_tool_arraytype of
242/// the Programmer's Manual of module \alib_characters_nl.
243/// - Concept \alib{characters;IsArray}.
244/// - A sibling alias exists with \alib{characters;ZTType}.
245///
246/// @tparam T The custom type to get the character type for.
247template<typename T>
248using ZTType= std::conditional_t<ZTArrayTraits<T,nchar>::Access != Policy::NONE, nchar,
249 std::conditional_t<ZTArrayTraits<T,wchar>::Access != Policy::NONE, wchar,
250 std::conditional_t<ZTArrayTraits<T,xchar>::Access != Policy::NONE, xchar,
251 void > > >;
252
253/// A concept to identify types which can be used to \b implicitly construct string types.
254/// @see Traits class \alib{characters;ArrayTraits} which may be specialized for custom types to
255/// satisfy this concept - with the aim to make type \p{T} compatible (exchangeable) with
256/// string types.
257/// @tparam T The type to be tested (with \c const and \c volatile removed).
258/// @tparam TChar The character type of the string type.
259template <typename T, typename TChar>
262
263/// A concept to identify types which can be used to \b explicitly construct string types.
264/// @see Traits class \alib{characters;ArrayTraits} which may be specialized for custom types to
265/// satisfy this concept - with the aim to make type \p{T} compatible (exchangeable) with
266/// string types.
267/// @tparam T The type to be tested (with \c const and \c volatile removed).
268/// @tparam TChar The character type of the string type
269/// to be constructed.
270template <typename T, typename TChar>
273
274/// A concept to identify <b>non-const</b> types which can be used to \b explicitly construct
275/// string types.
276/// Used for explicit string construction, when the access-traits need to
277/// perform changes on the source object, which imply that those are mutable.
278/// @see Traits class \alib{characters;ArrayTraits} which may be specialized for custom types to
279/// satisfy this concept - with the aim to make type \p{T} compatible (exchangeable) with
280/// string types.
281/// @tparam T The type to be tested (with \c const and \c volatile removed).
282/// @tparam TChar The character type of the string type
283/// to be constructed.
284template <typename T, typename TChar>
287 && !std::is_const_v<T>;
288
289/// A concept to identify types that string types can be \b implicitly cast to.
290/// In other words, types which are constructible from string types.
291/// @see Traits class \alib{characters;ArrayTraits} which may be specialized for custom types to
292/// satisfy this concept - with the aim to make type \p{T} compatible (exchangeable) with
293/// string types.
294/// @tparam T The type to be tested (with \c const and \c volatile removed).
295/// @tparam TChar The character type of the string type that should be cast to \p{T}.
296template <typename T, typename TChar>
299
300/// A concept to identify types that string types can be \b implicitly cast to.
301/// In other words, types which are constructible from string types.
302/// @see Traits class \alib{characters;ArrayTraits} which may be specialized for custom types to
303/// satisfy this concept - with the aim to make type \p{T} compatible (exchangeable) with
304/// string types.
305/// @tparam T The type to be tested (with \c const and \c volatile removed).
306/// @tparam TChar The character type of the string type that should be cast to \p{T}.
307template <typename T, typename TChar>
310
311/// A concept to identify types which can be used to \b implicitly construct zero-terminated
312/// strings.
313/// @see Traits class \alib{characters;ZTArrayTraits} which may be specialized for custom types to
314/// satisfy this concept - with the aim to make type \p{T} compatible (exchangeable) with
315/// zero-terminated string types.
316/// @tparam T The type to be tested (with \c const and \c volatile removed).
317/// @tparam TChar The character type of the string type.
318template <typename T, typename TChar>
321
322/// A concept to identify types which can be used to \b explicitly construct zero-terminated
323/// strings.
324/// @see Traits class \alib{characters;ZTArrayTraits} which may be specialized for custom types to
325/// satisfy this concept - with the aim to make type \p{T} compatible (exchangeable) with
326/// zero-terminated string types.
327/// @tparam T The type to be tested (with \c const and \c volatile removed).
328/// @tparam TChar The character type of the string type.
329template <typename T, typename TChar>
332
333/// A concept to identify <b>non-const</b> types which can be used to \b explicitly construct
334/// \alib \alib{strings;TString;strings}.
335/// Used for explicit string construction, when the access-traits need to
336/// perform changes on the source object, which imply that those are mutable.
337/// @see Traits class \alib{characters;ZTArrayTraits} which may be specialized for custom types to
338/// satisfy this concept - with the aim to make type \p{T} compatible (exchangeable) with
339/// zero-terminated string types.
340/// @tparam T The type to be tested (with \c const and \c volatile removed).
341/// @tparam TChar The character type of the string type to be constructed.
342template <typename T, typename TChar>
345 && !std::is_const_v<T>;
346
347
348/// Same as \alib{characters;IsImplicitZTArrayCast}, but uses type-traits class
349/// \alib{characters;ZTArrayTraits} instead of \alib{characters;ArrayTraits}.
350/// @tparam T The type to be tested (with \c const and \c volatile removed).
351/// @tparam TChar The character type of the zero-terminated string type that should be cast
352/// to \p{T}.
353template <typename T, typename TChar>
356
357/// Same as \alib{characters;IsExplicitZTArrayCast}, but uses type-traits class
358/// \alib{characters;ZTArrayTraits} instead of \alib{characters;ArrayTraits}.
359/// @tparam T The type to be tested.
360/// @tparam TChar The character type of the zero-terminated string type that should be cast
361/// to \p{T}.
362template <typename T, typename TChar>
365
366
367ALIB_WARNINGS_RESTORE // ignore docs due to clang not knowing concepts
368
369
370// #################################################################################################
371// Specializations of ArrayTraits and ZTArrayTraits for const and non-const character pointer types
372// #################################################################################################
373
374#if !DOXYGEN
375//------------------ Fixed length arrays ------------------
376template<size_t TCapacity, typename TChar>
377struct ArrayTraits<TChar[TCapacity], TChar>
378{
379 static constexpr Policy Access = Policy::Implicit;
380 static constexpr const TChar* Buffer( TChar const (&src) [TCapacity] ) { return src; }
381 static constexpr integer Length( TChar const (& ) [TCapacity] ) { return TCapacity -1; }
382};
383
384template<size_t TCapacity, typename TChar>
385struct ZTArrayTraits<TChar[TCapacity], TChar>
386{
387 static constexpr Policy Access = Policy::Implicit;
388 static constexpr const TChar* Buffer( TChar const (&src) [TCapacity] ) { return src; }
389 static constexpr integer Length( TChar const (& ) [TCapacity] ) { return TCapacity -1; }
390};
391
392// C++20 type 'char8_t'
393template<size_t TCapacity>
394struct ArrayTraits<char8_t[TCapacity], nchar>
395{
396 static constexpr Policy Access = Policy::Implicit;
397 static constexpr const nchar* Buffer( char8_t const (&src) [TCapacity] ) { return reinterpret_cast<const nchar*>(src); }
398 static constexpr integer Length( char8_t const (& ) [TCapacity] ) { return TCapacity -1; }
399};
400
401template<size_t TCapacity>
402struct ZTArrayTraits<char8_t[TCapacity], nchar>
403{
404 static constexpr Policy Access = Policy::Implicit;
405 static constexpr const nchar* Buffer( char8_t const (&src) [TCapacity] ) { return reinterpret_cast<const nchar*>(src); }
406 static constexpr integer Length( char8_t const (& ) [TCapacity] ) { return TCapacity -1; }
407};
408
409
410// ------------------ constant character pointers ------------------
411template<typename TChar> struct ArrayTraits<const TChar*, TChar>
412{
413 static constexpr Policy Access = Policy::Implicit;
414 static constexpr Policy Construction = Policy::ExplicitOnly;
415 static constexpr const TChar* Buffer(const TChar* const & src ) { return src; }
416 static constexpr integer Length(const TChar* const & src ) { return src ? integer( std::char_traits<TChar>::length(src) ) : 0; }
417 static constexpr const TChar* Construct(const TChar* array, integer ) { return array; }
418};
419
420template<typename TChar> struct ZTArrayTraits<const TChar*, TChar>
421{
422 static constexpr Policy Access = Policy::Implicit;
423 static constexpr Policy Construction = Policy::Implicit;
424 static constexpr const TChar* Buffer(const TChar* const & src ) { return src; }
425 static constexpr integer Length(const TChar* const & src ) { return src ? integer( std::char_traits<TChar>::length(src) ) : 0; }
426 static constexpr const TChar* Construct(const TChar* array, integer ) { return array; }
427};
428
429// C++20 type 'char8_t'
430template<> struct ArrayTraits<const char8_t*, nchar>
431{
432 static constexpr Policy Access = Policy::Implicit;
433 static constexpr Policy Construction = Policy::ExplicitOnly;
434 static const nchar* Buffer(const char8_t* const & src ) { return reinterpret_cast<const nchar*>(src); }
435 static constexpr integer Length(const char8_t* const & src ) { return src ? integer( std::char_traits<char8_t>::length(src) ) : 0; }
436 static const char8_t* Construct(const nchar* array, integer ) { return reinterpret_cast<const char8_t*>(array); }
437};
438
439template<> struct ZTArrayTraits<const char8_t*, nchar>
440{
441 static constexpr Policy Access = Policy::Implicit;
442 static constexpr Policy Construction = Policy::Implicit;
443 static const nchar* Buffer(const char8_t* const & src ) { return reinterpret_cast<const nchar*>(src); }
444 static constexpr integer Length(const char8_t* const & src ) { return src ? integer( std::char_traits<char8_t>::length(src) ) : 0; }
445 static const char8_t* Construct(const nchar* array, integer ) { return reinterpret_cast<const char8_t*>(array); }
446};
447
448// ------------------ mutable character pointers ------------------
449template<typename TChar> struct ArrayTraits<TChar*, TChar>
450{
451 static constexpr Policy Access = Policy::ExplicitOnly;
452 static constexpr Policy Construction = Policy::ExplicitOnly;
453 static constexpr const TChar* Buffer( TChar* const & src ) { return src; }
454 static constexpr integer Length( TChar* const & src ) { return src ? integer( std::char_traits<TChar>::length(src) ) : 0; }
455 static constexpr TChar* Construct(const TChar* array, integer ) { return const_cast<TChar*>( array ); }
456};
457
458template<typename TChar> struct ZTArrayTraits<TChar*, TChar>
459{
460 static constexpr Policy Access = Policy::ExplicitOnly;
461 static constexpr Policy Construction = Policy::ExplicitOnly;
462 static constexpr const TChar* Buffer( TChar* const & src ) { return src; }
463 static constexpr integer Length( TChar* const & src ) { return src ? integer( std::char_traits<TChar>::length(src) ) : 0; }
464 static constexpr TChar* Construct(const TChar* array, integer ) { return const_cast<TChar*>( array ); }
465};
466
467// C++20 type 'char8_t'
468template<> struct ArrayTraits<char8_t*, nchar>
469{
470 static constexpr Policy Access = Policy::ExplicitOnly;
471 static constexpr Policy Construction = Policy::ExplicitOnly;
472 static const nchar* Buffer( char8_t* const & src ) { return reinterpret_cast<nchar*>(src); }
473 static constexpr integer Length( char8_t* const & src ) { return src ? integer( std::char_traits<char8_t>::length(src) ) : 0; }
474 static char8_t* Construct(const nchar* array, integer ) { return const_cast<char8_t*>( reinterpret_cast<const char8_t*>(array) ); }
475};
476
477template<> struct ZTArrayTraits<char8_t*, nchar>
478{
479 static constexpr Policy Access = Policy::ExplicitOnly;
480 static constexpr Policy Construction = Policy::ExplicitOnly;
481 static const nchar* Buffer( char8_t* const & src ) { return reinterpret_cast<nchar*>(src); }
482 static constexpr integer Length( char8_t* const & src ) { return src ? integer( std::char_traits<char8_t>::length(src) ) : 0; }
483 static char8_t* Construct(const nchar* array, integer ) { return const_cast<char8_t*>( reinterpret_cast<const char8_t*>(array) ); }
484};
485
486#endif
487
488// #################################################################################################
489// Tools
490// #################################################################################################
491
492//==================================================================================================
493/// This templated alias type-definition provides the "complement" to the given character type.
494/// By that, it is defined to <c>wchar</c> if type <c>nchar</c> is given and vice versa.
495///
496/// Note that for type \alib{characters;xchar}, no complement is defined.
497/// For this type (and all other types), this alias evaluates to <c>void</c>.
498/// @tparam TChar The character type to get the complement for.
499//==================================================================================================
500template<typename TChar>
501using ComplementType= std::conditional_t<std::same_as<TChar, nchar>, wchar,
502 std::conditional_t<std::same_as<TChar, wchar>, nchar, void > >;
503
504
505//==================================================================================================
506/// This templated type alias can be used to determine the corresponding \alib character type
507/// from the size of the type. Typically (on common 64-bit platforms), this alias evaluates to:
508/// \alib{characters;nchar} if \p{TSizeOf} is given as \c 0. Values \c 2 and \c 4 evaluate to
509/// \alib{characters;wchar} or \alib{characters;xchar} depending on the compiler and platform.
510///
511/// As an example, the \https{QT Class Library,www.qt.io} uses a 2-byte character width,
512/// independent of compiler and platform. Therefore, to convert a \b QT character value to
513/// an \alib character value, the destination type is:
514///
515/// using qtChar= alib::characters::TypeBySize<2>;
516///
517/// @tparam TSizeOf The size of the required character type in bytes.
518//==================================================================================================
519template<size_t TSizeOf>
520using TypeBySize = std::conditional_t<TSizeOf == sizeof(nchar), nchar,
521 std::conditional_t<TSizeOf == sizeof(wchar), wchar,
522 std::conditional_t<TSizeOf == sizeof(xchar), xchar, void > > >;
523
524
526
527//==================================================================================================
528/// A concept to identify \alib character types \alib{characters;nchar}, \alib{characters;wchar}
529/// and \alib{characters;xchar}. For these three types the concept is satisfied.
530///
531/// @tparam T The type to test for being an \alib character type.
532//==================================================================================================
533template<typename T>
534concept IsCharacter= std::is_same_v<T,nchar>
535 || std::is_same_v<T,wchar>
536 || std::is_same_v<T,xchar>;
537
538//==================================================================================================
539/// Returns the length of C++ arrays like <c>std::extent</c>, but subtracts \c 1 if the
540/// array's element-type satisfies concept \alib{characters;IsCharacter}.
541/// (Thus, this "removes" the zero-termination character.)
542/// @tparam T The type to get the length for. Deduced by the compiler.
543/// @return In case of character-arrays, the string length. For other arrays the true length
544/// and \c 0 for non-array types.
545//==================================================================================================
546template<typename T>
547constexpr integer ArrayLength() {
548 if constexpr (!std::is_array_v<T>)
549 return 0;
550
551 using TChar= std::remove_cv_t<std::remove_pointer_t<std::decay_t<T>>>;
552 if constexpr( IsCharacter<TChar> )
553 return std::extent<T>::value - 1;
554 return std::extent<T>::value;
555}
556
558
559} // namespace [alib::character]
560
#define ALIB_WARNINGS_RESTORE
Definition alib.inl:705
#define ALIB_WARNINGS_IGNORE_DOCS
Definition alib.inl:688
#define ALIB_EXPORT
Definition alib.inl:488
PLATFORM_SPECIFIC wchar
Definition chartypes.inl:38
integer Length(const TChar *cstring)
Definition functions.inl:91
PLATFORM_SPECIFIC xchar
Definition chartypes.inl:51
std::conditional_t< TSizeOf==sizeof(nchar), nchar, std::conditional_t< TSizeOf==sizeof(wchar), wchar, std::conditional_t< TSizeOf==sizeof(xchar), xchar, void > > > TypeBySize
std::conditional_t< ArrayTraits< T, nchar >::Access !=Policy::NONE, nchar, std::conditional_t< ArrayTraits< T, wchar >::Access !=Policy::NONE, wchar, std::conditional_t< ArrayTraits< T, xchar >::Access !=Policy::NONE, xchar, void > > > Type
std::conditional_t< std::same_as< TChar, nchar >, wchar, std::conditional_t< std::same_as< TChar, wchar >, nchar, void > > ComplementType
std::conditional_t< ZTArrayTraits< T, nchar >::Access !=Policy::NONE, nchar, std::conditional_t< ZTArrayTraits< T, wchar >::Access !=Policy::NONE, wchar, std::conditional_t< ZTArrayTraits< T, xchar >::Access !=Policy::NONE, xchar, void > > > ZTType
constexpr integer ArrayLength()
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
static constexpr Policy Access
static integer Length(const TStringSource &src)
static TStringSource Construct(const TChar *array, integer length)
static constexpr Policy Construction
static const TChar * Buffer(const TStringSource &src)
static constexpr Policy Construction
static constexpr Policy Access
static const TChar * Buffer(const TStringSource &src)
static TStringSource Construct(const TChar *array, integer length)
static integer Length(const TStringSource &src)