ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
localstring.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_strings of the \aliblong.
4///
5/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace strings {
9
10//==================================================================================================
11/// This type specializes class #"%AString" by adding a character array member of templated size
12/// which is used as the "external" string buffer. The single motivation for the use of this
13/// class is performance optimization.
14///
15/// Assembled character strings often have a rather short lifetime and are disposed quickly
16/// after their usage. If the maximum length of the assembled string is known, such strings
17/// may be created as local values of this type, with that maximum length given as template
18/// parameter \p{TCapacity}. With that, the allocation and deallocation of heap memory
19/// (or whatever is used with the provided allocator) for the string is omitted and allocation
20/// and destruction of the local string is performed in constant time O(1).
21///
22/// But even if an instance of this class is allocated (instead of being a local variable on the
23/// stack), in common cases, still a performance improvement applies. This is due to the fact that
24/// besides the class object itself, no second memory allocation for the string buffer is performed.
25///
26/// If during the use of an instance of this class the buffer's fixed \p{TCapacity} is exceeded,
27/// a new buffer is allocated from the given allocator, just as it is always done with growing
28/// strings managed in #"%AString" objects. This means, the use of this class is safe and no
29/// restrictions apply.
30/// Of course, if possible, for performance-critical code sections, the predefined size
31/// \p{TCapacity} should be chosen large enough to allow the internal buffer to survive
32/// the use.<br>
33/// With debug-builds of \alib, parent class #"AString" optionally (and by default)
34/// #"alib_mod_assert;raises a warning" if an external buffer is replaced by a
35/// new (heap) allocation. (Note that from an #"%AString" perspective, this class's internal
36/// field #"%.buffer" is an external character array).
37/// With use cases that do not allow foreseeing a maximum buffer size, the warning has to be
38/// disabled by invoking #"TAString::DbgDisableBufferReplacementWarning".
39///
40/// For more information on warnings, see #"TAString::SetBuffer(integer)".
41///
42/// With the provision of an assignment operator, the explicit restriction of parent class to
43/// #"alib_ns_strings_astring_copymove;not allow assignments" is lifted with this type.
44///
45/// \note
46/// For commonly used sizes and character types, some convenient typedefs exists in namespace
47/// alib, which use #"HeapAllocator".
48/// These type definitions are:
49/// - #"String8", #"String16", #"String32" ... #"String1K", #"String2K", #"String4K",
50/// - #"NString8", #"NString16", #"NString32" ... #"NString1K", #"NString2K", #"NString4K", and
51/// - #"WString8", #"WString16", #"WString32" ... #"WString1K", #"WString2K", #"WString4K".
52///
53/// @tparam TChar The #"alib_characters_chars;character type" of this string.
54/// Alias names for specializations of this class are provided in namespace #"alib" with type
55/// definitions #"%LocalString",
56/// #"%NLocalString", #"%WLocalString",
57/// #"%XLocalString", #"%ComplementLocalString" and
58/// #"%StrangeLocalString".
59/// @tparam TCapacity The capacity of the string buffer allocated locally "inside" the class.
60/// @tparam TAllocator The allocator type to use. Defaults to #"HeapAllocator".
61//==================================================================================================
62template <typename TChar, integer TCapacity, typename TAllocator= lang::HeapAllocator>
63class TLocalString : public TAString<TChar, TAllocator> {
64 //################################################################################################
65 // protected fields
66 //################################################################################################
67 protected:
68 /// The base AString-type.
70
71 /// The base String-type.
73
74 /// The internal buffer with size specified by the template parameter \p{TCapacity}.
75 /// Passed as an external buffer to parent class #"%AString".
76 TChar localBuffer[TCapacity];
77
78 //################################################################################################
79 // Constructors/Destructor
80 //################################################################################################
81 public:
82 /// Constructs an empty #"%LocalString". Inherited field #"TString::buffer"
83 /// will be set as an the external buffer of parent #"%AString".
84 /// Unlike all other \alib string classes, objects of this type are not \e nulled
85 /// after default construction.
86 /// @param pAllocator The allocator to use.
87 constexpr
88 TLocalString(TAllocator& pAllocator)
89 : base( pAllocator, localBuffer, TCapacity )
90 , localBuffer {} {}
91
92 /// Constructs an empty #"%LocalString". Inherited field #"TString::buffer"
93 /// will be set as an the external buffer of parent #"%AString".
94 /// Unlike all other \alib string classes, objects of this type are not \e nulled
95 /// after default construction.
96 constexpr
98 : base( localBuffer, TCapacity )
99 , localBuffer {} {}
100
101 /// Copy constructor. Copies the string data of parameter \p{copy} to this instance
102 /// @param copy The object to copy the contents from.
109
110 /// Move constructor.
111 /// See #"alib_ns_strings_astring_copymove;Copy/Move Constructor and Assignment"
112 /// for details.
113 /// @param move The object to move.
114 TLocalString(TLocalString&& move) noexcept
115 : base( localBuffer, TCapacity ) {
116 // given move object has external buffer: we have to copy
117 if ( !move.HasInternalBuffer() ) {
118 ALIB_DBG( base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced; )
119 base::Append( move );
120 return;
121 }
122
123 // copy values
124 sBase::buffer = move.buffer;
125 sBase::length = move.length;
126 base::capacity= move.capacity;
127
128 // clean moved object (buffer does not need to be nulled as AString destructor
129 // checks capacity > 0 )
130 move.capacity= 0;
131
132 // in debug mode, more copying and more destructor prevention is needed
133 #if ALIB_DEBUG
134 base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced;
135 #if ALIB_DEBUG_STRINGS
136 base::debugLastAllocRequest = move.debugLastAllocRequest;
137 move.buffer = nullptr;
138 move.length = 0;
139 #endif
140 #endif
141 }
142
143 /// Copy assign operator.
144 /// Copies the contents of the given object \p{copy}.
145 ///
146 /// @param copy The object to copy the contents from.
147 /// @return \c *this to allow concatenated calls.
149 if ( copy.IsNull()) {
151 return *this;
152 }
153 base::Reset();
154 return static_cast<TLocalString&>( base::template Append<NC>( copy.Buffer(), copy.Length() ) );
155 }
156
157 /// Move assign operator.
158 /// Copies the contents of the given object \p{copy}.
159 ///
160 /// @param move The object to move the contents from.
161 /// @return \c *this to allow concatenated calls.
163 if( move.IsNull() ) {
164 ALIB_DBG( base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced; )
166 return *this;
167 }
168
169 // copy if move has its local buffer or this has lost its local buffer
170 if ( !move.HasInternalBuffer() || base::HasInternalBuffer() ) {
171 base::Reset( move );
172 return *this;
173 }
174
175 // copy other buffer from moved object
176 sBase::buffer = move.buffer;
177 sBase::length = move.length;
178 base::capacity= move.capacity;
179
180 // clean moved object (buffer does not need to be nulled)
181 move.capacity= 0;
182
183 // in debug mode, more copying and more destructor prevention is needed
184 #if ALIB_DEBUG
185 base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced;
186 #if ALIB_DEBUG_STRINGS
187 base::debugLastAllocRequest = move.debugLastAllocRequest;
188 move.buffer = nullptr;
189 move.length = 0;
190 #endif
191 #endif
192 return *this;
193 }
194
195 /// Constructs this instance and invokes parent's method #"%Append(const TAppendable&)" to
196 /// create a string representation of the given "appendable" object \p{src}.
197 ///
198 /// \see
199 /// Manual chapter #"alib_strings_assembly_ttostring" for more information
200 /// about which types are supported and how external, user-defined types can be made
201 /// compatible to this implicit constructor.
202 ///
203 /// @tparam TAppendable The type of parameter \p{src} that has a specialization of
204 /// functor #"AppendableTraits".
205 /// @param src The source to take the buffer and length from of template type T.
206 template <typename TAppendable>
207 TLocalString (const TAppendable& src )
208 : base( localBuffer, TCapacity ) { base::Append( src ); }
209
210
211 /// Assign operator.
212 /// Invokes inherited method #"Reset(const TAppendable&)".
213 ///
214 /// @tparam TAppendable The type of parameter \p{source}.
215 /// @param src The source of type \p{TAppendable} to append.
216 ///
217 /// @return \c *this to allow concatenated calls.
218 template <typename TAppendable>
219 TLocalString& operator= (const TAppendable& src ) { base::Reset( src ); return *this; }
220
221}; // class TLocalString
222
223//##################################################################################################
224// Specializations of ArrayTraits for this class String
225//##################################################################################################
226} namespace characters {
227#if !DOXYGEN
228template<typename TChar, integer TCapacity, typename TAllocator>
229struct ArrayTraits<strings::TLocalString<TChar,TCapacity,TAllocator>, TChar>
230{
231 using T= strings::TLocalString<TChar,TCapacity,TAllocator>;
232 static constexpr Policy Access = Policy::Implicit;
233 static constexpr Policy Construction = Policy::NONE;
234 static constexpr const TChar* Buffer(const T& src) { return src.Buffer(); }
235 static constexpr integer Length(const T& src) { return src.Length(); }
236};
237
238template<typename TChar, integer TCapacity, typename TAllocator>
239struct ZTArrayTraits<strings::TLocalString<TChar,TCapacity,TAllocator>, TChar>
240{
241 using T= strings::TLocalString<TChar,TCapacity,TAllocator>;
242 static constexpr Policy Access = Policy::Implicit;
243 static constexpr Policy Construction = Policy::NONE;
244 static constexpr const TChar* Buffer(const T& src) { return src.Terminate(); }
245 static constexpr integer Length(const T& src) { return src.Length(); }
246};
247#endif // !DOXYGEN
248
249
250} namespace strings {
251
252#if ALIB_DEBUG
253//==================================================================================================
254/// This simple specialization of #"str TLocalString" disables the warning about
255/// replacements of the internal buffer in debug-compilations. This may be used in situations,
256/// where it is not possible to disable this warning after construction, for example, if a local
257/// string is <em>emplaced</em> in a container and extensions of it's local capacity are well
258/// accepted (for a minority of the emplaced strings).
259///
260/// In release compilations, this type does not exist, but is replaced by a simple using
261/// statement.
262///
263/// @tparam TChar The character type.
264/// @tparam TCapacity The capacity of local, embedded string buffer.
265/// @tparam TAllocator The allocator to use.
266//==================================================================================================
267template <typename TChar, integer TCapacity, typename TAllocator>
268struct TLocalStringNoWarning : public TLocalString<TChar,TCapacity,TAllocator> {
269 /// Default constructor.
270 constexpr
274
275 /// Constructor taking a string to copy.
276 /// @param src The string to copy into this object.
283}; // TLocalStringNoWarning
284#else
285 template <typename TChar, integer TCapacity, typename TAllocator>
286 using TLocalStringNoWarning = TLocalString<TChar, TCapacity, TAllocator>;
287#endif
288
289}
290
291/// Type alias in namespace #"%alib".
292template<integer TCapacity>
294
295/// Type alias in namespace #"%alib".
296template<integer TCapacity>
298
299/// Type alias in namespace #"%alib".
300template<integer TCapacity>
302
303/// Type alias in namespace #"%alib".
304template<integer TCapacity>
305using NLocalString = strings::TLocalString <nchar , TCapacity, lang::HeapAllocator>;
306
307/// Type alias in namespace #"%alib".
308template<integer TCapacity>
309using WLocalString = strings::TLocalString <wchar , TCapacity, lang::HeapAllocator>;
310
311/// Type alias in namespace #"%alib".
312template<integer TCapacity>
313using XLocalString = strings::TLocalString <xchar , TCapacity, lang::HeapAllocator>;
314
315/// Type alias name for #"TLocalString;TLocalString<character,8>".
317
318/// Type alias name for #"TLocalString;TLocalString<character,16>".
320
321/// Type alias name for #"TLocalString;TLocalString<character,32>".
323
324/// Type alias name for #"TLocalString;TLocalString<character,64>".
326
327/// Type alias name for #"TLocalString;TLocalString<character,128>".
329
330/// Type alias name for #"TLocalString;TLocalString<character,256>".
332
333/// Type alias name for #"TLocalString;TLocalString<character,512>".
335
336/// Type alias name for #"TLocalString;TLocalString<character,1024>".
338
339/// Type alias name for #"TLocalString;TLocalString<character,2048>".
341
342/// Type alias name for #"TLocalString;TLocalString<character,4096>".
344
345/// Type alias name for #"TLocalString;TLocalString<character,8192>".
347
348/// Type alias name for #"TLocalString;TLocalString<nchar,8>".
350
351/// Type alias name for #"TLocalString;TLocalString<nchar,16>".
353
354/// Type alias name for #"TLocalString;TLocalString<nchar,32>".
356
357/// Type alias name for #"TLocalString;TLocalString<nchar,64>".
359
360/// Type alias name for #"TLocalString;TLocalString<nchar,128>".
362
363/// Type alias name for #"TLocalString;TLocalString<nchar,256>".
365
366/// Type alias name for #"TLocalString;TLocalString<nchar,512>".
368
369/// Type alias name for #"TLocalString;TLocalString<nchar,1024>".
371
372/// Type alias name for #"TLocalString;TLocalString<nchar,2048>".
374
375/// Type alias name for #"TLocalString;TLocalString<nchar,8192>".
377
378/// Type alias name for #"TLocalString;TLocalString<nchar,4096>".
380
381/// Type alias name for #"TLocalString;TLocalString<wchar,8>".
383
384/// Type alias name for #"TLocalString;TLocalString<wchar,16>".
386
387/// Type alias name for #"TLocalString;TLocalString<wchar,32>".
389
390/// Type alias name for #"TLocalString;TLocalString<wchar,64>".
392
393/// Type alias name for #"TLocalString;TLocalString<wchar,128>".
395
396/// Type alias name for #"TLocalString;TLocalString<wchar,256>".
398
399/// Type alias name for #"TLocalString;TLocalString<wchar,512>".
401
402/// Type alias name for #"TLocalString;TLocalString<wchar,1024>".
404
405/// Type alias name for #"TLocalString;TLocalString<wchar,2048>".
407
408/// Type alias name for #"TLocalString;TLocalString<wchar,4096>".
410
411/// Type alias name for #"TLocalString;TLocalString<wchar,8192>".
413} // namespace [alib::strings]
#define ALIB_EXPORT
#define ALIB_DBG(...)
constexpr TAString(lang::HeapAllocator &pAllocator, TChar *extBuffer, integer extBufferSize)
Definition tastring.hpp:259
TAString & Append(const TCharSrc *src, integer srcLength)
Definition tastring.hpp:782
TString< TChar > sBase
The base String-type.
TLocalString & operator=(const TLocalString &copy)
constexpr TLocalString(TAllocator &pAllocator)
TAString< TChar, TAllocator > base
The base AString-type.
constexpr integer Length() const
Definition string.hpp:300
constexpr const TChar * Buffer() const
Definition string.hpp:295
constexpr bool IsNull() const
Definition string.hpp:334
Definition alox.cpp:14
strings::TLocalString< strangeChar, TCapacity, lang::HeapAllocator > StrangeLocalString
Type alias in namespace #"%alib".
NLocalString< 32 > NString32
Type alias name for #"TLocalString;TLocalString<nchar,32>".
WLocalString< 8 > WString8
Type alias name for #"TLocalString;TLocalString<wchar,8>".
strings::TLocalString< character, TCapacity, lang::HeapAllocator > LocalString
Type alias in namespace #"%alib".
LocalString< 16 > String16
Type alias name for #"TLocalString;TLocalString<character,16>".
NLocalString< 1024 > NString1K
Type alias name for #"TLocalString;TLocalString<nchar,1024>".
strings::TLocalString< complementChar, TCapacity, lang::HeapAllocator > ComplementLocalString
Type alias in namespace #"%alib".
strings::TLocalString< wchar, TCapacity, lang::HeapAllocator > WLocalString
Type alias in namespace #"%alib".
WLocalString< 256 > WString256
Type alias name for #"TLocalString;TLocalString<wchar,256>".
LocalString< 64 > String64
Type alias name for #"TLocalString;TLocalString<character,64>".
WLocalString< 2048 > WString2K
Type alias name for #"TLocalString;TLocalString<wchar,2048>".
LocalString< 8 > String8
Type alias name for #"TLocalString;TLocalString<character,8>".
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
WLocalString< 128 > WString128
Type alias name for #"TLocalString;TLocalString<wchar,128>".
LocalString< 4096 > String4K
Type alias name for #"TLocalString;TLocalString<character,4096>".
NLocalString< 16 > NString16
Type alias name for #"TLocalString;TLocalString<nchar,16>".
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
strings::TLocalString< xchar, TCapacity, lang::HeapAllocator > XLocalString
Type alias in namespace #"%alib".
strings::TLocalString< nchar, TCapacity, lang::HeapAllocator > NLocalString
Type alias in namespace #"%alib".
NLocalString< 8 > NString8
Type alias name for #"TLocalString;TLocalString<nchar,8>".
WLocalString< 64 > WString64
Type alias name for #"TLocalString;TLocalString<wchar,64>".
LocalString< 8192 > String8K
Type alias name for #"TLocalString;TLocalString<character,8192>".
NLocalString< 2048 > NString2K
Type alias name for #"TLocalString;TLocalString<nchar,2048>".
LocalString< 1024 > String1K
Type alias name for #"TLocalString;TLocalString<character,1024>".
WLocalString< 32 > WString32
Type alias name for #"TLocalString;TLocalString<wchar,32>".
WLocalString< 4096 > WString4K
Type alias name for #"TLocalString;TLocalString<wchar,4096>".
LocalString< 128 > String128
Type alias name for #"TLocalString;TLocalString<character,128>".
NLocalString< 4096 > NString4K
Type alias name for #"TLocalString;TLocalString<nchar,8192>".
WLocalString< 8192 > WString8K
Type alias name for #"TLocalString;TLocalString<wchar,8192>".
NLocalString< 256 > NString256
Type alias name for #"TLocalString;TLocalString<nchar,256>".
WLocalString< 1024 > WString1K
Type alias name for #"TLocalString;TLocalString<wchar,1024>".
LocalString< 256 > String256
Type alias name for #"TLocalString;TLocalString<character,256>".
WLocalString< 512 > WString512
Type alias name for #"TLocalString;TLocalString<wchar,512>".
LocalString< 2048 > String2K
Type alias name for #"TLocalString;TLocalString<character,2048>".
NLocalString< 128 > NString128
Type alias name for #"TLocalString;TLocalString<nchar,128>".
NLocalString< 512 > NString512
Type alias name for #"TLocalString;TLocalString<nchar,512>".
NLocalString< 64 > NString64
Type alias name for #"TLocalString;TLocalString<nchar,64>".
LocalString< 32 > String32
Type alias name for #"TLocalString;TLocalString<character,32>".
NLocalString< 8192 > NString8K
Type alias name for #"TLocalString;TLocalString<nchar,4096>".
LocalString< 512 > String512
Type alias name for #"TLocalString;TLocalString<character,512>".
WLocalString< 16 > WString16
Type alias name for #"TLocalString;TLocalString<wchar,16>".
static constexpr Policy Access
static integer Length(const TStringSource &src)
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 integer Length(const TStringSource &src)
constexpr TLocalStringNoWarning()
Default constructor.