Skip to content

Commit

Permalink
Merge pull request #723 from bitshares/extension-serialization
Browse files Browse the repository at this point in the history
Update serialization for extension class
  • Loading branch information
abitmore authored Mar 19, 2018
2 parents c391be0 + 15c6d0d commit 27f4137
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
72 changes: 45 additions & 27 deletions libraries/chain/include/graphene/chain/protocol/ext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

namespace graphene { namespace chain {

using fc::unsigned_int;

template< typename T >
struct extension
{
Expand All @@ -54,43 +56,39 @@ struct graphene_extension_pack_count_visitor
template< typename Stream, typename T >
struct graphene_extension_pack_read_visitor
{
graphene_extension_pack_read_visitor( Stream& s, const T& v ) : stream(s), value(v) {}
graphene_extension_pack_read_visitor( Stream& s, const T& v, uint32_t _max_depth )
: stream(s), value(v), max_depth(_max_depth - 1)
{
FC_ASSERT( _max_depth > 0 );
}

template<typename Member, class Class, Member (Class::*member)>
void operator()( const char* name )const
{
if( (value.*member).valid() )
{
fc::raw::pack( stream, unsigned_int( which ) );
fc::raw::pack( stream, *(value.*member) );
fc::raw::pack( stream, unsigned_int( which ), max_depth );
fc::raw::pack( stream, *(value.*member), max_depth );
}
++which;
}

Stream& stream;
const T& value;
mutable uint32_t which = 0;
const uint32_t max_depth;
};

template< typename Stream, class T >
void operator<<( Stream& stream, const graphene::chain::extension<T>& value )
{
graphene_extension_pack_count_visitor<T> count_vtor( value.value );
fc::reflector<T>::visit( count_vtor );
fc::raw::pack( stream, unsigned_int( count_vtor.count ) );
graphene_extension_pack_read_visitor<Stream,T> read_vtor( stream, value.value );
fc::reflector<T>::visit( read_vtor );
}



template< typename Stream, typename T >
struct graphene_extension_unpack_visitor
{
graphene_extension_unpack_visitor( Stream& s, T& v ) : stream(s), value(v)
graphene_extension_unpack_visitor( Stream& s, T& v, uint32_t _max_depth )
: stream(s), value(v), max_depth(_max_depth - 1)
{
FC_ASSERT( _max_depth > 0 );
unsigned_int c;
fc::raw::unpack( stream, c );
fc::raw::unpack( stream, c, max_depth );
count_left = c.value;
maybe_read_next_which();
}
Expand All @@ -100,7 +98,7 @@ struct graphene_extension_unpack_visitor
if( count_left > 0 )
{
unsigned_int w;
fc::raw::unpack( stream, w );
fc::raw::unpack( stream, w, max_depth );
next_which = w.value;
}
}
Expand All @@ -111,7 +109,7 @@ struct graphene_extension_unpack_visitor
if( (count_left > 0) && (which == next_which) )
{
typename Member::value_type temp;
fc::raw::unpack( stream, temp );
fc::raw::unpack( stream, temp, max_depth );
(value.*member) = temp;
--count_left;
maybe_read_next_which();
Expand All @@ -127,17 +125,9 @@ struct graphene_extension_unpack_visitor

Stream& stream;
T& value;
const uint32_t max_depth;
};

template< typename Stream, typename T >
void operator>>( Stream& s, graphene::chain::extension<T>& value )
{
value = graphene::chain::extension<T>();
graphene_extension_unpack_visitor<Stream, T> vtor( s, value.value );
fc::reflector<T>::visit( vtor );
FC_ASSERT( vtor.count_left == 0 ); // unrecognized extension throws here
}

} } // graphene::chain

namespace fc {
Expand Down Expand Up @@ -209,4 +199,32 @@ void to_variant( const graphene::chain::extension<T>& value, fc::variant& var )
var = vtor.mvo;
}

namespace raw {

template< typename Stream, typename T >
void pack( Stream& stream, const graphene::chain::extension<T>& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH )
{
FC_ASSERT( _max_depth > 0 );
--_max_depth;
graphene::chain::graphene_extension_pack_count_visitor<T> count_vtor( value.value );
fc::reflector<T>::visit( count_vtor );
fc::raw::pack( stream, unsigned_int( count_vtor.count ), _max_depth );
graphene::chain::graphene_extension_pack_read_visitor<Stream,T> read_vtor( stream, value.value, _max_depth );
fc::reflector<T>::visit( read_vtor );
}


template< typename Stream, typename T >
void unpack( Stream& s, graphene::chain::extension<T>& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH )
{
FC_ASSERT( _max_depth > 0 );
--_max_depth;
value = graphene::chain::extension<T>();
graphene::chain::graphene_extension_unpack_visitor<Stream, T> vtor( s, value.value, _max_depth );
fc::reflector<T>::visit( vtor );
FC_ASSERT( vtor.count_left == 0 ); // unrecognized extension throws here
}

} // fc::raw

} // fc
3 changes: 3 additions & 0 deletions libraries/chain/include/graphene/chain/protocol/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include <fc/safe.hpp>
#include <fc/container/flat.hpp>
#include <fc/string.hpp>

#include <graphene/chain/protocol/ext.hpp>

#include <fc/io/raw.hpp>
#include <fc/uint128.hpp>
#include <fc/static_variant.hpp>
Expand Down
1 change: 0 additions & 1 deletion libraries/chain/protocol/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* THE SOFTWARE.
*/
#include <graphene/chain/protocol/account.hpp>
#include <graphene/chain/hardfork.hpp>

namespace graphene { namespace chain {

Expand Down
2 changes: 1 addition & 1 deletion libraries/fc
Submodule fc updated 101 files

0 comments on commit 27f4137

Please sign in to comment.