ALib C++ Library
Library Version: 2412 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/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_STRINGS_LOCALSTRING
9#define HPP_ALIB_STRINGS_LOCALSTRING 1
10#pragma once
12
13namespace alib { namespace strings {
14
15//==================================================================================================
16/// This type specializes class \b AString by adding a character array member of templated size
17/// which is used as the "external" string buffer. The single motivation for the use of this
18/// class is performance optimization.
19///
20/// Assembled character strings often have a rather short lifetime and are disposed quickly
21/// after their usage. If the maximum length of the assembled string is known, such strings
22/// may be created as local values of this type, with that maximum length given as template
23/// parameter \p{TCapacity}. With that, the allocation and de-allocation of heap memory
24/// (or whatever is used with the provided allocator) for the string is omitted and allocation
25/// and destruction of the local string is performed in constant time O(1).
26///
27/// But even if an instance of this class is allocated on the in an allocator instead of
28/// being a local variable on the stack, still some performance improvements apply, because
29/// besides the class object itself, no second memory allocation for the string buffer is
30/// performed.
31///
32/// If during the use of an instance of this class the buffer's fixed \p{TCapacity} is exceeded,
33/// a new buffer is allocated from the given allocator, just as it is always done with growing
34/// strings managed in \b AString objects. This means, the use of this class is safe and no
35/// restrictions apply.
36/// Of course, if possible, for performance-critical code sections, the predefined size
37/// \p{TCapacity} should be chosen large enough to allow the internal buffer to survive
38/// the use.<br>
39/// With debug-builds of \alib, parent class #AString optionally and by default raises a
40/// \ref alib::lang::Report::DoReport "warning" if an external buffer is replaced by a
41/// new (heap) allocation. (Note that from an \b %AString perspective, this class's internal
42/// field \b buffer is an external character array). With use cases that do not allow to foresee
43/// a maximum buffer size, the warning has to be disabled by invoking
44/// \alib{strings::TAString;DbgDisableBufferReplacementWarning}.
45///
46/// For more information on warnings, see \alib{strings;TAString::SetBuffer;AString::SetBuffer}.
47///
48/// \note
49/// For commonly used sizes and character types, some convenient typedefs exists in namespace
50/// alib, which use \alib{lang;HeapAllocator}.
51/// These type definitions are:
52/// - #String8, #String16, #String32 ... #String1K, #String2K, #String4K,
53/// - #NString8, #NString16, #NString32 ... #NString1K, #NString2K, #NString4K, and
54/// - #WString8, #WString16, #WString32 ... #WString1K, #WString2K, #WString4K.
55///
56/// @tparam TChar The \ref alib_characters_chars "character type" of this string.
57/// Alias names for specializations of this class are provided in namespace #alib with type
58/// definitions \b LocalString<TCapacity>,
59/// \b NLocalString<TCapacity>, \b WLocalString<TCapacity>,
60/// \b XLocalString<TCapacity>, \b ComplementLocalString<TCapacity> and
61/// \b StrangeLocalString<TCapacity>.
62/// @tparam TCapacity The capacity of the string buffer allocated locally "inside" the class.
63/// @tparam TAllocator The allocator type to use. Defaults to \alib{lang;HeapAllocator}.
64//==================================================================================================
65template <typename TChar, integer TCapacity, typename TAllocator= lang::HeapAllocator>
66class TLocalString : public TAString<TChar, TAllocator>
67{
68 // ###############################################################################################
69 // protected fields
70 // ###############################################################################################
71 protected:
72 /// The base AString-type.
74
75 /// The base String-type.
77
78 /// The internal buffer with size specified by the template parameter \p{TCapacity}.
79 /// Passed as an external buffer to parent class \b AString.
80 TChar localBuffer[TCapacity];
81
82 // ###############################################################################################
83 // Constructors/Destructor
84 // ###############################################################################################
85 public:
86 //==============================================================================================
87 /// Constructs an empty \b %LocalString. Inherited field \alib{strings::TString;buffer}
88 /// will be set as an the external buffer of parent \b AString.
89 /// Unlike all other \alib string classes, objects of this type are not \e nulled
90 /// after default construction.
91 /// @param pAllocator The allocator to use.
92 //==============================================================================================
93 constexpr
94 TLocalString(TAllocator& pAllocator)
95 : base( pAllocator, localBuffer, TCapacity )
96 , localBuffer {}
97 {}
98
99 //==============================================================================================
100 /// Constructs an empty \b %LocalString. Inherited field \alib{strings::TString;buffer}
101 /// will be set as an the external buffer of parent \b AString.
102 /// Unlike all other \alib string classes, objects of this type are not \e nulled
103 /// after default construction.
104 //==============================================================================================
105 constexpr
107 : base( localBuffer, TCapacity )
108 , localBuffer {}
109 {}
110
111 //==============================================================================================
112 /// Copy constructor. Copies the string data of parameter \p{copy} to this instance
113 /// @param copy The object to copy the contents from.
114 //==============================================================================================
121
122 //==============================================================================================
123 /// Move constructor.
124 /// See \ref alib_ns_strings_astring_copymove "Copy/Move Constructor and Assignment"
125 /// for details.
126 /// @param move The object to move.
127 //==============================================================================================
128 TLocalString(TLocalString&& move) noexcept
129 : base( localBuffer, TCapacity )
130 {
131 // given move object has external buffer: we have to copy
132 if ( !move.HasInternalBuffer() )
133 {
134 ALIB_DBG( base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced; )
135 base::Append( move );
136 return;
137 }
138
139 // copy values
140 sBase::buffer = move.buffer;
141 sBase::length = move.length;
142 base::capacity= move.capacity;
143
144 // clean moved object (buffer does not need to be nulled as AString destructor
145 // checks capacity > 0 )
146 move.capacity= 0;
147
148 // in debug mode, more copying and more destructor prevention is needed
149#if ALIB_DEBUG
150 base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced;
151#if ALIB_DEBUG_STRINGS
152 base::debugLastAllocRequest = move.debugLastAllocRequest;
153 move.buffer = nullptr;
154 move.length = 0;
155#endif
156#endif
157 }
158
159 //==============================================================================================
160 /// Copy assign operator.
161 /// Copies the contents of the given object \p{copy}.
162 ///
163 /// @param copy The object to copy the contents from.
164 /// @return \c *this to allow concatenated calls.
165 //==============================================================================================
167 {
168 if ( copy.IsNull())
169 {
171 return *this;
172 }
173 base::Reset();
174 return static_cast<TLocalString&>( base::template Append<NC>( copy.Buffer(), copy.Length() ) );
175 }
176
177 //==============================================================================================
178 /// Move assign operator.
179 /// Copies the contents of the given object \p{copy}.
180 ///
181 /// @param move The object to move the contents from.
182 /// @return \c *this to allow concatenated calls.
183 //==============================================================================================
185 {
186 if( move.IsNull() )
187 {
188 ALIB_DBG( base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced; )
190 return *this;
191 }
192
193 // copy if move has its local buffer or this has lost its local buffer
194 if ( !move.HasInternalBuffer() || base::HasInternalBuffer() )
195 {
196 base::Reset( move );
197 return *this;
198 }
199
200 // copy other buffer from moved object
201 sBase::buffer = move.buffer;
202 sBase::length = move.length;
203 base::capacity= move.capacity;
204
205 // clean moved object (buffer does not need to be nulled)
206 move.capacity= 0;
207
208 // in debug mode, more copying and more destructor prevention is needed
209#if ALIB_DEBUG
210 base::dbgWarnWhenExternalBufferIsReplaced= move.dbgWarnWhenExternalBufferIsReplaced;
211#if ALIB_DEBUG_STRINGS
212 base::debugLastAllocRequest = move.debugLastAllocRequest;
213 move.buffer = nullptr;
214 move.length = 0;
215#endif
216#endif
217 return *this;
218 }
219
220 //==============================================================================================
221 /// Constructs this instance and invokes parent's method \b %Append to create a string
222 /// representation of given "appendable" object \p{src}.
223 ///
224 /// \see
225 /// Manual chapter \ref alib_strings_assembly_ttostring for more information
226 /// about which types are supported and how external, user-defined types can be made
227 /// compatible to this implicit constructor.
228 ///
229 /// @tparam TAppendable The type of parameter \p{src} that has a specialization of
230 /// functor \alib{strings;T_Append}.
231 /// @param src The source to take the buffer and length from of template type T.
232 //==============================================================================================
233 template <class TAppendable>
234 TLocalString (const TAppendable& src )
235 : base( localBuffer, TCapacity )
236 {
237 base::Append( src );
238 }
239}; // class TLocalString
240
241
242#if ALIB_DEBUG
243//==================================================================================================
244/// This simple specialization of \alib{strings;TLocalString} disables the warning about
245/// replacements of the internal buffer in debug-compilations. This may be used in situations,
246/// where it is not possible to disable this warning after construction, for example if a local
247/// string is <em>emplaced</em> in a container and extensions of it's local capacity are well
248/// accepted (for a minority of the emplaced strings).
249///
250/// In release compilations, this type does not exist, but is replaced by a simple using
251/// statement.
252///
253/// @tparam TChar The character type.
254/// @tparam TCapacity The capacity of local, embedded string buffer.
255/// @tparam TAllocator The allocator to use.
256//==================================================================================================
257template <typename TChar, integer TCapacity, typename TAllocator>
258struct TLocalStringNoWarning : public TLocalString<TChar,TCapacity,TAllocator>
259{
260 /// Default constructor.
261 constexpr
267
268 /// Constructor taking a string to copy.
269 /// @param src The string to copy into this object.
276}; // TLocalStringNoWarning
277#else
278 template <typename TChar, integer TCapacity, typename TAllocator>
279 using TLocalStringNoWarning = TLocalString<TChar, TCapacity, TAllocator>;
280#endif
281
282}} // namespace [alib::strings]
283
284#endif // HPP_ALIB_STRINGS_LOCALSTRING
285
bool dbgWarnWhenExternalBufferIsReplaced
Definition tastring.inl:348
bool HasInternalBuffer() const
Definition tastring.inl:793
TAString & Append(const TCharSrc *src, integer srcLength)
void SetNull()
Invokes SetBuffer(0).
Definition tastring.inl:801
TLocalString & operator=(const TLocalString &copy)
TLocalString(const TLocalString &copy)
TAString< TChar, TAllocator > base
The base AString-type.
constexpr TLocalString(TAllocator &pAllocator)
TLocalString(TLocalString &&move) noexcept
TLocalString(const TAppendable &src)
TChar localBuffer[TCapacity]
constexpr bool IsNull() const
Definition string.hpp:364
const TChar * buffer
Definition string.hpp:117
constexpr integer Length() const
Definition string.hpp:326
constexpr const TChar * Buffer() const
Definition string.hpp:319
#define ALIB_DBG(...)
Definition alib.hpp:390
Definition alib.cpp:69
constexpr TLocalStringNoWarning()
Default constructor.