Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/preprocessor/repetition/repeat.hpp>
- #include <boost/preprocessor/repetition/enum_params.hpp>
- #include <boost/preprocessor/repetition/enum_binary_params.hpp>
- #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
- #include <iostream>
- #ifdef __GNUC__
- # define MACRO_THREAD_LOCAL __thread
- #elif _MSC_VER
- # define MACRO_THREAD_LOCAL __declspec(thread)
- #else
- # error No idea how you use TLS on other compilers.
- #endif
- #define MACRO_CREATE_CALLBACK_STRUCT(z, n, data) \
- template<typename ObjT, typename R BOOST_PP_ENUM_TRAILING_PARAMS(n, typename ArgT)> \
- class Callback ## n \
- { \
- public: \
- typedef R (*CallbackFunc)(BOOST_PP_ENUM_PARAMS(n, ArgT)); \
- \
- private: \
- static MACRO_THREAD_LOCAL ObjT const* obj; \
- \
- static R theCallbackFunc(BOOST_PP_ENUM_BINARY_PARAMS(n, ArgT, arg)) \
- { \
- if(obj) \
- { \
- (*obj)(BOOST_PP_ENUM_PARAMS(n, arg)); \
- } \
- } \
- public: \
- static CallbackFunc use(ObjT const& o) \
- { \
- obj = &o; \
- return &theCallbackFunc; \
- } \
- }; \
- \
- template<typename ObjT, typename R BOOST_PP_ENUM_TRAILING_PARAMS(n, typename ArgT)> \
- MACRO_THREAD_LOCAL ObjT const* Callback ## n<ObjT, R BOOST_PP_ENUM_TRAILING_PARAMS(n, ArgT)>::obj = 0; \
- \
- template<typename R BOOST_PP_ENUM_TRAILING_PARAMS(n, typename ArgT), typename ObjT> \
- typename Callback ## n<ObjT, R BOOST_PP_ENUM_TRAILING_PARAMS(n, ArgT)>::CallbackFunc \
- useCCallback ## n(ObjT const& obj) \
- { \
- return Callback ## n<ObjT, R BOOST_PP_ENUM_TRAILING_PARAMS(n, ArgT)>::use(obj); \
- } \
- #define MACRO_CREATE_C_CALLBACK_HELPER(n) BOOST_PP_REPEAT(n, MACRO_CREATE_CALLBACK_STRUCT, )
- // Create callback helper for up to 9 parameters.
- MACRO_CREATE_C_CALLBACK_HELPER(10)
- typedef void (*CallbackPtr)(char const*);
- void cFunction(CallbackPtr ptr)
- {
- ptr("Hello!");
- }
- struct Output
- {
- char const* user;
- void operator ()(char const* message) const
- {
- std::cout << user << ": " << message << std::endl;
- }
- };
- int main()
- {
- Output output = { "KFS1_GOD" };
- cFunction(useCCallback1<void, char const*>(output));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement