passing pointer to multiply classes to a member function - c++

I have the next classes:
"Integrator.h"
#include <vector>
#include <array>
using namespace std;
class Integrator {
public:
using coord_type = array<double, 3>;
protected:
void base_integrate_callback(const coord_type, double t_k) {
//does nothing
}
};
class MyIntegrator :public Integrator {
public:
template <class T>
void integrate(int mp_id, int t_span, int step ,
void(T::*callback)(const coord_type, double) = (Integrator::*)(const coord_type, double)){
//calls callback here
}
};
"main.cpp"
#include Integrator.h"
struct caller {
void callback(const Integrator::coord_type coord, double t_k) {
//does smth
}
};
int main(){
MyIntegrator integrator_1;
caller A;
int mp_id = 1;
int span = 365;
int step = 1;
integrator_1.integrate<caller>(mp_id,span,step,&A.callback);
return 0;
}
Trying to compile it I get an error:
file:integration.h, line 18, syntax error: '< tag>::*'
How can I call a callback which could belong to any class?
And the second question: when I try to call it without explicit template specification like
integrator_1.integrate(mp_id,span,step,&A.callback);
I get an error
file: main.cpp , line 65, 'MyIntegrator::integrate': no matching overloaded function found
So, why this function can not deduce its argument from its parameter?
Also I get the same error when calling it without the last parameter relying on the default parameter.
integrator_1.integrate(mp_id,span,step);

