Line data Source code
1 : /*! \file valarray.hpp
2 : \brief Support for types found in \<valarray\>
3 : \ingroup STLSupport */
4 :
5 : /*
6 : Copyright (c) 2014, Randolph Voorhies, Shane Grant
7 : All rights reserved.
8 :
9 : Redistribution and use in source and binary forms, with or without
10 : modification, are permitted provided that the following conditions are met:
11 : * Redistributions of source code must retain the above copyright
12 : notice, this list of conditions and the following disclaimer.
13 : * Redistributions in binary form must reproduce the above copyright
14 : notice, this list of conditions and the following disclaimer in the
15 : documentation and/or other materials provided with the distribution.
16 : * Neither the name of cereal nor the
17 : names of its contributors may be used to endorse or promote products
18 : derived from this software without specific prior written permission.
19 :
20 : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 : ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 : WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 : DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
24 : DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 : (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 : LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 : ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 : (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 : SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 : */
31 :
32 : #ifndef CEREAL_TYPES_VALARRAY_HPP_
33 : #define CEREAL_TYPES_VALARRAY_HPP_
34 :
35 : #include "cereal/cereal.hpp"
36 : #include <valarray>
37 :
38 : namespace cereal
39 : {
40 : //! Saving for std::valarray arithmetic types, using binary serialization, if supported
41 : template <class Archive, class T> inline
42 : typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>::value
43 : && std::is_arithmetic<T>::value, void>::type
44 400 : CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::valarray<T> const & valarray )
45 : {
46 400 : ar( make_size_tag( static_cast<size_type>(valarray.size()) ) ); // number of elements
47 400 : ar( binary_data( &valarray[0], valarray.size() * sizeof(T) ) ); // &valarray[0] ok since guaranteed contiguous
48 400 : }
49 200 :
50 : //! Loading for std::valarray arithmetic types, using binary serialization, if supported
51 200 : template <class Archive, class T> inline
52 200 : typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>::value
53 200 : && std::is_arithmetic<T>::value, void>::type
54 200 : CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::valarray<T> & valarray )
55 : {
56 200 : size_type valarraySize;
57 200 : ar( make_size_tag( valarraySize ) );
58 200 :
59 : valarray.resize( static_cast<std::size_t>( valarraySize ) );
60 : ar( binary_data( &valarray[0], static_cast<std::size_t>( valarraySize ) * sizeof(T) ) );
61 : }
62 :
63 : //! Saving for std::valarray all other types
64 400 : template <class Archive, class T> inline
65 : typename std::enable_if<!traits::is_output_serializable<BinaryData<T>, Archive>::value
66 : || !std::is_arithmetic<T>::value, void>::type
67 400 : CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::valarray<T> const & valarray )
68 : {
69 400 : ar( make_size_tag( static_cast<size_type>(valarray.size()) ) ); // number of elements
70 400 : for(auto && v : valarray)
71 400 : ar(v);
72 200 : }
73 :
74 : //! Loading for std::valarray all other types
75 200 : template <class Archive, class T> inline
76 : typename std::enable_if<!traits::is_input_serializable<BinaryData<T>, Archive>::value
77 200 : || !std::is_arithmetic<T>::value, void>::type
78 200 : CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::valarray<T> & valarray )
79 200 : {
80 200 : size_type valarraySize;
81 : ar( make_size_tag( valarraySize ) );
82 :
83 200 : valarray.resize( static_cast<size_t>( valarraySize ) );
84 : for(auto && v : valarray)
85 200 : ar(v);
86 200 : }
87 200 : } // namespace cereal
88 :
89 : #endif // CEREAL_TYPES_VALARRAY_HPP_
|