c++ pointer to a function as argument - c++

I have a C++ API with a cThread class, and this method to create a thread:
void cThread::start(void(*a_function)(void), CThreadPriority a_level);
I've done a class and a init() method to launch a thread and an updateHaptics() method to be executed by the thread:
void EntryClass::init()
{
typedef void (EntryClass::*method)();
method p;
p = &EntryClass::updateHaptics;
// create a thread which starts the main haptics rendering loop
cThread* hapticsThread = new cThread();
hapticsThread->start(p, CTHREAD_PRIORITY_HAPTICS);
}
void EntryClass::updateHaptics(void)
{
// ...
}
My problem is to pass the updateHaptics() method as an argument to the cThread::start() method.
I've got this error:
1>EntryClass.cpp(55): error C2664: 'void chai3d::cThread::start(void (__cdecl *)(void *),const chai3d::CThreadPriority,void *)' : impossible de convertir l'argument 1 de 'method' en 'void (__cdecl *)(void)'
REM: I'm under Windows 8/Visual Studio

The signature you indicated
void(*a_function)(void)
is for a function, not for a class method. A static method will work too
Note the difference with the typedef you used:
void (EntryClass::*method)();
The definition could be:
class EntryClass {
public:
void init();
static void updateHaptics(); // <--- NOTE the static
};
and your implementation
void EntryClass::init()
{
typedef void (*method)(); // <---- NOTE THIS CHANGE
method p;
p = &EntryClass::updateHaptics;
// create a thread which starts the main haptics rendering loop
cThread* hapticsThread = new cThread();
hapticsThread->start(p, CTHREAD_PRIORITY_HAPTICS);
}
void EntryClass::updateHaptics(void)
{
// ...
}

As I know, we can use only static function as a thread proc. Yes we can pass class static function also.

Related

How to use a C++ member function as an interrupt handler in Arduino? [duplicate]

This question already has answers here:
Use class member functions as callbacks?
(6 answers)
Closed 3 years ago.
Arduino's attachInterrupt requires a callback function of type void(*)(), but I'd like to pass it a member function instead. I can't use a C++ member function here because of its implicit this argument.
Background
I know it's possible to use C++ member functions as callbacks. For example, FreeRTOS' xTaskCreate(...) takes a callback function of type void(*)(*).
isocpp.org has a nice FAQ on the use of member functions as callbacks.
In this related question user thiton writes:
Most sane callback libraries allow you to pass this void* argument to the functions as a way to have user-defined data in it
Perhaps the Arduino library is not "sane?" or perhaps this is design decision made to simplify the Arduino API?
it's there... inside attachInterrupt
I'm programming for an ESP32. In the arduino-esp32 implementation of attachInterrupt, there's a function called __attachInterruptFunctionalArg(...) that seems to do exactly what I want, but since it's not part of the Arduino API, I'm hesitant to include it in a project that's for public consumption because it may break.
Example program
// An attempt to summarize https://github.com/pierremolinaro/acan2517/issues/4
#include <stdio.h>
#include <stdint.h>
#include <functional>
#define IRAM_ATTR __attribute__((section(".iram1")))
// from `esp32-hal-gpio.c`
typedef void (*voidFuncPtrArg)(void*);
extern void __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtrArg userFunc, void * arg, int intr_type, bool functional);
// from Arduino `FunctionalInterrupt.cpp`
void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode);
void IRAM_ATTR interruptFunctional(void* arg);
// from Arduino `FunctionalInterrupt.h`
struct InterruptArgStructure {
std::function<void(void)> interruptFunction;
};
// from ACAN2517
class ACAN2517
{
public: ACAN2517 (const int interrupt_pin);
public: void begin (void (* inInterruptServiceRoutine) (void));
public: void begin_functional (void (* inInterruptServiceRoutine) (void *), void *);
public: void isr(void);
private: const int interrupt_pin;
};
ACAN2517::ACAN2517 (const int interrupt_pin):
interrupt_pin(interrupt_pin)
{};
#define FALLING 0
// This won't work with a member function
void ACAN2517::begin (void (* inInterruptServiceRoutine) (void)) {
attachInterrupt(interrupt_pin, inInterruptServiceRoutine, FALLING);
}
// This will, but is prone to breakage when the Arduino internals change
void ACAN2517::begin_functional (void (* inInterruptServiceRoutine) (void *), void *arg)
{
__attachInterruptFunctionalArg(interrupt_pin, inInterruptServiceRoutine, arg, FALLING, true);
}
void ACAN2517::isr(void)
{
printf("fhtagn");
}
//===
// User code begin
//===
#define N_DRIVERS 3
ACAN2517 g_driver(23); // Initializing a driver instance statically
ACAN2517 *drivers[N_DRIVERS];
void call_ACAN_isr(void *arg)
{
ACAN2517 *driver = (ACAN2517 *)arg;
driver->isr();
}
int main()
{
g_driver.begin( []{g_driver.isr();} ); // No problem
for (int i = 0; i < N_DRIVERS; i++)
{
drivers[i] = &ACAN2517(i);
drivers[i]->begin( []{drivers[i]->isr();} );
// ERROR
// static void lambda []void ()->void::_FUN()
// an enclosing-function local variable cannot be referenced in a lambda body unless it is in the capture list
}
for (int i = 0; i < N_DRIVERS; i++)
{
drivers[i] = &ACAN2517(i);
drivers[i]->begin( [i]{drivers[i]->isr();} );
// ERROR
// no suitable conversion function from "lambda []void ()->void" to "void (*)()" exists
}
for (int i = 0; i < N_DRIVERS; i++)
{
drivers[i] = &ACAN2517(i);
ACAN2517 *driver = drivers[i];
drivers[i]->begin_functional( [driver]{driver->isr();}, driver);
// Not sure how to get this to work in a lambda...
}
for (int i = 0; i < N_DRIVERS; i++)
{
drivers[i] = &ACAN2517(i);
ACAN2517 *driver = drivers[i];
drivers[i]->begin_functional( call_ACAN_isr, driver);
// OK
}
}
//===
// User code end
//===
// from esp32-hal-gpio.c
extern void __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtrArg userFunc, void * arg, int intr_type, bool functional)
{
// ...
}
// from Arduino `FunctionalInterrupt.cpp`
void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode)
{
// use the local interrupt routine which takes the ArgStructure as argument
__attachInterruptFunctionalArg (pin, (voidFuncPtrArg)interruptFunctional, new InterruptArgStructure{intRoutine}, mode, true);
}
void IRAM_ATTR interruptFunctional(void* arg)
{
InterruptArgStructure* localArg = (InterruptArgStructure*)arg;
if (localArg->interruptFunction)
{
localArg->interruptFunction();
}
}
I can't use a C++ member function here because of its implicit this argument.
Yes, that is exactly the problem and this can't be solved without extra code, if your API did not provide something what lets you store some additional data like the this pointer.
What you simply can do is:
Write your own wrapper and register the callback to the original handler. But that creates another indirection which increases the latency.
The other way is not as simple, but a bit less slow:
Write your own interrupt handler and callback registration. As you have the original sources of the arduino libs, you simply can replace the stuff around the attachInterrupt function.
Sorry, but there is no magic way to generate a data store for this without any additional software.

Arduino: Use Timer in c++ class

I am trying to use a timer to repeatedly change the PWM Output over time to have a smooth transition when the brightness changes. I keep getting this error when trying to compile the code:
/Users/jt/Documents/Arduino/libraries/SingleColorLight/SingleColorLight.cpp: In constructor 'CSingleColorLight::CSingleColorLight(int)':
/Users/jt/Documents/Arduino/libraries/SingleColorLight/SingleColorLight.cpp:13:58: error: cannot convert 'CSingleColorLight::DimmerCallback' from type 'void (CSingleColorLight::)(void*)' to type 'void ()(void)'
ets_timer_setfn(&Dimmer, this->DimmerCallback, NULL);
Here is my code:
class CSingleColorLight {
private:
int pin;
int intensitySetPoint;
int intensityActual;
int percentageBuffer;
ETSTimer Dimmer;
int dimmerCount;
public:
CSingleColorLight(int _pin);
bool setIntensity(int _intensity);
int getIntensity();
bool getStatus(void);
bool setStatus(bool _status);
void DimmerCallback(void*);
};
and in the cpp file:
void CSingleColorLight::DimmerCallback(void*) {
if(dimmerCount>0){
dimmerCount--;
intensityActual++;
} else if(dimmerCount<0){
dimmerCount++;
intensityActual--;
} else {
ets_timer_disarm(&Dimmer);
}
analogWrite(pin, percentageToTime[intensityActual]);
return;
}
It asks for a pointer, right? Any idea how to fix this?
Thanks a lot!
If you want DimmerCallback to take a void* argument, then you need to name it, like
void CSingleColorLight::DimmerCallback(void* x)
but you are not using the void* in the code. It looks like you should just get rid of it, so it would be
void CSingleColorLight::DimmerCallback()
int the cpp and
void DimmerCallback();
in the header.
A void* argument is a pointer that can point to any data type, it is not the same as void which is just no argument.

C++ template overload call

I am trying to implement an execution pattern which takes any function and executes it with its own conditions/preparations. Regardless of this being a useful thing to do, it just doesn't work. It seems i can't access the template overload of the "Execute"-function (called in "main").
Specifically: Why can't i call the overloaded template function of Execute?
This is the full program:
#include "stdafx.h"
#include <functional>
class TransparentFunctionWrapper
{
public:
virtual void Execute(std::function<void()> executeFunction) = 0;
template<class C>
C Execute(std::function<C(void)> executeFunction) // template-overload of the abstract function which will implicitly call it
{
C ret;
Execute( // calls the abstract function with a lambda function as parameter
[ret, executeFunction](void) -> C // lambda declaraction
{ //
ret = executeFunction; // lambda body
}); //
return ret;
}
};
class ExampleExecutor : public TransparentFunctionWrapper
{
public:
virtual void Execute(std::function<void()> executeFunction)
{
printf("executed before.");
executeFunction();
printf("executed after.");
}
};
void DoStuff() {}
int ReturnStuff() { return -5; }
int main()
{
ExampleExecutor executor;
executor.Execute(DoStuff);
int i = executor.Execute<int>(ReturnStuff); // Why does this not work? ERROR: "type name is not allowed"
getchar();
return 0;
}
Note: Visual Studio marks
Execute<int>(ReturnStuff) // "int" is marked as Error: type name is not allowed
The compilation puts out the error
"type 'int' unexpected"
Thanks to everyone willing to help!
ExampleExecutor::Execute is not overriding TransparentFunctionWrapper::Execute, and it is hiding it in the executor.Execute<int> call.
You must explicitly call TransparentFunctionWrapper::Execute, as it is hidden by ExampleExecutor::Execute. Here's a possible way of doing that:
int i = executor.TransparentFunctionWrapper::Execute<int>(ReturnStuff);
live example on coliru

C++ pass function pointer with parameter to function