Decrypting what you have here with a little indentation
template <class T>
void integrate(int mp_id,
int t_span,
int step ,
void(T::*callback)(const coord_type, double) = (Integrator::*)(const coord_type, double))
{
//calls callback here
}
it looks like you are trying to declaring a method that takes a callback function as a parameter and assigning a default value. Unfortunately the default value looks like the declaration of another method pointer and not a method. You need to use a pointer to a method of T.
template <class T>
void integrate(int mp_id,
int t_span,
int step ,
void(T::*callback)(const coord_type, double) = &Integrator::base_integrate_callback)
{
//calls callback here
}
but I don't think this will be kosher as there is no way to ensure that T and Integrator are in any way related.
For example, after cleaning up
integrator_1.integrate < caller > (mp_id, span, step, &A.callback);
to
integrator_1.integrate < caller > (mp_id, span, step, &caller::callback);
because you need to provide a pointer to a method, not an object referring to a method. This exposes another problem we'll get to in a moment, but it will compile for now and let us continue.
But this would not
integrator_1.integrate < caller > (mp_id, span, step);
because the signature of Integrator::base_integrate_callback, void Integrator::base_integrate_callback(const coord_type, double), does not match the signature of void(caller::*callback)(const coord_type, double). They look the same, don't they? What's missing is the hidden this parameter all methods have. caller::*callbackexpects a caller *, but Integrator::base_integrate_callback provides Integrator *.
You can fix this by making caller and it's ilk inherit Integrator rather than MyIntegrator, but moving base_integrate_callback to a new struct Integrated and having caller and friends inherit Integrated would make more sense.
And back to the other problem I mentioned earlier. In
template <class T>
void integrate(int mp_id,
int t_span,
int step ,
void(T::*callback)(const coord_type, double) = &Integrated::base_integrate_callback)
{
coord_type x; // junk for example
double y; //junk for example
callback(x,y); //KABOOM!
}
On what object is callback being invoked? integrate will need one more parameter, a reference to T to provide context for callback.
template <class T>
void integrate(int mp_id,
int t_span,
int step,
T & integrated,
void(T::*callback)(const coord_type, double) = &Integrated::base_integrate_callback)
{
coord_type x; // junk for example
double y; //junk for example
integrated.callback(x,y);
}
Then you have to use the correct syntax to invoke the function pointer because the above will always call caller::callback.
template <class T>
void integrate(int mp_id,
int t_span,
int step,
T & integrated,
void(T::*callback)(const coord_type, double) = &Integrated::base_integrate_callback)
{
coord_type x; // junk for example
double y; //junk for example
(integrated.*callback)(x,y); //std::invoke would be preferred if available
}
All together:
#include <array>
#include <iostream>
class Integrator
{
public:
using coord_type = std::array<double, 3>;
};
struct Integrated
{
void base_integrate_callback(const Integrator::coord_type, double t_k)
{
std::cout << "made it to default" << std::endl;
}
};
class MyIntegrator: public Integrator
{
public:
template <class T>
void integrate(int mp_id,
int t_span,
int step,
T & integrated,
void(T::*callback)(const coord_type, double) = &Integrated::base_integrate_callback)
{
coord_type x; // junk for example
double y = 0; //junk for example
(integrated.*callback)(x,y);
}
};
struct caller:public Integrated
{
char val; // for test purposes
caller(char inval): val(inval) // for test purposes
{
}
void callback(const Integrator::coord_type coord, double t_k)
{
std::cout << "made it to " << val << std::endl;
}
};
int main()
{
MyIntegrator integrator_1;
caller A {'A'};
caller B {'B'};
caller C {'C'};
int mp_id = 1;
int span = 365;
int step = 1;
integrator_1.integrate < caller > (mp_id, span, step, A, &caller::callback);
integrator_1.integrate < caller > (mp_id, span, step, B, &caller::callback);
integrator_1.integrate < caller > (mp_id, span, step, C);
return 0;
}
Recommendation: Step into 2011 and see what std::function and lambda expressions can do for for you.
Here's an example:
#include <array>
#include <iostream>
#include <functional>
class Integrator
{
public:
using coord_type = std::array<double, 3>;
};
// no need for integrated to get default callback
class MyIntegrator: public Integrator
{
public:
template <class T>
void integrate(int mp_id,
int t_span,
int step,
// no need to provide object instance for callback. packed with std::bind
std::function<void(const coord_type, double)> callback =
[](const coord_type, double) { std::cout << "made it to default" << std::endl; })
// default callback is now lambda expression
{
coord_type x; // junk for example
double y = 0; //junk for example
callback(x,y); // no weird syntax. Just call a function
}
};
struct caller
{
char val; // for test purposes
// no need for test constructor
void callback(const Integrator::coord_type coord, double t_k)
{
std::cout << "made it to " << val << std::endl;
}
};
int main()
{
MyIntegrator integrator_1;
caller A {'A'};
caller B {'B'};
// no need for test object C
int mp_id = 1;
int span = 365;
int step = 1;
using namespace std::placeholders; // shorten placeholder names
integrator_1.integrate < caller > (mp_id,
span,
step,
std::bind(&caller::callback, A, _1, _2));
// std bind bundles the object and the callback together into one callable package
integrator_1.integrate < caller > (mp_id,
span,
step,
[B](const Integrator::coord_type p1,
double p2) mutable // lambda captures default to const
{
B.callback(p1, p2); // and callback is not a const method
});
// Using lambda in place of std::bind. Bit bulkier, but often swifter and no
//need for placeholders
integrator_1.integrate < caller > (mp_id,
span,
step,
[](const Integrator::coord_type p1,
double p2)
{
std::cout << "Raw Lambda. No callback object at all." << std::endl;
});
//custom callback without a callback object
integrator_1.integrate < caller > (mp_id, span, step);
//call default
return 0;
}

Related

How do I call template array operator overloading function?

