ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
boxes.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_boxing of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace boxing {
9
10//==================================================================================================
11/// A vector of objects of type \alib{boxing;Box}.
12/// Specializes class \c std::vector<Box> (publicly) with a constructor and methods to add a
13/// variable number of arbitrary values with one invocation.
14///
15/// If another \b %Boxes object or an array of boxes, or boxed versions of such, are added, this
16/// container is "flattened", so that instead of the container, the boxes are added. Such flatting
17/// is performed recursively.
18///
19/// \see
20/// Chapter \ref alib_boxing_boxes "11. Variadic Function Arguments and Class Boxes"
21/// of the Programmer's Manual of module \alib_boxing.
22///
23/// @tparam TAllocator The allocator to use with the <c>std::vector</c>, as prototyped with
24/// \alib{lang;Allocator}.
25//==================================================================================================
26template<typename TAllocator>
27class TBoxes : public std::vector<Box, lang::StdContainerAllocator<Box, TAllocator>>
28{
29 protected:
30 /// The allocator type that \p{TAllocator} specifies.
31 using vectorBase= std::vector<Box, lang::StdContainerAllocator<Box, TAllocator>>;
32
33 public:
34 /// The allocator type that \p{TAllocator} specifies.
35 using AllocatorType= TAllocator;
36
37 /// Defaulted default constructor, usable only with heap allocation.
38 TBoxes() {}
39
40 /// Constructor.
41 /// @param pAllocator The allocator to use.
42 TBoxes(TAllocator& pAllocator)
43 : vectorBase(pAllocator) {}
44
45
46 //==============================================================================================
47 /// Deleted copy constructor.
48 //==============================================================================================
49 TBoxes( TBoxes& ) = delete;
50
51 //==============================================================================================
52 /// Deleted copy assignment operator.
53 /// @return Nothing (deleted).
54 //==============================================================================================
55 TBoxes& operator=( TBoxes& ) = delete;
56
57 //==============================================================================================
58 /// Empty method. Needed to allow adding empty variadic template parameter packs.
59 /// @return A reference to this object.
60 //==============================================================================================
62 {
63 return *this;
64 }
65
66 //==============================================================================================
67 /// Adds one box for each given variadic argument.
68 ///
69 /// @tparam TBoxables The types of the variadic arguments.
70 /// @param args The variadic arguments. Each argument is converted into one box to be
71 /// appended.
72 /// @return A reference to this object.
73 //==============================================================================================
74 template <typename... TBoxables>
75 TBoxes& Add( TBoxables&&... args )
76 {
77 Box boxes[sizeof...(args)]= { std::forward<TBoxables>( args )... };
78 AddArray( boxes, sizeof...(args) );
79
80 return *this;
81 }
82
83 //==============================================================================================
84 /// Adds one boxt.
85 ///
86 /// @param box The box to append.
87 /// @return A reference to this object.
88 //==============================================================================================
89 TBoxes& Add(const Box& box )
90 {
91 AddArray( &box, 1 );
92
93 return *this;
94 }
95
96 //==============================================================================================
97 /// Adds an array of boxes.
98 ///
99 /// @tparam TExtend The size of the given array of boxes.
100 /// @param boxArray The array of boxes.
101 /// @return A reference to this object.
102 //==============================================================================================
103 template<size_t TExtend>
104 TBoxes& Add( const Box(& boxArray)[TExtend] )
105 {
106 AddArray( boxArray, TExtend );
107 return *this;
108 }
109
110 //==============================================================================================
111 /// Adds all elements of the given other \b %TBoxes object.
112 ///
113 /// @param boxes Another container of boxes to add.
114 /// @return A reference to this object.
115 //==============================================================================================
116 template<typename TAllocatorArgs>
118 {
119 AddArray( boxes.data(), integer(boxes.size()) );
120 return *this;
121 }
122
123 //==============================================================================================
124 /// Adds an array of boxes. Array elements of types \b %TBoxes itself and boxed arrays of
125 /// class \b %Box are recursively "flattened".
126 ///
127 /// This method is internally used by all overloads of #Add.
128 ///
129 /// @param boxArray Pointer to the start of the array of boxes.
130 /// @param length The number of boxes contained in \p{boxArray}.
131 //==============================================================================================
132 void AddArray( const Box* boxArray, integer length );
133
134
135 //==============================================================================================
136 /// Inline operator that simply aliases method #Add.
137 ///
138 /// @param src The value to be boxed and added.
139 /// @return Returns a mutable reference to \c this.
140 //==============================================================================================
141 template <typename TBoxable>
142 TBoxes& operator+=( TBoxable&& src )
143 {
144 return Add( src );
145 }
146
147 //==============================================================================================
148 /// Inline operator that simply aliases method #Add.
149 ///
150 /// @param src The value to be boxed and added.
151 /// @return Returns a mutable reference to \c this.
152 //==============================================================================================
153 template <typename TBoxable>
154 TBoxes& operator<<( TBoxable&& src )
155 {
156 return Add( src );
157 }
158
159 //==============================================================================================
160 /// Returns the quantity of elements stored in ths container.
161 /// @return The element count.
162 //==============================================================================================
163 integer Size() const
164 {
165 return integer( vectorBase::size() );
166 }
167
168 //==============================================================================================
169 /// Invokes the corresponding parent's method \c std::vector::reserve.
170 ///
171 /// @param newCapacity The new, higher capacity of the vector.
172 //==============================================================================================
173 void Reserve(integer newCapacity)
174 {
175 vectorBase::reserve( size_t( newCapacity ) );
176 }
177
178 //==============================================================================================
179 /// Invokes \alib{boxing;Box::Call} with each box in this list.
180 /// The result of the invocations of the box-functions is ignored.
181 ///
182 /// @tparam TFDecl The function type to call.
183 /// Needs to be specified with invocations of this method.
184 /// @tparam TArgs Types of the variadic arguments \p{args}.
185 /// Do not need to be specified.
186 /// @param args Variadic arguments forwarded to the functions.
187 //==============================================================================================
188 template <typename TFDecl, typename... TArgs>
189 void CallAll(TArgs&&... args) const
190 {
191 for( auto& box : *this )
192 box.template Call<TFDecl>( std::forward<TArgs>(args)... );
193 }
194
195 //==============================================================================================
196 /// Non-constant version of method #CallAll, which likewise chooses the non-constant version
197 /// of \alib{boxing;Box::Call} and hence this method is usable with functions that only
198 /// accept mutable (aka not constant) boxes.<br>
199 /// Technically, the only difference between this method and \b CallAll is that the latter
200 /// is declared \c const.
201 ///
202 /// @tparam TFDecl The function type to call.
203 /// Needs to be specified with invocations of this method.
204 /// @tparam TArgs Types of the variadic arguments \p{args} .
205 /// @param args Variadic arguments forwarded to the functions.
206 //==============================================================================================
207 template <typename TFDecl, typename... TArgs>
208 void CallAll(TArgs&&... args)
209 {
210 for( auto& box : *this )
211 box.template Call<TFDecl>( std::forward<TArgs>(args)... );
212 }
213
214 //==============================================================================================
215 /// Same as \ref CallAll "CallAll<FClone>", but uses method \alib{boxing;Box::Clone},
216 /// which internally invokes \alib{boxing;FClone}.
217 ///
218 /// Using this version leads to shorter code, because method \b Box::Clone is not inlined.
219 /// Cloning is performed 'into' the allocator used by the \b %Boxes instance.
220 ///
221 /// \par Availability
222 /// This method is available only if the module \alib_monomem is included in the \alibbuild.
223 //==============================================================================================
224 void CloneAll()
225 {
226 for( auto& box : *this )
227 box.Clone( vectorBase::get_allocator().GetAllocator() );
228 }
229
230}; // class TBoxes
231
232
233extern template ALIB_DLL void TBoxes<lang::HeapAllocator>::AddArray( const Box* boxArray, integer length );
234#if ALIB_MONOMEM
235extern template ALIB_DLL void TBoxes<MonoAllocator >::AddArray( const Box* boxArray, integer length );
236#endif
237
238} // namespace alib[::boxing]
239
240/// Type alias in namespace \b alib.
242
243#if ALIB_MONOMEM
244/// Type alias in namespace \b alib.
246
247/// Type alias in namespace \b alib.
249#endif
250
251
252} // namespace [alib]::boxing
253
254
TBoxes & operator<<(TBoxable &&src)
Definition boxes.inl:154
TBoxes & Add(TBoxables &&... args)
Definition boxes.inl:75
void CallAll(TArgs &&... args) const
Definition boxes.inl:189
TBoxes & Add(const Box &box)
Definition boxes.inl:89
TBoxes & Add(const TBoxes< TAllocatorArgs > &boxes)
Definition boxes.inl:117
TBoxes & Add(const Box(&boxArray)[TExtend])
Definition boxes.inl:104
std::vector< Box, lang::StdContainerAllocator< Box, TAllocator > > vectorBase
The allocator type that TAllocator specifies.
Definition boxes.inl:31
void CallAll(TArgs &&... args)
Definition boxes.inl:208
TBoxes()
Defaulted default constructor, usable only with heap allocation.
Definition boxes.inl:38
TBoxes & operator=(TBoxes &)=delete
TBoxes & operator+=(TBoxable &&src)
Definition boxes.inl:142
void AddArray(const Box *boxArray, integer length)
TAllocator AllocatorType
The allocator type that TAllocator specifies.
Definition boxes.inl:35
TBoxes & Add()
Definition boxes.inl:61
TBoxes(TBoxes &)=delete
Deleted copy constructor.
void Reserve(integer newCapacity)
Definition boxes.inl:173
TBoxes(TAllocator &pAllocator)
Definition boxes.inl:42
integer Size() const
Definition boxes.inl:163
#define ALIB_DLL
Definition alib.inl:496
#define ALIB_EXPORT
Definition alib.inl:488
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
boxing::TBoxes< PoolAllocator > BoxesPA
Type alias in namespace alib.
Definition boxes.inl:248
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1216
boxing::TBoxes< MonoAllocator > BoxesMA
Type alias in namespace alib.
Definition boxes.inl:245
boxing::TBoxes< lang::HeapAllocator > BoxesHA
Type alias in namespace alib.
Definition boxes.inl:241