2 \brief Support common types - always included automatically
5 Copyright (c) 2014, Randolph Voorhies, Shane Grant
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 * Neither the name of cereal nor the
16 names of its contributors may be used to endorse or promote products
17 derived from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef CEREAL_TYPES_COMMON_HPP_
31 #define CEREAL_TYPES_COMMON_HPP_
33 #include <cereal/cereal.hpp>
37 namespace common_detail
39 //! Serialization for arrays if BinaryData is supported and we are arithmetic
41 template <class Archive, class T> inline
42 void serializeArray( Archive & ar, T & array, std::true_type /* binary_supported */ )
44 ar( binary_data( array, sizeof(array) ) );
47 //! Serialization for arrays if BinaryData is not supported or we are not arithmetic
49 template <class Archive, class T> inline
50 void serializeArray( Archive & ar, T & array, std::false_type /* binary_supported */ )
52 for( auto & i : array )
59 struct underlying_type
61 typedef typename std::conditional<
63 typename std::make_signed<en>::type,
64 typename std::make_unsigned<en>::type
67 //! Gets the underlying type of an enum
69 template <class T, bool IsEnum>
70 struct enum_underlying_type : std::false_type {};
72 //! Gets the underlying type of an enum
73 /*! Specialization for when we actually have an enum
76 struct enum_underlying_type<T, true> { typedef typename underlying_type<T>::type type; };
79 //! Checks if a type is an enum
80 /*! This is needed over simply calling std::is_enum because the type
81 traits checking at compile time will attempt to call something like
82 load_minimal with a special NoConvertRef struct that wraps up the true type.
84 This will strip away any of that and also expose the true underlying type.
90 typedef typename std::decay<T>::type DecayedT;
91 typedef typename ::cereal::traits::strip_minimal<DecayedT>::type StrippedT;
94 static const bool value = std::is_enum<StrippedT>::value;
95 typedef StrippedT type;
96 typedef typename enum_underlying_type<StrippedT, value>::type base_type;
100 //! Saving for enum types
101 template <class Archive, class T> inline
102 typename std::enable_if<common_detail::is_enum<T>::value, typename common_detail::is_enum<T>::base_type>::type
103 save_minimal( Archive const &, T const & t )
105 return static_cast<typename common_detail::is_enum<T>::base_type>(t);
108 //! Loading for enum types
109 template <class Archive, class T> inline
110 typename std::enable_if<common_detail::is_enum<T>::value, void>::type
111 load_minimal( Archive const &, T && t, typename common_detail::is_enum<T>::base_type const & value )
113 t = reinterpret_cast<typename common_detail::is_enum<T>::type const &>( value );
116 //! Serialization for raw pointers
117 /*! This exists only to throw a static_assert to let users know we don't support raw pointers. */
118 template <class Archive, class T> inline
119 void serialize( Archive &, T * & )
121 static_assert(cereal::traits::detail::delay_static_assert<T>::value,
122 "Cereal does not support serializing raw pointers - please use a smart pointer");
125 //! Serialization for C style arrays
126 template <class Archive, class T> inline
127 typename std::enable_if<std::is_array<T>::value, void>::type
128 serialize(Archive & ar, T & array)
130 common_detail::serializeArray( ar, array,
131 std::integral_constant<bool, traits::is_output_serializable<BinaryData<T>, Archive>::value &&
132 std::is_arithmetic<typename std::remove_all_extents<T>::type>::value>() );
134 } // namespace cereal
136 #endif // CEREAL_TYPES_COMMON_HPP_