Alexandria  2.19
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tuple.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 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 <boost/serialization/split_free.hpp>
29 #include <memory>
30 #include <tuple>
31 #include <type_traits>
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
46  save(Archive& ar, const std::tuple<Args...>& t, const unsigned int version,
47  typename std::enable_if<
48  std::is_default_constructible<typename std::tuple_element<N - 1, std::tuple<Args...>>::type>::value>::type* = 0) {
49  ar << std::get<N - 1>(t);
50  Save<N - 1>::save(ar, t, version);
51  }
52 
56  template <typename Archive, typename... Args>
57  static void
58  save(Archive& ar, const std::tuple<Args...>& t, const unsigned int version,
59  typename std::enable_if<
60  !std::is_default_constructible<typename std::tuple_element<N - 1, std::tuple<Args...>>::type>::value>::type* = 0) {
61  // Do NOT delete this pointer! It points in the element of the tuple and
62  // the tuple will take care of the memory management
63  typename std::remove_reference<decltype(std::get<N - 1>(t))>::type* ptr = &std::get<N - 1>(t);
64  ar << ptr;
65  Save<N - 1>::save(ar, t, version);
66  }
67 };
68 
71 template <>
72 struct Save<0> {
74  template <typename Archive, typename... Args>
75  static void save(Archive&, const std::tuple<Args...>&, const unsigned int) {}
76 };
77 
82 template <size_t N>
83 struct Load {
84 
88  template <typename Archive, typename... Args>
89  static void
90  load(Archive& ar, std::tuple<Args...>& t, const unsigned int version,
91  typename std::enable_if<
92  std::is_default_constructible<typename std::tuple_element<N - 1, std::tuple<Args...>>::type>::value>::type* = 0) {
93  ar >> std::get<N - 1>(t);
94  Load<N - 1>::load(ar, t, version);
95  }
96 
101  template <typename Archive, typename... Args>
102  static void
103  load(Archive& ar, std::tuple<Args...>& t, const unsigned int version,
104  typename std::enable_if<
105  !std::is_default_constructible<typename std::tuple_element<N - 1, std::tuple<Args...>>::type>::value>::type* = 0) {
106  typedef typename std::remove_reference<decltype(std::get<N - 1>(t))>::type ElementType;
107  ElementType* ptr;
108  ar >> ptr;
109  // We use a unique_ptr to guarantee deletion of the pointer
110  std::unique_ptr<ElementType> deleter{ptr};
111  std::get<N - 1>(t) = *deleter;
112  Load<N - 1>::load(ar, t, version);
113  }
114 };
115 
118 template <>
119 struct Load<0> {
121  template <typename Archive, typename... Args>
122  static void load(Archive&, std::tuple<Args...>&, const unsigned int) {}
123 };
124 
127 template <typename Archive, typename... Args>
128 void save(Archive& ar, const std::tuple<Args...>& t, const unsigned int version) {
129  Save<sizeof...(Args)>::save(ar, t, version);
130 }
131 
134 template <typename Archive, typename... Args>
135 void load(Archive& ar, std::tuple<Args...>& t, const unsigned int version) {
136  Load<sizeof...(Args)>::load(ar, t, version);
137 }
138 
141 template <typename Archive, typename... Args>
142 void serialize(Archive& ar, std::tuple<Args...>& t, const unsigned int version) {
143  split_free(ar, t, version);
144 }
145 
146 } /* end of namespace serialization */
147 } /* end of namespace boost */
148 
149 #endif /* GRIDCONTAINER_SERIALIZATION_TUPLE_H */
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
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:69
void serialize(Archive &archive, std::array< CellType, ND > &array, const unsigned int)
Definition: array.h:37
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:58
STL class.
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:103
static void load(Archive &, std::tuple< Args...> &, const unsigned int)
This method does nothing. It exists to break the recursion.
Definition: tuple.h:122
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:90
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:46
static void save(Archive &, const std::tuple< Args...> &, const unsigned int)
This method does nothing. It exists to break the recursion.
Definition: tuple.h:75