common.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 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 the copyright holder 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. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef CEREAL_TYPES_COMMON_HPP_
  29. #define CEREAL_TYPES_COMMON_HPP_
  30. #include "cereal/cereal.hpp"
  31. namespace cereal
  32. {
  33. namespace common_detail
  34. {
  35. //! Serialization for arrays if BinaryData is supported and we are arithmetic
  36. /*! @internal */
  37. template <class Archive, class T> inline
  38. void serializeArray( Archive & ar, T & array, std::true_type /* binary_supported */ )
  39. {
  40. ar( binary_data( array, sizeof(array) ) );
  41. }
  42. //! Serialization for arrays if BinaryData is not supported or we are not arithmetic
  43. /*! @internal */
  44. template <class Archive, class T> inline
  45. void serializeArray( Archive & ar, T & array, std::false_type /* binary_supported */ )
  46. {
  47. for( auto & i : array )
  48. ar( i );
  49. }
  50. namespace
  51. {
  52. //! Gets the underlying type of an enum
  53. /*! @internal */
  54. template <class T, bool IsEnum>
  55. struct enum_underlying_type : std::false_type {};
  56. //! Gets the underlying type of an enum
  57. /*! Specialization for when we actually have an enum
  58. @internal */
  59. template <class T>
  60. struct enum_underlying_type<T, true> { using type = typename std::underlying_type<T>::type; };
  61. } // anon namespace
  62. //! Checks if a type is an enum
  63. /*! This is needed over simply calling std::is_enum because the type
  64. traits checking at compile time will attempt to call something like
  65. load_minimal with a special NoConvertRef struct that wraps up the true type.
  66. This will strip away any of that and also expose the true underlying type.
  67. @internal */
  68. template <class T>
  69. class is_enum
  70. {
  71. private:
  72. using DecayedT = typename std::decay<T>::type;
  73. using StrippedT = typename ::cereal::traits::strip_minimal<DecayedT>::type;
  74. public:
  75. static const bool value = std::is_enum<StrippedT>::value;
  76. using type = StrippedT;
  77. using base_type = typename enum_underlying_type<StrippedT, value>::type;
  78. };
  79. }
  80. //! Saving for enum types
  81. template <class Archive, class T> inline
  82. typename std::enable_if<common_detail::is_enum<T>::value,
  83. typename common_detail::is_enum<T>::base_type>::type
  84. CEREAL_SAVE_MINIMAL_FUNCTION_NAME( Archive const &, T const & t )
  85. {
  86. return static_cast<typename common_detail::is_enum<T>::base_type>(t);
  87. }
  88. //! Loading for enum types
  89. template <class Archive, class T> inline
  90. typename std::enable_if<common_detail::is_enum<T>::value, void>::type
  91. CEREAL_LOAD_MINIMAL_FUNCTION_NAME( Archive const &, T && t,
  92. typename common_detail::is_enum<T>::base_type const & value )
  93. {
  94. t = reinterpret_cast<typename common_detail::is_enum<T>::type const &>( value );
  95. }
  96. //! Serialization for raw pointers
  97. /*! This exists only to throw a static_assert to let users know we don't support raw pointers. */
  98. template <class Archive, class T> inline
  99. void CEREAL_SERIALIZE_FUNCTION_NAME( Archive &, T * & )
  100. {
  101. static_assert(cereal::traits::detail::delay_static_assert<T>::value,
  102. "Cereal does not support serializing raw pointers - please use a smart pointer");
  103. }
  104. //! Serialization for C style arrays
  105. template <class Archive, class T> inline
  106. typename std::enable_if<std::is_array<T>::value, void>::type
  107. CEREAL_SERIALIZE_FUNCTION_NAME(Archive & ar, T & array)
  108. {
  109. common_detail::serializeArray( ar, array,
  110. std::integral_constant<bool, traits::is_output_serializable<BinaryData<T>, Archive>::value &&
  111. std::is_arithmetic<typename std::remove_all_extents<T>::type>::value>() );
  112. }
  113. } // namespace cereal
  114. #endif // CEREAL_TYPES_COMMON_HPP_