ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
localstring.inl
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/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace strings {
9
10//==================================================================================================
11/// This type specializes class \b 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 de-allocation 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 \b 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/// \ref alib_mod_assert "raises a warning" if an external buffer is replaced by a
35/// new (heap) allocation. (Note that from an \b %AString perspective, this class's internal
36/// field \b 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 \alib{strings::TAString;DbgDisableBufferReplacementWarning}.
39///
40/// For more information on warnings, see \alib{strings;TAString::SetBuffer;AString::SetBuffer}.
41///
42/// With the provision of an assignment operator, the explicit restriction of parent class to
43/// \ref 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 \alib{lang;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 \ref 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 \b LocalString<TCapacity>,
56/// \b NLocalString<TCapacity>, \b WLocalString<TCapacity>,
57/// \b XLocalString<TCapacity>, \b ComplementLocalString<TCapacity> and
58/// \b StrangeLocalString<TCapacity>.
59/// @tparam TCapacity The capacity of the string buffer allocated locally "inside" the class.
60/// @tparam TAllocator The allocator type to use. Defaults to \alib{lang;HeapAllocator}.
61//==================================================================================================
62template <typename TChar, integer TCapacity, typename TAllocator= lang::HeapAllocator>
63class TLocalString : public TAString<TChar, TAllocator>
64{
65 // ###############################################################################################
66 // protected fields
67 // ###############################################################################################
68 protected:
69 /// The base AString-type.
71
72 /// The base String-type.
74
75 /// The internal buffer with size specified by the template parameter \p{TCapacity}.
76 /// Passed as an external buffer to parent class \b AString.
77 TChar localBuffer[TCapacity];
78
79 // ###############################################################################################
80 // Constructors/Destructor
81 // ###############################################################################################
82 public:
83 //==============================================================================================
84 /// Constructs an empty \b %LocalString. Inherited field \alib{strings::TString;buffer}
85 /// will be set as an the external buffer of parent \b AString.
86 /// Unlike all other \alib string classes, objects of this type are not \e nulled
87 /// after default construction.
88 /// @param pAllocator The allocator to use.
89 //==============================================================================================
90 constexpr
91 TLocalString(TAllocator& pAllocator)
92 : base( pAllocator, localBuffer, TCapacity )
93 , localBuffer {}
94 {}
95
96 //==============================================================================================
97 /// Constructs an empty \b %LocalString. Inherited field \alib{strings::TString;buffer}
98 /// will be set as an the external buffer of parent \b AString.
99 /// Unlike all other \alib string classes, objects of this type are not \e nulled
100 /// after default construction.
101 //==============================================================================================
102 constexpr
104 : base( localBuffer, TCapacity )
105 , localBuffer {}
106 {}
107
108 //==============================================================================================
109 /// Copy constructor. Copies the string data of parameter \p{copy} to this instance
110 /// @param copy The object to copy the contents from.
111 //==============================================================================================
118
119 //==============================================================================================
120 /// Move constructor.
121 /// See \ref alib_ns_strings_astring_copymove "Copy/Move Constructor and Assignment"
122 /// for details.
123 /// @param move The object to move.
124 //==============================================================================================
125 TLocalString(TLocalString&& move) noexcept
126 : base( localBuffer, TCapacity )
127 {
128 // given move object has external buffer: we have to copy
129 if ( !move.HasInternalBuffer() )
130 {
131 ALIB_DBG( base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced; )
132 base::Append( move );
133 return;
134 }
135
136 // copy values
137 sBase::buffer = move.buffer;
138 sBase::length = move.length;
139 base::capacity= move.capacity;
140
141 // clean moved object (buffer does not need to be nulled as AString destructor
142 // checks capacity > 0 )
143 move.capacity= 0;
144
145 // in debug mode, more copying and more destructor prevention is needed
146#if ALIB_DEBUG
147 base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced;
148#if ALIB_DEBUG_STRINGS
149 base::debugLastAllocRequest = move.debugLastAllocRequest;
150 move.buffer = nullptr;
151 move.length = 0;
152#endif
153#endif
154 }
155
156 //==============================================================================================
157 /// Copy assign operator.
158 /// Copies the contents of the given object \p{copy}.
159 ///
160 /// @param copy The object to copy the contents from.
161 /// @return \c *this to allow concatenated calls.
162 //==============================================================================================
164 {
165 if ( copy.IsNull())
166 {
168 return *this;
169 }
170 base::Reset();
171 return static_cast<TLocalString&>( base::template Append<NC>( copy.Buffer(), copy.Length() ) );
172 }
173
174 //==============================================================================================
175 /// Move assign operator.
176 /// Copies the contents of the given object \p{copy}.
177 ///
178 /// @param move The object to move the contents from.
179 /// @return \c *this to allow concatenated calls.
180 //==============================================================================================
182 {
183 if( move.IsNull() )
184 {
185 ALIB_DBG( base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced; )
187 return *this;
188 }
189
190 // copy if move has its local buffer or this has lost its local buffer
191 if ( !move.HasInternalBuffer() || base::HasInternalBuffer() )
192 {
193 base::Reset( move );
194 return *this;
195 }
196
197 // copy other buffer from moved object
198 sBase::buffer = move.buffer;
199 sBase::length = move.length;
200 base::capacity= move.capacity;
201
202 // clean moved object (buffer does not need to be nulled)
203 move.capacity= 0;
204
205 // in debug mode, more copying and more destructor prevention is needed
206#if ALIB_DEBUG
207 base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced;
208#if ALIB_DEBUG_STRINGS
209 base::debugLastAllocRequest = move.debugLastAllocRequest;
210 move.buffer = nullptr;
211 move.length = 0;
212#endif
213#endif
214 return *this;
215 }
216
217 //==============================================================================================
218 /// Constructs this instance and invokes parent's method \b %Append to create a string
219 /// representation of the given "appendable" object \p{src}.
220 ///
221 /// \see
222 /// Manual chapter \ref alib_strings_assembly_ttostring for more information
223 /// about which types are supported and how external, user-defined types can be made
224 /// compatible to this implicit constructor.
225 ///
226 /// @tparam TAppendable The type of parameter \p{src} that has a specialization of
227 /// functor \alib{strings;AppendableTraits}.
228 /// @param src The source to take the buffer and length from of template type T.
229 //==============================================================================================
230 template <typename TAppendable>
231 TLocalString (const TAppendable& src )
232 : base( localBuffer, TCapacity )
233 {
234 base::Append( src );
235 }
236
237
238 //==============================================================================================
239 /// Assign operator.
240 /// Invokes inherited method \alib{strings::TAString;Reset(const TAppendable&)}.
241 ///
242 /// @tparam TAppendable The type of parameter \p{source}.
243 /// @param src The source of type \p{TAppendable} to append.
244 ///
245 /// @return \c *this to allow concatenated calls.
246 //==============================================================================================
247 template <typename TAppendable>
248 TLocalString& operator= (const TAppendable& src )
249 {
250 base::Reset( src );
251 return *this;
252 }
253
254}; // class TLocalString
255
256// #################################################################################################
257// Specializations of ArrayTraits for this class String
258// #################################################################################################
259} namespace characters {
260#if !DOXYGEN
261template<typename TChar, integer TCapacity, typename TAllocator>
262struct ArrayTraits<strings::TLocalString<TChar,TCapacity,TAllocator>, TChar>
263{
264 using T= strings::TLocalString<TChar,TCapacity,TAllocator>;
265 static constexpr Policy Access = Policy::Implicit;
266 static constexpr Policy Construction = Policy::NONE;
267 static constexpr const TChar* Buffer(const T& src) { return src.Buffer(); }
268 static constexpr integer Length(const T& src) { return src.Length(); }
269};
270
271template<typename TChar, integer TCapacity, typename TAllocator>
272struct ZTArrayTraits<strings::TLocalString<TChar,TCapacity,TAllocator>, TChar>
273{
274 using T= strings::TLocalString<TChar,TCapacity,TAllocator>;
275 static constexpr Policy Access = Policy::Implicit;
276 static constexpr Policy Construction = Policy::NONE;
277 static constexpr const TChar* Buffer(const T& src) { return src.Terminate(); }
278 static constexpr integer Length(const T& src) { return src.Length(); }
279};
280#endif // !DOXYGEN
281
282
283} namespace strings {
284
285#if ALIB_DEBUG
286//==================================================================================================
287/// This simple specialization of \alib{strings;TLocalString} disables the warning about
288/// replacements of the internal buffer in debug-compilations. This may be used in situations,
289/// where it is not possible to disable this warning after construction, for example if a local
290/// string is <em>emplaced</em> in a container and extensions of it's local capacity are well
291/// accepted (for a minority of the emplaced strings).
292///
293/// In release compilations, this type does not exist, but is replaced by a simple using
294/// statement.
295///
296/// @tparam TChar The character type.
297/// @tparam TCapacity The capacity of local, embedded string buffer.
298/// @tparam TAllocator The allocator to use.
299//==================================================================================================
300template <typename TChar, integer TCapacity, typename TAllocator>
301struct TLocalStringNoWarning : public TLocalString<TChar,TCapacity,TAllocator>
302{
303 /// Default constructor.
304 constexpr
310
311 /// Constructor taking a string to copy.
312 /// @param src The string to copy into this object.
319}; // TLocalStringNoWarning
320#else
321 template <typename TChar, integer TCapacity, typename TAllocator>
322 using TLocalStringNoWarning = TLocalString<TChar, TCapacity, TAllocator>;
323#endif
324
325}
326
327/// Type alias in namespace \b alib.
328template<integer TCapacity>
330
331/// Type alias in namespace \b alib.
332template<integer TCapacity>
334
335/// Type alias in namespace \b alib.
336template<integer TCapacity>
338
339/// Type alias in namespace \b alib.
340template<integer TCapacity>
341using NLocalString = strings::TLocalString <nchar , TCapacity, lang::HeapAllocator>;
342
343/// Type alias in namespace \b alib.
344template<integer TCapacity>
345using WLocalString = strings::TLocalString <wchar , TCapacity, lang::HeapAllocator>;
346
347/// Type alias in namespace \b alib.
348template<integer TCapacity>
349using XLocalString = strings::TLocalString <xchar , TCapacity, lang::HeapAllocator>;
350
351/// Type alias name for \alib{strings;TLocalString;TLocalString<character,8>}.
353
354/// Type alias name for \alib{strings;TLocalString;TLocalString<character,16>}.
356
357/// Type alias name for \alib{strings;TLocalString;TLocalString<character,32>}.
359
360/// Type alias name for \alib{strings;TLocalString;TLocalString<character,64>}.
362
363/// Type alias name for \alib{strings;TLocalString;TLocalString<character,128>}.
365
366/// Type alias name for \alib{strings;TLocalString;TLocalString<character,256>}.
368
369/// Type alias name for \alib{strings;TLocalString;TLocalString<character,512>}.
371
372/// Type alias name for \alib{strings;TLocalString;TLocalString<character,1024>}.
374
375/// Type alias name for \alib{strings;TLocalString;TLocalString<character,2048>}.
377
378/// Type alias name for \alib{strings;TLocalString;TLocalString<character,4096>}.
380
381/// Type alias name for \alib{strings;TLocalString;TLocalString<character,8192>}.
383
384/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,8>}.
386
387/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,16>}.
389
390/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,32>}.
392
393/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,64>}.
395
396/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,128>}.
398
399/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,256>}.
401
402/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,512>}.
404
405/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,1024>}.
407
408/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,2048>}.
410
411/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,8192>}.
413
414/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,4096>}.
416
417/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,8>}.
419
420/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,16>}.
422
423/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,32>}.
425
426/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,64>}.
428
429/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,128>}.
431
432/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,256>}.
434
435/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,512>}.
437
438/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,1024>}.
440
441/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,2048>}.
443
444/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,4096>}.
446
447/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,8192>}.
449} // namespace [alib::strings]
constexpr TAString(lang::HeapAllocator &pAllocator, TChar *extBuffer, integer extBufferSize)
Definition tastring.inl:271
void SetNull()
Invokes SetBuffer(0).
Definition tastring.inl:664
TAString & Append(const TCharSrc *src, integer srcLength)
Definition tastring.inl:851
TAString< TChar, TAllocator > base
The base AString-type.
TLocalString & operator=(const TLocalString &copy)
TString< TChar > sBase
The base String-type.
constexpr TLocalString(TAllocator &pAllocator)
constexpr integer Length() const
Definition string.inl:318
constexpr const TChar * Buffer() const
Definition string.inl:313
constexpr bool IsNull() const
Definition string.inl:352
#define ALIB_EXPORT
Definition alib.inl:488
#define ALIB_DBG(...)
Definition alib.inl:836
WLocalString< 16 > WString16
Type alias name for TLocalString<wchar,16>.
NLocalString< 128 > NString128
Type alias name for TLocalString<nchar,128>.
LocalString< 512 > String512
Type alias name for TLocalString<character,512>.
NLocalString< 64 > NString64
Type alias name for TLocalString<nchar,64>.
LocalString< 256 > String256
Type alias name for TLocalString<character,256>.
NLocalString< 16 > NString16
Type alias name for TLocalString<nchar,16>.
LocalString< 16 > String16
Type alias name for TLocalString<character,16>.
WLocalString< 512 > WString512
Type alias name for TLocalString<wchar,512>.
LocalString< 4096 > String4K
Type alias name for TLocalString<character,4096>.
LocalString< 8192 > String8K
Type alias name for TLocalString<character,8192>.
NLocalString< 2048 > NString2K
Type alias name for TLocalString<nchar,2048>.
LocalString< 128 > String128
Type alias name for TLocalString<character,128>.
strings::TLocalString< character, TCapacity, lang::HeapAllocator > LocalString
Type alias in namespace alib.
LocalString< 64 > String64
Type alias name for TLocalString<character,64>.
NLocalString< 8 > NString8
Type alias name for TLocalString<nchar,8>.
WLocalString< 8 > WString8
Type alias name for TLocalString<wchar,8>.
strings::TLocalString< nchar, TCapacity, lang::HeapAllocator > NLocalString
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
LocalString< 8 > String8
Type alias name for TLocalString<character,8>.
NLocalString< 1024 > NString1K
Type alias name for TLocalString<nchar,1024>.
LocalString< 32 > String32
Type alias name for TLocalString<character,32>.
WLocalString< 128 > WString128
Type alias name for TLocalString<wchar,128>.
WLocalString< 2048 > WString2K
Type alias name for TLocalString<wchar,2048>.
WLocalString< 32 > WString32
Type alias name for TLocalString<wchar,32>.
LocalString< 1024 > String1K
Type alias name for TLocalString<character,1024>.
strings::TLocalString< xchar, TCapacity, lang::HeapAllocator > XLocalString
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
NLocalString< 512 > NString512
Type alias name for TLocalString<nchar,512>.
NLocalString< 256 > NString256
Type alias name for TLocalString<nchar,256>.
strings::TLocalString< strangeChar, TCapacity, lang::HeapAllocator > StrangeLocalString
Type alias in namespace alib.
strings::TLocalString< complementChar, TCapacity, lang::HeapAllocator > ComplementLocalString
Type alias in namespace alib.
NLocalString< 4096 > NString4K
Type alias name for TLocalString<nchar,8192>.
WLocalString< 1024 > WString1K
Type alias name for TLocalString<wchar,1024>.
strings::TLocalString< wchar, TCapacity, lang::HeapAllocator > WLocalString
Type alias in namespace alib.
WLocalString< 64 > WString64
Type alias name for TLocalString<wchar,64>.
NLocalString< 32 > NString32
Type alias name for TLocalString<nchar,32>.
LocalString< 2048 > String2K
Type alias name for TLocalString<character,2048>.
WLocalString< 8192 > WString8K
Type alias name for TLocalString<wchar,8192>.
WLocalString< 4096 > WString4K
Type alias name for TLocalString<wchar,4096>.
NLocalString< 8192 > NString8K
Type alias name for TLocalString<nchar,4096>.
WLocalString< 256 > WString256
Type alias name for TLocalString<wchar,256>.
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.