Adding cereal lib
[platform/upstream/iotivity.git] / extlibs / cereal / include / cereal / types / common.hpp
1 /*! \file common.hpp
2     \brief Support common types - always included automatically
3     \ingroup OtherTypes */
4 /*
5   Copyright (c) 2014, Randolph Voorhies, Shane Grant
6   All rights reserved.
7
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.
18
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.
29 */
30 #ifndef CEREAL_TYPES_COMMON_HPP_
31 #define CEREAL_TYPES_COMMON_HPP_
32
33 #include <cereal/cereal.hpp>
34
35 namespace cereal
36 {
37   namespace common_detail
38   {
39     //! Serialization for arrays if BinaryData is supported and we are arithmetic
40     /*! @internal */
41     template <class Archive, class T> inline
42     void serializeArray( Archive & ar, T & array, std::true_type /* binary_supported */ )
43     {
44       ar( binary_data( array, sizeof(array) ) );
45     }
46
47     //! Serialization for arrays if BinaryData is not supported or we are not arithmetic
48     /*! @internal */
49     template <class Archive, class T> inline
50     void serializeArray( Archive & ar, T & array, std::false_type /* binary_supported */ )
51     {
52       for( auto & i : array )
53         ar( i );
54     }
55
56     namespace
57     {
58       template<class en>
59       struct underlying_type
60       {
61           typedef typename std::conditional<
62                   en(-1)<en(0),
63                   typename std::make_signed<en>::type,
64                   typename std::make_unsigned<en>::type
65                   > ::type type;
66       };
67       //! Gets the underlying type of an enum
68       /*! @internal */
69       template <class T, bool IsEnum>
70       struct enum_underlying_type : std::false_type {};
71
72       //! Gets the underlying type of an enum
73       /*! Specialization for when we actually have an enum
74           @internal */
75       template <class T>
76       struct enum_underlying_type<T, true> { typedef typename underlying_type<T>::type type; };
77     } // anon namespace
78
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.
83
84         This will strip away any of that and also expose the true underlying type.
85         @internal */
86     template <class T>
87     class is_enum
88     {
89       private:
90         typedef typename std::decay<T>::type DecayedT;
91         typedef typename ::cereal::traits::strip_minimal<DecayedT>::type StrippedT;
92
93       public:
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;
97     };
98   }
99
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 )
104   {
105     return static_cast<typename common_detail::is_enum<T>::base_type>(t);
106   }
107
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 )
112   {
113     t = reinterpret_cast<typename common_detail::is_enum<T>::type const &>( value );
114   }
115
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 * & )
120   {
121     static_assert(cereal::traits::detail::delay_static_assert<T>::value,
122       "Cereal does not support serializing raw pointers - please use a smart pointer");
123   }
124
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)
129   {
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>() );
133   }
134 } // namespace cereal
135
136 #endif // CEREAL_TYPES_COMMON_HPP_