I want to pass a class function as a parameter of C function
in Pascal. It is achieved with the keyword (procedure of object) so the compiler will take care of 'this' parameter.
But it seems complicated in c++.
#include <stdio.h>
typedef void (*func)(void);
class Class{
public:
void sub(void)
{
printf("Foo");
}
};
void test(func f)
{
f();
}
int main()
{
Class c;
test(c.sub);
}
You'll need the function to take a generic function type, either making it a template:
template <typename F>
void test(F f) {
f();
}
or using type erasure:
#include <functional>
void test(std::function<void()> f) {
f();
}
Then use either std::bind or a lambda to bind the member function to the object:
test(std::bind(&Class::sub, &c));
test([&]{c.sub();});
I have been searching for similar issue. There is an answer from someone zayats80888 (thank you).
#include <functional>
using TEventHandler = std::function<void(int)>;
class Button
{
TEventHandler onClick;
public:
Button(){ onClick = NULL;};
void Run() {
if (onClick) onClick(42);
};
};
class Page
{
Button Bnt1;
void ClickHandler(int Arg){/*do smth by Btn1->onClick*/};
Page() {
Btn1 = new Button();
Btn1->onClick = [this](int arg) { goToCountersPage(arg); };
}
}
Page Page1;
void main() {
Page1 = new Page();
//
}
Related
Inspired by another article here on SO (C++ callback using class member) I tried to write a universal CallbackHandler.
CallbackHandler.hpp
#pragma once
#include <functional>
template <typename CallbackClass, typename CallbackArgType>
class CallbackHandler
{
public:
std::function<void(CallbackArgType ct)> m_callbackFunc;
CallbackHandler(CallbackClass * handler, std::function<void(CallbackArgType)> method)
{
//m_callbackFunc is supposed to stand for a member to pointer callback function with one
//parameter of any type
m_callbackFunc = std::bind(method, handler, std::placeholders::_1);
}
};
#include "wrapper_T.cpp"
I want to use it in several other templated namespaces/classes like here:
wrapper.hpp
//this wrappers main purpose is to combine the constructor of a non templated class (MyModule)
//and hold a (global) callback method for it (m_parentCallback)
namespace wrapper
{
extern std::function<void(wxImage *)> m_parentCallback;
template<typename ParentClass>
MyModule GetNewModule(ParentClass* parent, void (ParentClass::* method)(wxImage *));
}
wrapper.cpp
namespace wrapper
{
//This is only to avoid multiple definition error - actual definition is in wrapper_T.cpp
std::function<void(wxImage *)> m_parentCallback;
}
wrapper_T.cpp
namespace wrapper
{
template<typename ParentClass>
MyModule GetNewModule(ParentClass* parent, void (ParentClass::* method)(wxImage *))
{
//the callback type of this wrapper/class is wxImage*
std::shared_ptr<CallbackHandler<ParentClass, wxImage*>> handler =
std::make_shared< CallbackHandler<ParentClass, wxImage*>>(parent, method);
//EDIT - SOLVED: <- Error C2664: Cant convert argument 2 from "void (__thiscall MyModule::*)(void)" to "std::function<void(wxImage*)>"
m_parentCallback = std::bind(&CallbackHandler<ParentClass, wxImage*>::m_callbackFunc, handler, std::placeholders::_1);
//<- Error C2679: no suitable binary operator "=" found
return std::make_unique<MyModule>();
}
}
I wanted to use the callback like this:
MyModule.cpp
wrapper::m_parentCallback(&img);
I want to initialize the whole thing like this:
MainClass.cpp
MainClass::MainClass()
{
//declared in header: std::unique_ptr<MyModule> module
module = std::move(wrapper::GetNewModule(this, &MainClass::CallbackFunc));
}
void MainClass::CallbackFunc(wxImage * img)
{ /* do something with it */ }
I have the class with the "this" pointer and the pointer to method "CallbackFunc", which should be alright.
But I dont see how to use my CallbackHandler class for a std::function callback pointer.
Or did I overdo it with the wrapper holding a pointer to a method of the CallbackHandler, which holds a pointer to a method of the actual callback method?
All of this is no design choice, I just want the CallbackHandler to be portable and working, while having an interface which is easy to use.
EDIT:
I tried to apply the comments suggestions on the code, but I was to fast
with claiming the first problem was solved. The error was just hidden by the next error. If I try to compile with just this line:
std::shared_ptr<CallbackHandler<ParentClass, wxImage*>> handler =
std::make_shared< CallbackHandler<ParentClass, wxImage*>>(parent, method);
//<- Error C2664: "CallbackHandler<ParentClass,wxImage *>::
//CallbackHandler(CallbackHandler<ParentClass,wxImage *> &&)"
//: converting argument 2 from "void (__thiscall MainClass::* )(wxImage *)"
//to "std::function<void (wxImage *)>" not possible
// with
// [
// ParentClass=MainClass
]
//(freely translated into english by me)
So, the missing argument was not the only problem.
If std::bind on methods (member functions) does not work, I do have to change the CallbackClass as well, dont I?
Maybe something along those lines:
std::function<void(CallbackArgType cat)> m_callbackFunc;
CallbackHandler(CallbackClass * handler, std::function<void(CallbackArgType)> method)
{
//m_callbackFunc = std::bind(method, handler, std::placeholders::_1);
m_callbackFunc = [method](auto img) { method(img); };
}
Replace
m_parentCallback = std::bind(&CallbackHandler<ParentClass, wxImage*>::m_callbackFunc, handler, std::placeholders::_1);
with
m_parentCallback = [handler](auto img){ handler->m_parentCallback(img); };
I don't think bind was designed to work with member functor objects on top of everything. But lambdas handle that seamlessly.
I noticed that my problem is beeing solved by every event handler, calling an event function. So I used an event handler as basis vor my CallbackHandler. I found and used this event handler, as there are most certainly many other good written examples:
https://www.codeproject.com/Articles/1256352/CppEvent-How-to-Implement-Events-using-Standard-Cp#holdingHandlerFunction
I just wrapped it in a class and it is a little overkill for my purpose, but it works. All credits to Shmuel Zang (see link above)
EventHandler.hpp
#ifndef EventHandler_hpp
#define EventHandler_hpp
// https://www.codeproject.com/Articles/1256352/CppEvent-How-to-Implement-Events-using-Standard-Cp#holdingHandlerFunction
#include <functional>
#include <list>
#include <algorithm>
#include <utility>
#include <atomic>
#include <mutex>
#include <future>
namespace EventHandler
{
template <typename... Args> class EventHandler_Base
{
public:
typedef std::function<void(Args...)> handler_func_type;
typedef unsigned int handler_id_type;
explicit EventHandler_Base(const handler_func_type& handlerFunc)
: m_handlerFunc(handlerFunc)
{
m_handlerId = ++m_handlerIdCounter;
}
// copy constructor
EventHandler_Base(const EventHandler_Base& src)
: m_handlerFunc(src.m_handlerFunc), m_handlerId(src.m_handlerId)
{
}
// move constructor
EventHandler_Base(EventHandler_Base&& src)
: m_handlerFunc(std::move(src.m_handlerFunc)), m_handlerId(src.m_handlerId)
{
}
// copy assignment operator
EventHandler_Base& operator=(const EventHandler_Base& src)
{
m_handlerFunc = src.m_handlerFunc;
m_handlerId = src.m_handlerId;
return *this;
}
// move assignment operator
EventHandler_Base& operator=(EventHandler_Base&& src)
{
std::swap(m_handlerFunc, src.m_handlerFunc);
m_handlerId = src.m_handlerId;
return *this;
}
// function call operator
void operator()(Args... params) const
{
if (m_handlerFunc)
{
m_handlerFunc(params...);
}
}
bool operator==(const EventHandler_Base& other) const
{
return m_handlerId == other.m_handlerId;
}
operator bool() const
{
return m_handlerFunc;
}
handler_id_type id() const
{
return m_handlerId;
}
private:
handler_func_type m_handlerFunc;
handler_id_type m_handlerId;
static std::atomic_uint m_handlerIdCounter;
};
template <typename... Args> std::atomic_uint EventHandler_Base<Args...>::m_handlerIdCounter(0);
template <typename... Args> class Event_Base
{
public:
typedef EventHandler_Base<Args...> handler_type;
Event_Base()
{
}
// copy constructor
Event_Base(const Event_Base& src)
{
std::lock_guard<std::mutex> lock(src.m_handlersLocker);
m_handlers = src.m_handlers;
}
// move constructor
Event_Base(Event_Base&& src)
{
std::lock_guard<std::mutex> lock(src.m_handlersLocker);
m_handlers = std::move(src.m_handlers);
}
// copy assignment operator
Event_Base& operator=(const Event_Base& src)
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
std::lock_guard<std::mutex> lock2(src.m_handlersLocker);
m_handlers = src.m_handlers;
return *this;
}
// move assignment operator
Event_Base& operator=(Event_Base&& src)
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
std::lock_guard<std::mutex> lock2(src.m_handlersLocker);
std::swap(m_handlers, src.m_handlers);
return *this;
}
typename handler_type::handler_id_type add(const handler_type& handler)
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
m_handlers.push_back(handler);
return handler.id();
}
inline typename handler_type::handler_id_type add(const typename handler_type::handler_func_type& handler)
{
return add(handler_type(handler));
}
bool remove(const handler_type& handler)
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
auto it = std::find(m_handlers.begin(), m_handlers.end(), handler);
if (it != m_handlers.end())
{
m_handlers.erase(it);
return true;
}
return false;
}
bool remove_id(const typename handler_type::handler_id_type& handlerId)
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
auto it = std::find_if(m_handlers.begin(), m_handlers.end(),
[handlerId](const handler_type& handler) { return handler.id() == handlerId; });
if (it != m_handlers.end())
{
m_handlers.erase(it);
return true;
}
return false;
}
void call(Args... params) const
{
handler_collection_type handlersCopy = get_handlers_copy();
call_impl(handlersCopy, params...);
}
std::future<void> call_async(Args... params) const
{
return std::async(std::launch::async, [this](Args... asyncParams) { call(asyncParams...); }, params...);
}
inline void operator()(Args... params) const
{
call(params...);
}
inline typename handler_type::handler_id_type operator+=(const handler_type& handler)
{
return add(handler);
}
inline typename handler_type::handler_id_type operator+=(const typename handler_type::handler_func_type& handler)
{
return add(handler);
}
inline bool operator-=(const handler_type& handler)
{
return remove(handler);
}
protected:
typedef std::list<handler_type> handler_collection_type;
void call_impl(const handler_collection_type& handlers, Args... params) const
{
for (const auto& handler : handlers)
{
handler(params...);
}
}
handler_collection_type get_handlers_copy() const
{
std::lock_guard<std::mutex> lock(m_handlersLocker);
// Since the function return value is by copy,
// before the function returns (and destruct the lock_guard object),
// it creates a copy of the m_handlers container.
return m_handlers;
}
private:
handler_collection_type m_handlers;
mutable std::mutex m_handlersLocker;
};
}
#endif // EventHandler_hpp
I then use this generic class to save a callback member method:
CallbackHandler.hpp
#ifndef CallbackHandler_hpp
#define CallbackHandler_hpp
#pragma once
#include "EventHandler.hpp"
#include <functional>
#include <mutex>
using namespace EventHandler;
template <typename... Args>
class CallbackHandler
{
public:
typedef std::function<void(Args...)> callbackFunc_type;
explicit CallbackHandler() : mb_callbackIsSet(false), mi_handlerID(0){}
explicit CallbackHandler(const callbackFunc_type& handlerFunc) : mb_callbackIsSet(false), mi_handlerID(0)
{
SetCallbackMethod(handlerFunc);
}
~CallbackHandler()
{
m_callbackEvent.remove_id(mi_handlerID);
}
void SetCallbackMethod(const callbackFunc_type& handlerFunc)
{
m_callbackEvent.remove_id(mi_handlerID);
mi_handlerID = m_callbackEvent.add([=](Args... params) {
handlerFunc(params...);
});
mb_callbackIsSet = true;
}
bool DoCallback(Args... params)
{
if (mb_callbackIsSet)
{
std::lock_guard<std::mutex> lock(callbackLocker);
m_callbackEvent(params...);
return true;
}
return false;
}
private:
unsigned int mi_handlerID;
Event_Base<Args...> m_callbackEvent;
bool mb_callbackIsSet;
std::mutex callbackLocker;
};
#endif //CallbackHandler_hpp
I can use the CallbackHandler in every other class now:
//Imagine an example class named Screenshotmodul
//In this example I use wxImage as a callback object, it is the output of my Screenshotmodul class and should be given back to my main class
//It could be any other (or several others like this: <ObjectType, OtherCallbackType> )
class Screenshotmodul
{
public:
//[...]
CallbackHandler<wxImage> m_callbackHandler;
template<typename ParentClass>
void SetCallbackMethod(ParentClass* parent, void (ParentClass::* method)(wxImage))
{
//using the given object pointer and the given member method in a lambda function
//saving that lambda as callback method
m_callbackHandler.SetCallbackMethod([=](wxImage img) {
(parent->*method)(img); });
}
}
In my main class I can then set a callback method:
std::unique_ptr<Screenshotmodul> sm = std::make_unique<Screenshotmodul>();
sm->SetCallbackMethod(this, &MyMainClass::CallbackfuncForScreenshotmodul);
//Of course there should be a callback function as just described:
void MyMainClass::CallbackfuncForScreenshotmodul(wxImage img)
{
//img now contains the callback value, that the Screenshotmodul class created
}
This is my approach and it is not perfect, but it works for me.
Cheers
Natu
I am doing a small project using arduino and I need to use an array of methods.
I've tried doing what I would normally do in C# / Java but that didn't work. I went online and tried many different examples and I kept getting lead to the same error message.
class ColourSensor{
public:
void (*routine[3])() = {
routine0,
routine1,
routine2
};
void routine0();
void routine1();
void routine2();
};
When I compile I get the following error:
cannot convert 'ColourSensor::routine0' from type 'void (ColourSensor::)()' to type 'void (*)()'
Things get complicated because they are methods. A method has an implicit hidden this parameter, so it's a different type than a free functions.
This is the correct syntax:
class ColourSensor
{
public:
using Routine = void (ColourSensor::*)();
Routine routines[3] = {
&ColourSensor::routine0,
&ColourSensor::routine1,
&ColourSensor::routine2,
};
void routine0();
void routine1();
void routine2();
void test()
{
// calling syntax:
(this->*routines[0])();
}
};
An alternative which simplifies the calling syntax using non-capturing lambdas (which can decay to function pointer):
class ColourSensor
{
public:
using Routine = void (*)(ColourSensor*);
Routine routines[3] = {
[](ColourSensor* cs) { return cs->routine0(); },
[](ColourSensor* cs) { return cs->routine1(); },
[](ColourSensor* cs) { return cs->routine2(); }
};
void routine0();
void routine1();
void routine2();
void test()
{
// simpler calling syntax
routines[0](this);
}
};
Going one step further (and off the rails), if you know you always use this object to call the methods you need to capture this in lambda. The problem now is that capturing lambdas can't decay to function pointers and since every lambda is a different type you can't store them in an array. The usual solution in C++ is type erasure with std::function. Unfortunately arduino C++ environment doesn't have the C++ standard library (because of the size constraints). For reference, this is how it would have looked (and since we are using the standard library, we use std::array):
/// !! not working in Arduino !!
#include <functional>
#include <array>
class ColourSensor
{
public:
std::array<std::function<void(void)>, 3> routines = {
[this]() { return this->routine0(); },
[this]() { return this->routine1(); },
[this]() { return this->routine2(); }
};
void routine0();
void routine1();
void routine2();
void test()
{
// simple calling syntax
routines[0]();
}
};
There is a workaround for this. And although it's a bit of work to setup, it's actually faster than the std::function because we don't use type erasure:
class ColourSensor;
class Routine
{
private:
using R = void (ColourSensor::*)();
R routine_;
ColourSensor* calling_obj_;
public:
Routine(R routine, ColourSensor* calling_obj)
: routine_{routine}, calling_obj_{calling_obj}
{}
void operator()();
};
class ColourSensor
{
public:
Routine routines[3] = {
Routine{&ColourSensor::routine0, this},
Routine{&ColourSensor::routine1, this},
Routine{&ColourSensor::routine2, this}
};
void routine0();
void routine1();
void routine2();
void test()
{
// simple calling syntax
routines[0]();
}
};
void Routine::operator()() { return (calling_obj_->*routine_)(); }
If however your routines don't use the state of the ColourSensor, than you can make them static functions:
class ColourSensor
{
public:
using Routine = void (*)();
Routine routines[3] = {
routine0,
routine1,
routine2,
};
static void routine0();
static void routine1();
static void routine2();
void test()
{
routines[0]();
}
};
I have several classes that each of them has an ID and the Id is passed to the class as a template parameter:
typedef class1<1> baseClass;
typedef class2<2> baseClass;
typedef class<100> baseClass;
Now I need a map so if I can associate 1 with Class1 and 2 with Class2 and so on.
How can I create such vector? I am working on a header only library, so it should be a header only definition.
I am looking something that do the same thing that this code would do (if someone can compile it!):
std::map<int,Type> getMap()
{
std::map<int,Type> output;
output.add(1,class1);
output.add(2,class2);
output.add(100,class100);
}
The idea is that when I get as input 1, I create a class1 and when I receive 2, I create class2.
Any suggestion is very appreciated.
using this data, then I can write a function like this:
void consume(class1 c)
{
// do something interesting with c
}
void consume(class2 c)
{
// do something interesting with c
}
void consume(class3 c)
{
// do something interesting with c
}
void consume(int id,void * buffer)
{
auto map=getMap();
auto data= new map[id](buffer); // assuming that this line create a class based on map, so the map provide the type that it should be created and then this line create that class and pass buffer to it.
consume(data);
}
As a sketch:
class BaseClass { virtual ~BaseClass() = default; };
template<std::size_t I>
class SubClass : public BaseClass {};
namespace detail {
template<std::size_t I>
std::unique_ptr<BaseClass> makeSubClass() { return { new SubClass<I> }; }
template<std::size_t... Is>
std::vector<std::unique_ptr<BaseClass>(*)> makeFactory(std::index_sequence<Is...>)
{ return { makeSubclass<Is>... }; }
}
std::vector<std::unique_ptr<BaseClass>(*)> factory = detail::makeFactory(std::make_index_sequence<100>{});
We populate the vector by expanding a parameter pack, so we don't have to write out all 100 instantiations by hand. This gives you Subclass<0> at factory[0], Subclass<1> at factory[1], etc. up to Subclass<99> at factory[99].
If I understand correctly you want a map to create different types according to a given number.
If that is so, then the code should look something like this:
class Base
{
};
template <int number>
class Type : public Base
{
public:
Type()
{
std::cout << "type is " << number << std::endl;
}
};
using Type1 = Type<1>;
using Type2 = Type<2>;
using Type3 = Type<3>;
using CreateFunction = std::function<Base*()>;
std::map<int, CreateFunction> creators;
int main()
{
creators[1] = []() -> Base* { return new Type1(); };
creators[2] = []() -> Base* { return new Type2(); };
creators[3] = []() -> Base* { return new Type3(); };
std::vector<Base*> vector;
vector.push_back(creators[1]());
vector.push_back(creators[2]());
vector.push_back(creators[3]());
}
output:
type is 1
type is 2
type is 3
If you need only to create object, it would be enough to implement template creator function like:
template<int ID>
Base<ID> Create()
{
return Base<ID>();
}
And then use it:
auto obj1 = Create<1>();
auto obj2 = Create<2>();
// etc
Working example: https://ideone.com/urh7h6
Due to C++ being a statically-typed language, you may choose to either have arbitrary types that do a fixed set of things or have a fixed set of types do arbitrary things, but not both.
Such limitations is embodied by std::function and std::variant. std::function can have arbitrary types call operator() with a fixed signature, and std::variant can have arbitrary functions visit the fixed set of types.
Since you already said the types may be arbitrary, you may only have a fixed set of things you can do with such a type (e.g. consume). The simplest way is to delegate the hard work to std::function
struct Type
{
template<typename T>
Type(T&& t)
: f{[t = std::forward<T>(t)]() mutable { consume(t); }} {}
std::function<void()> f;
};
void consume(Type& t)
{
t.f();
}
What you are looking for is either the Stategy pattern:
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class A {
public:
A() {}
virtual void doIt() {};
};
class Aa : public A {
public:
Aa() {}
virtual void doIt() {
std::cout << "do it the Aa way" << std::endl;
}
};
class Ab : public A {
public:
Ab() {}
virtual void doIt() {
std::cout << "do it the Ab way" << std::endl;
}
};
class Concrete {
public:
Concrete(std::string const& type) {
if (type == ("Aa")) {
_a.reset(new Aa());
} else if (type == "Ab") {
_a.reset(new Ab());
}
}
void doIt () const {
_a->doIt();
}
private:
std::unique_ptr<A> _a;
};
int main() {
std::vector<Concrete> vc;
vc.push_back(Concrete("Aa"));
vc.push_back(Concrete("Ab"));
for (auto const& i : vc) {
i.doIt();
}
return 0;
}
Will output:
do it the Aa way
do it the Ab way
I want to have objects with one method which calls a function (but every object should have a different function to call). I will try to show you what I mean by showing an example:
class Human
{
public:
void setMyFunction(void func); // specify which function to call
void callMyFunction(); // Call the specified function
};
void Human::setMyFunction(void func) // ''
{
myFunction = func;
}
void Human::callMyFunction() // ''
{
myFunction();
}
void someRandomFunction() // A random function
{
// Some random code
}
int main()
{
Human Lisa; // Create Object
Lisa.setMyFunction(); // Set the function for that object
Lisa.callMyFunction(); // Call the function specified earlier
}
This code (obviously) doesn't work but I hope you understand what I am trying to accomplish.
MfG, TPRammus
You might use std::function.
#include <functional>
class Human
{
std::function<void()> mFunc;
public:
void setMyFunction(std::function<void()> func) { mFunc = func; }
void callMyFunction() { if (mFunc) mFunc(); }
};
Demo
I would suggest using a simple function pointer. Just do this:
class Human
{
public:
using func_t = void (*)();
void setMyFunction(func_t f) {
func = f;
}
void callMyFunction() {
func();
}
private:
func_t func;
};
The reasons why one might prefer function pointers to std::function are:
Performance. Calling std::function tends to be slower, than calling a function by pointer.
std::function needs truly ugly syntax when one needs to bind it to an overloaded function.
Example:
void foo();
void foo(int x = 0);
void check() {
Human h;
h.setMyFunction(&foo);
}
Will fail to compile.
Here is some code:
typedef void (*ACallBack)(int i);
class SomeClass
{
private:
ACallBack aCallBack;
public:
void SetCallBack(ACallBack aCallBack);
};
void SomeClass::SetCallBack(ACallBack aCallBack)
{
this->aCallBack = aCallBack;
}
class SomeOtherClass
{
private:
SomeClass someClass;
public:
void InitializeSomeClass();
private:
void callBackMethod(int i);
};
void SomeOtherClass::InitializeSomeClass()
{
this->changeVariable = 10;
this->someClass.SetCallBack(this->callBackMethod); // DOESN'T WORK
this->someClass.UseCallBack();
}
void SomeOtherClass::callBackMethod(int i)
{
}
void globalCallBack(int i)
{
int myInt = i;
}
int main()
{
SomeClass sC;
sC.SetCallBack(globalCallBack); //WORKS!!
}
Basically if I try to set my callback function in SomeOtherClass it doesn't work but when I set it globally in main it does. What am I missing here?
Just use std::function and std::bind():
typedef std::function<void(int i)> ACallBack;
// old code pretty much the same
int main()
{
using namespace std::placeholders;
SomeClass sC;
sC.SetCallBack(globalCallBack); //WORKS!!
SomeOtherClass oC;
sC.SetCallBack(std::bind(&SomeOtherClass::callBackMethod,oC,_1)); //WORKS AS WELL!!
}
In this case you do not really need to pass void *userData but may add it as well if you need old code to compile.
You have to make the method static:
static void callBackMethod(int i, void* userData);
if you need a pointer to a method that is not static, ie an instance method, it becomes more complex.
typedef void ( myclass::*FUNC ) (int i, void* userData);
and if you want to use it, it becomes a hastlle :
myclass obj; // instantiate myclass
FUNC f = &myclass::myfunc; // assign address
( obj.*f ) ( 123, NULL ); // and call it