mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
Moving common serialization routines into common
This commit is contained in:
@@ -28,6 +28,7 @@
|
|||||||
#define CEREAL_ARCHIVES_BINARY_HPP_
|
#define CEREAL_ARCHIVES_BINARY_HPP_
|
||||||
|
|
||||||
#include <cereal/cereal.hpp>
|
#include <cereal/cereal.hpp>
|
||||||
|
#include <cereal/types/common.hpp>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
@@ -126,16 +127,6 @@ namespace cereal
|
|||||||
ar.loadBinary(std::addressof(t), sizeof(t));
|
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
|
//! Serializing NVP types to binary
|
||||||
template <class Archive, class T> inline
|
template <class Archive, class T> inline
|
||||||
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
|
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
|
||||||
@@ -169,14 +160,6 @@ namespace cereal
|
|||||||
{
|
{
|
||||||
ar.loadBinary(bd.data, bd.size);
|
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
|
} // namespace cereal
|
||||||
|
|
||||||
#endif // CEREAL_ARCHIVES_BINARY_HPP_
|
#endif // CEREAL_ARCHIVES_BINARY_HPP_
|
||||||
|
|||||||
52
include/cereal/types/common.hpp
Normal file
52
include/cereal/types/common.hpp
Normal 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_
|
||||||
52
sandbox.cpp
52
sandbox.cpp
@@ -210,35 +210,43 @@ namespace cereal
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
int x[5];
|
||||||
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};
|
|
||||||
|
|
||||||
{
|
{
|
||||||
std::ofstream os("out.txt");
|
std::ostringstream os;
|
||||||
cereal::BinaryOutputArchive archive(os);
|
cereal::BinaryOutputArchive archive(os);
|
||||||
archive(CEREAL_NVP(e_out));
|
archive( x );
|
||||||
archive(t2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Everything e_in;
|
|
||||||
|
|
||||||
{
|
//Everything e_out;
|
||||||
std::ifstream is("out.txt");
|
//e_out.x = 99;
|
||||||
cereal::BinaryInputArchive archive(is);
|
//e_out.y = 100;
|
||||||
archive(CEREAL_NVP(e_in));
|
//e_out.t1 = {1};
|
||||||
archive(t2);
|
//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");
|
// std::ofstream os("pt//r.txt");
|
||||||
|
|||||||
Reference in New Issue
Block a user