ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
strings/util/stringvector.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_files 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_UTIL_STRINGVECTOR
9#define HPP_ALIB_STRINGS_UTIL_STRINGVECTOR 1
10#pragma once
11
14#include <vector>
15namespace alib { namespace strings::util {
16
17//==================================================================================================
18/// This is a simple type that publicly inherits container type <c>std::vector</c> to store a
19/// list of \alib strings, using an \alib {lang;Allocator}.
20///
21/// The (current) design of this class could be named "open" or "weak". This is due to the fact
22/// that
23/// a) The interface of the \c std::vector is public by inheritance, and
24/// b) Only a few additional interface methods have been added.
25///
26/// As a consequence, it is up to the user of the type to care about proper allocation and
27/// deallocation of string data: If a string is added using method #Add, its content is copied
28/// to memory allocated with the allocator provided with construction. However, any other string may
29/// be added by using the interface of the \c std::vector.
30///
31/// The typical use case is with class \alib{monomem;TMonoAllocator} or
32/// \alib{monomem;TLocalAllocator} provided with module \alib_monomem:
33/// - Create a Mono- or LocalAllocator
34/// - Create a \alib{StringVectorMA}, passing the allocator.
35/// - Gather some string data (copied or otherwise referenced).
36/// - Pass it over to a function or otherwise use the vector.
37/// - Destruct the objects.
38///
39/// \attention
40/// With other use cases, especially when using type \alib{StringVectorPA} in combination with
41/// class \alib{monomem;TPoolAllocator;PoolAllocator}, it has to be well thought how this type
42/// is used in respect to the need of freeing memory, especially when strings got allocated with
43/// method #Add.
44///
45/// @tparam TAllocator The allocator type, as prototyped with \alib{lang;Allocator}.
46//==================================================================================================
47template<typename TChar, typename TAllocator>
48class TStringVector : public std::vector< strings::TString<TChar>
49 , lang::StdContainerAllocator<strings::TString<TChar>,
50 TAllocator > >
51{
52 public:
53 /// Type definition of the given template parameter \p{TChar}.
54 using CharType = TChar;
55
56 /// Type definition of the stored strings.
58
59 /// The allocator type that \p{TAllocator} specifies.
60 using AllocatorType = TAllocator;
61
62 protected:
63 /// The vector type that \p{TAllocator} specifies.
64 using vectorBase = std::vector< StringType,
66
67 public:
68
69 /// Constructor.
70 /// @param pAllocator The allocator to use.
72 : vectorBase(pAllocator) {}
73
74 /// Destructor.
75 ~TStringVector() = default;
76
77 /// Returns the allocator provided with construction. If this was \e nulled, then this method
78 /// can be used to set an external (or new) allocator using the assignment operator.
79 /// Has to be set before the first insertion of data.
80 ///
81 /// @return A reference to the internal allocator.
83 { return vectorBase::get_allocator().GetAllocator(); }
84
85 /// Returns the size of this vector as \alib{integer}.
86 /// @return The number of strings stored.
87 integer Size() const noexcept
88 { return integer(vectorBase::size()); }
89
90 /// Adds a string to the end of the list of strings.
91 /// @param src Source string to be copied.
92 /// @return The index of the created string in this vector.
94 {
95 vectorBase::emplace_back( String( GetAllocator(), src ) );
96 return integer(vectorBase::size()) - 1;
97 }
98
99 /// Receives the string at a valid \p{idx}. If the index is out of bounds, a nulled string
100 /// is returned.
101 /// @param idx The index to try.
102 /// @return The string stored at \p{idx}, if available.
104 {
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
115
119
120} // namespace [alib]
121
122
123#endif // HPP_ALIB_STRINGS_UTIL_STRINGVECTOR
124
TAllocator AllocatorType
The allocator type that TAllocator specifies.
strings::TString< TChar > StringType
Type definition of the stored strings.
integer Add(const strings::TString< TChar > &src)
std::vector< StringType, lang::StdContainerAllocator< StringType, TAllocator > > vectorBase
The vector type that TAllocator specifies.
~TStringVector()=default
Destructor.
TChar CharType
Type definition of the given template parameter TChar.
Definition alib.cpp:69
strings::TString< character > String
Type alias in namespace alib.
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.hpp:2549
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273