I need to create an adapter C++ class, which accepts an integer index, and retrieves some types of data from a C module by the index, and then returns it to the C++ module.
The data retrieving functions in the C module are like:
int getInt(int index);
double getDouble(int index);
const char* getString(int index);
// ...and etc.
I want to implement an array-like interface for the C++ module, so I created the following class:
class Arguments {
public:
template<typename T> T operator[] (int index);
};
template<> int Arguments::operator[] (int index) { return getInt(index); }
template<> double Arguments::operator[] (int index) { return getdouble(index); }
template<> std::string Arguments::operator[] (int index) { return getString(index); }
(Template class doesn't help in this case, but only template member functions)
The adapter class is no biggie, but calling the Arguments::operator[] is a problem!
I found out that I can only call it in this way:
Arguments a;
int i = a.operator[]<int>(0); // OK
double d = a.operator[]<double>(1); // OK
int x = a[0]; // doesn't compile! it doesn't deduce.
But it looks like a joke, doesn't it?
If this is the case, I would rather create normal member functions, like template<T> T get(int index).
So here comes the question: if I create array-operator-overloading function T operator[]() and its specializations, is it possible to call it like accessing an array?
Thank you!
The simple answer is: No, not possible. You cannot overload a function based on its return type. See here for a similar quesiton: overload operator[] on return type
However, there is a trick that lets you deduce a type from the lhs of an assignment:
#include <iostream>
#include <type_traits>
struct container;
struct helper {
container& c;
size_t index;
template <typename T> operator T();
};
struct container {
helper operator[](size_t i){
return {*this,i};
}
template <typename T>
T get_value(size_t i){
if constexpr (std::is_same_v<T,int>) {
return 42;
} else {
return 0.42;
}
}
};
template <typename T>
helper::operator T(){
return c.get_value<T>(index);
}
int main() {
container c;
int x = c[0];
std::cout << x << "\n";
double y = c[1];
std::cout << y ;
}
Output is:
42
0.42
The line int x = c[0]; goes via container::get_value<int> where the int is deduced from the type of x. Similarly double y = c[1]; uses container::get_value<double> because y is double.
The price you pay is lots of boilerplate and using auto like this
auto x = c[1];
will get you a helper, not the desired value which might be a bit unexpected.

Declaring a function that can take a lambda as a parameter, but that lambda must be able to capture other variables

I would like to make a function that can take a function as a parameter, so that I can call the function with a specified lambda in the client code. Currently my code is this:
void Execute(float threshold,
void (*behaviour)(std::shared_ptr<cv::Mat>& image_i,
pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud_i,
const int* position, int index))
However this will not compile.
using :
template <typename func>
void Execute(float threshold, func behaviour)
Will compile and work fine but I would like to constrain the client code to adhere to the function signature at compile time. I am using C++17.
Example client code:
caster.Execute(thresh,
[&](std::shared_ptr<cv::Mat>& image,
pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud,
const int* position, int index) -> void {
pcl::PointXYZ origin(0, 0, 0);
float d = beam::distance(cloud->points[index], origin);
if (d > max_depth_) { max_depth_ = d; }
if (d < min_depth_) { min_depth_ = d; }
image->at<float>(position[0], position[1]) = d;
num_extracted++;
});
As you can see I would like to access variables num_extracted, min_depth_ and max_depth_ which are declared outside the scope of the lambda.
You are asking to meet two requirements:
Constrain any lambda to a given signature.
Access specific variables from within a given lambda.
Here's a working version of what I could infer from your code:
namespace cv {
struct Mat {
float m_mat [3][3] {};
template<typename TYPE_T>
float& at( int ROW, int COL ) { return m_mat[ROW][COL]; }
};
}
namespace pcl {
struct PointXYZ { int X{}, Y{}, Z{}; };
template<typename TYPE_T>
struct PointCloud {
using Ptr = std::shared_ptr<PointCloud<TYPE_T>>;
TYPE_T *points{};
int length{};
};
}
struct beam {
static float distance( const pcl::PointXYZ &PT, const pcl::PointXYZ &ORIGIN ) { return 0.0f; }
};
std::shared_ptr<cv::Mat> g_image { new cv::Mat{} };
pcl::PointCloud<pcl::PointXYZ>::Ptr g_cloud { new pcl::PointCloud<pcl::PointXYZ>{} };
int g_position [] { 0, 0 };
template<typename func>
requires std::is_invocable_v<func,
std::shared_ptr<cv::Mat>&,
pcl::PointCloud<pcl::PointXYZ>::Ptr&,
const int*,
int
>
void Execute( float threshold, func behaviour )
{
behaviour(g_image, g_cloud, g_position, 1);
}
int main()
{
int num_extracted {};
float min_depth_ {},
max_depth_ {};
Execute(1.0f, [&] ( auto &&IMAGE, auto &&CLOUD, auto &&POSITION, auto &&INDEX )
{
pcl::PointXYZ origin { 0, 0, 0 };
float d = beam::distance(CLOUD->points[INDEX], origin);
if( d > max_depth_ ) { max_depth_ = d; }
if( d < min_depth_ ) { min_depth_ = d; }
IMAGE->at<float>(POSITION[0], POSITION[1]) = d;
num_extracted++;
});
return 0;
}
There are a couple ways to constrain the lambda signature, but they all involve using std::is_invovable (or making behaviour a std::function, as others have suggested). I've opted to use the newer requires syntax.
See: C++ Constraints and Concepts
There are two sets of variables you need, but you haven't described where they will come from: i) Those to be passed to Execute, and ii) Those to be used in a specific lambda that will be passed to Execute.
2.i) These are the g_* variables. They must be visible from the scope Execute is being called in so they can be passed to the invocable behavior. In this case Execute is a global function so the variables must also be global.
2.ii) These are the variables in main. They must be visible from the scope the lambda is being created in. In this case they must be in main or global.
[EDIT]
For C++ 17 you can change Execute from:
template<typename func> requires std::is_invocable_v<...>
to
template<typename func, typename = std::include_if_t<std::is_invocable_v<...>>>

