mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
const_cast for save(), seeking for binary_oarchive
Using const cast to get rid of having two versions of save (changed vector and array so far) Initial implementation of seeking to allow saving and restoring position within a binary archive
This commit is contained in:
5
Makefile
5
Makefile
@@ -5,3 +5,8 @@ unittests: unittests.cpp
|
||||
g++ -std=c++0x unittests.cpp -o unittests -lboost_unit_test_framework -I./..
|
||||
./unittests --show_progress
|
||||
|
||||
boost_serialize: boost_serialize.cpp
|
||||
g++ -std=c++11 boost_serialize.cpp -o boost_serialize -lboost_serialization -I./..
|
||||
|
||||
clean:
|
||||
rm test; rm unittests;
|
||||
|
||||
@@ -31,15 +31,6 @@ namespace cereal
|
||||
ar & i;
|
||||
}
|
||||
|
||||
//! Saving for non-const std::array all other types to binary
|
||||
template <class T, size_t N>
|
||||
typename std::enable_if<!std::is_arithmetic<T>::value, void>::type
|
||||
save( BinaryOutputArchive & ar, std::array<T, N> & array )
|
||||
{
|
||||
for( auto & i : array )
|
||||
ar & i;
|
||||
}
|
||||
|
||||
//! Loading for std::array all other types to binary
|
||||
template <class T, size_t N>
|
||||
typename std::enable_if<!std::is_arithmetic<T>::value, void>::type
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define CEREAL_BINARY_ARCHIVE_BINARY_ARCHIVE_HPP_
|
||||
|
||||
#include <cereal/cereal.hpp>
|
||||
#include <stack>
|
||||
|
||||
namespace cereal
|
||||
{
|
||||
@@ -23,8 +24,40 @@ namespace cereal
|
||||
throw 1; // TODO: something terrible
|
||||
}
|
||||
|
||||
//! Pushes a placeholder for data onto the archive and saves its position
|
||||
void pushPosition( size_t size )
|
||||
{
|
||||
itsPositionStack.push( itsStream.tellp() );
|
||||
itsStream.write( 0, size );
|
||||
}
|
||||
|
||||
//! Pops the most recently pushed position onto the archive, going to the end
|
||||
//! of the archive if the stack is empty
|
||||
/*! @return true if the stack is empty and we are at the end of the archive */
|
||||
bool popPosition()
|
||||
{
|
||||
if( itsPositionStack.empty() ) // seek to end of stream
|
||||
{
|
||||
itsStream.seekp( 0, std::ios_base::end );
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
itsStream.seekp( itsPositionStack.top() );
|
||||
itsPositionStack.pop();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//! Resets the position stack and seeks to the end of the archive
|
||||
void resetPosition()
|
||||
{
|
||||
while( !popPosition() );
|
||||
}
|
||||
|
||||
private:
|
||||
std::ostream & itsStream;
|
||||
std::stack<std::ostream::pos_type> itsPositionStack;
|
||||
};
|
||||
|
||||
// ######################################################################
|
||||
|
||||
@@ -10,22 +10,42 @@ namespace cereal
|
||||
template <class T, class A>
|
||||
void save( BinaryOutputArchive & ar, std::forward_list<T, A> const & forward_list )
|
||||
{
|
||||
// TODO: This is linear time! We may want to do something smarter
|
||||
ar & std::distance(forward_list.begin(), forward_list.end());
|
||||
// save position for size of list
|
||||
ar.pushPosition(sizeof(size_t));
|
||||
|
||||
// write the list
|
||||
size_t size = 0;
|
||||
for( const auto & i : forward_list )
|
||||
{
|
||||
ar & i;
|
||||
++size;
|
||||
}
|
||||
|
||||
// write the size
|
||||
ar.popPosition();
|
||||
ar & size;
|
||||
ar.resetPosition();
|
||||
}
|
||||
|
||||
//! Saving for std::forward_list all other types to binary (non-const version)
|
||||
template <class T, class A>
|
||||
void save( BinaryOutputArchive & ar, std::forward_list<T, A> & forward_list )
|
||||
{
|
||||
// TODO: This is linear time! We may want to do something smarter
|
||||
ar & std::distance(forward_list.begin(), forward_list.end());
|
||||
// save position for size of list
|
||||
ar.pushPosition(sizeof(size_t));
|
||||
|
||||
// write the list
|
||||
size_t size = 0;
|
||||
for( auto & i : forward_list )
|
||||
{
|
||||
ar & i;
|
||||
++size;
|
||||
}
|
||||
|
||||
// write the size
|
||||
ar.popPosition();
|
||||
ar & size;
|
||||
ar.resetPosition();
|
||||
}
|
||||
|
||||
//! Loading for std::forward_list all other types from binary
|
||||
|
||||
@@ -43,16 +43,6 @@ namespace cereal
|
||||
ar & (*it);
|
||||
}
|
||||
|
||||
//! Serialization for non-arithmetic (and bool) vector types to binary (non-const version)
|
||||
template <class T, class A>
|
||||
typename std::enable_if<!std::is_arithmetic<T>::value || std::is_same<T, bool>::value, void>::type
|
||||
save( BinaryOutputArchive & ar, std::vector<T, A> & vector )
|
||||
{
|
||||
ar & vector.size(); // number of elements
|
||||
for( auto it = vector.begin(), end = vector.end(); it != end; ++it )
|
||||
ar & (*it);
|
||||
}
|
||||
|
||||
//! Serialization for non-arithmetic (and bool) vector types from binary
|
||||
template <class T, class A>
|
||||
typename std::enable_if<!std::is_arithmetic<T>::value || std::is_same<T, bool>::value, void>::type
|
||||
|
||||
34
boost_serialize.cpp
Normal file
34
boost_serialize.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <boost/serialization/serialization.hpp>
|
||||
#include <boost/archive/text_oarchive.hpp>
|
||||
#include <boost/archive/text_iarchive.hpp>
|
||||
#include <boost/serialization/vector.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
struct myStruct
|
||||
{
|
||||
myStruct() : x(0) {}
|
||||
int x;
|
||||
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int version)
|
||||
{
|
||||
ar & x;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::archive::text_oarchive oa(std::cout);
|
||||
|
||||
const myStruct v;
|
||||
|
||||
oa & v;
|
||||
|
||||
std::cout << BOOST_NO_FUNCTION_TEMPLATE_ORDERING << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
10
cereal.hpp
10
cereal.hpp
@@ -45,9 +45,9 @@ namespace cereal
|
||||
template <class T>
|
||||
typename std::enable_if<traits::is_output_serializable<T, ArchiveType>() && traits::has_member_serialize<T, ArchiveType>(),
|
||||
ArchiveType &>::type
|
||||
operator & (T && t)
|
||||
operator & (T const & t)
|
||||
{
|
||||
t.serialize(*self);
|
||||
const_cast<T &>(t).serialize(*self);
|
||||
return *self;
|
||||
}
|
||||
|
||||
@@ -55,9 +55,9 @@ namespace cereal
|
||||
template <class T>
|
||||
typename std::enable_if<traits::is_output_serializable<T, ArchiveType>() && traits::has_non_member_serialize<T, ArchiveType>(),
|
||||
ArchiveType &>::type
|
||||
operator & (T && t)
|
||||
operator & (T const & t)
|
||||
{
|
||||
serialize(*self, std::forward<T>(t));
|
||||
serialize(*self, const_cast<T &>(t));
|
||||
return *self;
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ namespace cereal
|
||||
ArchiveType &>::type
|
||||
operator & (T && t)
|
||||
{
|
||||
serialize(*self, t);
|
||||
serialize(*self, std::forward<T>(t));
|
||||
return *self;
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ namespace cereal
|
||||
template<typename T, class A>
|
||||
struct has_member_save< T, A,
|
||||
typename Void<
|
||||
decltype( std::declval<T&>().save( std::declval<A&>() ) )
|
||||
decltype( std::declval<T const &>().save( std::declval<A&>() ) )
|
||||
>::type
|
||||
>: std::true_type {};
|
||||
|
||||
|
||||
4
test.cpp
4
test.cpp
@@ -205,5 +205,9 @@ int main()
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
std::ostringstream os;
|
||||
std::cout << "Testing:" << std::endl;
|
||||
//os.write(, 5);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ struct StructInternalSplit : StructBase
|
||||
StructInternalSplit() : StructBase{0,0} {}
|
||||
StructInternalSplit(int x_, int y_) : StructBase{x_,y_} {}
|
||||
template<class Archive>
|
||||
void save(Archive & ar)
|
||||
void save(Archive & ar) const
|
||||
{
|
||||
ar & x & y;
|
||||
}
|
||||
@@ -74,7 +74,7 @@ struct StructExternalSplit : StructBase
|
||||
};
|
||||
|
||||
template<class Archive>
|
||||
void save(Archive & ar, StructExternalSplit & s)
|
||||
void save(Archive & ar, StructExternalSplit const & s)
|
||||
{
|
||||
ar & s.x & s.y;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user