From 55b0daa8aa0447c237d1ccb5a6ed094675089438 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Mon, 12 Nov 2012 21:45:41 -0500 Subject: [PATCH] updating value casting for vectors --- include/fc/json_rpc_connection.hpp | 9 +---- include/fc/json_rpc_error_object.hpp | 3 ++ include/fc/tuple.hpp | 12 ++++++- include/fc/value_cast.hpp | 49 +++++++++++++++++++++++----- include/fc/vector_fwd.hpp | 2 -- src/json_rpc_error_object.cpp | 2 +- 6 files changed, 57 insertions(+), 20 deletions(-) diff --git a/include/fc/json_rpc_connection.hpp b/include/fc/json_rpc_connection.hpp index 5a28956..a2d4ff0 100644 --- a/include/fc/json_rpc_connection.hpp +++ b/include/fc/json_rpc_connection.hpp @@ -57,20 +57,13 @@ namespace fc { namespace json { template \ static value to_value( X&& x ) { return value( x.a0 ); }\ }; \ - template<> \ - struct named_param< fc::tuple > { \ - typedef fc::true_type type; \ - static tuple cast( const value& v ) { return make_tuple(fc::value_cast(v)); } \ - template \ - static value to_value( X&& x ) { return value( x.a0 ); }\ - }; \ } } } template struct rpc_server_method_impl : public rpc_server_method { rpc_server_method_impl( const std::function& f ):func(f){} virtual value call( const value& v ) { - return value( call_fused(func, named_param::cast(v) ) ); + return value( fc::call_fused(func, named_param::type>::cast(v) ) ); } std::function func; }; diff --git a/include/fc/json_rpc_error_object.hpp b/include/fc/json_rpc_error_object.hpp index 656c150..6c6e999 100644 --- a/include/fc/json_rpc_error_object.hpp +++ b/include/fc/json_rpc_error_object.hpp @@ -1,4 +1,7 @@ #pragma once +#include +#include +#include namespace fc { namespace json { diff --git a/include/fc/tuple.hpp b/include/fc/tuple.hpp index ed2653b..3bc5f67 100644 --- a/include/fc/tuple.hpp +++ b/include/fc/tuple.hpp @@ -60,6 +60,10 @@ namespace fc { template void visit( V&& v)const{}; }; + template + auto call_fused( Functor f, Tuple&& t ) -> decltype( f( ) ) { + return f(); + } inline tuple<> make_tuple(){ return tuple<>(); } @@ -73,6 +77,7 @@ namespace fc { #define ILIST_PARAMS_COPY(z,n,data) BOOST_PP_CAT(a,n)( t.BOOST_PP_CAT(a,n) ) #define VISIT_PARAMS(z,n,data) v(BOOST_PP_CAT(a,n)); #define LIST_MEMBERS_ON(z,n,data) data.BOOST_PP_CAT(a,n) + #define DEDUCE_MEMBERS(z,n,data) typename fc::deduce::type #define FORWARD_PARAMS(z,n,data) fc::forward(BOOST_PP_CAT(a,n)) #define MEM_PARAMS(z,n,data) BOOST_PP_CAT(A,n) BOOST_PP_CAT(a,n); #define TUPLE(z,n,unused) \ @@ -102,12 +107,17 @@ namespace fc { template \ struct is_tuple > { \ typedef fc::true_type type; \ - }; + }; \ + template \ + struct deduce > { \ + typedef fc::tuple type; \ + }; BOOST_PP_REPEAT_FROM_TO( 1, 8, TUPLE, unused ) #undef FORWARD_PARAMS + #undef DEDUCE_MEMBERS #undef RREF_PARAMS #undef LIST_MEMBERS_ON #undef ILIST_PARAMS diff --git a/include/fc/value_cast.hpp b/include/fc/value_cast.hpp index 60e0ce5..19f78fb 100644 --- a/include/fc/value_cast.hpp +++ b/include/fc/value_cast.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -107,6 +108,40 @@ namespace fc { private: value::object& m_out; }; + + template + struct cast_visitor> : value::const_visitor { + cast_visitor( fc::vector& out ) + :m_out(out){} + virtual void operator()( const int8_t& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const int16_t& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const int32_t& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const int64_t& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const uint8_t& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const uint16_t& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const uint32_t& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const uint64_t& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const float& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const double& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const bool& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const fc::string& v ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const value::object& a ) { FC_THROW_MSG("bad cast");} + virtual void operator()( const value::array& a ) { + m_out.resize(0); + m_out.reserve( a.fields.size() ); + for( auto i = a.fields.begin(); i != a.fields.end(); ++i ) { + m_out.push_back( value_cast( *i ) ); + } + } + + virtual void operator()( ) { FC_THROW_MSG("bad cast");} + + private: + fc::vector& m_out; + }; + + + template<> struct cast_visitor : value::value::const_visitor { virtual void operator()( const int8_t& v ) { FC_THROW_MSG("bad cast");} @@ -130,8 +165,8 @@ namespace fc { template static T cast( const value& v ) { slog( "cast non tuple %s", typeid(T).name() ); - T out; - v.visit(cast_visitor(out)); + typename fc::deduce::type out; + v.visit(cast_visitor(out)); return out; } }; @@ -149,12 +184,10 @@ namespace fc { int idx; }; - template - static T cast( const value& v ) { - T out; + template + static Tuple cast( const value& v ) { + typename fc::deduce::type out; out.visit( member_visitor(v) ); - slog( "cast tuple" ); - // v.visit(cast_visitor(out)); return out; } }; @@ -219,7 +252,7 @@ namespace fc { */ template T value_cast( const value& v ) { - return detail::cast_if_reflected::is_defined>::template cast(v); + return detail::cast_if_reflected::type>::is_defined>::template cast< typename fc::deduce::type >(v); } template diff --git a/include/fc/vector_fwd.hpp b/include/fc/vector_fwd.hpp index e1f3d7d..a57fec7 100644 --- a/include/fc/vector_fwd.hpp +++ b/include/fc/vector_fwd.hpp @@ -5,8 +5,6 @@ #else namespace fc { template class vector; - template struct reflector; - template struct reflector< fc::vector >; }; #endif diff --git a/src/json_rpc_error_object.cpp b/src/json_rpc_error_object.cpp index 3fc186d..944d5f1 100644 --- a/src/json_rpc_error_object.cpp +++ b/src/json_rpc_error_object.cpp @@ -1,6 +1,6 @@ #include namespace fc { namespace json { - error_object::error_object( const fc::string& msg, fc::value v, int c ) + error_object::error_object( const fc::string& m, fc::value v, int64_t c ) :code(c),message(m),data(fc::move(v)){} }}