Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
api.hpp
Go to the documentation of this file.
1#pragma once
2#include <fc/thread/future.hpp>
3#include <fc/any.hpp>
4#include <functional>
5#include <boost/config.hpp>
6
7// ms visual c++ (as of 2013) doesn't accept the standard syntax for calling a
8// templated member function (foo->template bar();)
9#ifdef _MSC_VER
10# define FC_CALL_MEMBER_TEMPLATE_KEYWORD
11#else
12# define FC_CALL_MEMBER_TEMPLATE_KEYWORD template
13#endif
14
15namespace fc {
17 template<typename R, typename C, typename P, typename... Args>
18 static std::function<R(Args...)> functor( P&& p, R (C::*mem_func)(Args...) );
19 template<typename R, typename C, typename P, typename... Args>
20 static std::function<R(Args...)> functor( P&& p, R (C::*mem_func)(Args...)const );
21 };
22
23 template< typename Interface, typename Transform >
24 struct vtable : public std::enable_shared_from_this<vtable<Interface,Transform>>
25 { private: vtable(); };
26
27 template<typename OtherType>
29 typedef OtherType other_type;
30
31 vtable_copy_visitor( OtherType& s):_source( s ){}
32
33 template<typename R, typename MemberPtr, typename... Args>
34 void operator()( const char* name, std::function<R(Args...)>& memb, MemberPtr m )const {
35 OtherType* src = &_source;
36 memb = [src,m]( Args... args ){ return (src->*m)(args...); };
37 }
38 OtherType& _source;
39 };
40
41 template<typename Interface, typename Transform >
42 class api;
43
44 class api_connection;
45
47
49 {
50 public:
52 virtual ~api_base() {}
53
54 virtual uint64_t get_handle()const = 0;
55
56 virtual api_id_type register_api( api_connection& conn )const = 0;
57
58 // defined in api_connection.hpp
59 template< typename T >
61 };
62 typedef std::shared_ptr< api_base > api_ptr;
63
64 class api_connection;
65
66 template<typename Interface, typename Transform = identity_member >
67 class api : public api_base {
68 public:
70
71 api():_vtable( std::make_shared<vtable_type>() ) {}
72
74 template<typename T >
75 api( const T& p )
76 :_vtable( std::make_shared<vtable_type>() )
77 {
78 _data = std::make_shared<fc::any>(p);
79 T& ptr = boost::any_cast<T&>(*_data);
80 auto& pointed_at = *ptr;
81 typedef typename std::remove_reference<decltype(pointed_at)>::type source_vtable_type;
82 _vtable->FC_CALL_MEMBER_TEMPLATE_KEYWORD visit_other( vtable_copy_visitor<source_vtable_type>(pointed_at) );
83 }
84
85 api( const api& cpy ):_vtable(cpy._vtable),_data(cpy._data) {}
86 virtual ~api() {}
87
88 friend bool operator == ( const api& a, const api& b ) { return a._data == b._data && a._vtable == b._vtable; }
89 friend bool operator != ( const api& a, const api& b ) { return !(a._data == b._data && a._vtable == b._vtable); }
90 virtual uint64_t get_handle()const override { return uint64_t(_data.get()); }
91 virtual api_id_type register_api( api_connection& conn )const override; // defined in api_connection.hpp
92
94 vtable_type* operator->()const { FC_ASSERT( _vtable ); return _vtable.get(); }
95
96 protected:
97 std::shared_ptr<vtable_type> _vtable;
98 std::shared_ptr<fc::any> _data;
99 };
100
101} // namespace fc
102
103#include <boost/preprocessor/repeat.hpp>
104#include <boost/preprocessor/repetition/enum_binary_params.hpp>
105#include <boost/preprocessor/repetition/enum_params.hpp>
106#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
107#include <boost/preprocessor/facilities/empty.hpp>
108#include <boost/preprocessor/seq/for_each.hpp>
109#include <boost/preprocessor/stringize.hpp>
110
111#define FC_API_VTABLE_DEFINE_MEMBER( r, data, elem ) \
112 decltype(Transform::functor( (data*)nullptr, &data::elem)) elem;
113#define FC_API_VTABLE_DEFINE_VISIT_OTHER( r, data, elem ) \
114 { typedef typename Visitor::other_type OtherType; \
115 v( BOOST_PP_STRINGIZE(elem), elem, &OtherType::elem ); }
116#define FC_API_VTABLE_DEFINE_VISIT( r, data, elem ) \
117 v( BOOST_PP_STRINGIZE(elem), elem );
118
119#define FC_API( CLASS, METHODS ) \
120namespace fc { \
121 template<typename Transform> \
122 struct vtable<CLASS,Transform> : public std::enable_shared_from_this<vtable<CLASS,Transform>> { \
123 BOOST_PP_SEQ_FOR_EACH( FC_API_VTABLE_DEFINE_MEMBER, CLASS, METHODS ) \
124 template<typename Visitor> \
125 void visit_other( Visitor&& v ){ \
126 BOOST_PP_SEQ_FOR_EACH( FC_API_VTABLE_DEFINE_VISIT_OTHER, CLASS, METHODS ) \
127 } \
128 template<typename Visitor> \
129 void visit( Visitor&& v ){ \
130 BOOST_PP_SEQ_FOR_EACH( FC_API_VTABLE_DEFINE_VISIT, CLASS, METHODS ) \
131 } \
132 }; \
133}
134
const mie::Vuint & p
Definition bn.cpp:27
std::string name
virtual uint64_t get_handle() const =0
api< T, identity_member > as()
virtual ~api_base()
Definition api.hpp:52
virtual api_id_type register_api(api_connection &conn) const =0
virtual api_id_type register_api(api_connection &conn) const override
vtable_type & operator*() const
Definition api.hpp:93
virtual ~api()
Definition api.hpp:86
friend bool operator==(const api &a, const api &b)
Definition api.hpp:88
api()
Definition api.hpp:71
virtual uint64_t get_handle() const override
Definition api.hpp:90
vtable< Interface, Transform > vtable_type
Definition api.hpp:69
api(const api &cpy)
Definition api.hpp:85
vtable_type * operator->() const
Definition api.hpp:94
std::shared_ptr< fc::any > _data
Definition api.hpp:98
api(const T &p)
Definition api.hpp:75
friend bool operator!=(const api &a, const api &b)
Definition api.hpp:89
std::shared_ptr< vtable_type > _vtable
Definition api.hpp:97
#define P
Definition dtoa.c:437
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
namespace sysio::chain
Definition authority.cpp:3
uint32_t api_id_type
Definition api.hpp:46
std::shared_ptr< api_base > api_ptr
Definition api.hpp:62
Definition name.hpp:106
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define T(meth, val, expected)
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
static std::function< R(Args...)> functor(P &&p, R(C::*mem_func)(Args...))
static std::function< R(Args...)> functor(P &&p, R(C::*mem_func)(Args...) const)
OtherType & _source
Definition api.hpp:38
OtherType other_type
Definition api.hpp:29
vtable_copy_visitor(OtherType &s)
Definition api.hpp:31
void operator()(const char *name, std::function< R(Args...)> &memb, MemberPtr m) const
Definition api.hpp:34
#define R
char * s