Cleanup API code

This commit is contained in:
Nathan Hourt 2015-03-11 11:15:11 -04:00
parent 171eb212cd
commit 3b6f2978b0
2 changed files with 16 additions and 49 deletions

View file

@ -3,33 +3,19 @@
#include <fc/any.hpp>
#include <functional>
#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 {
struct identity_member {
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...); });
}
static std::function<R(Args...)> functor( P&& p, R (C::*mem_func)(Args...) );
template<typename R, typename C, typename P, typename... Args>
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...); });
}
static std::function<R(Args...)> functor( P&& p, R (C::*mem_func)(Args...)const );
};
template< typename Interface, typename Transform >
struct vtable : public std::enable_shared_from_this<vtable<Interface,Transform>>
{
private:
vtable();
};
{ private: vtable(); };
template<typename OtherType>
struct vtable_copy_visitor {
typedef OtherType other_type;
@ -39,10 +25,7 @@ namespace fc {
template<typename R, typename MemberPtr, typename... Args>
void operator()( const char* name, std::function<R(Args...)>& memb, MemberPtr m )const {
OtherType* src = &_source;
memb = [src,m]( Args... args ){
wdump( (uint64_t(src) ) );
return (src->*m)(args...);
};
memb = [src,m]( Args... args ){ return (src->*m)(args...); };
}
OtherType& _source;
};
@ -52,9 +35,7 @@ namespace fc {
public:
typedef vtable<Interface,Transform> vtable_type;
api()
:_vtable( std::make_shared<vtable_type>() )
{}
api():_vtable( std::make_shared<vtable_type>() ) {}
/** T is anything with pointer semantics */
template<typename T >
@ -68,30 +49,26 @@ namespace fc {
_vtable->template visit_other( vtable_copy_visitor<source_vtable_type>(pointed_at) );
}
api( const api& cpy )
:_vtable(cpy._vtable),_data(cpy._data)
{
}
api( const api& cpy ):_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); }
vtable_type& operator*()const { wdump((uint64_t(this))); assert(_vtable); FC_ASSERT( _vtable ); return *_vtable; }
vtable_type* operator->()const {
assert(_vtable);
FC_ASSERT( _vtable );
return _vtable.get();
}
void test();
vtable_type& operator*()const { FC_ASSERT( _vtable ); return *_vtable; }
vtable_type* operator->()const { FC_ASSERT( _vtable ); return _vtable.get(); }
protected:
std::shared_ptr<vtable_type> _vtable;
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/stringize.hpp>
@ -117,6 +94,5 @@ namespace fc { \
BOOST_PP_SEQ_FOR_EACH( FC_API_VTABLE_DEFINE_VISIT, CLASS, METHODS ) \
} \
}; \
}
//#undef FC_API_VTABLE_DEFINE_MEMBER
//#undef FC_API_VTABLE_DEFINE_VISIT
}

View file

@ -54,15 +54,6 @@ namespace fc {
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>
R call_generic( const std::function<R(Arg0,Args...)>& f, variants::const_iterator a0, variants::const_iterator e )const
{