ALib C++ Library
Library Version: 2511 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 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 \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 /// Constructs an empty \b %LocalString. Inherited field \alib{strings::TString;buffer}
84 /// will be set as an the external buffer of parent \b AString.
85 /// Unlike all other \alib string classes, objects of this type are not \e nulled
86 /// after default construction.
87 /// @param pAllocator The allocator to use.
88 constexpr
89 TLocalString(TAllocator& pAllocator)
90 : base( pAllocator, localBuffer, TCapacity )
91 , localBuffer {} {}
92
93 /// Constructs an empty \b %LocalString. Inherited field \alib{strings::TString;buffer}
94 /// will be set as an the external buffer of parent \b AString.
95 /// Unlike all other \alib string classes, objects of this type are not \e nulled
96 /// after default construction.
97 constexpr
99 : base( localBuffer, TCapacity )
100 , localBuffer {} {}
101
102 /// Copy constructor. Copies the string data of parameter \p{copy} to this instance
103 /// @param copy The object to copy the contents from.
110
111 /// Move constructor.
112 /// See \ref alib_ns_strings_astring_copymove "Copy/Move Constructor and Assignment"
113 /// for details.
114 /// @param move The object to move.
115 TLocalString(TLocalString&& move) noexcept
116 : base( localBuffer, TCapacity ) {
117 // given move object has external buffer: we have to copy
118 if ( !move.HasInternalBuffer() ) {
119 ALIB_DBG( base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced; )
120 base::Append( move );
121 return;
122 }
123
124 // copy values
125 sBase::buffer = move.buffer;
126 sBase::length = move.length;
127 base::capacity= move.capacity;
128
129 // clean moved object (buffer does not need to be nulled as AString destructor
130 // checks capacity > 0 )
131 move.capacity= 0;
132
133 // in debug mode, more copying and more destructor prevention is needed
134 #if ALIB_DEBUG
135 base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced;
136 #if ALIB_DEBUG_STRINGS
137 base::debugLastAllocRequest = move.debugLastAllocRequest;
138 move.buffer = nullptr;
139 move.length = 0;
140 #endif
141 #endif
142 }
143
144 /// Copy assign operator.
145 /// Copies the contents of the given object \p{copy}.
146 ///
147 /// @param copy The object to copy the contents from.
148 /// @return \c *this to allow concatenated calls.
150 if ( copy.IsNull()) {
152 return *this;
153 }
154 base::Reset();
155 return static_cast<TLocalString&>( base::template Append<NC>( copy.Buffer(), copy.Length() ) );
156 }
157
158 /// Move assign operator.
159 /// Copies the contents of the given object \p{copy}.
160 ///
161 /// @param move The object to move the contents from.
162 /// @return \c *this to allow concatenated calls.
164 if( move.IsNull() ) {
165 ALIB_DBG( base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced; )
167 return *this;
168 }
169
170 // copy if move has its local buffer or this has lost its local buffer
171 if ( !move.HasInternalBuffer() || base::HasInternalBuffer() ) {
172 base::Reset( move );
173 return *this;
174 }
175
176 // copy other buffer from moved object
177 sBase::buffer = move.buffer;
178 sBase::length = move.length;
179 base::capacity= move.capacity;
180
181 // clean moved object (buffer does not need to be nulled)
182 move.capacity= 0;
183
184 // in debug mode, more copying and more destructor prevention is needed
185 #if ALIB_DEBUG
186 base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced;
187 #if ALIB_DEBUG_STRINGS
188 base::debugLastAllocRequest = move.debugLastAllocRequest;
189 move.buffer = nullptr;
190 move.length = 0;
191 #endif
192 #endif
193 return *this;
194 }
195
196 /// Constructs this instance and invokes parent's method \b %Append to create a string
197 /// representation of the given "appendable" object \p{src}.
198 ///
199 /// \see
200 /// Manual chapter \ref alib_strings_assembly_ttostring for more information
201 /// about which types are supported and how external, user-defined types can be made
202 /// compatible to this implicit constructor.
203 ///
204 /// @tparam TAppendable The type of parameter \p{src} that has a specialization of
205 /// functor \alib{strings;AppendableTraits}.
206 /// @param src The source to take the buffer and length from of template type T.
207 template <typename TAppendable>
208 TLocalString (const TAppendable& src )
209 : base( localBuffer, TCapacity ) { base::Append( src ); }
210
211
212 /// Assign operator.
213 /// Invokes inherited method \alib{strings::TAString;Reset(const TAppendable&)}.
214 ///
215 /// @tparam TAppendable The type of parameter \p{source}.
216 /// @param src The source of type \p{TAppendable} to append.
217 ///
218 /// @return \c *this to allow concatenated calls.
219 template <typename TAppendable>
220 TLocalString& operator= (const TAppendable& src ) { base::Reset( src ); return *this; }
221
222}; // class TLocalString
223
224//##################################################################################################
225// Specializations of ArrayTraits for this class String
226//##################################################################################################
227} namespace characters {
228#if !DOXYGEN
229template<typename TChar, integer TCapacity, typename TAllocator>
230struct ArrayTraits<strings::TLocalString<TChar,TCapacity,TAllocator>, TChar>
231{
232 using T= strings::TLocalString<TChar,TCapacity,TAllocator>;
233 static constexpr Policy Access = Policy::Implicit;
234 static constexpr Policy Construction = Policy::NONE;
235 static constexpr const TChar* Buffer(const T& src) { return src.Buffer(); }
236 static constexpr integer Length(const T& src) { return src.Length(); }
237};
238
239template<typename TChar, integer TCapacity, typename TAllocator>
240struct ZTArrayTraits<strings::TLocalString<TChar,TCapacity,TAllocator>, TChar>
241{
242 using T= strings::TLocalString<TChar,TCapacity,TAllocator>;
243 static constexpr Policy Access = Policy::Implicit;
244 static constexpr Policy Construction = Policy::NONE;
245 static constexpr const TChar* Buffer(const T& src) { return src.Terminate(); }
246 static constexpr integer Length(const T& src) { return src.Length(); }
247};
248#endif // !DOXYGEN
249
250
251} namespace strings {
252
253#if ALIB_DEBUG
254//==================================================================================================
255/// This simple specialization of \alib{strings;TLocalString} disables the warning about
256/// replacements of the internal buffer in debug-compilations. This may be used in situations,
257/// where it is not possible to disable this warning after construction, for example, if a local
258/// string is <em>emplaced</em> in a container and extensions of it's local capacity are well
259/// accepted (for a minority of the emplaced strings).
260///
261/// In release compilations, this type does not exist, but is replaced by a simple using
262/// statement.
263///
264/// @tparam TChar The character type.
265/// @tparam TCapacity The capacity of local, embedded string buffer.
266/// @tparam TAllocator The allocator to use.
267//==================================================================================================
268template <typename TChar, integer TCapacity, typename TAllocator>
269struct TLocalStringNoWarning : public TLocalString<TChar,TCapacity,TAllocator>
270{
271 /// Default constructor.
272 constexpr
276
277 /// Constructor taking a string to copy.
278 /// @param src The string to copy into this object.
285}; // TLocalStringNoWarning
286#else
287 template <typename TChar, integer TCapacity, typename TAllocator>
288 using TLocalStringNoWarning = TLocalString<TChar, TCapacity, TAllocator>;
289#endif
290
291}
292
293/// Type alias in namespace \b alib.
294template<integer TCapacity>
296
297/// Type alias in namespace \b alib.
298template<integer TCapacity>
300
301/// Type alias in namespace \b alib.
302template<integer TCapacity>
304
305/// Type alias in namespace \b alib.
306template<integer TCapacity>
307using NLocalString = strings::TLocalString <nchar , TCapacity, lang::HeapAllocator>;
308
309/// Type alias in namespace \b alib.
310template<integer TCapacity>
311using WLocalString = strings::TLocalString <wchar , TCapacity, lang::HeapAllocator>;
312
313/// Type alias in namespace \b alib.
314template<integer TCapacity>
315using XLocalString = strings::TLocalString <xchar , TCapacity, lang::HeapAllocator>;
316
317/// Type alias name for \alib{strings;TLocalString;TLocalString<character,8>}.
319
320/// Type alias name for \alib{strings;TLocalString;TLocalString<character,16>}.
322
323/// Type alias name for \alib{strings;TLocalString;TLocalString<character,32>}.
325
326/// Type alias name for \alib{strings;TLocalString;TLocalString<character,64>}.
328
329/// Type alias name for \alib{strings;TLocalString;TLocalString<character,128>}.
331
332/// Type alias name for \alib{strings;TLocalString;TLocalString<character,256>}.
334
335/// Type alias name for \alib{strings;TLocalString;TLocalString<character,512>}.
337
338/// Type alias name for \alib{strings;TLocalString;TLocalString<character,1024>}.
340
341/// Type alias name for \alib{strings;TLocalString;TLocalString<character,2048>}.
343
344/// Type alias name for \alib{strings;TLocalString;TLocalString<character,4096>}.
346
347/// Type alias name for \alib{strings;TLocalString;TLocalString<character,8192>}.
349
350/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,8>}.
352
353/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,16>}.
355
356/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,32>}.
358
359/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,64>}.
361
362/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,128>}.
364
365/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,256>}.
367
368/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,512>}.
370
371/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,1024>}.
373
374/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,2048>}.
376
377/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,8192>}.
379
380/// Type alias name for \alib{strings;TLocalString;TLocalString<nchar,4096>}.
382
383/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,8>}.
385
386/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,16>}.
388
389/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,32>}.
391
392/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,64>}.
394
395/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,128>}.
397
398/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,256>}.
400
401/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,512>}.
403
404/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,1024>}.
406
407/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,2048>}.
409
410/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,4096>}.
412
413/// Type alias name for \alib{strings;TLocalString;TLocalString<wchar,8192>}.
415} // namespace [alib::strings]
constexpr TAString(lang::HeapAllocator &pAllocator, TChar *extBuffer, integer extBufferSize)
Definition tastring.inl:268
TAString & Append(const TCharSrc *src, integer srcLength)
Definition tastring.inl:828
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:316
constexpr const TChar * Buffer() const
Definition string.inl:311
constexpr bool IsNull() const
Definition string.inl:350
#define ALIB_EXPORT
Definition alib.inl:497
#define ALIB_DBG(...)
Definition alib.inl:853
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:2189
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.