📄 interface.hpp
字号:
}; \ \ vtable const * ptr_vtable; \ void * ptr_that; \ \ template<class T> struct vtable_holder \ { \ static vtable const val_vtable; \ }; \ \ public: \ \ template<class T> \ inline name (T & that) \ : ptr_vtable(& vtable_holder<T>::val_vtable) \ , ptr_that(boost::addressof(that)) \ { } \ \ BOOST_EXAMPLE_INTERFACE__MEMBERS(def,FUNCTION) \ }; \ \ template<typename T> \ name ::vtable const name ::vtable_holder<T>::val_vtable \ = { BOOST_EXAMPLE_INTERFACE__MEMBERS(def,INIT_VTABLE) }#ifdef BOOST_PP_NIL // never defined -- a comment with syntax highlightingBOOST_EXAMPLE_INTERFACE( interface_x, (( a_func, (void)(int), const_qualified )) (( another_func, (int), non_const )) );// expands to:class interface_x { struct vtable { // meta information for first function template<typename T = void*> struct inf0 { typedef void result; typedef ::boost::mpl::vector< T, int > params; }; // function pointer with void* context pointer and parameters optimized // for forwarding ::example::vtable_entry<inf0<> >::type func0; // second function template<typename T = void*> struct inf1 { typedef int result; typedef ::boost::mpl::vector< T > params; }; ::example::vtable_entry<inf1<> >::type func1; }; // data members vtable const * ptr_vtable; void * ptr_that; // this template is instantiated for every class T this interface is created // from, causing the compiler to emit an initialized vtable for this type // (see aggregate assignment, below) template<class T> struct vtable_holder { static vtable const val_vtable; }; public: // converting ctor, creates an interface from an arbitrary class template<class T> inline interface_x (T & that) : ptr_vtable(& vtable_holder<T>::val_vtable) , ptr_that(boost::addressof(that)) { } // the member functions from the interface definition, parameters are // optimized for forwarding inline vtable::inf0<> ::result a_func ( ::example::param<vtable::inf0<>,1>::type p0) const { return ptr_vtable-> func0(ptr_that , p0); } inline vtable::inf1<> ::result another_func () { return ptr_vtable-> func1(ptr_that ); } }; template<typename T> interface_x ::vtable const interface_x ::vtable_holder<T>::val_vtable = { // instantiate function templates that wrap member function pointers (which // are known at compile time) by taking their addresses in assignment to // function pointer context & ::example::member< vtable::inf0<T>, ::example::ft:: const_qualified > ::template wrap < &T:: a_func > , & ::example::member< vtable::inf1<T>, ::example::ft:: non_const > ::template wrap < &T:: another_func > };#endif// preprocessing code details// iterate all of the interface's members and invoke a macro (prefixed with// BOOST_EXAMPLE_INTERFACE_)#define BOOST_EXAMPLE_INTERFACE__MEMBERS(seq,macro) \ BOOST_PP_REPEAT(BOOST_PP_SEQ_SIZE(seq), \ BOOST_EXAMPLE_INTERFACE__ ## macro,seq)// extract signature sequence from entry #define BOOST_EXAMPLE_INTERFACE__VTABLE(z,i,seq) \ BOOST_EXAMPLE_INTERFACE__VTABLE_I(z,i, \ BOOST_PP_TUPLE_ELEM(3,1,BOOST_PP_SEQ_ELEM(i,seq)))// split the signature sequence result/params and insert T at the beginning of// the params part#define BOOST_EXAMPLE_INTERFACE__VTABLE_I(z,i,seq) \ BOOST_EXAMPLE_INTERFACE__VTABLE_II(z,i, \ BOOST_PP_SEQ_HEAD(seq),(T)BOOST_PP_SEQ_TAIL(seq))// emit the meta information structure and function pointer declaration#define BOOST_EXAMPLE_INTERFACE__VTABLE_II(z,i,result_type,param_types) \ template<typename T = void*> \ struct BOOST_PP_CAT(inf,i) \ { \ typedef result_type result; \ typedef ::boost::mpl::vector< BOOST_PP_SEQ_ENUM(param_types) > params; \ }; \ ::example::vtable_entry<BOOST_PP_CAT(inf,i)<> >::type BOOST_PP_CAT(func,i);// extract tuple entry from sequence and precalculate the name of the function// pointer variable#define BOOST_EXAMPLE_INTERFACE__INIT_VTABLE(z,i,seq) \ BOOST_EXAMPLE_INTERFACE__INIT_VTABLE_I(i,seq,BOOST_PP_CAT(func,i), \ BOOST_PP_SEQ_ELEM(i,seq))// emit a function pointer expression that encapsulates the corresponding// member function of T#define BOOST_EXAMPLE_INTERFACE__INIT_VTABLE_I(i,seq,func,desc) \ BOOST_PP_COMMA_IF(i) & ::example::member< BOOST_PP_CAT(vtable::inf,i)<T>, \ ::example::ft:: BOOST_PP_TUPLE_ELEM(3,2,desc) >::template wrap \ < &T:: BOOST_PP_TUPLE_ELEM(3,0,desc) > // extract tuple entry from sequence#define BOOST_EXAMPLE_INTERFACE__FUNCTION(z,i,seq) \ BOOST_EXAMPLE_INTERFACE__FUNCTION_I(z,i,BOOST_PP_SEQ_ELEM(i,seq))// precalculate function name, arity, name of meta info structure and cv-// qualifiers#define BOOST_EXAMPLE_INTERFACE__FUNCTION_I(z,i,desc) \ BOOST_EXAMPLE_INTERFACE__FUNCTION_II(z,i, \ BOOST_PP_TUPLE_ELEM(3,0,desc), \ BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(BOOST_PP_TUPLE_ELEM(3,1,desc))), \ BOOST_PP_CAT(vtable::inf,i)<>, \ BOOST_PP_CAT(BOOST_EXAMPLE_INTERFACE___,BOOST_PP_TUPLE_ELEM(3,2,desc)) \ )// emit the definition for a member function of the interface#define BOOST_EXAMPLE_INTERFACE__FUNCTION_II(z,i,name,arity,types,cv) \ inline types ::result name \ (BOOST_PP_ENUM_ ## z (arity,BOOST_EXAMPLE_INTERFACE__PARAM,types)) cv() \ { \ return ptr_vtable-> BOOST_PP_CAT(func,i)(ptr_that \ BOOST_PP_ENUM_TRAILING_PARAMS_Z(z,arity,p)); \ }// emit a parameter of the function definition#define BOOST_EXAMPLE_INTERFACE__PARAM(z,j,types) \ ::example::param<types,BOOST_PP_INC(j)>::type BOOST_PP_CAT(p,j)// helper macros to map 'const_qualified' to 'const' an 'non_const' to ''#define BOOST_EXAMPLE_INTERFACE___const_qualified BOOST_PP_IDENTITY(const)#define BOOST_EXAMPLE_INTERFACE___non_const BOOST_PP_EMPTY
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -