ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
mastring.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_monomem 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_MONOMEM_MASTRING
9#define HPP_ALIB_MONOMEM_MASTRING
10
11#if !defined (HPP_ALIB_MONOMEM_MONOALLOCATOR)
13#endif
14
15#if ALIB_STRINGS
16
17#if !defined (HPP_ALIB_STRINGS_LOCALSTRING)
19#endif
20
21
22namespace alib { namespace monomem {
23
24/** ************************************************************************************************
25 * The name of this class stands for "monotonically allocated string".
26 * Consequently, it publicly derives from \alib{strings;TAString;AString} and provides some
27 * special constructors that perform an initial allocation using a given
28 * \alib{monomem;MonoAllocator}. The constructors allow to specify an (additional)
29 * capacity to allocate, besides what is needed when copying the construction parameter.
30 *
31 * If the string exceeds this initial capacity, dynamic memory is allocated, just likewise
32 * class \alib{strings;TLocalString;LocalString} does, in the case its stack-memory is exceeded.
33 *
34 * If it is assured that no dynamic allocation was performed during the life-cycle of an instance,
35 * the instance does not need to be destructed. In this case, the instance itself might be allocated
36 * in the same \alib{monomem;MonoAllocator} (without performing additional precautions for proper
37 * destruction).
38 *
39 * @tparam TChar The character type.<br>
40 * Alias names for specializations of this class using character types
41 * \alib{characters;character}, \alib{characters;nchar}, \alib{characters;wchar},
42 * \alib{characters;xchar}, \alib{characters;complementChar} and \alib{characters;strangeChar}
43 * are provided in namespace #alib with type definitions \alib{MAString}, \alib{NMAString},
44 * \alib{WMAString}, \alib{XMAString}, \alib{ComplementMAString} and \alib{StrangeMAString}.
45 **************************************************************************************************/
46template<typename TChar>
47class TMAString : public strings::TAString<TChar>
48{
49 public:
50
51 /**
52 * Allocates a scope string of given capacity.
53 * @param allocator The monotonic allocator to take the initial memory from.
54 * @param pCapacity The capacity of the of the string.
55 */
56 TMAString( MonoAllocator& allocator, integer pCapacity )
57 : strings::TAString<TChar>( allocator.AllocArray<TChar>( static_cast<size_t>(pCapacity + 1) ) ,
58 pCapacity + 1 )
59 {}
60
61 /**
62 * Allocates a scope string of given capacity plus the length of the string \p{src} and copies
63 * \p{source} to it.
64 * @param allocator The monotonic allocator to take the initial memory from.
65 * @param src The source string to copy.
66 * @param additionalCapacity The extra capacity to allocate.
67 */
68 TMAString( MonoAllocator& allocator, const strings::TString<TChar>& src, integer additionalCapacity )
69 : strings::TAString<TChar>( allocator.AllocArray<TChar>( src.Length() + additionalCapacity + 1),
70 src.Length() + additionalCapacity + 1 )
71 {
73 }
74
75 /**
76 * Returns a string with contents of the given \p{initialValue} written at the start.
77 * and the given additional capacity.<br>
78 * Internally, a \alib{strings;TLocalString;LocalString<1024>} is used to first convert
79 * \p{initialValue} to a string, which is then copied to this object.
80 * @tparam TInitialValue The type of the initial object to write into the string.
81 * @param allocator The monotonic allocator to take the initial memory from.
82 * @param initialValue A boxed object to write to the start of the string.
83 * @param additionalCapacity Additional capacity to allocate in \p{scope}.
84 */
85 template<typename TInitialValue>
86 TMAString( MonoAllocator& allocator, const TInitialValue& initialValue, integer additionalCapacity)
87 : strings::TAString<TChar>()
88 {
91 tmp << initialValue;
92
93 strings::TAString<TChar>::SetBuffer( allocator.AllocArray<TChar>( tmp.Length() + additionalCapacity + 1 ),
94 tmp.Length() + additionalCapacity + 1 ,
96
98 }
99};
100
101
102} // namespace alib[::monomem]
103
104/// Type alias in namespace \b alib.
106
107/// Type alias in namespace \b alib.
109
110/// Type alias in namespace \b alib.
112
113/// Type alias in namespace \b alib.
115
116/// Type alias in namespace \b alib.
118
119/// Type alias in namespace \b alib.
121
122} // namespace [alib]
123
124
125// set character array type traits for TMAString
126#if !defined(ALIB_DOX)
127
128namespace alib::characters {
129template<typename TChar>
130struct T_CharArray<monomem::TMAString<TChar>, TChar>
131{
132 static constexpr AccessType Access = AccessType::Implicit;
133 static constexpr const TChar* Buffer(const monomem::TMAString<TChar>& src) { return src.Buffer(); }
134 static constexpr integer Length(const monomem::TMAString<TChar>& src) { return src.Length(); }
135
137};
138} // namespace [alib::characters]
139
140#endif // !defined(ALIB_DOX)
141
142
143#endif // ALIB_STRINGS
144#endif // HPP_ALIB_MONOMEM_MASTRING
ALIB_FORCE_INLINE T * AllocArray(TSize length)
TMAString(MonoAllocator &allocator, const strings::TString< TChar > &src, integer additionalCapacity)
Definition mastring.hpp:68
TMAString(MonoAllocator &allocator, const TInitialValue &initialValue, integer additionalCapacity)
Definition mastring.hpp:86
TMAString(MonoAllocator &allocator, integer pCapacity)
Definition mastring.hpp:56
void DbgDisableBufferReplacementWarning()
Definition astring.hpp:353
ALIB_API void SetBuffer(integer newCapacity)
Definition astring.cpp:107
constexpr integer Length() const
Definition string.hpp:357
integer CopyTo(TChar *dest) const
Definition string.hpp:1974
@ KeepWithSender
Keeps responsibility, e.g. when passing an object.
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
static const TChar * Buffer(const TString &src)
static constexpr ConstructionType Construction
static constexpr AccessType Access
static integer Length(const TString &src)