Invoke a function in context of a class instance or method for performance analysis

Performance analysis question: Is there a way to execute a function in context of a class, or a method of a class?
I would like to analyze the performance of a specific segment of logic. What I envision is something like this
(Disclaimer: rough example just to illustrate a point. Will not compile).
const int DEBUG_LEVEL = 7;
class PerfWrapper {
public:
PerfWrapper(int f) {} // Constructor: take function as argument
void invoke() {} // Invoke the function passed as argument
double execution_time() {
begin = std::chrono::high_resolution_clock::now();
// etc..
}
double memory_usage() {}
private:
}
int foo() {
int sum{0}
for (int i=0; i<1000; ++i)
for (int j=0; j<MAX; ++j)
sum += i * j;
return sum;
}
int main() {
if (DEBUG_LEVEL = 7)
PerfWrapper p(foo); // Create an instance, passing foo as an argument
// below foo() is called in context of the performance wrapper
int myTime = p.invoke().execution_time(); // Invokes foo in context of p and tracks execution time
int myMemory = p.invoke().memory_usage(); // Same, except gathering memory usage info.
// etc..
}
}
Here we have class PerfWrapper. When instantiated, resulting methods on the object have the ability to accept a function as an argument, and execute a function in context of the class. It will take perf measurements, results of which are accessible through the interface.
Note the "DEBUG_LEVEL" setting. If performance profiling is needed then simply set the DEBUG_LEVEL to 7.
Have you seen anything like this? If not, how is the analysis best accomplished? I know that it seems a bit out there, but hopefully not so much. Thx, Keith :^)
Maybe you are looking for function pointers, which could be used as shown in the following simplified code:
typedef int(*aFooFunctionType)(void);
class PerformanceTest {
public:
PerformanceTest(aFooFunctionType fooFuncPtr) { m_fooFuncPtr = fooFuncPtr; }
void test() {
int x = m_fooFuncPtr();
// do something with x (or not...)
};
private:
aFooFunctionType m_fooFuncPtr;
};
int fooFunc(void) {
return 100;
}
int main(int argc, char* argv[]) {
PerformanceTest pTest(fooFunc);
pTest.test();
return 0;
}
You can wrap almost anything in a std::function. I would suggest use of a std::function in PerfWrapper to get the execution time. I don't have anything for measuring memory usage, though.
Example code:
#include <iostream>
#include <functional>
#include <chrono>
class PerfWrapper
{
public:
PerfWrapper(std::function<void()> f) : f_(f), execution_time_{} {}
void invoke()
{
auto begin = std::chrono::high_resolution_clock::now();
f_();
auto end = std::chrono::high_resolution_clock::now();
execution_time_ = end-begin;
}
double execution_time()
{
return execution_time_.count();
}
std::function<void()> f_;
std::chrono::duration<double> execution_time_;
};
unsigned long foo()
{
unsigned long sum{0};
for (int i=0; i<10000; ++i)
for (int j=0; j<2000; ++j)
sum += i * j;
return sum;
}
int main()
{
PerfWrapper pr([](){std::cout << foo() << std::endl;});
pr.invoke();
std::cout << "Execution time: " << pr.execution_time() << std::endl;
}
Output on my setup:
99940005000000
Execution time: 0.0454077
Consider using a template free function, with a reference parameter to extract the performance data. This example will:
Accept function pointers and functors (including std::function, which means it can work with methods, too).
Return the same value the proxied function call returns, so you can use both the measurement data and the call result.
struct measurement {
double execution_time;
double memory_usage;
};
template <typename FN, typename... T>
inline auto measure(FN fn, measurement& m, T&&... args) -> decltype(fn(std::forward<T>(args)...))
{
auto&& result = fn(std::forward<T>(args)...);
m.execution_time = 0; // example
m.memory_usage = 0;
return result;
}

