Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle trivially-copyable types correctly #769

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/cereal/types/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace cereal
//! using binary serialization, if supported
template <class Archive, class T, size_t N> inline
typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>::value
&& std::is_arithmetic<T>::value, void>::type
&& std::is_trivially_copyable<T>::value, void>::type
CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::array<T, N> const & array )
{
ar( binary_data( array.data(), sizeof(array) ) );
Expand All @@ -49,7 +49,7 @@ namespace cereal
//! using binary serialization, if supported
template <class Archive, class T, size_t N> inline
typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>::value
&& std::is_arithmetic<T>::value, void>::type
&& std::is_trivially_copyable<T>::value, void>::type
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::array<T, N> & array )
{
ar( binary_data( array.data(), sizeof(array) ) );
Expand All @@ -58,7 +58,7 @@ namespace cereal
//! Saving for std::array all other types
template <class Archive, class T, size_t N> inline
typename std::enable_if<!traits::is_output_serializable<BinaryData<T>, Archive>::value
|| !std::is_arithmetic<T>::value, void>::type
|| !std::is_trivially_copyable<T>::value, void>::type
CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::array<T, N> const & array )
{
for( auto const & i : array )
Expand All @@ -68,7 +68,7 @@ namespace cereal
//! Loading for std::array all other types
template <class Archive, class T, size_t N> inline
typename std::enable_if<!traits::is_input_serializable<BinaryData<T>, Archive>::value
|| !std::is_arithmetic<T>::value, void>::type
|| !std::is_trivially_copyable<T>::value, void>::type
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::array<T, N> & array )
{
for( auto & i : array )
Expand Down
6 changes: 3 additions & 3 deletions include/cereal/types/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ namespace cereal
{
namespace common_detail
{
//! Serialization for arrays if BinaryData is supported and we are arithmetic
//! Serialization for arrays if BinaryData is supported and we are trivially-copyable
/*! @internal */
template <class Archive, class T> inline
void serializeArray( Archive & ar, T & array, std::true_type /* binary_supported */ )
{
ar( binary_data( array, sizeof(array) ) );
}

//! Serialization for arrays if BinaryData is not supported or we are not arithmetic
//! Serialization for arrays if BinaryData is not supported or we are not trivially-copyable
/*! @internal */
template <class Archive, class T> inline
void serializeArray( Archive & ar, T & array, std::false_type /* binary_supported */ )
Expand Down Expand Up @@ -122,7 +122,7 @@ namespace cereal
{
common_detail::serializeArray( ar, array,
std::integral_constant<bool, traits::is_output_serializable<BinaryData<T>, Archive>::value &&
std::is_arithmetic<typename std::remove_all_extents<T>::type>::value>() );
std::is_trivially_copyable<typename std::remove_all_extents<T>::type>::value>() );
}
} // namespace cereal

Expand Down
12 changes: 6 additions & 6 deletions include/cereal/types/valarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

namespace cereal
{
//! Saving for std::valarray arithmetic types, using binary serialization, if supported
//! Saving for std::valarray trivially-copyable types, using binary serialization, if supported
template <class Archive, class T> inline
typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>::value
&& std::is_arithmetic<T>::value, void>::type
&& std::is_trivially_copyable<T>::value, void>::type
CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::valarray<T> const & valarray )
{
ar( make_size_tag( static_cast<size_type>(valarray.size()) ) ); // number of elements
ar( binary_data( &valarray[0], valarray.size() * sizeof(T) ) ); // &valarray[0] ok since guaranteed contiguous
}

//! Loading for std::valarray arithmetic types, using binary serialization, if supported
//! Loading for std::valarray trivially-copyable types, using binary serialization, if supported
template <class Archive, class T> inline
typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>::value
&& std::is_arithmetic<T>::value, void>::type
&& std::is_trivially_copyable<T>::value, void>::type
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::valarray<T> & valarray )
{
size_type valarraySize;
Expand All @@ -63,7 +63,7 @@ namespace cereal
//! Saving for std::valarray all other types
template <class Archive, class T> inline
typename std::enable_if<!traits::is_output_serializable<BinaryData<T>, Archive>::value
|| !std::is_arithmetic<T>::value, void>::type
|| !std::is_trivially_copyable<T>::value, void>::type
CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::valarray<T> const & valarray )
{
ar( make_size_tag( static_cast<size_type>(valarray.size()) ) ); // number of elements
Expand All @@ -74,7 +74,7 @@ namespace cereal
//! Loading for std::valarray all other types
template <class Archive, class T> inline
typename std::enable_if<!traits::is_input_serializable<BinaryData<T>, Archive>::value
|| !std::is_arithmetic<T>::value, void>::type
|| !std::is_trivially_copyable<T>::value, void>::type
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::valarray<T> & valarray )
{
size_type valarraySize;
Expand Down
16 changes: 8 additions & 8 deletions include/cereal/types/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@

namespace cereal
{
//! Serialization for std::vectors of arithmetic (but not bool) using binary serialization, if supported
//! Serialization for std::vectors of trivially-copyable (but not bool) using binary serialization, if supported
template <class Archive, class T, class A> inline
typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>::value
&& std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, void>::type
&& std::is_trivially_copyable<T>::value && !std::is_same<T, bool>::value, void>::type
CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::vector<T, A> const & vector )
{
ar( make_size_tag( static_cast<size_type>(vector.size()) ) ); // number of elements
ar( binary_data( vector.data(), vector.size() * sizeof(T) ) );
}

//! Serialization for std::vectors of arithmetic (but not bool) using binary serialization, if supported
//! Serialization for std::vectors of trivially-copyable (but not bool) using binary serialization, if supported
template <class Archive, class T, class A> inline
typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>::value
&& std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, void>::type
&& std::is_trivially_copyable<T>::value && !std::is_same<T, bool>::value, void>::type
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::vector<T, A> & vector )
{
size_type vectorSize;
Expand All @@ -58,21 +58,21 @@ namespace cereal
ar( binary_data( vector.data(), static_cast<std::size_t>( vectorSize ) * sizeof(T) ) );
}

//! Serialization for non-arithmetic vector types
//! Serialization for non-trivially-copyable vector types
template <class Archive, class T, class A> inline
typename std::enable_if<(!traits::is_output_serializable<BinaryData<T>, Archive>::value
|| !std::is_arithmetic<T>::value) && !std::is_same<T, bool>::value, void>::type
|| !std::is_trivially_copyable<T>::value) && !std::is_same<T, bool>::value, void>::type
CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::vector<T, A> const & vector )
{
ar( make_size_tag( static_cast<size_type>(vector.size()) ) ); // number of elements
for(auto && v : vector)
ar( v );
}

//! Serialization for non-arithmetic vector types
//! Serialization for non-trivially-copyable vector types
template <class Archive, class T, class A> inline
typename std::enable_if<(!traits::is_input_serializable<BinaryData<T>, Archive>::value
|| !std::is_arithmetic<T>::value) && !std::is_same<T, bool>::value, void>::type
|| !std::is_trivially_copyable<T>::value) && !std::is_same<T, bool>::value, void>::type
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::vector<T, A> & vector )
{
size_type size;
Expand Down