Thrill  0.1
serialization_cereal.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/data/serialization_cereal.hpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
9  ******************************************************************************/
10 
11 #pragma once
12 #ifndef THRILL_DATA_SERIALIZATION_CEREAL_HEADER
13 #define THRILL_DATA_SERIALIZATION_CEREAL_HEADER
14 
15 #include <cereal/cereal.hpp>
16 #include <cereal/details/traits.hpp>
20 #include <tlx/meta/is_std_pair.hpp>
23 
24 #include <sstream>
25 #include <string>
26 
27 namespace thrill {
28 namespace data {
29 
30 //! \defgroup data_internal Data Internals
31 //! \ingroup data
32 //! \{
33 
34 namespace serialization_cereal {
35 
36 /*
37  Original Archive Code from cereal library is
38  Copyright (c) 2014, Randolph Voorhies, Shane Grant
39  All rights reserved.
40 
41  Redistribution and use in source and binary forms, with or without
42  modification, are permitted provided that the following conditions are met:
43  * Redistributions of source code must retain the above copyright
44  notice, this list of conditions and the following disclaimer.
45  * Redistributions in binary form must reproduce the above copyright
46  notice, this list of conditions and the following disclaimer in the
47  documentation and/or other materials provided with the distribution.
48  * Neither the name of cereal nor the
49  names of its contributors may be used to endorse or promote products
50  derived from this software without specific prior written permission.
51 
52  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
53  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
54  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55  DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
56  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
58  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
59  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 */
63 
64 // ######################################################################
65 
66 /*!
67  * An output archive designed to save data in a compact binary
68  * representation. Originally cereal::BinaryOutputArchive, adapted for thrill
69  * BlockWriter interface.
70  */
71 template <typename Writer>
73  : public cereal::OutputArchive<ThrillOutputArchive<Writer>,
74  cereal::AllowEmptyClassElision>
75 {
76 public:
77  //! Construct, outputting to the provided thrill writer
78  explicit ThrillOutputArchive(Writer& writer)
79  : cereal::OutputArchive<ThrillOutputArchive<Writer>,
80  cereal::AllowEmptyClassElision>(this),
81  writer_(writer)
82  { }
83 
84  //! Writes size bytes of data to the thrill writer
85  void saveBinary(const void* data, std::size_t size) {
86  writer_.Append(data, size);
87  }
88 
89 private:
90  Writer& writer_;
91 };
92 
93 //! An input archive designed to load data saved using ThrillOutputArchive
94 template <typename Reader>
96  : public cereal::InputArchive<ThrillInputArchive<Reader>,
97  cereal::AllowEmptyClassElision>
98 {
99 public:
100  //! Construct, loading from the provided thrill reader
101  explicit ThrillInputArchive(Reader& reader)
102  : cereal::InputArchive<ThrillInputArchive<Reader>,
103  cereal::AllowEmptyClassElision>(this),
104  reader_(reader)
105  { }
106 
107  //! Reads size bytes of data from the input thrill reader
108  void loadBinary(void* const data, std::size_t size) {
109  reader_.Read(data, size);
110  }
111 
112 private:
113  Reader& reader_;
114 };
115 
116 // ######################################################################
117 // Common BinaryArchive serialization functions
118 
119 //! Saving for POD types to binary
120 template <typename T, typename Writer>
121 inline
124  ar.saveBinary(std::addressof(t), sizeof(t));
125 }
126 
127 //! Loading for POD types from binary
128 template <typename Reader, typename T>
129 inline
132  ar.loadBinary(std::addressof(t), sizeof(t));
133 }
134 
135 //! Serializing NVP types to binary
136 template <typename Writer, typename T>
137 inline void
139  ThrillOutputArchive<Writer>& ar, cereal::NameValuePair<T>& t) {
140  ar(t.value);
141 }
142 
143 //! Serializing NVP types to binary
144 template <typename Reader, typename T>
145 inline void
147  ThrillInputArchive<Reader>& ar, cereal::NameValuePair<T>& t) {
148  ar(t.value);
149 }
150 
151 //! Serializing SizeTags to binary
152 template <typename Writer, typename T>
153 inline void
155  ThrillOutputArchive<Writer>& ar, cereal::SizeTag<T>& t) {
156  ar(t.size);
157 }
158 
159 //! Serializing SizeTags to binary
160 template <typename Reader, typename T>
161 inline void
163  ThrillInputArchive<Reader>& ar, cereal::SizeTag<T>& t) {
164  ar(t.size);
165 }
166 
167 //! Saving binary data
168 template <typename T, typename Writer>
169 inline
171  ThrillOutputArchive<Writer>& ar, cereal::BinaryData<T> const& bd) {
172  ar.saveBinary(bd.data, static_cast<std::size_t>(bd.size));
173 }
174 
175 //! Loading binary data
176 template <typename Reader, typename T>
177 inline
179  ThrillInputArchive<Reader>& ar, cereal::BinaryData<T>& bd) {
180  ar.loadBinary(bd.data, static_cast<std::size_t>(bd.size));
181 }
182 
183 } // namespace serialization_cereal
184 
185 //! \}
186 
187 //! \addtogroup data_layer
188 //! \{
189 
190 /************** Use cereal if serialization function is given *****************/
191 
192 template <typename Archive, typename T>
193 struct Serialization<Archive, T, typename std::enable_if<
194  cereal::traits::is_input_serializable<T, Archive>::value &&
195  !std::is_pod<T>::value &&
196  !std::is_same<T, std::string>::value &&
197  !tlx::is_std_pair<T>::value &&
198  !tlx::is_std_array<T>::value &&
199  !tlx::is_std_vector<T>::value &&
200  !tlx::is_std_tuple<T>::value
201  >::type> {
202  static void Serialize(const T& t, Archive& ar) {
203  // Create an output archive
205  // Write the data to the archive
206  oarchive(t);
207  }
208 
209  static T Deserialize(Archive& ar) {
210  // Create an output archive
212  // Read the data from the archive
213  T res;
214  iarchive(res);
215  return res;
216  }
217  static constexpr bool is_fixed_size = false;
218  static constexpr size_t fixed_size = 0;
219 };
220 
221 //! \}
222 
223 } // namespace data
224 } // namespace thrill
225 
226 // register archives for polymorphic support
227 // CEREAL_REGISTER_ARCHIVE(thrill::data::ThrillOutputArchive)
228 // CEREAL_REGISTER_ARCHIVE(thrill::data::ThrillInputArchive)
229 
230 // tie input and output archives together
231 // CEREAL_SETUP_ARCHIVE_TRAITS(thrill::data::ThrillInputArchive,
232 // thrill::data::ThrillOutputArchive)
233 
234 #endif // !THRILL_DATA_SERIALIZATION_CEREAL_HEADER
235 
236 /******************************************************************************/
double T
STL namespace.
ThrillInputArchive(Reader &reader)
Construct, loading from the provided thrill reader.
ThrillOutputArchive(Writer &writer)
Construct, outputting to the provided thrill writer.
std::enable_if< std::is_arithmetic< T >::value, void >::type CEREAL_SAVE_FUNCTION_NAME(ThrillOutputArchive< Writer > &ar, T const &t)
Saving for POD types to binary.
void CEREAL_SERIALIZE_FUNCTION_NAME(ThrillOutputArchive< Writer > &ar, cereal::NameValuePair< T > &t)
Serializing NVP types to binary.
std::enable_if< std::is_arithmetic< T >::value, void >::type CEREAL_LOAD_FUNCTION_NAME(ThrillInputArchive< Reader > &ar, T &t)
Loading for POD types from binary.
void loadBinary(void *const data, std::size_t size)
Reads size bytes of data from the input thrill reader.
int value
Definition: gen_data.py:41
An output archive designed to save data in a compact binary representation.
void saveBinary(const void *data, std::size_t size)
Writes size bytes of data to the thrill writer.
An input archive designed to load data saved using ThrillOutputArchive.