diff --git a/include/fc/time.hpp b/include/fc/time.hpp index 57acead..d2bda19 100644 --- a/include/fc/time.hpp +++ b/include/fc/time.hpp @@ -10,6 +10,7 @@ namespace fc { explicit microseconds( int64_t c = 0) :_count(c){} static microseconds max() { return microseconds(0x7fffffffffffffffll); } friend microseconds operator + (const microseconds& l, const microseconds& r ) { return microseconds(l._count+r._count); } + friend microseconds operator - (const microseconds& l, const microseconds& r ) { return microseconds(l._count-r._count); } bool operator==(const microseconds& c)const { return _count == c._count; } friend bool operator>(const microseconds& a, const microseconds& b){ return a._count > b._count; } @@ -41,6 +42,7 @@ namespace fc { bool operator !=( const time_point& t )const { return elapsed._count !=t.elapsed._count; } time_point& operator += ( const microseconds& m ) { elapsed+=m; return *this; } friend time_point operator + ( const time_point& t, const microseconds& m ) { return time_point(t.elapsed+m); } + friend time_point operator - ( const time_point& t, const microseconds& m ) { return time_point(t.elapsed-m); } friend microseconds operator - ( const time_point& t, const time_point& m ) { return microseconds(t.elapsed.count() - m.elapsed.count()); } private: microseconds elapsed; diff --git a/include/fc/value_cast.hpp b/include/fc/value_cast.hpp index 52aaa55..d29ca2c 100644 --- a/include/fc/value_cast.hpp +++ b/include/fc/value_cast.hpp @@ -9,6 +9,7 @@ #include #include #include +#include //#include @@ -34,6 +35,28 @@ namespace fc { void cast_value( const value& v, T& t) { unpack(v,t); } + template + void cast_value( const value& v, std::vector& out ) { + if( v.type() != value::array_type ) { + FC_THROW_REPORT( "Error casting ${type} to array", fc::value("type", fc::reflector::to_string(v.type()) ) ); + } + out.resize(v.size()); + slog( "out .size %d", out.size() ); + const fc::vector& val = v.as_array(); + auto oitr = out.begin(); + int idx = 0; + for( auto itr = val.begin(); itr != val.end(); ++itr, ++oitr, ++idx ) { + try { + *oitr = itr->cast(); //value_cast(*itr); + // value_cast( *itr, *oitr ); + } catch ( fc::error_report& er ) { + throw FC_REPORT_PUSH( er, "Error casting value[${index}] to ${type}", + fc::value("index",idx) + ("type", fc::get_typename::name()) + ); + } + } + } template void cast_value( const value& v, fc::vector& out ) { @@ -41,12 +64,13 @@ namespace fc { FC_THROW_REPORT( "Error casting ${type} to array", fc::value("type", fc::reflector::to_string(v.type()) ) ); } out.resize(v.size()); + slog( "out .size %d", out.size() ); const fc::vector& val = v.as_array(); auto oitr = out.begin(); int idx = 0; for( auto itr = val.begin(); itr != val.end(); ++itr, ++oitr, ++idx ) { try { - *oitr = value_cast(*itr); + *oitr = itr->cast(); //value_cast(*itr); // value_cast( *itr, *oitr ); } catch ( fc::error_report& er ) { throw FC_REPORT_PUSH( er, "Error casting value[${index}] to ${type}", diff --git a/include/fc/value_io.hpp b/include/fc/value_io.hpp index a9efa4a..e69f002 100644 --- a/include/fc/value_io.hpp +++ b/include/fc/value_io.hpp @@ -54,6 +54,8 @@ namespace fc { void pack( fc::value& jsv, const fc::vector& value ); template void pack( fc::value& jsv, const fc::vector& value ); + template + void pack( fc::value& jsv, const std::vector& value ); inline void unpack( const fc::value& jsv, fc::value& v ) { v = jsv; } @@ -82,6 +84,8 @@ namespace fc { void unpack( const fc::value& jsv, fc::vector& value ); template void unpack( const fc::value& jsv, fc::vector& value ); + template + void unpack( const fc::value& jsv, std::vector& value ); namespace detail { template @@ -249,6 +253,19 @@ namespace fc { ++i; } } + template + inline void pack( fc::value& jsv, const std::vector& value ) { + jsv = fc::value::array(); + jsv.resize(value.size()); + auto itr = value.begin(); + auto end = value.end(); + uint32_t i = 0; + while( itr != end ) { + fc::pack( jsv[i], *itr ); + ++itr; + ++i; + } + } struct tuple_to_value_visitor { tuple_to_value_visitor( value& v ):_val(v),_count(0) { } template @@ -291,6 +308,14 @@ namespace fc { unpack( jsv[i], val[i] ); } } + template + inline void unpack( const fc::value& jsv, std::vector& val ) { + val.resize( jsv.size() ); + uint32_t s = jsv.size(); + for( uint32_t i = 0; i < s; ++i ) { + unpack( jsv[i], val[i] ); + } + } // default case template diff --git a/include/fc/vector.hpp b/include/fc/vector.hpp index 1a56e3e..4396c4e 100644 --- a/include/fc/vector.hpp +++ b/include/fc/vector.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace fc { diff --git a/src/value.cpp b/src/value.cpp index 9bfc788..3a11de4 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -202,6 +202,7 @@ namespace fc { void cast_value( const value& v, int64_t& out ){ v.visit( cast_visitor(out) ); + slog( "cast_value( v, int64: %lld )", out ); } void cast_value( const value& v, uint8_t& out ){