Moving common serialization routines into common

This commit is contained in:
Shane Grant
2013-06-20 13:27:41 -07:00
parent d5579d696d
commit ad513228f1
3 changed files with 83 additions and 40 deletions

View File

@@ -28,6 +28,7 @@
#define CEREAL_ARCHIVES_BINARY_HPP_
#include <cereal/cereal.hpp>
#include <cereal/types/common.hpp>
#include <stack>
#include <sstream>
@@ -126,16 +127,6 @@ namespace cereal
ar.loadBinary(std::addressof(t), sizeof(t));
}
//! Serialization for enum types to binary
template<class Archive, class T> inline
typename std::enable_if<std::is_enum<T>::value &&
(std::is_same<Archive, BinaryInputArchive>::value || std::is_same<Archive, BinaryOutputArchive>::value),
void>::type
serialize(Archive & ar, T & t)
{
ar( reinterpret_cast<typename std::underlying_type<T>::type &>(t) );
}
//! Serializing NVP types to binary
template <class Archive, class T> inline
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
@@ -169,14 +160,6 @@ namespace cereal
{
ar.loadBinary(bd.data, bd.size);
}
template <class Archive, class T> inline
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
serialize( Archive & ar, T * & t )
{
static_assert(!sizeof(T), "Cereal does not support serializing raw pointers - please use a smart pointer");
}
} // namespace cereal
#endif // CEREAL_ARCHIVES_BINARY_HPP_

View File

@@ -0,0 +1,52 @@
/*
Copyright (c) 2013, Randolph Voorhies, Shane Grant
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of cereal nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CEREAL_TYPES_COMMON_HPP_
#define CEREAL_TYPES_COMMON_HPP_
#include <cereal/cereal.hpp>
namespace cereal
{
//! Serialization for enum types
template<class Archive, class T> inline
typename std::enable_if<std::is_enum<T>::value, void>::type
serialize(Archive & ar, T & t)
{
ar( reinterpret_cast<typename std::underlying_type<T>::type &>(t) );
}
//! Serialization for raw pointers
template <class Archive, class T> inline
void serialize( Archive & ar, T * & t )
{
static_assert(!sizeof(T), "Cereal does not support serializing raw pointers - please use a smart pointer");
}
// TODO: move C style arrays into here
} // namespace cereal
#endif // CEREAL_TYPES_COMMON_HPP_

View File

@@ -210,35 +210,43 @@ namespace cereal
int main()
{
Everything e_out;
e_out.x = 99;
e_out.y = 100;
e_out.t1 = {1};
e_out.t2 = {2};
e_out.t3 = {3};
e_out.t4 = {4};
e_out.s = "Hello, World!";
Test2 t2 = {22};
int x[5];
{
std::ofstream os("out.txt");
std::ostringstream os;
cereal::BinaryOutputArchive archive(os);
archive(CEREAL_NVP(e_out));
archive(t2);
archive( x );
}
Everything e_in;
{
std::ifstream is("out.txt");
cereal::BinaryInputArchive archive(is);
archive(CEREAL_NVP(e_in));
archive(t2);
}
//Everything e_out;
//e_out.x = 99;
//e_out.y = 100;
//e_out.t1 = {1};
//e_out.t2 = {2};
//e_out.t3 = {3};
//e_out.t4 = {4};
//e_out.s = "Hello, World!";
assert(e_in == e_out);//
//Test2 t2 = {22};
//{
// std::ofstream os("out.txt");
// cereal::BinaryOutputArchive archive(os);
// archive(CEREAL_NVP(e_out));
// archive(t2);
//}
//Everything e_in;
//{
// std::ifstream is("out.txt");
// cereal::BinaryInputArchive archive(is);
// archive(CEREAL_NVP(e_in));
// archive(t2);
//}
//assert(e_in == e_out);//
//
//{ //
// std::ofstream os("pt//r.txt");