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