I am trying to avoid this repetitive code by writing a template function.
#include <algorithm>
class X {
public:
void get_amin(double *a){}
void set_amin(double a){}
void get_bmin(double *b){}
void set_bmin(double b){}
//...many pairs like above
};
int main(){
X *x1 = new X;
X *x2 = new X;
//code that will be repeated
{
double x1_amin;
x1->get_amin(&x1_amin);
double x2_amin;
x2->get_amin(&x2_amin);
x1->set_amin(std::min(x1_amin, x2_amin));
}
//repeatation
{
double x1_bmin;
x1->get_bmin(&x1_bmin);
double x2_bmin;
x2->get_bmin(&x2_bmin);
x1->set_bmin(std::min(x1_bmin, x2_bmin));
}
//
delete x1;
delete x2;
}
Now my attempts are below. It seems I am able to write the template but not able to use it. Other posts at stack overflow mostly focus on how to write the template. Also I could not find an example where a class member function is used.
#include <algorithm>
#include <functional>
class X {
public:
void get_amin(double *a){}
void set_amin(double a){}
void get_bmin(double *b){}
void set_bmin(double b){}
//...many pairs like above
};
template <typename F11,typename F12, typename F2>
void templatisedFunction(F12 f11,F12 f12,F2 f2)
{
double x1_amin;
f11(&x1_amin);
double x2_amin;
f12(&x2_amin);
f2(std::min(x1_amin, x2_amin));
}
int main(){
X *x1 = new X;
X *x2 = new X;
//templatisedFunction(x1->get_amin,x2->get_amin,x1->set_amin);
//templatisedFunction(x1->get_amin(double*),x2->get_amin(double*),x1->set_amin(double));
//templatisedFunction<x1->get_amin(double*),x2->get_amin(double*),x1->set_amin(double)>();
//templatisedFunction<x1->get_amin,x2->get_amin,x1->set_amin>();
std::function<void(X*)> memfun(&X::get_amin);//not sure here
//templatisedFunction<x1->get_amin,x2->get_amin,x1->set_amin>();
//
delete x1;
delete x2;
}
void (X::*getf)(double *) and void (X::*setf)(double) are the function signatures for the two pointer to member function that you need.
Using C++11:
int main()
{
X x1;
X x2;
auto lamb = [&](void (X::*getf)(double *), void (X::*setf)(double))
{
double x1_amin;
(x1.*getf)(&x1_amin);
double x2_amin;
(x2.*getf)(&x2_amin);
(x1.*setf)(std::min(x1_amin, x2_amin));
};
lamb(&X::get_amin, &X::set_amin);
lamb(&X::get_bmin, &X::set_bmin);
return 0;
}
You can use pointers to member functions to reduce repetition:
void set_min(X &x1, X &x2, void (X::*get_min)(double *), void (X::*set_min)(double)) {
double x1_amin;
(x1.*get_min)(&x1_amin);
double x2_amin;
(x2.*get_min)(&x2_amin);
(x1.*set_min)(std::min(x1_amin, x2_amin));
}
to be used like this:
set_min(*x1, *x2, &X::get_amin, &X::set_amin);
set_min(*x1, *x2, &X::get_bmin, &X::set_bmin);
If you have many pairs you could go even further and use a loop:
std::pair<void (X::*)(double *), void (X::*)(double)> get_set_pairs[] = {
{&X::get_amin, &X::set_amin},
{&X::get_bmin, &X::set_bmin},
};
for (auto &get_set_pair : get_set_pairs){
set_min(*x1, *x2, get_set_pair.first, get_set_pair.second);
}
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;
}
first of all I know that this is not possible in C++. But I hope someone can tell be a workaround for my problem. I have a class which represents a mathematical function:
class myClass:
{
private:
public:
myClass() {};
double value(double, double){ /* doing some complicated calculation here */} };
double integrate { /*calc*/ return integral; };
}
In integrate() I want to create a struct with a reference to value(). The struct is defined as follows:
struct gsl_monte_function_struct {
double (*f)(double * x_array, size_t dim, void * params);
size_t dim;
void * params;
};
(I need this struct to call the Monte-Carlo integration routines from GSL)
As said before I know that this is forbidden in C++. But is there any possibility to use gsl_monte_function_struct with a member function of myClass? If it is not possible that myClass can integrate itself, is it possible to call gsl_monte_function_struct from outside the class with value() as reference? Thanks in advance!
If understand you corretly, you want a pointer to a member function of myClass. You can achieve this by declaring the member function pointer as:
double (myClass::*value)(double,double)
This function can later be called on an instance as:
(instance.*value)(x,y);
Alternatively you can use std::bind to create a function object which can be called as an ordinary function without having to keep track of the instance on which it is called after the call to std::bind:
auto& value = std::bind(myClass::value, instance);
// ....
value(x,y);
Ok so far I found two solutions:
1) (General solution) Using an abstract base class which has a static pointer to the current instance and a static function that calls a function of the derived class. The static function can be used with a function pointer.
Example:
struct gsl_monte{
double (*f)(double y);
};
class myBase {
private:
static myBase* instance;
public:
myBase(){};
static void setInstance(myBase* newOne);
virtual double value(double x) =0;
static double callValue(double x);//{return value(x);}
};
class myClass : public myBase {
public:
myClass(){};
double value(double x) { return x; };
};
myBase* myBase::instance = new myClass();
double myBase::callValue(double x){return instance->value(x);}
void myBase::setInstance(myBase* newOne){instance=newOne;};
double g(double xx) {return xx;};
int main(int argc, char** argv ){
double x[2]; x[0]=1.3; x[1]=1.3;
myClass* instance = new myClass();
myBase::setInstance(instance);
instance->value(3);
std::cout << "Test " << myBase::callValue(5) << std::endl;
gsl_monte T;
T.f=&myBase::callValue;
double (*f)(double y, void*) = &myBase::callValue;
}
2) (Solution specific to my problem) Fortunatly the desired function accepts a parameter pointer, which I can use to pass the current object:
#include <iostream>
#include <functional>
using namespace std::placeholders;
struct gsl_monte{
double (*f)(double y, void*);
};
class myClass {
public:
myClass(){};
double value(double x) { return x; };
};
double valueTT(double x, void* param) { return static_cast<myClass*>(param)->value(x); };
int main(int argc, char** argv ){
double x[2]; x[0]=1.3; x[1]=1.3;
myClass* instance = new myClass();
instance->value(3);
gsl_monte T;
T.f=&valueTT;
double (*f)(double y, void*) = &valueTT;
}
Abstract
I have a class that stores a optimization problem and runs a solver on that problem.
If the solver fails I want to consider a sub-problem and solve using the same solver (and class).
Introduction
An optimization problem is essencially a lot of (mathematical) functions. The problem functions are defined outside the class, but the sub-problem functions are defined inside the class, so they have different types (e.g. void (*) and void (MyClass::*).
At first I thought that I could cast the member function to the non-member pointer-to-function type, but I found out that I cannot. So I'm searching for some other way.
Example Code
An example code to simulate my issue:
#include <iostream>
using namespace std;
typedef void (*ftype) (int, double);
// Suppose foo is from another file. Can't change the definition
void foo (int n, double x) {
cout << "foo: " << n*x << endl;
}
class TheClass {
private:
double value;
ftype m_function;
void print (int n, double x) {
m_function(size*n, value*x);
}
public:
static int size;
TheClass () : value(1.2), m_function(0) { size++; }
void set_function (ftype p) { m_function = p; }
void call_function() {
if (m_function) m_function(size, value);
}
void call_ok_function() {
TheClass ok_class;
ok_class.set_function(foo);
ok_class.call_function();
}
void call_nasty_function() {
TheClass nasty_class;
// nasty_class.set_function(print);
// nasty_class.set_function(&TheClass::print);
nasty_class.call_function();
}
};
int TheClass::size = 0;
int main () {
TheClass one_class;
one_class.set_function(foo);
one_class.call_function();
one_class.call_ok_function();
one_class.call_nasty_function();
}
As the example suggests, the member function can't be static. Also, I can't redefine the original problem function to receive an object.
Thanks for any help.
Edit
I forgot to mention. I tried changing to std::function, but my original function has more than 10 arguments (It is a Fortran subroutine).
Solution
I made the change to std::function and std::bind as suggested, but did not went for the redesign of a function with more 10 arguments. I decided to create an intermediate function. The following code illustrates what I did, but with fewer variables. Thanks to all.
#include <iostream>
#include <boost/tr1/functional.hpp>
using namespace std;
class TheClass;
typedef tr1::function<void(int *, double *, double *, double *)> ftype;
// Suppose foo is from another file. Can't change the definition
void foo (int n, int m, double *A, double *x, double *b) {
// Performs matrix vector multiplication x = A*b, where
// A is m x n
}
void foo_wrapper (int DIM[], double *A, double *x, double *b) {
foo(DIM[0], DIM[1], A, x, b);
}
class TheClass {
private:
ftype m_function;
void my_function (int DIM[], double *A, double *x, double *b) {
// Change something before performing MV mult.
m_function(DIM, A, x, b);
}
public:
void set_function (ftype p) { m_function = p; }
void call_function() {
int DIM[2] = {2,2};
if (m_function) m_function(DIM, 0, 0, 0);
}
void call_nasty_function() {
TheClass nasty_class;
ftype f = tr1::bind(&TheClass::my_function, this, _1, _2, _3, _4);
nasty_class.set_function(f);
nasty_class.call_function();
}
};
int main () {
TheClass one_class;
one_class.set_function(foo_wrapper);
one_class.call_function();
one_class.call_nasty_function();
}
PS. Creating a std::function with more than 10 variables seemed possible (compiled, but I didn't test) with
#define BOOST_FUNCTION_NUM_ARGS 15
#include <boost/function/detail/maybe_include.hpp>
#undef BOOST_FUNCTION_NUM_ARGS
But creating a std::bind for more than 10 arguments does not seem as easy.
std::function, std::bind, and lambdas are what you are looking for. In short, function pointers are very bad things and should be burned in fire. In long, std::function can store any function object which can be called with the correct signature, and you can use std::bind or a lambda to generate a function object that calls your member function quickly and easily.
Edit: Then you will just have to roll your own std::function equivalent that supports more than 10 arguments.
Okay, I have posted a few questions lately related to wrapping a C callback API with a C++11-ish interface. I have almost got a satisfying solution, but I think it could be more elegant and need the help of some template metaprogramming wizards :)
Bear with me, as the example code is a little long, but I've tried to demonstrate the problem in one shot. Basically, the idea is that, given a list of function pointers and data context pointers, I want to provide a callback mechanism that can be provided with,
Function pointers
Function objects (functors)
Lambdas
Moreover, I want to make these functions callable by a variety of prototypes. What I mean is, the C API provides about 7 different parameters to the callback, but in most cases the user code is really only interested in one or two of these. So I'd like the user to be able to specify only the arguments he is interested in. (This extends from the point of allowing lambdas in the first place... to allow conciseness.)
In this example, the nominal C callback takes an int and a float parameter, and an optional float* which can be used to return some extra data. So the intention of the C++ code is to be able to provide a callback of any of these prototypes, in any form that is "callable". (e.g. functor, lambda, etc.)
int callback2args(int a, float b);
int callback3args(int a, float b, float *c);
Here is my solution so far.
#include <cstdio>
#include <vector>
#include <functional>
typedef int call2args(int,float);
typedef int call3args(int,float,float*);
typedef std::function<call2args> fcall2args;
typedef std::function<call3args> fcall3args;
typedef int callback(int,float,float*,void*);
typedef std::pair<callback*,void*> cb;
std::vector<cb> callbacks;
template <typename H>
static
int call(int a, float b, float *c, void *user);
template <>
int call<call2args>(int a, float b, float *c, void *user)
{
call2args *h = (call2args*)user;
return (*h)(a, b);
}
template <>
int call<call3args>(int a, float b, float *c, void *user)
{
call3args *h = (call3args*)user;
return (*h)(a, b, c);
}
template <>
int call<fcall2args>(int a, float b, float *c, void *user)
{
fcall2args *h = (fcall2args*)user;
return (*h)(a, b);
}
template <>
int call<fcall3args>(int a, float b, float *c, void *user)
{
fcall3args *h = (fcall3args*)user;
return (*h)(a, b, c);
}
template<typename H>
void add_callback(const H &h)
{
H *j = new H(h);
callbacks.push_back(cb(call<H>, (void*)j));
}
template<>
void add_callback<call2args>(const call2args &h)
{
callbacks.push_back(cb(call<call2args>, (void*)h));
}
template<>
void add_callback<call3args>(const call3args &h)
{
callbacks.push_back(cb(call<call3args>, (void*)h));
}
template<>
void add_callback<fcall2args>(const fcall2args &h)
{
fcall2args *j = new fcall2args(h);
callbacks.push_back(cb(call<fcall2args>, (void*)j));
}
template<>
void add_callback<fcall3args>(const fcall3args &h)
{
fcall3args *j = new fcall3args(h);
callbacks.push_back(cb(call<fcall3args>, (void*)j));
}
// Regular C-style callback functions (context-free)
int test1(int a, float b)
{
printf("test1 -- a: %d, b: %f", a, b);
return a*b;
}
int test2(int a, float b, float *c)
{
printf("test2 -- a: %d, b: %f", a, b);
*c = a*b;
return a*b;
}
void init()
{
// A functor class
class test3
{
public:
test3(int j) : _j(j) {};
int operator () (int a, float b)
{
printf("test3 -- a: %d, b: %f", a, b);
return a*b*_j;
}
private:
int _j;
};
// Regular function pointer of 2 parameters
add_callback(test1);
// Regular function pointer of 3 parameters
add_callback(test2);
// Some lambda context!
int j = 5;
// Wrap a 2-parameter functor in std::function
add_callback(fcall2args(test3(j)));
// Wrap a 2-parameter lambda in std::function
add_callback(fcall2args([j](int a, float b)
{
printf("test4 -- a: %d, b: %f", a, b);
return a*b*j;
}));
// Wrap a 3-parameter lambda in std::function
add_callback(fcall3args([j](int a, float b, float *c)
{
printf("test5 -- a: %d, b: %f", a, b);
*c = a*b*j;
return a*b*j;
}));
}
int main()
{
init();
auto c = callbacks.begin();
while (c!=callbacks.end()) {
float d=0;
int r = c->first(2,3,&d,c->second);
printf(" result: %d (%f)\n", r, d);
c ++;
}
}
Okay, as you can see, this actually works. However, I find the solution of having to explicitly wrap the functors/lambdas as std::function types kind of inelegant. I really wanted to make the compiler match the function type automatically but this doesn't seem to work. If I remove the 3-parameter variant, then the fcall2args wrapper is not needed, however the presence of the fcall3args version of add_callback makes it apparently ambiguous to the compiler. In other words it seems to not be able to do pattern matching based on the lambda call signature.
A second problem is that I'm of course making copies of the functor/lambda objects using new, but not deleteing this memory. I'm not at the moment sure what the best way will be to track these allocations, although I guess in a real implementation I could track them in an object of which add_callback is a member, and free them in the destructor.
Thirdly, I don't find it very elegant to have specific types call2args, call3args, etc., for each variation of the callback I want to allow. It means I'll need an explosion of types for every combination of parameters the user might need. I was hoping there could be some template solution to make this more generic, but I am having trouble coming up with it.
Edit for explanation: The definition in this code, std::vector<std::pair<callback*,void*>> callbacks, is part of the problem definition, not part of the answer. The problem I am trying to solve is to map C++ objects onto this interface--therefore, proposing better ways to organize this std::vector doesn't solve the problem for me. Thanks. Just to clarify.
Edit #2: Okay, forget the fact that my example code uses std::vector<std::pair<callback*,void*>> callbacks to hold the callbacks. Imagine instead, as this is the actual scenario, that I have some C library implementing the following interface:
struct someobject *create_object();
free_object(struct someobject *obj);
add_object_callback(struct someobject *obj, callback *c, void *context);
where callback is,
typedef int callback(int a,float b,float *c, void *context);
Okay. So "someobject" will experience external events of some kind, network data, or input events, etc., and call its list of callbacks when these happen.
This is a pretty standard implementation of callbacks in C. Importantly, this is an existing library, something for which I cannot change, but I am trying to write a nice, idiomatic C++ wrapper around it. I want my C++ users to be able to add lambdas as callbacks. So, I want to design a C++ interface that allows users to be able to do the following:
add_object_callback(struct someobject *obj, func);
where func is one of the following:
a regular C function that doesn't use context.
a functor object
a lambda
Additionally, in each case, it should be possible for the function/functor/lambda to have either of the following signatures:
int cb2args(int a, float b);
int cb2args(int a, float b, float *c);
I think this should be possible, and I got about 80% of the way there, but I'm stuck on template polymorphism based on the call signature. I don't know offhand whether it's possible. Maybe it needs some voodoo involving function_traits or something, but it's a little beyond my experience. In any case, there are many, many C libraries that use such an interface, and I think it would be great to allow this kind of convenience when using them from C++.
Since you are using the C API in C++11, you could as well just wrap the whole thing in a C++ class. This is also necessary, as you mentioned in the 2nd problem, to solve the resource leak.
Also remember that a lambda expression without capture can be implicitly converted to a function pointer. This could remove all the call<*> because they can be moved into the add_callbacks.
And finally, we could use SFINAE to remove the fcall3args types. Here is the result.
class SomeObject {
// The real object being wrapped.
struct someobject* m_self;
// The vector of callbacks which requires destruction. This vector is only a
// memory store, and serves no purpose otherwise.
typedef std::function<int(int, float, float*)> Callback;
std::vector<std::unique_ptr<Callback>> m_functions;
// Add a callback to the object. Note the capture-less lambda.
template <typename H>
void add_callback_impl(H&& h) {
std::unique_ptr<Callback> callback (new Callback(std::forward<H>(h)));
add_object_callback(m_self, [](int a, float b, float* c, void* raw_ctx) {
return (*static_cast<Callback*>(raw_ctx))(a, b, c);
}, callback.get());
m_functions.push_back(std::move(callback));
}
public:
SomeObject() : m_self(create_object()) {}
~SomeObject() { free_object(m_self); }
// We create 4 public overloads to add_callback:
// This only accepts function objects having 2 arguments.
template <typename H>
auto add_callback(H&& h) -> decltype(h(1, 10.f), void()) {
using namespace std::placeholders;
add_callback_impl(std::bind(std::forward<H>(h), _1, _2));
}
// This only accepts function objects having 3 arguments.
template <typename H>
auto add_callback(H&& h) -> decltype(h(1, 1.0f, (float*)0), void()) {
add_callback_impl(std::forward<H>(h));
}
// This only accepts function pointers.
void add_callback(int(*h)(int, float)) const {
add_object_callback(m_self, [](int a, float b, float* c, void* d) {
return reinterpret_cast<int(*)(int, float)>(d)(a, b);
}, reinterpret_cast<void*>(h));
}
// This only accepts function pointers.
void add_callback(int(*h)(int, float, float*)) const {
add_object_callback(m_self, [](int a, float b, float* c, void* d) {
return reinterpret_cast<int(*)(int, float, float*)>(d)(a, b, c);
}, reinterpret_cast<void*>(h));
}
// Note that the last 2 overloads violates the C++ standard by assuming
// sizeof(void*) == sizeof(func pointer). This is valid in POSIX, though.
struct someobject* get_raw_object() const {
return m_self;
}
};
So the init() becomes:
void init(SomeObject& so) {
// A functor class
class test3 { ... };
so.add_callback(test1);
so.add_callback(test2);
// Some lambda context!
int j = 5;
so.add_callback(test3(j));
so.add_callback([j](int a, float b) -> int {
printf("test4 -- a: %d, b: %f", a, b);
return a*b*j;
});
so.add_callback([j](int a, float b, float *c) -> int {
printf("test5 -- a: %d, b: %f", a, b);
*c = a*b*j;
return a*b*j;
});
}
The full testing code (I'm not putting that to ideone here, because g++ 4.5 doesn't support implicitly converting a lambda to a function pointer, nor the range-based for.)
#include <vector>
#include <functional>
#include <cstdio>
#include <memory>
struct someobject;
struct someobject* create_object(void);
void free_object(struct someobject* obj);
void add_object_callback(struct someobject* obj,
int(*callback)(int, float, float*, void*),
void* context);
class SomeObject {
// The real object being wrapped.
struct someobject* m_self;
// The vector of callbacks which requires destruction. This vector is only a
// memory store, and serves no purpose otherwise.
typedef std::function<int(int, float, float*)> Callback;
std::vector<std::unique_ptr<Callback>> m_functions;
// Add a callback to the object. Note the capture-less lambda.
template <typename H>
void add_callback_impl(H&& h) {
std::unique_ptr<Callback> callback (new Callback(std::forward<H>(h)));
add_object_callback(m_self, [](int a, float b, float* c, void* raw_ctx) {
return (*static_cast<Callback*>(raw_ctx))(a, b, c);
}, callback.get());
m_functions.push_back(std::move(callback));
}
public:
SomeObject() : m_self(create_object()) {}
~SomeObject() { free_object(m_self); }
// We create 4 public overloads to add_callback:
// This only accepts function objects having 2 arguments.
template <typename H>
auto add_callback(H&& h) -> decltype(h(1, 10.f), void()) {
using namespace std::placeholders;
add_callback_impl(std::bind(std::forward<H>(h), _1, _2));
}
// This only accepts function objects having 3 arguments.
template <typename H>
auto add_callback(H&& h) -> decltype(h(1, 1.0f, (float*)0), void()) {
add_callback_impl(std::forward<H>(h));
}
// This only accepts function pointers.
void add_callback(int(*h)(int, float)) const {
add_object_callback(m_self, [](int a, float b, float* c, void* d) {
return reinterpret_cast<int(*)(int, float)>(d)(a, b);
}, reinterpret_cast<void*>(h));
}
// This only accepts function pointers.
void add_callback(int(*h)(int, float, float*)) const {
add_object_callback(m_self, [](int a, float b, float* c, void* d) {
return reinterpret_cast<int(*)(int, float, float*)>(d)(a, b, c);
}, reinterpret_cast<void*>(h));
}
// Note that the last 2 overloads violates the C++ standard by assuming
// sizeof(void*) == sizeof(func pointer). This is required in POSIX, though.
struct someobject* get_raw_object() const {
return m_self;
}
};
//------------------------------------------------------------------------------
int test1(int a, float b) {
printf("test1 -- a: %d, b: %f", a, b);
return a*b;
}
int test2(int a, float b, float *c) {
printf("test2 -- a: %d, b: %f", a, b);
*c = a*b;
return a*b;
}
void init(SomeObject& so) {
// A functor class
class test3
{
public:
test3(int j) : _j(j) {};
int operator () (int a, float b)
{
printf("test3 -- a: %d, b: %f", a, b);
return a*b*_j;
}
private:
int _j;
};
so.add_callback(test1);
so.add_callback(test2);
// Some lambda context!
int j = 5;
so.add_callback(test3(j));
so.add_callback([j](int a, float b) -> int {
printf("test4 -- a: %d, b: %f", a, b);
return a*b*j;
});
so.add_callback([j](int a, float b, float *c) -> int {
printf("test5 -- a: %d, b: %f", a, b);
*c = a*b*j;
return a*b*j;
});
}
//------------------------------------------------------------------------------
struct someobject {
std::vector<std::pair<int(*)(int,float,float*,void*),void*>> m_callbacks;
void call() const {
for (auto&& cb : m_callbacks) {
float d=0;
int r = cb.first(2, 3, &d, cb.second);
printf(" result: %d (%f)\n", r, d);
}
}
};
struct someobject* create_object(void) {
return new someobject;
}
void free_object(struct someobject* obj) {
delete obj;
}
void add_object_callback(struct someobject* obj,
int(*callback)(int, float, float*, void*),
void* context) {
obj->m_callbacks.emplace_back(callback, context);
}
//------------------------------------------------------------------------------
int main() {
SomeObject so;
init(so);
so.get_raw_object()->call();
}