Debian Codegraph @

source/code/Libs/include/cereal/specialize.hpp

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