ALib C++ Library
Library Version: 2510 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::StdContainerAllocator<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
68 /// Constructor.
69 /// @param pAllocator The allocator to use.
71 : vectorBase(pAllocator) {}
72
73 /// Destructor.
74 ~TStringVector() = default;
75
76 /// Returns the allocator provided with construction. If this was \e nulled, then this method
77 /// can be used to set an external (or new) allocator using the assignment operator.
78 /// Has to be set before the first insertion of data.
79 ///
80 /// @return A reference to the internal allocator.
82 { return vectorBase::get_allocator().GetAllocator(); }
83
84 /// Returns the size of this vector as \alib{integer}.
85 /// @return The number of strings stored.
86 integer Size() const noexcept
87 { return integer(vectorBase::size()); }
88
89 /// Adds a string to the end of the list of strings.
90 /// @param src Source string to be copied.
91 /// @return The index of the created string in this vector.
93 {
94 vectorBase::emplace_back( String( GetAllocator(), src ) );
95 return integer(vectorBase::size()) - 1;
96 }
97
98 /// Receives the string at a valid \p{idx}. If the index is out of bounds, a nulled string
99 /// is returned.
100 /// @param idx The index to try.
101 /// @return The string stored at \p{idx}, if available.
103 {
104 if (idx >= 0 && idx < Size())
105 return vectorBase::at(size_t(idx));
106
107 return NULL_STRING;
108 }
109
110}; // class TStringVector
111
112} // namespace alib[::strings::util]
113
117
118#if ALIB_MONOMEM
119 using StringVectorMA= strings::util::TStringVector<character, MonoAllocator>; ///< Type alias in namespace \b alib.
120 using NStringVectorMA= strings::util::TStringVector<nchar , MonoAllocator>; ///< Type alias in namespace \b alib.
121 using WStringVectorMA= strings::util::TStringVector<wchar , MonoAllocator>; ///< Type alias in namespace \b alib.
122 using StringVectorPA= strings::util::TStringVector<character, PoolAllocator>; ///< Type alias in namespace \b alib.
123 using NStringVectorPA= strings::util::TStringVector<nchar , PoolAllocator>; ///< Type alias in namespace \b alib.
124 using WStringVectorPA= strings::util::TStringVector<wchar , PoolAllocator>; ///< Type alias in namespace \b alib.
125#endif
126
127} // namespace [alib]
128
129#endif // ALIB_STRINGS
130#endif // H_ALIB_STRINGS_VECTOR
131
132
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::StdContainerAllocator< 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:2463
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:2381