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