Alexandria  2.19
Please provide a description of the project.
GridAxis.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_GRIDAXIS_H
26 #define GRIDCONTAINER_SERIALIZATION_GRIDAXIS_H
27 
28 #include "GridContainer/GridAxis.h"
29 #include <boost/serialization/utility.hpp>
30 #include <memory>
31 #include <type_traits>
32 
33 namespace boost {
34 namespace serialization {
35 
38 template <typename Archive, typename T>
39 void serialize(Archive&, Euclid::GridContainer::GridAxis<T>&, const unsigned int) {
40  // Nothing here as everything is done with the data passed to the constructor
41 }
42 
46 template <typename Archive, typename T>
47 void saveType(Archive& ar, const T& t, typename std::enable_if<std::is_default_constructible<T>::value>::type* = 0) {
48  ar << t;
49 }
50 
56 template <typename Archive, typename T>
57 void saveType(Archive& ar, const T& t, typename std::enable_if<!std::is_default_constructible<T>::value>::type* = 0) {
58  const T* ptr = &t;
59  ar << ptr;
60 }
61 
68 template <typename Archive, typename T>
69 void save_construct_data(Archive& ar, const Euclid::GridContainer::GridAxis<T>* t, const unsigned int) {
70  std::string name = t->name();
71  ar << name;
72  size_t size = t->size();
73  ar << size;
74  for (size_t i = 0; i < size; ++i) {
75  saveType(ar, (*t)[i]);
76  }
77 }
78 
82 template <typename Archive, typename T>
83 T loadType(Archive& ar, typename std::enable_if<std::is_default_constructible<T>::value>::type* = 0) {
84  T value;
85  ar >> value;
86  return value;
87 }
88 
94 template <typename Archive, typename T>
95 T loadType(Archive& ar, typename std::enable_if<!std::is_default_constructible<T>::value>::type* = 0) {
96  T* ptr;
97  ar >> ptr;
98  // We use a unique_ptr to guarantee deletion of the pointer
99  std::unique_ptr<T> deleter{ptr};
100  T value{*deleter};
101  return value;
102 }
103 
110 template <typename Archive, typename T>
111 void load_construct_data(Archive& ar, Euclid::GridContainer::GridAxis<T>* t, const unsigned int) {
112  std::string name;
113  ar >> name;
114  size_t size;
115  ar >> size;
116  std::vector<T> values;
117  for (size_t i = 0; i < size; ++i) {
118  T value = loadType<Archive, T>(ar);
119  values.push_back(value);
120  }
121  ::new (t) Euclid::GridContainer::GridAxis<T>(name, values);
122 }
123 
124 } /* end of namespace serialization */
125 } /* end of namespace boost */
126 
127 #endif /* GRIDCONTAINER_SERIALIZATION_GRIDAXIS_H */
boost::serialization::save_construct_data
void save_construct_data(Archive &ar, const Euclid::GridContainer::GridAxis< T > *t, const unsigned int)
Definition: GridAxis.h:69
Euclid::GridContainer::GridAxis
Provides information related with an axis of a GridContainer.
Definition: GridAxis.h:49
std::string
STL class.
Euclid::GridContainer::GridAxis::size
size_t size() const
Returns the number of knots of the axis.
GridAxis.h
std::vector
STL class.
std::is_default_constructible
boost
Definition: array.h:33
std::vector::push_back
T push_back(T... args)
std::enable_if
Euclid::GridContainer::GridAxis::name
const std::string & name() const
Returns the name of the axis.
boost::serialization::loadType
T loadType(Archive &ar, typename std::enable_if< std::is_default_constructible< T >::value >::type *=0)
Definition: GridAxis.h:83
boost::serialization::serialize
void serialize(Archive &archive, std::array< CellType, ND > &array, const unsigned int)
Definition: array.h:37
std::unique_ptr
STL class.
boost::serialization::load_construct_data
void load_construct_data(Archive &ar, Euclid::GridContainer::GridAxis< T > *t, const unsigned int)
Definition: GridAxis.h:111
boost::serialization::saveType
void saveType(Archive &ar, const T &t, typename std::enable_if< std::is_default_constructible< T >::value >::type *=0)
Definition: GridAxis.h:47