NDDEM
common.hpp
Go to the documentation of this file.
1 
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 the copyright holder 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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  {
40 
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 
48 
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  {
59 
60  template <class T, bool IsEnum>
61  struct enum_underlying_type : std::false_type {};
62 
64 
66  template <class T>
67  struct enum_underlying_type<T, true> { using type = typename std::underlying_type<T>::type; };
68  } // anon namespace
69 
71 
77  template <class T>
78  class is_enum
79  {
80  private:
81  using DecayedT = typename std::decay<T>::type;
83 
84  public:
86  using type = StrippedT;
88  };
89  }
90 
92  template <class Archive, class T> inline
95  CEREAL_SAVE_MINIMAL_FUNCTION_NAME( Archive const &, T const & t )
96  {
97  return static_cast<typename common_detail::is_enum<T>::base_type>(t);
98  }
99 
101  template <class Archive, class T> inline
103  CEREAL_LOAD_MINIMAL_FUNCTION_NAME( Archive const &, T && t,
104  typename common_detail::is_enum<T>::base_type const & value )
105  {
106  t = reinterpret_cast<typename common_detail::is_enum<T>::type const &>( value );
107  }
108 
110 
111  template <class Archive, class T> inline
112  void CEREAL_SERIALIZE_FUNCTION_NAME( Archive &, T * & )
113  {
115  "Cereal does not support serializing raw pointers - please use a smart pointer");
116  }
117 
119  template <class Archive, class T> inline
121  CEREAL_SERIALIZE_FUNCTION_NAME(Archive & ar, T & array)
122  {
124  std::integral_constant<bool, traits::is_output_serializable<BinaryData<T>, Archive>::value &&
125  std::is_arithmetic<typename std::remove_all_extents<T>::type>::value>() );
126  }
127 } // namespace cereal
128 
129 #endif // CEREAL_TYPES_COMMON_HPP_
Main cereal functionality.
Checks if a type is an enum.
Definition: common.hpp:79
StrippedT type
Definition: common.hpp:86
static const bool value
Definition: common.hpp:85
typename std::decay< T >::type DecayedT
Definition: common.hpp:81
typename ::cereal::traits::strip_minimal< DecayedT >::type StrippedT
Definition: common.hpp:82
typename enum_underlying_type< StrippedT, value >::type base_type
Definition: common.hpp:87
type
The type the bitset is encoded with.
Definition: bitset.hpp:44
void serializeArray(Archive &ar, T &array, std::true_type)
Serialization for arrays if BinaryData is supported and we are arithmetic.
Definition: common.hpp:42
in certain simple scenarios. They should probably not be used if maximizing performance is the main o...
Definition: access.hpp:42
std::enable_if< common_detail::is_enum< T >::value, typename common_detail::is_enum< T >::base_type >::type CEREAL_SAVE_MINIMAL_FUNCTION_NAME(Archive const &, T const &t)
Saving for enum types.
Definition: common.hpp:95
CEREAL_SERIALIZE_FUNCTION_NAME(Archive &ar, NameValuePair< T > &t)
Serializing NVP types to binary.
Definition: binary.hpp:134
std::enable_if< common_detail::is_enum< T >::value, void >::type CEREAL_LOAD_MINIMAL_FUNCTION_NAME(Archive const &, T &&t, typename common_detail::is_enum< T >::base_type const &value)
Loading for enum types.
Definition: common.hpp:103
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
A wrapper around data that can be serialized in a binary fashion.
Definition: helpers.hpp:212
typename std::underlying_type< T >::type type
Definition: common.hpp:67
Gets the underlying type of an enum.
Definition: common.hpp:61
Used to delay a static_assert until template instantiation.
Definition: traits.hpp:57
Definition: traits.hpp:1112