Cleanup API code
This commit is contained in:
parent
171eb212cd
commit
3b6f2978b0
2 changed files with 16 additions and 49 deletions
|
|
@ -3,33 +3,19 @@
|
||||||
#include <fc/any.hpp>
|
#include <fc/any.hpp>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <boost/config.hpp>
|
#include <boost/config.hpp>
|
||||||
#include <boost/preprocessor/repeat.hpp>
|
|
||||||
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
|
|
||||||
#include <boost/preprocessor/repetition/enum_params.hpp>
|
|
||||||
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
|
||||||
#include <boost/preprocessor/facilities/empty.hpp>
|
|
||||||
|
|
||||||
namespace fc {
|
namespace fc {
|
||||||
struct identity_member {
|
struct identity_member {
|
||||||
template<typename R, typename C, typename P, typename... Args>
|
template<typename R, typename C, typename P, typename... Args>
|
||||||
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...) );
|
||||||
return std::function<R(Args...)>([=](Args... args){ return (p->*mem_func)(args...); });
|
|
||||||
}
|
|
||||||
template<typename R, typename C, typename P, typename... Args>
|
template<typename R, typename C, typename P, typename... Args>
|
||||||
static std::function<R(Args...)> functor( P&& p, R (C::*mem_func)(Args...)const ) {
|
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...); });
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template< typename Interface, typename Transform >
|
template< typename Interface, typename Transform >
|
||||||
struct vtable : public std::enable_shared_from_this<vtable<Interface,Transform>>
|
struct vtable : public std::enable_shared_from_this<vtable<Interface,Transform>>
|
||||||
{
|
{ private: vtable(); };
|
||||||
private:
|
|
||||||
vtable();
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template<typename OtherType>
|
template<typename OtherType>
|
||||||
struct vtable_copy_visitor {
|
struct vtable_copy_visitor {
|
||||||
typedef OtherType other_type;
|
typedef OtherType other_type;
|
||||||
|
|
@ -39,10 +25,7 @@ namespace fc {
|
||||||
template<typename R, typename MemberPtr, typename... Args>
|
template<typename R, typename MemberPtr, typename... Args>
|
||||||
void operator()( const char* name, std::function<R(Args...)>& memb, MemberPtr m )const {
|
void operator()( const char* name, std::function<R(Args...)>& memb, MemberPtr m )const {
|
||||||
OtherType* src = &_source;
|
OtherType* src = &_source;
|
||||||
memb = [src,m]( Args... args ){
|
memb = [src,m]( Args... args ){ return (src->*m)(args...); };
|
||||||
wdump( (uint64_t(src) ) );
|
|
||||||
return (src->*m)(args...);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
OtherType& _source;
|
OtherType& _source;
|
||||||
};
|
};
|
||||||
|
|
@ -52,9 +35,7 @@ namespace fc {
|
||||||
public:
|
public:
|
||||||
typedef vtable<Interface,Transform> vtable_type;
|
typedef vtable<Interface,Transform> vtable_type;
|
||||||
|
|
||||||
api()
|
api():_vtable( std::make_shared<vtable_type>() ) {}
|
||||||
:_vtable( std::make_shared<vtable_type>() )
|
|
||||||
{}
|
|
||||||
|
|
||||||
/** T is anything with pointer semantics */
|
/** T is anything with pointer semantics */
|
||||||
template<typename T >
|
template<typename T >
|
||||||
|
|
@ -68,30 +49,26 @@ namespace fc {
|
||||||
_vtable->template visit_other( vtable_copy_visitor<source_vtable_type>(pointed_at) );
|
_vtable->template visit_other( vtable_copy_visitor<source_vtable_type>(pointed_at) );
|
||||||
}
|
}
|
||||||
|
|
||||||
api( const api& cpy )
|
api( const api& cpy ):_vtable(cpy._vtable),_data(cpy._data) {}
|
||||||
:_vtable(cpy._vtable),_data(cpy._data)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
friend bool operator == ( const api& a, const api& b ) { return a._data == b._data && a._vtable == b._vtable; }
|
friend bool operator == ( const api& a, const api& b ) { return a._data == b._data && a._vtable == b._vtable; }
|
||||||
friend bool operator != ( const api& a, const api& b ) { return !(a._data == b._data && a._vtable == b._vtable); }
|
friend bool operator != ( const api& a, const api& b ) { return !(a._data == b._data && a._vtable == b._vtable); }
|
||||||
|
|
||||||
vtable_type& operator*()const { wdump((uint64_t(this))); assert(_vtable); FC_ASSERT( _vtable ); return *_vtable; }
|
vtable_type& operator*()const { FC_ASSERT( _vtable ); return *_vtable; }
|
||||||
vtable_type* operator->()const {
|
vtable_type* operator->()const { FC_ASSERT( _vtable ); return _vtable.get(); }
|
||||||
assert(_vtable);
|
|
||||||
FC_ASSERT( _vtable );
|
|
||||||
return _vtable.get();
|
|
||||||
}
|
|
||||||
void test();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::shared_ptr<vtable_type> _vtable;
|
std::shared_ptr<vtable_type> _vtable;
|
||||||
std::shared_ptr<fc::any> _data;
|
std::shared_ptr<fc::any> _data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace fc
|
||||||
|
|
||||||
}
|
#include <boost/preprocessor/repeat.hpp>
|
||||||
|
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
|
||||||
|
#include <boost/preprocessor/repetition/enum_params.hpp>
|
||||||
|
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
||||||
|
#include <boost/preprocessor/facilities/empty.hpp>
|
||||||
#include <boost/preprocessor/seq/for_each.hpp>
|
#include <boost/preprocessor/seq/for_each.hpp>
|
||||||
#include <boost/preprocessor/stringize.hpp>
|
#include <boost/preprocessor/stringize.hpp>
|
||||||
|
|
||||||
|
|
@ -117,6 +94,5 @@ namespace fc { \
|
||||||
BOOST_PP_SEQ_FOR_EACH( FC_API_VTABLE_DEFINE_VISIT, CLASS, METHODS ) \
|
BOOST_PP_SEQ_FOR_EACH( FC_API_VTABLE_DEFINE_VISIT, CLASS, METHODS ) \
|
||||||
} \
|
} \
|
||||||
}; \
|
}; \
|
||||||
}
|
}
|
||||||
//#undef FC_API_VTABLE_DEFINE_MEMBER
|
|
||||||
//#undef FC_API_VTABLE_DEFINE_VISIT
|
|
||||||
|
|
|
||||||
|
|
@ -54,15 +54,6 @@ namespace fc {
|
||||||
return f();
|
return f();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
template<typename R, typename Arg0>
|
|
||||||
R call_generic( const std::function<R(Arg0)>& f, variants::const_iterator a0, variants::const_iterator e )const
|
|
||||||
{
|
|
||||||
FC_ASSERT( a0 != e );
|
|
||||||
return f(a0->as<Arg0>());
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
template<typename R, typename Arg0, typename ... Args>
|
template<typename R, typename Arg0, typename ... Args>
|
||||||
R call_generic( const std::function<R(Arg0,Args...)>& f, variants::const_iterator a0, variants::const_iterator e )const
|
R call_generic( const std::function<R(Arg0,Args...)>& f, variants::const_iterator a0, variants::const_iterator e )const
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue