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:
Shane Grant
2013-06-14 12:17:21 -07:00
parent 6821955323
commit e9d277025f
10 changed files with 114 additions and 37 deletions

View File

@@ -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;
};
// ######################################################################