Alexandria  2.16
Please provide a description of the project.
tuple.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
25 #ifndef GRIDCONTAINER_SERIALIZATION_TUPLE_H
26 #define GRIDCONTAINER_SERIALIZATION_TUPLE_H
27 
28 #include <tuple>
29 #include <type_traits>
30 #include <memory>
31 #include <boost/serialization/split_free.hpp>
32 
33 namespace boost {
34 namespace serialization {
35 
39 template<size_t N>
40 struct Save {
41 
44  template<typename Archive, typename... Args>
45  static void save(Archive& ar, const std::tuple<Args...>& t, const unsigned int version,
46  typename std::enable_if<std::is_default_constructible<typename std::tuple_element<N-1, std::tuple<Args...>>::type>::value>::type* = 0) {
47  ar << std::get<N-1>(t);
48  Save<N-1>::save(ar, t, version);
49  }
50 
54  template<typename Archive, typename... Args>
55  static void save(Archive& ar, const std::tuple<Args...>& t, const unsigned int version,
56  typename std::enable_if<!std::is_default_constructible<typename std::tuple_element<N-1, std::tuple<Args...>>::type>::value>::type* = 0) {
57  // Do NOT delete this pointer! It points in the element of the tuple and
58  // the tuple will take care of the memory management
59  typename std::remove_reference<decltype(std::get<N-1>(t))>::type* ptr = &std::get<N-1>(t);
60  ar << ptr;
61  Save<N-1>::save(ar, t, version);
62  }
63 };
64 
67 template<>
68 struct Save<0> {
70  template<typename Archive, typename... Args>
71  static void save(Archive&, const std::tuple<Args...>&, const unsigned int) { }
72 };
73 
78 template<size_t N>
79 struct Load {
80 
84  template<typename Archive, typename... Args>
85  static void load(Archive& ar, std::tuple<Args...>& t, const unsigned int version,
86  typename std::enable_if<std::is_default_constructible<typename std::tuple_element<N-1, std::tuple<Args...>>::type>::value>::type* = 0) {
87  ar >> std::get<N-1>(t);
88  Load<N-1>::load(ar, t, version);
89  }
90 
95  template<typename Archive, typename... Args>
96  static void load(Archive& ar, std::tuple<Args...>& t, const unsigned int version,
97  typename std::enable_if<!std::is_default_constructible<typename std::tuple_element<N-1, std::tuple<Args...>>::type>::value>::type* = 0) {
98  typedef typename std::remove_reference<decltype(std::get<N-1>(t))>::type ElementType;
99  ElementType* ptr;
100  ar >> ptr;
101  // We use a unique_ptr to guarantee deletion of the pointer
102  std::unique_ptr<ElementType> deleter {ptr};
103  std::get<N-1>(t) = *deleter;
104  Load<N-1>::load(ar, t, version);
105  }
106 };
107 
110 template<>
111 struct Load<0> {
113  template<typename Archive, typename... Args>
114  static void load(Archive&, std::tuple<Args...>&, const unsigned int) { }
115 };
116 
119 template<typename Archive, typename... Args>
120 void save(Archive& ar, const std::tuple<Args...>& t, const unsigned int version) {
121  Save<sizeof...(Args)>::save(ar, t, version);
122 }
123 
126 template<typename Archive, typename... Args>
127 void load(Archive& ar, std::tuple<Args...>& t, const unsigned int version) {
128  Load<sizeof...(Args)>::load(ar, t, version);
129 }
130 
133 template<typename Archive, typename... Args>
134 void serialize(Archive& ar, std::tuple<Args...>& t, const unsigned int version) {
135  split_free(ar, t, version);
136 }
137 
138 } /* end of namespace serialization */
139 } /* end of namespace boost */
140 
141 #endif /* GRIDCONTAINER_SERIALIZATION_TUPLE_H */
142 
Definition: array.h:34
static void save(Archive &ar, const std::tuple< Args... > &t, const unsigned int version, typename std::enable_if< std::is_default_constructible< typename std::tuple_element< N-1, std::tuple< Args... >>::type >::value >::type *=0)
Definition: tuple.h:45
void load(Archive &ar, Euclid::GridContainer::GridContainer< GridCellManager, AxesTypes... > &grid, const unsigned int, typename std::enable_if< std::is_default_constructible< typename Euclid::GridContainer::GridCellManagerTraits< GridCellManager >::data_type >::value >::type *=0)
Definition: GridContainer.h:67
static void load(Archive &ar, std::tuple< Args... > &t, const unsigned int version, typename std::enable_if<!std::is_default_constructible< typename std::tuple_element< N-1, std::tuple< Args... >>::type >::value >::type *=0)
Definition: tuple.h:96
static void save(Archive &, const std::tuple< Args... > &, const unsigned int)
This method does nothing. It exists to break the recursion.
Definition: tuple.h:71
static void load(Archive &ar, std::tuple< Args... > &t, const unsigned int version, typename std::enable_if< std::is_default_constructible< typename std::tuple_element< N-1, std::tuple< Args... >>::type >::value >::type *=0)
Definition: tuple.h:85
void serialize(Archive &archive, std::array< CellType, ND > &array, const unsigned int)
Definition: array.h:38
constexpr std::size_t N
static void save(Archive &ar, const std::tuple< Args... > &t, const unsigned int version, typename std::enable_if<!std::is_default_constructible< typename std::tuple_element< N-1, std::tuple< Args... >>::type >::value >::type *=0)
Definition: tuple.h:55
void save(Archive &ar, const Euclid::GridContainer::GridContainer< GridCellManager, AxesTypes... > &grid, const unsigned int, typename std::enable_if< std::is_default_constructible< typename Euclid::GridContainer::GridCellManagerTraits< GridCellManager >::data_type >::value >::type *=0)
Definition: GridContainer.h:44
STL class.
static void load(Archive &, std::tuple< Args... > &, const unsigned int)
This method does nothing. It exists to break the recursion.
Definition: tuple.h:114