array.hpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*! \file array.hpp
  2. \brief Support for types found in \<array\>
  3. \ingroup STLSupport */
  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_ARRAY_HPP_
  29. #define CEREAL_TYPES_ARRAY_HPP_
  30. #include "cereal/cereal.hpp"
  31. #include <array>
  32. namespace cereal
  33. {
  34. //! Saving for std::array primitive types
  35. //! using binary serialization, if supported
  36. template <class Archive, class T, size_t N> inline
  37. typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>::value
  38. && std::is_arithmetic<T>::value, void>::type
  39. CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::array<T, N> const & array )
  40. {
  41. ar( binary_data( array.data(), sizeof(array) ) );
  42. }
  43. //! Loading for std::array primitive types
  44. //! using binary serialization, if supported
  45. template <class Archive, class T, size_t N> inline
  46. typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>::value
  47. && std::is_arithmetic<T>::value, void>::type
  48. CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::array<T, N> & array )
  49. {
  50. ar( binary_data( array.data(), sizeof(array) ) );
  51. }
  52. //! Saving for std::array all other types
  53. template <class Archive, class T, size_t N> inline
  54. typename std::enable_if<!traits::is_output_serializable<BinaryData<T>, Archive>::value
  55. || !std::is_arithmetic<T>::value, void>::type
  56. CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::array<T, N> const & array )
  57. {
  58. for( auto const & i : array )
  59. ar( i );
  60. }
  61. //! Loading for std::array all other types
  62. template <class Archive, class T, size_t N> inline
  63. typename std::enable_if<!traits::is_input_serializable<BinaryData<T>, Archive>::value
  64. || !std::is_arithmetic<T>::value, void>::type
  65. CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::array<T, N> & array )
  66. {
  67. for( auto & i : array )
  68. ar( i );
  69. }
  70. } // namespace cereal
  71. #endif // CEREAL_TYPES_ARRAY_HPP_