| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
1 |
/*! \file specialize.hpp |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
2 |
\brief Serialization disambiguation */ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
3 |
/* |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
4 |
Copyright (c) 2014, Randolph Voorhies, Shane Grant |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
5 |
All rights reserved. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
6 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
7 |
Redistribution and use in source and binary forms, with or without |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
8 |
modification, are permitted provided that the following conditions are met: |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
9 |
* Redistributions of source code must retain the above copyright |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
10 |
notice, this list of conditions and the following disclaimer. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
11 |
* Redistributions in binary form must reproduce the above copyright |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
12 |
notice, this list of conditions and the following disclaimer in the |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
13 |
documentation and/or other materials provided with the distribution. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
14 |
* Neither the name of cereal nor the |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
15 |
names of its contributors may be used to endorse or promote products |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
16 |
derived from this software without specific prior written permission. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
17 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
18 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
19 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
20 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
21 |
DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
22 |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
23 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
24 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
25 |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
26 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
27 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
28 |
*/ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
29 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
30 |
#ifndef CEREAL_SPECIALIZE_HPP_ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
31 |
#define CEREAL_SPECIALIZE_HPP_ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
32 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
33 |
namespace cereal |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
34 |
{ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
35 |
// Forward declaration of access class that users can become friends with |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
36 |
class access; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
37 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
38 |
// ###################################################################### |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
39 |
//! A specifier used in conjunction with cereal::specialize to disambiguate |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
40 |
//! serialization in special cases |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
41 |
/*! @relates specialize |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
42 |
@ingroup Access */ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
43 |
enum class specialization |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
44 |
{ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
45 |
member_serialize, //!< Force the use of a member serialize function |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
46 |
member_load_save, //!< Force the use of a member load/save pair |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
47 |
member_load_save_minimal, //!< Force the use of a member minimal load/save pair |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
48 |
non_member_serialize, //!< Force the use of a non-member serialize function |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
49 |
non_member_load_save, //!< Force the use of a non-member load/save pair |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
50 |
non_member_load_save_minimal //!< Force the use of a non-member minimal load/save pair |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
51 |
}; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
52 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
53 |
//! A class used to disambiguate cases where cereal cannot detect a unique way of serializing a class |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
54 |
/*! cereal attempts to figure out which method of serialization (member vs. non-member serialize |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
55 |
or load/save pair) at compile time. If for some reason cereal cannot find a non-ambiguous way |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
56 |
of serializing a type, it will produce a static assertion complaining about this. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
57 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
58 |
This can happen because you have both a serialize and load/save pair, or even because a base |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
59 |
class has a serialize (public or private with friend access) and a derived class does not |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
60 |
overwrite this due to choosing some other serialization type. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
61 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
62 |
Specializing this class will tell cereal to explicitly use the serialization type you specify |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
63 |
and it will not complain about ambiguity in its compile time selection. However, if cereal detects |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
64 |
an ambiguity in specializations, it will continue to issue a static assertion. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
65 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
66 |
@code{.cpp} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
67 |
class MyParent |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
68 |
{ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
69 |
friend class cereal::access; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
70 |
template <class Archive> |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
71 |
void serialize( Archive & ar ) {} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
72 |
}; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
73 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
74 |
// Although serialize is private in MyParent, to cereal::access it will look public, |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
75 |
// even through MyDerived |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
76 |
class MyDerived : public MyParent |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
77 |
{ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
78 |
public: |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
79 |
template <class Archive> |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
80 |
void load( Archive & ar ) {} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
81 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
82 |
template <class Archive> |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
83 |
void save( Archive & ar ) {} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
84 |
}; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
85 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
86 |
// The load/save pair in MyDerived is ambiguous because serialize in MyParent can |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
87 |
// be accessed from cereal::access. This looks the same as making serialize public |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
88 |
// in MyParent, making it seem as though MyDerived has both a serialize and a load/save pair. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
89 |
// cereal will complain about this at compile time unless we disambiguate: |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
90 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
91 |
namespace cereal |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
92 |
{ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
93 |
// This struct specialization will tell cereal which is the right way to serialize the ambiguity |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
94 |
template <class Archive> struct specialize<Archive, MyDerived, cereal::specialization::member_load_save> {}; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
95 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
96 |
// If we only had a disambiguation for a specific archive type, it would look something like this |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
97 |
template <> struct specialize<cereal::BinaryOutputArchive, MyDerived, cereal::specialization::member_load_save> {}; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
98 |
} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
99 |
@endcode |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
100 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
101 |
You can also choose to use the macros CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES or |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
102 |
CEREAL_SPECIALIZE_FOR_ARCHIVE if you want to type a little bit less. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
103 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
104 |
@tparam T The type to specialize the serialization for |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
105 |
@tparam S The specialization type to use for T |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
106 |
@ingroup Access */ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
107 |
template <class Archive, class T, specialization S> |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
108 |
struct specialize : public std::false_type {}; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
109 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
110 |
//! Convenient macro for performing specialization for all archive types |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
111 |
/*! This performs specialization for the specific type for all types of archives. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
112 |
This macro should be placed at the global namespace. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
113 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
114 |
@code{cpp} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
115 |
struct MyType {}; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
116 |
CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES( MyType, cereal::specialization::member_load_save ); |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
117 |
@endcode |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
118 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
119 |
@relates specialize |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
120 |
@ingroup Access */ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
121 |
#define CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES( Type, Specialization ) \ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
122 |
namespace cereal { template <class Archive> struct specialize<Archive, Type, Specialization> {}; } |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
123 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
124 |
//! Convenient macro for performing specialization for a single archive type |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
125 |
/*! This performs specialization for the specific type for a single type of archive. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
126 |
This macro should be placed at the global namespace. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
127 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
128 |
@code{cpp} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
129 |
struct MyType {}; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
130 |
CEREAL_SPECIALIZE_FOR_ARCHIVE( cereal::XMLInputArchive, MyType, cereal::specialization::member_load_save ); |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
131 |
@endcode |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
132 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
133 |
@relates specialize |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
134 |
@ingroup Access */ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
135 |
#define CEREAL_SPECIALIZE_FOR_ARCHIVE( Archive, Type, Specialization ) \ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
136 |
namespace cereal { template <> struct specialize<Archive, Type, Specialization> {}; } |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
137 |
} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
138 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
139 |
#endif |
|
|
1970-01-01 00:00 |
140 |
|