Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Take the member function of the C++ class as the callback function of the C function

License

NotificationsYou must be signed in to change notification settings

znone/call_thunk

Repository files navigation

Generally, the C function can not directly callback the member functions of the C++ class. Some C functions pass the this pointer by providing additional application data pointer parameters to solve this problem. But other C functions do not have such a design, but thunk technology can be used to solve this problem. Call_thunk is the library of the member functions that use thunk technology to callback the C++ class from the C function.

Note: all the code for call_thunk is in the namespace call_thunk.

In the 32 - bit environment of X86, the common function calling conventions are cdecl, stdcall, fastcall, and the member functions, as well as thiscall. On windows, the default calling Convention for common functions is cdecl, and the default calling convention of the member functions of the C++ class is thiscall. On Linux, all the default calling conventions for all functions are cdecl.

In the 64 bit environment of x86-64, the only function calling convention is fastcall.

Call_thunk can convert between these function calling conventions.Call_thunk supports Windows and Linux.

enum call_declare{cc_fastcall,//__FASTCALL__cc_cdecl,//__CDECL__cc_stdcall,//__STDCALL__cc_thiscall,//__THISCALL__};

Usage

Demo

There are the following C function declarations:

typedefvoid (*cb_type)(int,void*);voidtest_cb(cb_typecb);

The called C++ class is defined as follows:

structTestClass{voidfun(int,void*);};

Class unsafe_thunk

Class unsafe_thunk is:

classunsafe_thunkexplicitunsafe_thunk(size_t argc,const argument_info* arginfos =NULL, call_declare caller = default_caller, call_declare callee = default_callee);template<typename T, typename PROC>unsafe_thunk(T* object, PROC proc,size_t argc,const argument_info* arginfos =NULL, call_declare caller = default_caller, call_declare callee = default_callee);template<typename T, typename PROC>void bind(T& object, PROC proc);template<typename Callback>operator Callback()const;};

Using the class unsafe_thunk is very simple to implement the member functions of the C++ class from the C function:

TestClass obj;call_thunk::unsafe_thunkthunk(2);//Two integer parametersthunk.bind(obj, &TestClass::fun);test_cb(thunk);

Structure argument_info

When the parameters of the callback function are not all integers or pointers that are consistent with the length of the CPU word, when using the class unsafe_thunk, we need to provide more parameter information through second parameters. The parameter information required for thunk is defined by the structure argument_info:

structargument_info{short _size;//Parameter size calculated in bytesbool _is_floating;//Whether the parameter is floating point};

Class thunk (Only support C++11)

Class unsafe_thunk does not check the consistency of the number and type of member functions and callback functions. Therefore, if the wrong parameters are passed, it may cause the program to crash. If you use the C++11 development program, you can use the class thunk instead of unsafe_thunk. The class thunk checks whether the number and type of the parameter of the member function and the callback function are consistent, and it is simpler to use and does not need to provide parameter information.

template<typename CallerRet,typename... CallerArgs>classthunk{typedefCallerRet (*CallbackType)(CallerArgs...);explicitthunk(call_declare callee = default_caller);template<typename CalleeClass,typename CalleeProc>thunk(CalleeClass& object, CalleeProc proc);template<typename CalleeClass,typename CalleeProc,typename =typename std::enable_if<check_call<CalleeProc>::value>::type>voidbind(CalleeClass& object, CalleeProc proc);operatorCallbackType()const;};

The usage is as follows:

TestClass obj;call_thunk::thunk thunk<cb_type>(obj, &TestClass::fun);test_cb(thunk);

About

Take the member function of the C++ class as the callback function of the C function

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp