api progess
This commit is contained in:
parent
4194a609c2
commit
ee9c2ac4ed
5 changed files with 73 additions and 28 deletions
|
|
@ -232,12 +232,16 @@ target_include_directories(fc
|
|||
#target_link_libraries( fc PUBLIC easylzma_static scrypt udt ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library})
|
||||
target_link_libraries( fc PUBLIC easylzma_static udt ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library})
|
||||
|
||||
add_executable( api tests/api.cpp )
|
||||
target_link_libraries( api fc )
|
||||
|
||||
add_executable( ntp_test ntp_test.cpp )
|
||||
target_link_libraries( ntp_test fc )
|
||||
|
||||
add_executable( task_cancel_test tests/task_cancel.cpp )
|
||||
target_link_libraries( task_cancel_test fc )
|
||||
|
||||
|
||||
add_executable( real128_test tests/real128_test.cpp )
|
||||
target_link_libraries( real128_test fc )
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#pragma once
|
||||
#include <fc/ptr.hpp>
|
||||
#include <fc/api.hpp>
|
||||
#include <fc/thread/thread.hpp>
|
||||
|
||||
namespace fc {
|
||||
|
|
@ -50,7 +50,7 @@ namespace fc {
|
|||
* returns a future.
|
||||
*/
|
||||
template<typename Interface>
|
||||
class actor : public ptr<Interface, detail::actor_member> {
|
||||
class actor : public api<Interface, detail::actor_member> {
|
||||
public:
|
||||
actor(){}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,17 +12,6 @@ namespace fc {
|
|||
|
||||
namespace detail {
|
||||
struct identity_member {
|
||||
#ifdef BOOST_NO_VARIADIC_TEMPLATES
|
||||
#define RPC_MEMBER_FUNCTOR(z,n,IS_CONST) \
|
||||
template<typename R, typename C, typename P BOOST_PP_ENUM_TRAILING_PARAMS( n, typename A)> \
|
||||
static std::function<R( BOOST_PP_ENUM_PARAMS(n,A) ) > \
|
||||
functor( P p, R (C::*mem_func)(BOOST_PP_ENUM_PARAMS(n,A)) IS_CONST ){ \
|
||||
return [=](BOOST_PP_ENUM_BINARY_PARAMS(n,A,a)){ return (p->*mem_func)(BOOST_PP_ENUM_PARAMS(n,a)); }; \
|
||||
}
|
||||
BOOST_PP_REPEAT( 8, RPC_MEMBER_FUNCTOR, const )
|
||||
BOOST_PP_REPEAT( 8, RPC_MEMBER_FUNCTOR, BOOST_PP_EMPTY() )
|
||||
#undef RPC_MEMBER_FUNCTOR
|
||||
#else
|
||||
template<typename R, typename C, typename P, typename... Args>
|
||||
static std::function<R(Args...)> functor( P&& p, R (C::*mem_func)(Args...) ) {
|
||||
return std::function<R(Args...)>([=](Args... args){ return (p->*mem_func)(args...); });
|
||||
|
|
@ -31,7 +20,6 @@ namespace fc {
|
|||
static std::function<R(Args...)> functor( P&& p, R (C::*mem_func)(Args...)const ) {
|
||||
return std::function<R(Args...)>([=](Args... args){ return (p->*mem_func)(args...); });
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
template< typename Interface, typename Transform = detail::identity_member >
|
||||
|
|
@ -51,20 +39,20 @@ namespace fc {
|
|||
} // namespace detail
|
||||
|
||||
template<typename Interface, typename Transform = detail::identity_member >
|
||||
class ptr {
|
||||
class api {
|
||||
public:
|
||||
typedef detail::vtable<Interface,Transform> vtable_type;
|
||||
|
||||
ptr(){}
|
||||
api(){}
|
||||
|
||||
template<typename InterfaceType>
|
||||
ptr( InterfaceType* p )
|
||||
api( InterfaceType* p )
|
||||
:_vtable( new vtable_type() ) {
|
||||
_vtable->template visit_other<InterfaceType>( detail::vtable_visitor<InterfaceType*>(p) );
|
||||
}
|
||||
|
||||
template<typename InterfaceType>
|
||||
ptr( const fc::shared_ptr<InterfaceType>& p )
|
||||
api( const fc::shared_ptr<InterfaceType>& p )
|
||||
:_vtable( new vtable_type() ),_self(p){
|
||||
_vtable->template visit_other<InterfaceType>( detail::vtable_visitor<InterfaceType*>(p.get()) );
|
||||
}
|
||||
|
|
@ -86,28 +74,28 @@ namespace fc {
|
|||
#include <boost/preprocessor/seq/for_each.hpp>
|
||||
#include <boost/preprocessor/stringize.hpp>
|
||||
|
||||
#define FC_STUB_VTABLE_DEFINE_MEMBER( r, data, elem ) \
|
||||
#define FC_API_VTABLE_DEFINE_MEMBER( r, data, elem ) \
|
||||
decltype(Transform::functor( (data*)nullptr, &data::elem)) elem;
|
||||
#define FC_STUB_VTABLE_DEFINE_VISIT_OTHER( r, data, elem ) \
|
||||
#define FC_API_VTABLE_DEFINE_VISIT_OTHER( r, data, elem ) \
|
||||
v( BOOST_PP_STRINGIZE(elem), elem, &T::elem );
|
||||
#define FC_STUB_VTABLE_DEFINE_VISIT( r, data, elem ) \
|
||||
#define FC_API_VTABLE_DEFINE_VISIT( r, data, elem ) \
|
||||
v( BOOST_PP_STRINGIZE(elem), elem );
|
||||
|
||||
#define FC_STUB( CLASS, METHODS ) \
|
||||
#define FC_API( CLASS, METHODS ) \
|
||||
namespace fc { namespace detail { \
|
||||
template<typename Transform> \
|
||||
struct vtable<CLASS,Transform> : public fc::retainable { \
|
||||
vtable(){} \
|
||||
BOOST_PP_SEQ_FOR_EACH( FC_STUB_VTABLE_DEFINE_MEMBER, CLASS, METHODS ) \
|
||||
BOOST_PP_SEQ_FOR_EACH( FC_API_VTABLE_DEFINE_MEMBER, CLASS, METHODS ) \
|
||||
template<typename T, typename Visitor> \
|
||||
void visit_other( Visitor&& v ){ \
|
||||
BOOST_PP_SEQ_FOR_EACH( FC_STUB_VTABLE_DEFINE_VISIT_OTHER, CLASS, METHODS ) \
|
||||
BOOST_PP_SEQ_FOR_EACH( FC_API_VTABLE_DEFINE_VISIT_OTHER, CLASS, METHODS ) \
|
||||
} \
|
||||
template<typename Visitor> \
|
||||
void visit( Visitor&& v ){ \
|
||||
BOOST_PP_SEQ_FOR_EACH( FC_STUB_VTABLE_DEFINE_VISIT, CLASS, METHODS ) \
|
||||
BOOST_PP_SEQ_FOR_EACH( FC_API_VTABLE_DEFINE_VISIT, CLASS, METHODS ) \
|
||||
} \
|
||||
}; \
|
||||
} }
|
||||
//#undef FC_STUB_VTABLE_DEFINE_MEMBER
|
||||
//#undef FC_STUB_VTABLE_DEFINE_VISIT
|
||||
} }
|
||||
//#undef FC_API_VTABLE_DEFINE_MEMBER
|
||||
//#undef FC_API_VTABLE_DEFINE_VISIT
|
||||
45
tests/api.cpp
Normal file
45
tests/api.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include <fc/api.hpp>
|
||||
#include <fc/log/logger.hpp>
|
||||
#include <fc/rpc/api_server.hpp>
|
||||
|
||||
class calculator
|
||||
{
|
||||
public:
|
||||
int32_t add( int32_t a, int32_t b ); // not implemented
|
||||
int32_t sub( int32_t a, int32_t b ); // not implemented
|
||||
};
|
||||
|
||||
FC_API( calculator, (add)(sub) )
|
||||
|
||||
class some_calculator
|
||||
{
|
||||
public:
|
||||
int32_t add( int32_t a, int32_t b ) { return a+b; }
|
||||
int32_t sub( int32_t a, int32_t b ) { return a-b; }
|
||||
};
|
||||
class variant_calculator
|
||||
{
|
||||
public:
|
||||
double add( fc::variant a, fc::variant b ) { return a.as_double()+b.as_double(); }
|
||||
double sub( fc::variant a, fc::variant b ) { return a.as_double()-b.as_double(); }
|
||||
};
|
||||
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
some_calculator calc;
|
||||
variant_calculator vcalc;
|
||||
fc::api<calculator> api_calc( &calc );
|
||||
fc::api<calculator> api_vcalc( &vcalc );
|
||||
fc::api<calculator> api_nested_calc( api_calc );
|
||||
wdump( (api_calc->add(5,4)) );
|
||||
wdump( (api_calc->sub(5,4)) );
|
||||
wdump( (api_vcalc->add(5,4)) );
|
||||
wdump( (api_vcalc->sub(5,4)) );
|
||||
wdump( (api_nested_calc->sub(5,4)) );
|
||||
wdump( (api_nested_calc->sub(5,4)) );
|
||||
|
||||
fc::api_server server;
|
||||
server.register_api( api_calc );
|
||||
return 0;
|
||||
}
|
||||
8
tests/rpc.cpp
Normal file
8
tests/rpc.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
}
|
||||
Loading…
Reference in a new issue