-
-
Notifications
You must be signed in to change notification settings - Fork 420
/
portable_binary_iarchive.hpp
449 lines (408 loc) · 15 KB
/
portable_binary_iarchive.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#ifndef PORTABLE_BINARY_IARCHIVE_HPP
#define PORTABLE_BINARY_IARCHIVE_HPP
#include <boost/version.hpp>
#include <hpx/config.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/type_traits/is_unsigned.hpp>
#include <boost/type_traits/is_integral.hpp>
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
#if defined(_MSC_VER)
#pragma warning( push )
#pragma warning( disable : 4244 )
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// portable_binary_iarchive.hpp
// (C) Copyright 2002-7 Robert Ramey - http://www.rrsd.com .
// Copyright (c) 2007-2013 Hartmut Kaiser
//
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for updates, documentation, and revision history.
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/serialization/array.hpp>
#include <boost/serialization/string.hpp>
#include <boost/archive/archive_exception.hpp>
#include <boost/archive/detail/common_iarchive.hpp>
#if BOOST_VERSION < 105600
#include <boost/archive/shared_ptr_helper.hpp>
#endif
#include <boost/archive/detail/register_archive.hpp>
#if BOOST_VERSION >= 104400
#include <boost/serialization/item_version_type.hpp>
#include <boost/serialization/collection_size_type.hpp>
#endif
#include <hpx/config.hpp>
#include <hpx/util/portable_binary_archive.hpp>
#include <hpx/util/basic_binary_iprimitive.hpp>
#include <limits>
namespace hpx { namespace util
{
#if defined(BOOST_MSVC) || defined(BOOST_INTEL_WIN)
#define HPX_SERIALIZATION_EXPORT
#else
#define HPX_SERIALIZATION_EXPORT HPX_ALWAYS_EXPORT
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// exception to be thrown if integer read from archive doesn't fit
// variable being loaded
class HPX_SERIALIZATION_EXPORT portable_binary_iarchive_exception :
public virtual boost::archive::archive_exception
{
public:
enum exception_code {
incompatible_integer_size
};
portable_binary_iarchive_exception(exception_code c = incompatible_integer_size )
: boost::archive::archive_exception(
static_cast<boost::archive::archive_exception::exception_code>(c))
{}
virtual const char *what() const throw()
{
const char *msg = "programmer error";
switch (static_cast<exception_code>(code)) {
case incompatible_integer_size:
msg = "integer cannot be represented";
default:
boost::archive::archive_exception::what();
}
return msg;
}
};
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// "Portable" input binary archive. It addresses integer size and endianness so
// that binary archives can be passed across systems. Note:floating point types
// are passed through as is.
class HPX_SERIALIZATION_EXPORT portable_binary_iarchive :
public hpx::util::basic_binary_iprimitive<
portable_binary_iarchive
>,
public boost::archive::detail::common_iarchive<
portable_binary_iarchive
>
#if BOOST_VERSION < 105600
, public boost::archive::detail::shared_ptr_helper
#endif
{
typedef hpx::util::basic_binary_iprimitive<
portable_binary_iarchive
> primitive_base_t;
typedef boost::archive::detail::common_iarchive<
portable_binary_iarchive
> archive_base_t;
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
public:
#else
friend archive_base_t;
friend primitive_base_t; // since with override load below
friend class boost::archive::detail::interface_iarchive<
portable_binary_iarchive>;
friend class boost::archive::load_access;
protected:
#endif
HPX_ALWAYS_EXPORT void load_impl(boost::int64_t& l, char const maxsize);
HPX_ALWAYS_EXPORT void load_impl(boost::uint64_t& l, char const maxsize);
// default fall through for any types not specified here
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
#if defined(HPX_GCC_DIAGNOSTIC_PRAGMA_CONTEXTS)
#pragma GCC diagnostic push
#endif
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
template <typename T>
void load_integral(T& t, boost::mpl::false_)
{
boost::int64_t l = 0;
load_impl(l, sizeof(T));
t = static_cast<T>(l); // use cast to avoid compile time warning
}
template <typename T>
void load_integral(T& t, boost::mpl::true_)
{
boost::uint64_t l = 0;
load_impl(l, sizeof(T));
t = static_cast<T>(l); // use cast to avoid compile time warning
}
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
#if defined(HPX_GCC_DIAGNOSTIC_PRAGMA_CONTEXTS)
#pragma GCC diagnostic pop
#endif
#endif
template <typename T>
void load(T& t, typename boost::enable_if<boost::is_integral<T> >::type* = 0)
{
load_integral(t, typename boost::is_unsigned<T>::type());
}
template <typename T>
void load(T& t, typename boost::disable_if<boost::is_integral<T> >::type* = 0)
{
this->primitive_base_t::load(t);
}
void load(std::string& t) {
this->primitive_base_t::load(t);
}
#if BOOST_VERSION >= 104400
void load(boost::archive::class_id_reference_type& t) {
boost::int64_t l = 0;
load_impl(l, sizeof(boost::int16_t));
t = boost::archive::class_id_reference_type(
boost::archive::class_id_type(std::size_t(l)));
}
void load(boost::archive::class_id_optional_type& t) {
boost::int64_t l = 0;
load_impl(l, sizeof(boost::int16_t));
t = boost::archive::class_id_optional_type(
boost::archive::class_id_type(std::size_t(l)));
}
void load(boost::archive::class_id_type& t) {
boost::int64_t l = 0;
load_impl(l, sizeof(boost::int16_t));
t = boost::archive::class_id_type(std::size_t(l));
}
void load(boost::archive::object_id_type& t) {
boost::int64_t l = 0;
load_impl(l, sizeof(boost::uint32_t));
t = boost::archive::object_id_type(static_cast<std::size_t>(l));
}
void load(boost::archive::object_reference_type& t) {
boost::int64_t l = 0;
load_impl(l, sizeof(boost::uint32_t));
t = boost::archive::object_reference_type(
boost::archive::object_id_type(std::size_t(l)));
}
void load(boost::archive::tracking_type& t) {
bool l = false;
this->primitive_base_t::load(l);
t = boost::archive::tracking_type(l);
}
void load(boost::archive::version_type& t) {
boost::int64_t l = 0;
load_impl(l, sizeof(boost::uint32_t));
t = boost::archive::version_type(static_cast<unsigned int>(l));
}
void load(boost::archive::library_version_type& t) {
boost::int64_t l = 0;
load_impl(l, sizeof(boost::uint16_t));
t = boost::archive::library_version_type(static_cast<unsigned int>(l));
}
void load(boost::serialization::item_version_type& t) {
boost::int64_t l = 0;
load_impl(l, sizeof(boost::int64_t));
if (l > static_cast<boost::int64_t>((std::numeric_limits<unsigned int>::max)())) {
BOOST_THROW_EXCEPTION(portable_binary_iarchive_exception());
}
t = boost::serialization::item_version_type(static_cast<unsigned int>(l));
}
void load(boost::serialization::collection_size_type& t) {
boost::int64_t l = 0;
load_impl(l, sizeof(boost::int64_t));
if (l > static_cast<boost::int64_t>((std::numeric_limits<unsigned int>::max)())) {
BOOST_THROW_EXCEPTION(portable_binary_iarchive_exception());
}
t = boost::serialization::collection_size_type(static_cast<unsigned int>(l)); //-V106
}
#endif
#ifndef BOOST_NO_STD_WSTRING
void load(std::wstring& t) {
this->primitive_base_t::load(t);
}
#endif
void load(float& t) {
this->primitive_base_t::load(t);
}
void load(double& t) {
this->primitive_base_t::load(t);
}
void load(char& t) {
this->primitive_base_t::load(t);
}
void load(unsigned char& t) {
this->primitive_base_t::load(t);
}
void load(signed char& t) {
this->primitive_base_t::load(t);
}
// intermediate level to support override of operators
// for templates in the absence of partial function
// template ordering
typedef boost::archive::detail::common_iarchive<portable_binary_iarchive>
detail_common_iarchive;
template <typename T>
void load_override(T& t, BOOST_PFTO int) {
this->detail_common_iarchive::load_override(t, 0);
}
HPX_ALWAYS_EXPORT void
load_override(boost::archive::class_name_type& t, int);
// binary files don't include the optional information
void load_override(boost::archive::class_id_optional_type&, int) {}
HPX_ALWAYS_EXPORT boost::uint32_t init(boost::uint32_t flags);
public:
template <typename Container>
portable_binary_iarchive(Container const& buffer,
boost::uint64_t inbound_data_size, unsigned flags_value = 0)
: primitive_base_t(buffer, inbound_data_size, flags_value),
archive_base_t(flags_value)
{
init(flags_value);
}
template <typename Container>
portable_binary_iarchive(Container const& buffer,
std::vector<serialization_chunk> const* chunks,
boost::uint64_t inbound_data_size, unsigned flags_value = 0)
: primitive_base_t(buffer, chunks, inbound_data_size, flags_value),
archive_base_t(flags_value)
{
init(flags_value);
}
// the optimized load_array dispatches to load_binary
template <typename T>
void load_array(boost::serialization::array<T>& a, unsigned int)
{
// If we need to potentially flip bytes we serialize each element
// separately.
#ifdef BOOST_BIG_ENDIAN
if (this->flags() & (endian_little | disable_array_optimization)) {
for (std::size_t i = 0; i != a.count(); ++i)
load(a.address()[i]);
}
#else
if (this->flags() & (endian_big | disable_array_optimization)) {
for (std::size_t i = 0; i != a.count(); ++i)
load(a.address()[i]);
}
#endif
else {
this->primitive_base_t::load_array(a);
}
}
void load_array(boost::serialization::array<float>& a, unsigned int)
{
this->primitive_base_t::load_array(a);
}
void load_array(boost::serialization::array<double>& a, unsigned int)
{
this->primitive_base_t::load_array(a);
}
void load_array(boost::serialization::array<char>& a, unsigned int)
{
this->primitive_base_t::load_array(a);
}
void load_array(boost::serialization::array<unsigned char>& a, unsigned int)
{
this->primitive_base_t::load_array(a);
}
void load_array(boost::serialization::array<signed char>& a, signed int)
{
this->primitive_base_t::load_array(a);
}
};
#undef HPX_SERIALIZATION_EXPORT
}}
// required by export in boost version > 1.34
BOOST_SERIALIZATION_REGISTER_ARCHIVE(hpx::util::portable_binary_iarchive)
BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(hpx::util::portable_binary_iarchive)
///////////////////////////////////////////////////////////////////////////////
namespace boost { namespace archive { namespace detail
{
template <>
struct load_pointer_type<hpx::util::portable_binary_iarchive>
{
typedef hpx::util::portable_binary_iarchive archive_type;
struct abstract
{
template <typename T>
static const basic_pointer_iserializer*
register_type(archive_type & /* ar */)
{
// it has? to be polymorphic
BOOST_STATIC_ASSERT(boost::is_polymorphic< T >::value);
return static_cast<basic_pointer_iserializer *>(NULL);
}
};
struct non_abstract
{
template <typename T>
static const basic_pointer_iserializer*
register_type(archive_type& ar)
{
return ar.register_type(static_cast<T *>(NULL));
}
};
template <typename T>
static const basic_pointer_iserializer*
register_type(archive_type &ar, const T & /*t*/)
{
// there should never be any need to load an abstract polymorphic
// class pointer. Inhibiting code generation for this
// permits abstract base classes to be used - note: exception
// virtual serialize functions used for plug-ins
typedef BOOST_DEDUCED_TYPENAME
mpl::eval_if<
boost::serialization::is_abstract<const T>,
boost::mpl::identity<abstract>,
boost::mpl::identity<non_abstract>
>::type typex;
return typex::template register_type< T >(ar);
}
template <typename T>
static T * pointer_tweak(
const boost::serialization::extended_type_info & eti,
void const * const t,
const T &)
{
// tweak the pointer back to the base class
return static_cast<T *>(
const_cast<void *>(
boost::serialization::void_upcast(
eti,
boost::serialization::singleton<
BOOST_DEDUCED_TYPENAME
boost::serialization::type_info_implementation< T >::type
>::get_const_instance(),
t
)
)
);
}
template <typename T>
static void check_load(T& /* t */)
{
check_pointer_level< T >();
// check_pointer_tracking< T >(); // this has to be disabled to avoid warnings
}
static const basic_pointer_iserializer *
find(const boost::serialization::extended_type_info & type)
{
return static_cast<const basic_pointer_iserializer *>(
archive_serializer_map<archive_type>::find(type)
);
}
template <typename Tptr>
static void invoke(archive_type & ar, Tptr & t)
{
check_load(*t);
const basic_pointer_iserializer * bpis_ptr = register_type(ar, *t);
const basic_pointer_iserializer * newbpis_ptr = ar.load_pointer(
// note major hack here !!!
// I tried every way to convert Tptr &t (where Tptr might
// include const) to void * &. This is the only way
// I could make it work. RR
(void * & )t,
bpis_ptr,
find
);
// if the pointer isn't that of the base class
if (newbpis_ptr != bpis_ptr){
t = pointer_tweak(newbpis_ptr->get_eti(), t, *t);
}
}
};
}}}
#if defined(_MSC_VER)
#pragma warning( pop )
#endif
#endif // PORTABLE_BINARY_IARCHIVE_HPP