C++ member function pointer to global function pointer

I have to solve, at least for me, a tricky problem in C++. There is a dll which i can not modify. It gets a function pointer as argument. If I pass a pointer to a global function everything works fine. Unfortunatelly there is a list of same class objects to pass to the dll. In C# I solved this by using delegates. How can this be done in C++? Using std::function does not work. With that there are coding convention errors during runtime. Further using MSVC2010 would be optimal.
I wrote a sample which describes the problem:
#include <stdio.h>
// global function which works
void __stdcall task_global(float x, float y) { printf("Called global function with: %f %f\n", x, y); }
typedef void(__stdcall *f_pointer)(float, float);
// try of using a member function
class BaseTask {
public:
virtual void __stdcall task(float x, float y) = 0;
};
class ListeningTask :public BaseTask {
public:
void __stdcall task(float x, float y) { printf("Called this member function with: %f %f\n", x, y); }
};
typedef void (BaseTask::*member_f_pointer)(float, float);
// the dll to use
class RegisterTask {
public:
// no posibility to access or modify!
void __stdcall subscribe(f_pointer fp) { fp(1.0f, 2.0f); }
// just for demonstration how to use a member function pointer
void __stdcall subscribeMemberDemo(member_f_pointer mfp) { /*how to use mfp?*/};
};
int main() {
RegisterTask register_task{};
// use global function
f_pointer pointer_to_global_task = task_global;
register_task.subscribe(pointer_to_global_task);
/*---------------------------------------------------------------*/
// use member function?
std::list<ListeningTask> listening_task_list;
for(int i = 0; i < 10; i++) {
listening_task_list.push_back(ListeningTask lt);
member_f_pointer pointer_to_member_task = &listening_task_list.back().task; //error C2276: '&': illegal operation on bound member function expression
register_task.subscribeMemberDemo(pointer_to_member_task);
// the tricky and important one to solve
// how to pass the member function to this subscribe(f_pointer)?
register_task.subscribe(pointer_to_member_task);
}
getchar();
return 0;
}
The important question is how to pass a member function pointer to the RegisterTask::subscribe(f_pointer)?
The parenthetic question is how to pass a member function to the RegisterTask::subscribeMemberDemo(member_f_pointer)?
I hope someone can help me to solve this? I am working on this since days.
Edit:
I modified the question to emphasize the problem with the list of ListenerTask. How to pass a member function pointer is now clear to me through the answers of #pokey909 and #AndyG. Both of them provide a pointer to one object or rather a list of objects. If the callback is called the one ListenerTask or all std::list<*ListenerTask> are called at once. But how to let only one ListenerTask of the list to be called. Passing more than one callback to the dll. It (RegisterTask) can do that, because the following example with global functions works.
void __stdcall task_global_1(float x, float y) { printf("Called global function 1 with: %f %f\n", x, y); }
void __stdcall task_global_2(float x, float y) { printf("Called global function 2 with: %f %f\n", x, y); }
void __stdcall task_global_3(float x, float y) { printf("Called global function 3 with: %f %f\n", x, y); }
typedef void(__stdcall *f_pointer)(float, float);
int main() {
// give the functions to the dll.
f_pointer pointer_to_global_task_1 = task_global_1;
register_task.subscribe(pointer_to_global_task_1);
f_pointer pointer_to_global_task_2 = task_global_2;
register_task.subscribe(pointer_to_global_task_2);
f_pointer pointer_to_global_task_3 = task_global_3;
register_task.subscribe(pointer_to_global_task_3);
}
There are three global function pointers. They are all given to the dll. Now, if the dll has a task for task_global_2 it notifies this only! How to achive this distinction with member function pointer?
Note:
I got the source of the dll. Hope this helps. Unfortunately modifying, building is not possible. Here is the callback definition:
type TCallback = procedure( x : single; y : single; ); stdcall;
procedure subscribe(aCallback: TCallback ); StdCall;
begin
TaskSocket.addTask( aCallback );
end;
procedure TSocket.addTask( aCallback : TCallback);
var newTask : TTask;
begin
newTask := TTask.Create(aCallback);
TaskList.addItem(newTask);
end;
You can use a freestanding function that calls a wrapper which binds your instance to it.
Here is rough example
#include <iostream>
#include <string>
#include <functional>
// global function which works
std::function<void(float, float)> memberCb;
void task_global(float x, float y) { memberCb(x, y); }
typedef void(*f_pointer)(float, float);
// try of using a member function
class BaseTask {
public:
virtual void task(float x, float y) = 0;
};
class ListeningTask :public BaseTask {
public:
void task(float x, float y) { printf("Called this member function with: %f %f\n", x, y); }
};
typedef void (BaseTask::*member_f_pointer)(float, float);
void callbackWrapper(BaseTask* t, float x, float y) { t->task(x, y); }
// the dll to use
class RegisterTask {
public:
// no posibility to access or modify!
void subscribe(f_pointer fp) {
fp(1.0f, 2.0f);
}
// just for demonstration how to use a member function pointer
void subscribeMemberDemo(member_f_pointer mfp) { /*???*/ };
};
int main() {
RegisterTask register_task{};
ListeningTask listening_task{};
memberCb = std::bind(&callbackWrapper, &listening_task, std::placeholders::_1, std::placeholders::_2);
register_task.subscribe(task_global);
return 0;
}
Note
Based on the comment, I'm not sure if all of this works in MSVC2010, since I don't have this compiler version. But rudimentary C++11 support should be in there.
Edit
I'm not sure if thats what you are after, but would this solve your problem?
void callbackWrapper(const std::list<BaseTask*> &taskList, float x, float y) {
for (auto t : taskList)
t->task(x, y);
}
int main() {
RegisterTask register_task{};
std::list<BaseTask*> taskList;
for (int i = 0; i < 4; ++i)
taskList.push_back(new ListeningTask);
memberCb = std::bind(&callbackWrapper, taskList, std::placeholders::_1, std::placeholders::_2);
register_task.subscribe(task_global);
return 0;
}
Edit 2
Ok I think I got what you want. The best I can come up with without splattering your code with global functions manually is with template magic.
Note however, that it is not as flexible as you might want because you have to bind those methods at compile time.
If you need to add them at runtime, you can probably use the same trick but without templates. Simply put all the std::function objects in a vector and wrap that up in a singleton or something similar.
#include <iostream>
#include <string>
#include <functional>
#include <list>
/* Simulated DLL */
typedef void(*f_pointer)(float, float);
class RegisterTask {
public:
void subscribe(f_pointer fp) {
fp(1.0f, 2.0f);
}
};
/* Static function generator to ease the pain to define all of them manually */
template<unsigned int T>
std::function<void(float, float)> &getWrapper() {
static std::function<void(float, float)> fnc;
return fnc;
}
/* Same here */
template<unsigned int T>
void task_global(float x, float y) { getWrapper<T>()(x, y); }
class BaseTask {
public:
virtual void task(float x, float y) = 0;
};
class ListeningTask :public BaseTask {
public:
ListeningTask(int taskNum) : m_taskNum(taskNum) {}
void task(float x, float y) { printf("Called this member of task %d function with: %f %f\n", getTaskNum(), x, y); }
int getTaskNum() const { return m_taskNum; }
private:
int m_taskNum;
};
/* Context injector */
void callbackWrapper(BaseTask* t, float x, float y) {
t->task(x, y);
}
/* Convenience function to bind an instance to a task */
template<unsigned int T>
void bindTask(ListeningTask* t) {
getWrapper<T>() = std::bind(&callbackWrapper, t, std::placeholders::_1, std::placeholders::_2);
}
int main() {
RegisterTask register_task{};
auto task0 = new ListeningTask(1337);
auto task1 = new ListeningTask(1984);
auto task2 = new ListeningTask(42);
bindTask<0>(task0);
register_task.subscribe(task_global<0>);
bindTask<1>(task1);
register_task.subscribe(task_global<1>);
bindTask<2>(task2);
register_task.subscribe(task_global<2>);
return 0;
}
Run Code demo
pokey909's answer is totally great, but if you don't even have access to std::function and std::bind, we can hack our way around it.
The gist of the approach is that we are going to define a template class with an implicit conversion to the desired function type. The downside is that each new additional wrapper requires a new type declaration.
// assumes two function arguments
template<class Ret, class Mem, class Arg1, class Arg2, int>
struct MemBind
{
typedef Ret(Mem::*mem_fn_type)(Arg1, Arg2);
static void Set(mem_fn_type _fn, Mem* _instance)
{
fn = _fn;
instance = _instance;
}
static Ret DoTheThing(Arg1 first, Arg2 second)
{
return ((*instance).*fn)(first, second);
}
typedef Ret(*fn_type)(Arg1, Arg2);
operator fn_type ()
{
return DoTheThing;
}
static mem_fn_type fn;
static Mem* instance;
};
Given some struct Foo with our desired callback:
struct Foo
{
void Bar(float a, float b)
{
std::cout << "Foo::Bar(float, float) " << a << " , " << b << std::endl;
}
};
We have to define our static members:
typedef MemBind<void, Foo, float, float, 0> MemBindZero;
template<> Foo* MemBindZero::instance = nullptr;
template<> void(Foo::*MemBindZero::fn)(float, float) = nullptr;
We can have a caller that takes in a function pointer:
void Caller(void(*_fn)(float, float))
{
_fn(42.0, 1337.0);
}
The key here is that MemBind has an implicit conversion to the desired function type. The 0 in the typedef for MemBindZero allows us to re-use the same types for the other arguments, but increment the counter to 1 when used. I think you could probably replace it with a __COUNTER__ macro or something like that, but it would be nonstandard so I did it manually.
Now the next bit is to create an instance of MemBindZero, then set the static members, and finally pass our instance into Caller:
Foo f;
MemBindZero bound;
bound.Set(&Foo::Bar, &f);
Caller(bound);
Demo
In the demo I wrapped the static member initialization into a more convenient macro:
#define MEMBIND(RET, CLASS, ARG1, ARG2, COUNT, ALIAS) \
typedef MemBind<RET, CLASS, ARG1, ARG2, COUNT> ALIAS; \
template<> CLASS * ALIAS::instance = nullptr; \
template<> RET(CLASS::*ALIAS::fn)(ARG1, ARG2) = nullptr;
So that I could call it like so:
MEMBIND(void, Foo, float, float, 0, MemBindZero)
MEMBIND(void, OtherFoo, float, float, 1, MemBindOne)

