cereal
A C++11 library for serialization
valarray.hpp
Go to the documentation of this file.
1 
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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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 {
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  CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::valarray<T> const & valarray )
45  {
46  ar( make_size_tag( static_cast<size_type>(valarray.size()) ) ); // number of elements
47  ar( binary_data( &valarray[0], valarray.size() * sizeof(T) ) ); // &valarray[0] ok since guaranteed contiguous
48  }
49 
51  template <class Archive, class T> inline
52  typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>::value
53  && std::is_arithmetic<T>::value, void>::type
54  CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::valarray<T> & valarray )
55  {
56  size_type valarraySize;
57  ar( make_size_tag( valarraySize ) );
58 
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 
64  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  CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::valarray<T> const & valarray )
68  {
69  ar( make_size_tag( static_cast<size_type>(valarray.size()) ) ); // number of elements
70  for(auto && v : valarray)
71  ar(v);
72  }
73 
75  template <class Archive, class T> inline
76  typename std::enable_if<!traits::is_input_serializable<BinaryData<T>, Archive>::value
77  || !std::is_arithmetic<T>::value, void>::type
78  CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::valarray<T> & valarray )
79  {
80  size_type valarraySize;
81  ar( make_size_tag( valarraySize ) );
82 
83  valarray.resize( static_cast<size_t>( valarraySize ) );
84  for(auto && v : valarray)
85  ar(v);
86  }
87 } // namespace cereal
88 
89 #endif // CEREAL_TYPES_VALARRAY_HPP_
CEREAL_LOAD_FUNCTION_NAME
#define CEREAL_LOAD_FUNCTION_NAME
The deserialization (load) function name to search for.
Definition: macros.hpp:85
CEREAL_SAVE_FUNCTION_NAME
#define CEREAL_SAVE_FUNCTION_NAME
The serialization (save) function name to search for.
Definition: macros.hpp:92
cereal.hpp
Main cereal functionality.
cereal::size_type
CEREAL_SIZE_TYPE size_type
The size type used by cereal.
Definition: helpers.hpp:61