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