PLaSK library
Loading...
Searching...
No Matches
serialize.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK__SERIALIZE_H
15#define PLASK__SERIALIZE_H
16
17#include <type_traits>
18
19#include <cereal/cereal.hpp>
20#include "plask/data.hpp"
21
22namespace cereal
23{
24 // Portable serialization for std::size_t
25
26 template <class Archive> inline
27 void CEREAL_SAVE_FUNCTION_NAME(Archive& ar, std::size_t const& size)
28 {
29 size_type portable = static_cast<size_type>(size);
30 ar(binary_data(&portable, sizeof(size_type)));
31 }
32
33 template <class Archive> inline
34 void CEREAL_LOAD_FUNCTION_NAME(Archive& ar, std::size_t& size)
35 {
36 size_type portable;
37 ar(binary_data(&portable, sizeof(size_type)));
38 size = static_cast<std::size_t>(portable);
39 }
40
41
42 // Serialization for data of arithmetic using binary serialization, if supported
43
44 template <class Archive, class T> inline
45 typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>::value && std::is_arithmetic<T>::value, void>::type
47 {
48 ar(make_size_tag(static_cast<size_type>(data.size()))); // number of elements
49 ar(binary_data(data.data(), data.size() * sizeof(T)));
50 }
51
52 template <class Archive, class T> inline
53 typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>::value && std::is_arithmetic<T>::value, void>::type
55 {
56 size_type size;
57 ar(make_size_tag(size));
58
59 data.reset(static_cast<std::size_t>(size));
60 ar(binary_data(data.data(), static_cast<std::size_t>(size) * sizeof(T)));
61 }
62
63
64 // Serialization for non-arithmetic data types
65
66 template <class Archive, class T, class A> inline
67 typename std::enable_if<!traits::is_output_serializable<BinaryData<T>, Archive>::value || !std::is_arithmetic<T>::value, void>::type
69 {
70 ar(make_size_tag(static_cast<size_type>(data.size()))); // number of elements
71 for(auto&& v: data) ar(v);
72 }
73
74 template <class Archive, class T, class A> inline
75 typename std::enable_if<!traits::is_input_serializable<BinaryData<T>, Archive>::value || !std::is_arithmetic<T>::value, void>::type
77 {
78 size_type size;
79 ar(make_size_tag(size));
80
81 data.reset(static_cast<std::size_t>(size));
82 for(auto&& v: data) ar(v);
83 }
84
85}; // namespace cereal
86
87
88
89namespace plask {
90
91
92
93}; // namespace plask
94
95#endif // PLASK__SSERIALIZE_H