All Projects → znone → call_thunk

znone / call_thunk

Licence: Apache-2.0 License
Take the member function of the C++ class as the callback function of the C function

Programming Languages

C++
36643 projects - #6 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to call thunk

hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-21.05%)
Mutual labels:  thunk
React-MaterialUI-Starter-Kit
The ReactJs 16 start kit that include Redux, Material UI, Babel 7 and Webpack 4
Stars: ✭ 48 (+152.63%)
Mutual labels:  thunk
simplevk
SimpleVK это PHP библиотека для быстрой разработки ботов для VK | vk api php class bot sdk library client framework longpoll callback streaming auth
Stars: ✭ 84 (+342.11%)
Mutual labels:  callback
SwiftObserver
Elegant Reactive Primitives for Clean Swift Architecture #NoRx
Stars: ✭ 14 (-26.32%)
Mutual labels:  callback
ant-arduino
An implementation of a ANT driver for Arduino, Mbed and ESP-IDF
Stars: ✭ 69 (+263.16%)
Mutual labels:  callback
movie-trailer
🎥 Fetch movie trailers: "Crash" ➔ http://path/to/trailer
Stars: ✭ 19 (+0%)
Mutual labels:  callback
markright
A customizable markdown parser in Elixir: pure pattern matching.
Stars: ✭ 14 (-26.32%)
Mutual labels:  callback
k-ramel
State manager for your components apps, the safe and easy way
Stars: ✭ 20 (+5.26%)
Mutual labels:  thunk
keras-stochastic-weight-averaging
Keras callback function for stochastic weight averaging
Stars: ✭ 53 (+178.95%)
Mutual labels:  callback
cpsfy
🚀 Tiny goodies for Continuation-Passing-Style functions, fully tested
Stars: ✭ 58 (+205.26%)
Mutual labels:  callback
synapse
Non-intrusive C++ signal programming library
Stars: ✭ 48 (+152.63%)
Mutual labels:  callback
async-chainable
An extension to Async adding better handling of mixed Series / Parallel tasks via object chaining
Stars: ✭ 25 (+31.58%)
Mutual labels:  callback
epump
ePump是一个基于I/O事件通知、非阻塞通信、多路复用、多线程等机制开发的事件驱动模型的 C 语言应用开发框架,利用该框架可以很容易地开发出高性能、大并发连接的服务器程序。
Stars: ✭ 26 (+36.84%)
Mutual labels:  callback
image-cache
NodeJS Image cache with Base64 format
Stars: ✭ 18 (-5.26%)
Mutual labels:  callback
react-tabllist
React-based customizable style table or list components that support event and callback functions.
Stars: ✭ 20 (+5.26%)
Mutual labels:  callback
ProtoPromise
Robust and efficient library for management of asynchronous operations in C#/.Net.
Stars: ✭ 20 (+5.26%)
Mutual labels:  callback
datamosh
✨💾 Edit images via buffers. 💯✨
Stars: ✭ 23 (+21.05%)
Mutual labels:  callback
run exclusive
⚡🔒 Wait queue for function execution 🔒 ⚡
Stars: ✭ 22 (+15.79%)
Mutual labels:  callback
eBay-node-client
Ebay NodeJS Wrapper
Stars: ✭ 50 (+163.16%)
Mutual labels:  callback
Flask-Shell2HTTP
Execute shell commands via HTTP server (via flask's endpoints).
Stars: ✭ 93 (+389.47%)
Mutual labels:  callback

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.

Calling Conventions

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:

typedef void (*cb_type)(int, void*);

void test_cb(cb_type cb);

The called C++ class is defined as follows:

struct TestClass
{
	void fun(int, void*);
};

Class unsafe_thunk

Class unsafe_thunk is:

class unsafe_thunkexplicit unsafe_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_thunk thunk(2);	//Two integer parameters
thunk.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:

struct argument_info
{
	short _size;			//Parameter size calculated in bytes
	bool _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>
class thunk
{
	typedef CallerRet (*CallbackType)(CallerArgs...);

	explicit thunk(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>
	void bind(CalleeClass& object, CalleeProc proc);
	operator CallbackType() const;
};

The usage is as follows:

TestClass obj;
call_thunk::thunk thunk<cb_type>(obj, &TestClass::fun);
test_cb(thunk);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].