I think I have misunderstood how function pointers work. In this example:
class Helper
{
public:
typedef void (*SIMPLECALLBK)(const char*);
Helper(){};
void NotifyHelperbk(SIMPLECALLBK pCbk)
{ m_pSimpleCbk = pSbk; }
private:
SIMPLECALLBK m_pSimpleCbk;
}
// where i call the func
class Main
{
public:
Main(){};
private:
Helper helper
void SessionHelper(const char* msg);
}
Main.cpp
void Main::SessionHelper(const char* msg)
{
....
}
helper.NotifyHelperbk(&Main::SessionHelper);
I get the following error:
error C2664: 'Main::NotifyHelperbk' : cannot convert parameter 1 from 'void (__thiscall Main::* )(const char *)' to 'Helper::SIMPLECALLBK'
1> There is no context in which this conversion is possible
What am I missing here?
Main::SessionHelper is a non static method. So add static to it to be able to use it as function pointer. Or use member method pointer (you will need a instance to call it).
if you use c++11 you can use std::bind
class Helper
{
public:
void NotifyHelperbk(std::function<void(char*)> func){
/* Do your stuff */
func("your char* here");
}
And your main :
Main.cpp
Main m;
helper.NotifyHelperbk(std::bind(&Main::SessionHelper, m, std::placeholder_1));

error: cannot convert 'void (CApp::*)()' to 'void (*)()' for argument '1' to 'void Mix_HookMusicFinished(void (*)())'

I'm trying to create a C++ application using SDL and SDL_Mixer for audio, and am trying to follow this tutorial. However, using SDL_Mixer's Mix_HookMusicFinished() isn't working, giving the error: argument of type 'void (CApp::)()' does not match 'void (*)()'
I've researched this error, and it seems the problem is that cleanMusic is a member function of CApp. I can't tell how to solve the problem, however, since most problems similar to this one are centered around pthread_create(). My cleanMusic() function needs to be able to access music_ which is a private variable of CApp. How can I resolve the error?
Here is the code for CApp.h, CApp::handleKeyEvents(), and CApp::cleanMusic(). Let me know if you need to see something else.
CApp.h
#ifndef CAPP_H
#define CAPP_H
#include <SDL.h>
#include <SDL_mixer.h>
#include <gl\gl.h>
#include <gl\glu.h>
class CApp {
private:
bool isRunning_;
private:
void cleanMusic();
private:
SDL_Surface *surfDisplay_;
Mix_Music *music_;
bool isRotating_;
GLfloat rQuad_;
public:
CApp();
int run();
public:
bool initialize();
void handleEvents(SDL_Event *event);
void loopData();
void render();
void clean();
public:
void handleKeyEvents(SDL_KeyboardEvent *key);
};
#endif // CAPP_H
CApp::handleKeyEvents()
#include "CApp.h"
void CApp::handleKeyEvents(SDL_KeyboardEvent *key) {
switch(key->keysym.sym) {
case SDLK_m:
if (key->state == SDL_PRESSED) {
if(music_ == NULL) {
music_ = Mix_LoadMUS("resources\\audio\\boop.wav");
Mix_PlayMusic(music_, 0);
Mix_HookMusicFinished(cleanMusic);
isRotating_ = true;
} else {
Mix_HaltMusic();
cleanMusic();
isRotating_ = false;
}
}
break;
default:
break;
}
}
CApp::cleanMusic()
#include "CApp.h"
void CApp::cleanMusic() {
Mix_FreeMusic(music_);
music_ = NULL;
}
Two changes. cleanMusic needs to be static.
static void cleanMusic();
Second, you register the hook with:
Mix_HookMusicFinished(&CApp::cleanMusic);
Since your method is now static, music_ needs to be static as well.
static Mix_Music *music_;
This means that there will only be one instance of this variable shared between all instantiations of CApp. Since I haven't seen all of your code, I can't tell if this is an issue.
void cleanMusic(); is what is known as a member function. A member function is very different from a normal function. The reason your compiler complains is because Mix_HookMusicFinished expects a normal function pointer of type void (*)(), but you are trying to pass a member function pointer of type void (CApp::*)(). These types are incompatible.
The simplest solution is just to make cleanMusic a normal function and Mix_Music *music; a global:
Mix_Music *music;
void cleanMusic() {
Mix_FreeMusic(music);
music = NULL;
}
Another way is to make them both static members:
static void cleanMusic();
static Mix_Music *music_;