Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ptr_tuple.hpp
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <compare>
27
28#include <ext/ostream>
29
31#include <extensions/clone.hpp>
32
34
35namespace ext {
36
43template < class ... Types >
44class ptr_tuple {
49 std::tuple < Types * ... > m_data;
50
59 template < std::size_t ... Indices >
60 void copyCons ( std::index_sequence < Indices ... >, const ptr_tuple & tpl ) {
61 ( ( std::get < Indices > ( m_data ) = ext::clone ( std::get < Indices > ( * tpl.m_data ) ) ), ... );
62 }
63
72 template < std::size_t ... Indices >
73 void moveCons ( std::index_sequence < Indices ... >, ptr_tuple && tpl ) {
74 ( std::swap ( std::get < Indices > ( m_data ), std::get < Indices > ( tpl.m_data ) ), ... );
75 }
76
86 template < std::size_t ... Indices, class ... UTypes >
87 void cons ( std::index_sequence < Indices ... >, UTypes && ... elems ) {
88 ( ( std::get < Indices > ( m_data ) = ext::clone ( std::forward < UTypes > ( elems ) ) ), ... );
89 }
90
97 template < std::size_t ... Indices >
98 void des ( std::index_sequence < Indices ... > ) {
99 ( delete std::get < Indices > ( m_data ), ... );
100 }
101
102public:
109 ptr_tuple ( const ptr_tuple & tpl ) {
110 copyCons ( std::make_index_sequence < sizeof ... ( Types ) > ( ), tpl );
111 }
112
119 ptr_tuple ( ptr_tuple && tpl ) {
120 moveCons ( std::make_index_sequence < sizeof ... ( Types ) > ( ), std::move ( tpl ) );
121 }
122
123/* template < class ... UTypes>
124 ptr_tuple ( const ptr_tuple < UTypes ... > & tpl ) {
125 }
126
127 template < class ... UTypes >
128 ptr_tuple ( ptr_tuple < UTypes ... > && tpl ) {
129
130 }*/
131
140 template < class ... UTypes >
141 explicit ptr_tuple ( UTypes && ... elems ) {
142 cons ( std::make_index_sequence < sizeof ... ( Types ) > ( ), std::forward < UTypes > ( elems ) ... );
143 }
144
150 des ( std::make_index_sequence < sizeof ... ( Types ) > ( ) );
151 }
152
159 ptr_tuple < Types ... > & operator = ( const ptr_tuple < Types ... > & other ) {
160 if ( this == & other )
161 return * this;
162
163 des ( std::make_index_sequence < sizeof ... ( Types ) > ( ) );
164 copyCons ( std::make_index_sequence < sizeof ... ( Types ) > ( ), other );
165
166 return *this;
167 }
168
175 ptr_tuple < Types ... > & operator = ( ptr_tuple < Types ... > && other ) {
176 std::swap ( m_data, other.m_data );
177
178 return *this;
179 }
180
187 void swap ( ptr_tuple & other ) {
188 swap ( this->m_data, other.m_data );
189 }
190
199 template < std::size_t I >
200 auto & get ( ) & {
201 return * std::get < I > ( m_data );
202 }
203
212 template < std::size_t I >
213 const auto & get ( ) const & {
214 return * std::get < I > ( m_data );
215 }
216
225 template < std::size_t I >
226 auto && get ( ) && {
227 return std::move ( * std::get < I > ( m_data ) );
228 }
229
237 template < class R, std::size_t I >
238 void set ( R && value ) {
239 set < I > ( ext::clone ( std::forward < R > ( value ) ) );
240 }
241
242private:
243 auto asTie ( ) const {
244 auto accessor = [ & ] < std::size_t ... Is > ( std::index_sequence < Is ... > ) {
245 return std::tie ( this->template get < Is > ( ) ... );
246 };
247 return accessor ( std::make_index_sequence < sizeof ... ( Types ) > ( ) );
248 }
249
250public:
256 return this->asTie ( ) == second.asTie ( );
257 }
258
264 return this->asTie ( ) <=> second.asTie ( );
265 }
266
267};
268
280template< class... Ts>
282 out << "(";
283
284 bool first = true;
285 auto outCallback = [ & ] ( const auto & arg0 ) {
286 if ( ! first ) {
287 out << ", ";
288 } else {
289 first = false;
290 }
291 out << arg0;
292 };
293
294 ext::foreach ( tuple, outCallback ); // TODO use std::apply like in ext::tuple, should work since C++20
295 out << ")";
296 return out;
297}
298
299} /* namespace ext */
300
301namespace std {
302
314template < std::size_t I, class ... Types >
315const auto & get ( ext::ptr_tuple < Types ... > & tpl ) {
316 return tpl.template get < I > ( );
317}
318
330template < std::size_t I, class ... Types >
331const auto & get ( const ext::ptr_tuple < Types ... > & tpl ) {
332 return tpl.template get < I > ( );
333}
334
346template < std::size_t I, class ... Types >
348 return tpl.template get < I > ( );
349}
350
351} /* namespace std */
352
353namespace ext {
354
365template < typename... Elements >
366constexpr auto make_ptr_tuple ( Elements && ... args ) {
367 return ptr_tuple < typename ext::strip_reference_wrapper < std::decay_t < Elements > >::type ... > ( std::forward < Elements > ( args ) ... );
368}
369
370} /* namespace ext */
371
372namespace std {
373
380template < class ... Types >
381struct tuple_size < ext::ptr_tuple < Types ... > > : public std::integral_constant < std::size_t, sizeof...( Types ) > { };
382
392template < std::size_t I, class... Types >
393struct tuple_element < I, ext::ptr_tuple < Types... > > {
394 typedef typename std::tuple_element < I, std::tuple < Types ... > >::type type;
395};
396
397} /* namespace std */
398
Definition: ostream.h:14
Implementation of tuple storing dynamicaly allocated instances of given type. The class mimicks the i...
Definition: ptr_tuple.hpp:44
bool operator==(const ext::ptr_tuple< Types ... > &second) const
Specialisation of the compare structure implementing the three-way comparison.
Definition: ptr_tuple.hpp:255
auto operator<=>(const ext::ptr_tuple< Types ... > &second) const
Specialisation of the compare structure implementing the three-way comparison.
Definition: ptr_tuple.hpp:263
void swap(ptr_tuple &other)
Swaps two instances of pointer tuple.
Definition: ptr_tuple.hpp:187
ptr_tuple< Types ... > & operator=(const ptr_tuple< Types ... > &other)
The pointer tuple copy operator of assignemnt.
Definition: ptr_tuple.hpp:159
void set(R &&value)
Setter method of a value inside the pointer tuple.
Definition: ptr_tuple.hpp:238
const auto & get() const &
Access method to inside the pointer tuple.
Definition: ptr_tuple.hpp:213
ptr_tuple(const ptr_tuple &tpl)
The copy constructor of the pointer tuple.
Definition: ptr_tuple.hpp:109
~ptr_tuple()
The pointer tuple destructor.
Definition: ptr_tuple.hpp:149
auto && get() &&
Access method to inside the pointer tuple.
Definition: ptr_tuple.hpp:226
ptr_tuple(ptr_tuple &&tpl)
The move constructor of the pointer tuple.
Definition: ptr_tuple.hpp:119
ptr_tuple(UTypes &&... elems)
Tuple constructor from pack of source values.
Definition: ptr_tuple.hpp:141
auto & get() &
Access method to inside the pointer tuple.
Definition: ptr_tuple.hpp:200
Definition: set.hpp:44
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
p second
Definition: ToRegExpAlgebraic.h:126
Definition: sigHandler.cpp:20
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
constexpr auto make_ptr_tuple(Elements &&... args)
Helper of pointer tuple construction. The tuple is constructed from values pack, types are deduced.
Definition: ptr_tuple.hpp:366
void foreach(Tuple &&t, Callable callback)
Definition: tuple_common.hpp:19
auto clone(T &&tmp)
Wrapper around clone by means of using copy constructor or clone method if available.
Definition: clone.hpp:41
std::ostream & operator<<(ext::reference_wrapper< std::ostream > &os, std::ostream &(*const func)(std::ostream &))
Overloaded function allowing same operations on wrapped output stream as on the actual output stream,...
Definition: GlobalData.cpp:33
Definition: FordFulkerson.hpp:16
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
void swap(ext::managed_linear_set< T, Compare, Alloc > &x, ext::managed_linear_set< T, Compare, Alloc > &y)
Specialisation of swap for linear set.
Definition: managed_linear_set.hpp:864
std::tuple_element< I, std::tuple< Types... > >::type type
Definition: ptr_tuple.hpp:394