valarray.hpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*! \file valarray.hpp
  2. \brief Support for types found in \<valarray\>
  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 cereal 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_VALARRAY_HPP_
  29. #define CEREAL_TYPES_VALARRAY_HPP_
  30. #include "cereal/cereal.hpp"
  31. #include <valarray>
  32. namespace cereal
  33. {
  34. //! Saving for std::valarray arithmetic types, using binary serialization, if supported
  35. template <class Archive, class T> inline
  36. typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>::value
  37. && std::is_arithmetic<T>::value, void>::type
  38. CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::valarray<T> const & valarray )
  39. {
  40. ar( make_size_tag( static_cast<size_type>(valarray.size()) ) ); // number of elements
  41. ar( binary_data( &valarray[0], valarray.size() * sizeof(T) ) ); // &valarray[0] ok since guaranteed contiguous
  42. }
  43. //! Loading for std::valarray arithmetic types, using binary serialization, if supported
  44. template <class Archive, class T> inline
  45. typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>::value
  46. && std::is_arithmetic<T>::value, void>::type
  47. CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::valarray<T> & valarray )
  48. {
  49. size_type valarraySize;
  50. ar( make_size_tag( valarraySize ) );
  51. valarray.resize( static_cast<std::size_t>( valarraySize ) );
  52. ar( binary_data( &valarray[0], static_cast<std::size_t>( valarraySize ) * sizeof(T) ) );
  53. }
  54. //! Saving for std::valarray all other types
  55. template <class Archive, class T> inline
  56. typename std::enable_if<!traits::is_output_serializable<BinaryData<T>, Archive>::value
  57. || !std::is_arithmetic<T>::value, void>::type
  58. CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::valarray<T> const & valarray )
  59. {
  60. ar( make_size_tag( static_cast<size_type>(valarray.size()) ) ); // number of elements
  61. for(auto && v : valarray)
  62. ar(v);
  63. }
  64. //! Loading for std::valarray all other types
  65. template <class Archive, class T> inline
  66. typename std::enable_if<!traits::is_input_serializable<BinaryData<T>, Archive>::value
  67. || !std::is_arithmetic<T>::value, void>::type
  68. CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::valarray<T> & valarray )
  69. {
  70. size_type valarraySize;
  71. ar( make_size_tag( valarraySize ) );
  72. valarray.resize( static_cast<size_t>( valarraySize ) );
  73. for(auto && v : valarray)
  74. ar(v);
  75. }
  76. } // namespace cereal
  77. #endif // CEREAL_TYPES_VALARRAY_HPP_