ALib C++ Library
Library Version: 2402 R1
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
11#if !defined (HPP_ALIB_STRINGS_ASTRING)
13#endif
14
15namespace alib { namespace strings {
16
17 /** ************************************************************************************************
18 * This type specializes class \b AString by adding a character array member of templated size
19 * which is used as the "external" string buffer. The single motivation for the use of this
20 * class is performance optimization.
21 *
22 * Assembled character strings often have a rather short lifetime and are disposed quickly
23 * after their usage. If the maximum length of the assembled string is known, such strings
24 * may be created as local values of this type, with that maximum length given as template
25 * parameter \p{TCapacity}. With that, the allocation and de-allocation of heap memory for the
26 * string is omitted and allocation and destruction of the local string is performed in constant
27 * time O(1).
28 *
29 * But even if an instance of this class is allocated on the heap (using keyword \c new), still some
30 * performance improvements apply, because besides the class object itself, no second memory
31 * allocation for the string buffer is performed.
32 *
33 * If during the use of an instance of this class the buffer's fixed \p{TCapacity} is exceeded,
34 * a new buffer is allocated from the heap, just as it is always done with growing strings managed
35 * in \b AString objects. This means, the use of this class is safe and no restrictions apply.
36 * Of-course, if possible, for performance critical code sections, the predefined size \p{TCapacity}
37 * should be chosen large enough to allow the internal buffer to survive the use.<br>
38 * With debug builds of \alib, parent class #AString optionally and by default raises a
39 * \ref alib::lang::Report::DoReport "warning" if an external buffer is replaced by a
40 * new (heap) allocation. (Note that from an \b %AString perspective, this class's internal
41 * field \b buffer is an external character array). With use cases that do not allow to foresee a
42 * maximum buffer size, the warning has to be disabled by invoking
43 * \alib{strings::TAString;DbgDisableBufferReplacementWarning}.
44 *
45 * For more information on warnings, see \alib{strings;TAString::SetBuffer;AString::SetBuffer}.
46 *
47 * \note
48 * For commonly used sizes and character types, some convenient typedefs exists in namespace alib.
49 * Type definitions are:
50 * - #String8, #String16, #String32 ... #String1K, #String2K, #String4K,
51 * - #NString8, #NString16, #NString32 ... #NString1K, #NString2K, #NString4K, and
52 * - #WString8, #WString16, #WString32 ... #WString1K, #WString2K, #WString4K.
53 *
54 * @tparam TChar The character type.<br>
55 * Alias names for specializations of this class using character types
56 * \alib{characters;character}, \alib{characters;nchar}, \alib{characters;wchar},
57 * \alib{characters;xchar}, \alib{characters;complementChar} and \alib{characters;strangeChar}
58 * are provided in namespace #alib with type 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 **************************************************************************************************/
64 template <typename TChar, integer TCapacity>
65 class TLocalString : public TAString<TChar>
66 {
67 // #############################################################################################
68 // protected fields
69 // #############################################################################################
70 protected:
71 /** The internal buffer with size specified by the template parameter \p{TCapacity}.
72 * Passed as an external buffer to parent class \b AString.*/
73 TChar localBuffer[TCapacity];
74
75 // #############################################################################################
76 // Constructors/Destructor
77 // #############################################################################################
78 public:
79 /** ****************************************************************************************
80 * Constructs an empty \b %LocalString. Inherited field \alib{strings::TString;buffer}
81 * will be set as an the external buffer of parent \b AString.
82 * Unlike all other \alib string classes, objects of this type are not \e nulled
83 * after default construction.
84 ******************************************************************************************/
85 constexpr
87 : TAString<TChar>( localBuffer, TCapacity )
88 , localBuffer {}
89 {}
90
91 /** ****************************************************************************************
92 * Copy constructor. Copies the string data of parameter \p{copy} to this instance
93 * @param copy The object to copy the contents from.
94 ******************************************************************************************/
101
102 /** ****************************************************************************************
103 * Move constructor.
104 * See \ref alib_ns_strings_astring_copymove "Copy/Move Constructor and Assignment"
105 * for details.
106 * @param move The object to move.
107 ******************************************************************************************/
108 TLocalString(TLocalString&& move) noexcept
109 : TAString<TChar>( localBuffer, TCapacity )
110 {
111 // given move object has external buffer: we have to copy
112 if ( !move.HasInternalBuffer() )
113 {
116 return;
117 }
118
119 // copy values
123
124 // clean moved object (buffer does not need to be nulled as AString destructor
125 // checks capacity > 0 )
126 move.capacity= 0;
127
128 // in debug mode, more copying and more destructor prevention is needed
129#if ALIB_DEBUG
131#if ALIB_DEBUG_STRINGS
133 move.buffer = nullptr;
134 move.length = 0;
135#endif
136#endif
137 }
138
139 /** ****************************************************************************************
140 * Copy assign operator.
141 * Copies the contents of the given object \p{copy}.
142 *
143 * @param copy The object to copy the contents from.
144 * @return \c *this to allow concatenated calls.
145 ******************************************************************************************/
147 {
148 if ( copy.IsNull())
149 {
151 return *this;
152 }
154 return static_cast<TLocalString&>( TAString<TChar>::template Append<false>( copy.Buffer(), copy.Length() ) );
155 }
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.
163 ******************************************************************************************/
165 {
166 if( move.IsNull() )
167 {
170 return *this;
171 }
172
173 // copy if move has its local buffer or this has lost its local buffer
174 if ( !move.HasInternalBuffer() || TAString<TChar>::HasInternalBuffer() )
175 {
177 return *this;
178 }
179
180 // copy other buffer from moved object
184
185 // clean moved object (buffer does not need to be nulled)
186 move.capacity= 0;
187
188 // in debug mode, more copying and more destructor prevention is needed
189#if ALIB_DEBUG
191#if ALIB_DEBUG_STRINGS
193 move.buffer = nullptr;
194 move.length = 0;
195#endif
196#endif
197 return *this;
198 }
199
200 /** ****************************************************************************************
201 * Constructs the object and uses parent's method \b %Append to create a string
202 * representation of given "appendable" object \p{src}.
203 *
204 * \see
205 * Manual chapter \ref alib_strings_assembly_ttostring for more information
206 * about which types are supported and how external, user defined types can be made
207 * compatible to this implicit constructor.
208 *
209 * @tparam TAppendable The type of parameter \p{src} that has a specialization of
210 * functor \alib{strings;T_Append}.
211 * @param src The source to take the buffer and length from of template type T.
212 ******************************************************************************************/
213 template <class TAppendable>
214 TLocalString (const TAppendable& src )
215 : TAString<TChar>( localBuffer, TCapacity )
216 {
218 }
219 }; // class TLocalString
220
221
222#if ALIB_DEBUG
223 /** ************************************************************************************************
224 * This simple specialization of \alib{strings;TLocalString} disables the warning about
225 * replacements of the internal buffer in debug-compilations. This may be used in situations,
226 * where it is not possible to disable this warning after construction, for example if a local
227 * string is <em>emplaced</em> in a container and extensions of it's local capacity are well
228 * accepted (for a minority of the emplaced strings).
229 *
230 * In release compilations, this type does not exist, but is replaced by a simple using
231 * statement.
232 *
233 * @tparam TChar The character type.
234 * @tparam TCapacity The capacity of local, embedded string buffer.
235 **************************************************************************************************/
236 template <typename TChar, integer TCapacity>
237 struct TLocalStringNoWarning : public TLocalString<TChar,TCapacity>
238 {
239 /**
240 * Default constructor.
241 */
242 constexpr
248
249 /**
250 * Constructor taking a string to copy.
251 * @param src The string to copy into this object.
252 */
254 : TLocalString<TChar,TCapacity>()
255 {
257 TAString<TChar>::Append( src.Buffer(), src.Length() );
258 }
259 };
260#else
261 template <typename TChar, integer TCapacity>
262 using TLocalStringNoWarning = TLocalString<TChar, TCapacity>;
263#endif
264
265}} // namespace [alib::strings]
266
267#endif // HPP_ALIB_STRINGS_LOCALSTRING
bool dbgWarnWhenExternalBufferIsReplaced
Definition astring.hpp:340
TAString & Append(const TCharSrc *src, integer srcLength)
TLocalString & operator=(const TLocalString &copy)
TLocalString(const TLocalString &copy)
TLocalString(TLocalString &&move) noexcept
TLocalString(const TAppendable &src)
TChar localBuffer[TCapacity]
constexpr bool IsNull() const
Definition string.hpp:395
const TChar * buffer
Definition string.hpp:129
constexpr integer Length() const
Definition string.hpp:357
constexpr const TChar * Buffer() const
Definition string.hpp:350
#define ALIB_DBG(...)
Definition alib.hpp:457
Definition alib.cpp:57