Construct variadic template argument list

Say i have the following scenario:
namespace detail
{
using duk_c_function_t = std::function<duk_ret_t(duk_context*)>;
template<typename T_Return(typename ... T_Params), std::function<T_Return(T_Params)>
duk_ret_t duk_function_proxy(duk_context* ctx)
{
const int n = sizeof...(T_Params); //real number of arguments passed.
duk_idx_t num_arguments = duk_get_top(ctx); //Number of arguments from javascript
int x = duk_require_int(ctx, 0); //Get integer -> parameter no 1
const char* c = duk_require_string(ctx, 1); //Get string -> parameter no 2
}
}
template<typename T_Return(typename ... T_Params)>
duk_c_function_t duk_function(std::function<T_Return(T_Params ...) function_item)
{
return duk_c_function_t(detail::duk_function_proxy<function_item>);
}
Where duk_function returns a function of the signature duk_ret_t function(duk_context* ctx) { ... }. Now, i know how to call the function object in duk_function_proxy with the variadic templates and such. But there is one problem for me: The javascript interpreter i am using requires that, for every c-function i want to expose, i need query the parameters from the stack, as shown in duk_function_proxy. However, with that being the case, i dont know how to call the function object - i can only store the queried parameters in some sort of container, and thus i cannot call the function object with the given signature. Is there any way to store my queried parameters in some sort of container, and then unpack and pass it to the function object as parameters? I query the required parameters based on the variadic template list, as seen in the prototype.
In short:
I want to iterate over T_Params, get and store the corresponding variables using duk_require_* (in some sort of container), and then use those values to call the function object, which is passed as a template argument.
this should get you started. I've simulated the DUK interface since it's not installed on my machine but you'll get the idea:
#include <iostream>
#include <string>
#include <functional>
#include <utility>
struct duk_context;
const char* duk_require_string(duk_context*, int i)
{
static constexpr const char * strings[] = {
"i like",
"hairy",
"ducks"
};
return strings[i];
}
int duk_require_int(duk_context*, int i)
{
return i * 6;
}
template<class Type> auto get_arg(duk_context* ctx, int i) -> Type;
template<> auto get_arg<std::string>(duk_context* ctx, int i) -> std::string
{
return duk_require_string(ctx, i);
}
template<> auto get_arg<int>(duk_context* ctx, int i) -> int
{
return duk_require_int(ctx, i);
}
template<class...Args, size_t...Is>
void get_args_impl(duk_context* context, const std::function<void(Args...)>& f, std::index_sequence<Is...>)
{
using tuple_type = std::tuple<Args...>;
f(get_arg<std::tuple_element_t<Is, tuple_type>>(context, Is)...);
}
template<class...Args>
void get_args(duk_context* context, const std::function<void(Args...)>& f)
{
get_args_impl<Args...>(context, f, std::index_sequence_for<Args...>());
}
void foo(std::string a, int b, std::string c)
{
std::cout << a << " " << b << " " << c << std::endl;
}
int main()
{
duk_context* ctx = nullptr;
get_args(ctx, std::function<void(std::string, int, std::string)>(&foo));
return 0;
}
expected output:
i like 6 ducks