ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
boxes.hpp
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/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace boxing {
9
10//==================================================================================================
11/// A vector of objects of type #"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 #"%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 #"alib_boxing_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 class
24/// #"lang::Allocator".
25//==================================================================================================
26template<typename TAllocator>
27class TBoxes : public std::vector<Box, lang::StdAllocator<Box, TAllocator>> {
28 protected:
29 /// The base type.
30 using vectorBase= std::vector<Box, lang::StdAllocator<Box, TAllocator>>;
31
32 public:
33 /// The allocator type that \p{TAllocator} specifies.
34 using AllocatorType= TAllocator;
35
36 /// Defaulted default constructor, usable only with heap allocation.
37 TBoxes() {}
38
39 /// Constructor.
40 /// @param pAllocator The allocator to use.
41 TBoxes(TAllocator& pAllocator)
42 : vectorBase(pAllocator) {}
43
44
45 /// Deleted copy constructor.
46 TBoxes( TBoxes& ) =delete;
47
48 /// Deleted copy assignment operator.
49 /// @return Nothing (deleted).
50 TBoxes& operator=( TBoxes& ) =delete;
51
52 /// Empty method. Needed to allow adding empty variadic template parameter packs.
53 /// @return A reference to this object.
54 TBoxes& Add() { return *this; }
55
56 /// Adds one box for each given variadic argument.
57 ///
58 /// @tparam TBoxables The types of the variadic arguments.
59 /// @param args The variadic arguments. Each argument is converted into one box to be
60 /// appended.
61 /// @return A reference to this object.
62 template <typename... TBoxables>
63 TBoxes& Add( TBoxables&&... args ) {
64 Box boxes[sizeof...(args)]= { std::forward<TBoxables>( args )... };
65 AddArray( boxes, sizeof...(args) );
66
67 return *this;
68 }
69
70 /// Adds one boxt.
71 ///
72 /// @param box The box to append.
73 /// @return A reference to this object.
74 TBoxes& Add(const Box& box ) {
75 AddArray( &box, 1 );
76
77 return *this;
78 }
79
80 /// Adds an array of boxes.
81 ///
82 /// @tparam TExtend The size of the given array of boxes.
83 /// @param boxArray The array of boxes.
84 /// @return A reference to this object.
85 template<size_t TExtend>
86 TBoxes& Add( const Box(& boxArray)[TExtend] ) { AddArray( boxArray, TExtend ); return *this; }
87
88 /// Adds all elements of the given other #"%TBoxes" object.
89 ///
90 /// @param boxes Another container of boxes to add.
91 /// @return A reference to this object.
92 template<typename TAllocatorArgs>
94 {
95 AddArray( boxes.data(), integer(boxes.size()) );
96 return *this;
97 }
98
99 /// Adds an array of boxes. Array elements of types #"%TBoxes" itself and boxed arrays of
100 /// class #"%Box" are recursively "flattened".
101 ///
102 /// This method is internally used by all overloads of #".Add".
103 ///
104 /// @param boxArray Pointer to the start of the array of boxes.
105 /// @param length The number of boxes contained in \p{boxArray}.
106 void AddArray( const Box* boxArray, integer length );
107
108
109 /// Inline operator that simply aliases method #".Add".
110 ///
111 /// @param src The value to be boxed and added.
112 /// @return Returns a mutable reference to \c this.
113 template <typename TBoxable>
114 TBoxes& operator+=( TBoxable&& src ) { return Add( src ); }
115
116 /// Inline operator that simply aliases method #".Add".
117 ///
118 /// @param src The value to be boxed and added.
119 /// @return Returns a mutable reference to \c this.
120 template <typename TBoxable>
121 TBoxes& operator<<( TBoxable&& src ) { return Add( src ); }
122
123 /// Returns the quantity of elements stored in ths container.
124 /// @return The element count.
125 integer Size() const { return integer( vectorBase::size() ); }
126
127 /// Invokes the corresponding parent's method \c std::vector::reserve.
128 ///
129 /// @param newCapacity The new, higher capacity of the vector.
130 void Reserve(integer newCapacity) { vectorBase::reserve( size_t( newCapacity ) ); }
131
132 /// Invokes #"Box::Call" with each box in this list.
133 /// The result of the invocations of the box-functions is ignored.
134 ///
135 /// @tparam TFDecl The function type to call.
136 /// Needs to be specified with invocations of this method.
137 /// @tparam TArgs Types of the variadic arguments \p{args}.
138 /// Do not need to be specified.
139 /// @param args Variadic arguments forwarded to the functions.
140 template <typename TFDecl, typename... TArgs>
141 void CallAll(TArgs&&... args) const {
142 for( auto& box : *this )
143 box.template Call<TFDecl>( std::forward<TArgs>(args)... );
144 }
145
146 /// Non-constant version of method #"CallAll", which likewise chooses the non-constant version
147 /// of #"Box::Call" and hence this method is usable with functions that only
148 /// accept mutable (aka not constant) boxes.<br>
149 /// Technically, the only difference between this method and #"%CallAll" is that the latter
150 /// is declared \c const.
151 ///
152 /// @tparam TFDecl The function type to call.
153 /// Needs to be specified with invocations of this method.
154 /// @tparam TArgs Types of the variadic arguments \p{args} .
155 /// @param args Variadic arguments forwarded to the functions.
156 template <typename TFDecl, typename... TArgs>
157 void CallAll(TArgs&&... args)
158 {
159 for( auto& box : *this )
160 box.template Call<TFDecl>( std::forward<TArgs>(args)... );
161 }
162
163 /// Same as #"CallAll;CallAll<FClone>", but uses method #"Box::Clone",
164 /// which internally invokes #"FClone".
165 ///
166 /// Using this version leads to shorter code, because method #"%Box::Clone" is not inlined.
167 /// Cloning is performed 'into' the allocator used by the #"%Boxes" instance.
168 ///
169 /// \par Availability
170 /// This method is available only if the module \alib_monomem is included in the \alibbuild.
171 void CloneAll()
172 {
173 for( auto& box : *this )
174 box.Clone( vectorBase::get_allocator().GetAllocator() );
175 }
176
177}; // class TBoxes
178
179
180extern template ALIB_DLL void TBoxes<lang::HeapAllocator>::AddArray( const Box* boxArray, integer length );
181#if ALIB_MONOMEM
182extern template ALIB_DLL void TBoxes<MonoAllocator >::AddArray( const Box* boxArray, integer length );
183#endif
184
185} // namespace alib[::boxing]
186
187/// Type alias in namespace #"%alib".
189
190#if ALIB_MONOMEM
191/// Type alias in namespace #"%alib".
193
194/// Type alias in namespace #"%alib".
196#endif
197
198
199} // namespace [alib]::boxing
200
#define ALIB_DLL
#define ALIB_EXPORT
#define ALIB_BOXING_VTABLE_DECLARE(TMapped, Identifier)
void AddArray(const Box *boxArray, integer length)
TBoxes & operator<<(TBoxable &&src)
Definition boxes.hpp:121
TBoxes & Add(TBoxables &&... args)
Definition boxes.hpp:63
void CallAll(TArgs &&... args) const
Definition boxes.hpp:141
TBoxes & Add(const Box &box)
Definition boxes.hpp:74
TBoxes & Add(const TBoxes< TAllocatorArgs > &boxes)
Definition boxes.hpp:93
TBoxes & Add(const Box(&boxArray)[TExtend])
Definition boxes.hpp:86
void CallAll(TArgs &&... args)
Definition boxes.hpp:157
TBoxes()
Defaulted default constructor, usable only with heap allocation.
Definition boxes.hpp:37
TBoxes & operator=(TBoxes &)=delete
TBoxes & operator+=(TBoxable &&src)
Definition boxes.hpp:114
TBoxes & Add()
Definition boxes.hpp:54
TBoxes(TBoxes &)=delete
Deleted copy constructor.
void Reserve(integer newCapacity)
Definition boxes.hpp:130
TBoxes(TAllocator &pAllocator)
Definition boxes.hpp:41
std::vector< Box, lang::StdAllocator< Box, TAllocator > > vectorBase
The base type.
Definition boxes.hpp:30
TAllocator AllocatorType
The allocator type that TAllocator specifies.
Definition boxes.hpp:34
integer Size() const
Definition boxes.hpp:125
DOXYGEN.
Definition box.cpp:17
Definition alox.cpp:14
boxing::TBoxes< lang::HeapAllocator > Boxes
Type alias in namespace #"%alib".
Definition boxes.hpp:188
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
boxing::Box Box
Type alias in namespace #"%alib".
Definition box.hpp:1128
boxing::TBoxes< PoolAllocator > BoxesPA
Type alias in namespace #"%alib".
Definition boxes.hpp:195
boxing::TBoxes< MonoAllocator > BoxesMA
Type alias in namespace #"%alib".
Definition boxes.hpp:192