ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
ALib.Strings.Vector.H
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef H_ALIB_STRINGS_VECTOR
9#define H_ALIB_STRINGS_VECTOR
10#pragma once
11#ifndef INL_ALIB
12# include "alib/alib.inl"
13#endif
14
15#if ALIB_STRINGS
16#include <vector>
17#include "ALib.Lang.H"
18#include "ALib.Strings.H"
19#include "ALib.Monomem.H"
20
21namespace alib { namespace strings::util {
22
23//==================================================================================================
24/// This is a simple type that publicly inherits container type <c>std::vector</c> to store a
25/// list of \alib strings, using an \alib {lang;Allocator}.
26///
27/// The (current) design of this class could be named "open" or "weak". This is due to the fact
28/// that
29/// a) The interface of the \c std::vector is public by inheritance, and
30/// b) Only a few additional interface methods have been added.
31///
32/// As a consequence, it is up to the user of the type to care about proper allocation and
33/// deallocation of string data: If a string is added using method #Add, its content is copied
34/// to memory allocated with the allocator provided with construction. However, any other string may
35/// be added by using the interface of the \c std::vector.
36///
37/// The typical use case is with class \alib{monomem;TMonoAllocator} or
38/// \alib{monomem;TLocalAllocator} provided with module \alib_monomem:
39/// - Create a Mono- or LocalAllocator
40/// - Create a \alib{StringVectorMA}, passing the allocator.
41/// - Gather some string data (copied or otherwise referenced).
42/// - Pass it over to a function or otherwise use the vector.
43/// - Destruct the objects.
44///
45/// \attention
46/// With other use cases, especially when using type \alib{StringVectorPA} in combination with
47/// class \alib{monomem;TPoolAllocator;PoolAllocator}, it has to be well thought how this type
48/// is used in respect to the need of freeing memory, especially when strings got allocated with
49/// method #Add.
50///
51/// @tparam TAllocator The allocator type, as prototyped with \alib{lang;Allocator}.
52//==================================================================================================
53template<typename TChar, typename TAllocator>
54class TStringVector : public std::vector<TString<TChar>,
55 lang::StdAllocator<TString<TChar>, TAllocator>>
56{
57 public:
58 /// The allocator type that \p{TAllocator} specifies.
59 using AllocatorType = TAllocator;
60
61 protected:
62 /// The vector type that \p{TAllocator} specifies.
63 using vectorBase = std::vector<TString<TChar>,
65
66 public:
67 /// Constructor.
70
71 /// Constructor taking an allocator.
72 /// @param pAllocator The allocator to use.
74 : vectorBase(pAllocator) {}
75
76 /// Destructor.
77 ~TStringVector() =default;
78
79 /// Returns the allocator provided with construction. If this was \e nulled, then this method
80 /// can be used to set an external (or new) allocator using the assignment operator.
81 /// Has to be set before the first insertion of data.
82 ///
83 /// @return A reference to the internal allocator.
85 { return vectorBase::get_allocator().GetAllocator(); }
86
87 /// Returns the size of this vector as \alib{integer}.
88 /// @return The number of strings stored.
89 integer Size() const noexcept { return integer(vectorBase::size()); }
90
91 /// Adds a string to the end of the list of strings.
92 /// @param src Source string to be copied.
93 /// @return The index of the created string in this vector.
95 {
96 vectorBase::emplace_back( TString<TChar>( GetAllocator(), src ) );
97 return integer(vectorBase::size()) - 1;
98 }
99
100 /// Receives the string at a valid \p{idx}. If the index is out of bounds, a nulled string
101 /// is returned.
102 /// @param idx The index to try.
103 /// @return The string stored at \p{idx}, if available.
105 if (idx >= 0 && idx < Size())
106 return vectorBase::at(size_t(idx));
107
108 return NULL_STRING;
109 }
110
111}; // class TStringVector
112
113} // namespace alib[::strings::util]
114
118
119#if ALIB_MONOMEM
120 using StringVectorMA= strings::util::TStringVector<character, MonoAllocator>; ///< Type alias in namespace \b alib.
121 using NStringVectorMA= strings::util::TStringVector<nchar , MonoAllocator>; ///< Type alias in namespace \b alib.
122 using WStringVectorMA= strings::util::TStringVector<wchar , MonoAllocator>; ///< Type alias in namespace \b alib.
123 using StringVectorPA= strings::util::TStringVector<character, PoolAllocator>; ///< Type alias in namespace \b alib.
124 using NStringVectorPA= strings::util::TStringVector<nchar , PoolAllocator>; ///< Type alias in namespace \b alib.
125 using WStringVectorPA= strings::util::TStringVector<wchar , PoolAllocator>; ///< Type alias in namespace \b alib.
126#endif
127
128} // namespace [alib]
129
130#endif // ALIB_STRINGS
131#endif // H_ALIB_STRINGS_VECTOR
integer Add(const strings::TString< TChar > &src)
TAllocator AllocatorType
The allocator type that TAllocator specifies.
AllocatorType & GetAllocator() noexcept
TStringVector(AllocatorType &pAllocator)
~TStringVector()=default
Destructor.
std::vector< TString< TChar >, lang::StdAllocator< TString< TChar >, TAllocator > > vectorBase
The vector type that TAllocator specifies.
strings::util::TStringVector< wchar, MonoAllocator > WStringVectorMA
Type alias in namespace alib.
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.inl:2271
strings::util::TStringVector< wchar, PoolAllocator > WStringVectorPA
Type alias in namespace alib.
strings::util::TStringVector< nchar, MonoAllocator > NStringVectorMA
Type alias in namespace alib.
strings::util::TStringVector< character, MonoAllocator > StringVectorMA
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::util::TStringVector< character, PoolAllocator > StringVectorPA
Type alias in namespace alib.
strings::util::TStringVector< nchar, lang::HeapAllocator > NStringVector
Type alias in namespace alib.
strings::util::TStringVector< character, lang::HeapAllocator > StringVector
Type alias in namespace alib.
strings::util::TStringVector< wchar, lang::HeapAllocator > WStringVector
Type alias in namespace alib.
strings::util::TStringVector< nchar, PoolAllocator > NStringVectorPA
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189