المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : how to set a class member function as parameter of an other class in its constructor



C++ Programming
04-08-2009, 12:30 PM
Here the code of a "pulsar" in Qt context. QMTPulsar is an abstract class used for execution of periodic switches, and execute a function at the end of each period. The compilation message is given.
Best regards.

> QMThreadOneAction is used to run asynchronously a function;

La création des QMThreadOneAction est incorrecte.

//-------------------------------------
/** QMTPulsar.h
> Pulse component
- mechanism of periodic (or not) raise and fall call
*/

#ifndef __TESTUTILS_H__
#define __TESTUTILS_H__

#ifdef DEVICELOADER_EXPORTS
#define DEVICELOADER_API __declspec(dllexport)
#else
#define DEVICELOADER_API __declspec(dllimport)
#endif

#include "QtCore/QtGlobal"
#include "QtCore/QThread"
#include "MTDevicePrv.h"


//-------------------------------------
/** QMTPulsar
> process up and down switches, and function execution at each switch.
*/
class DEVICELOADER_API QMTPulsar :
public QThread
{
//-------------------------------------
/** QMThreadOneAction
> execute asynchronously a function
*/
class QMThreadOneAction :
public QThread
{
private:
MTDevicePrv *_device;
void (__cdecl* QMTPulsar::_fonc)(MTDevicePrv *dev);
bool _running;

//! execute function once
void run(void)
{
if (_fonc) {
if (!_running) {
_running = true;

try {
_fonc( _device);
}
catch(...){}

_running = false;
}
}
}
public:
//! constructeur
QMThreadOneAction (
MTDevicePrv *dev,
void (__cdecl* QMTPulsar::fonc)(MTDevicePrv *)
)
{
_running = false;
_device = dev;
_fonc = fonc;
}

//! execute asynchronously function
void go( void) {
run();
}
};

private:

MTDevicePrv *_device;

//! start with action or not
/**
_startWithAction = false

F1 F2
_______________
D1 | D2 |
__________

_startWithAction = true

F1 D1 F2
_______________
| | D2
__________

*/
bool _startWithAction;
//! D1 (microsecond)
qint64 _firstDurationMUS;
//! D2 (microsecond)
qint64 _secondDurationMUS;
//! repeat mechanism
bool _periodic;
//! is operating
bool _active;

QMThreadOneAction *_thFirst;
QMThreadOneAction *_thSecond;

//! processing
void run() {
do {
if (!_active) {
QThread::yieldCurrentThread();
}
else {
if (!_startWithAction) {
QThread::usleep( _firstDurationMUS);
_firstAction();
QThread::usleep( _secondDurationMUS);
_secondAction();
}
else {
_firstAction();
QThread::usleep( _firstDurationMUS);
_secondAction();
QThread::usleep( _secondDurationMUS);
}
}
} while( _periodic);
};

//! first action call to implement
void _firstAction( void) {
_thFirst->go();
}

//! second action call to implement
virtual void _secondAction( void) {
_thSecond->go();
}

public:

//! first action call to implement
virtual void fnFirstAction( MTDevicePrv *dev) = 0;

//! second action call to implement
virtual void fnSecondAction( MTDevicePrv *dev) = 0;

/** create pulsar
@param startWithAction start immediately firstAction or not
@param firstDurationMUS first delay in micro sec
@param secondDurationMUS second delay in micro sec
@param periodic periodic or not
@param startActive start activity immediately
*/
QMTPulsar(
MTDevicePrv *dev,
bool startWithAction,
qint64 firstDurationMUS,
qint64 secondDurationMUS,
bool periodic,
bool startActive)
{
_device = dev;
_startWithAction = startWithAction;
_periodic = periodic;
_firstDurationMUS = firstDurationMUS;
_secondDurationMUS = secondDurationMUS;
_active = startActive;

/* error #171)*/ _thFirst = new QMThreadOneAction( _device, fnFirstAction);
/* error #172)*/ _thSecond = new QMThreadOneAction( _device, fnSecondAction);
/*
Compilation en cours...
QMTPulsar.cpp
c:\_projets-cpp2008\deviceframework\deviceframework\devicemanager\qmtpulsar.h(171) : error C3867: &'QMTPulsar::fnFirstAction' : liste d'arguments manquante dans l'appel de fonction ; utilisez 'QMTPulsar::fnFirstAction' pour créer un pointeur vers membre
c:\_projets-cpp2008\deviceframework\deviceframework\devicemanager\qmtpulsar.h(172) : error C3867: &'QMTPulsar::fnSecondAction' : liste d'arguments manquante dans l'appel de fonction ; utilisez 'QMTPulsar::fnSecondAction' pour créer un pointeur vers membre
Le journal de génération a été enregistré à l'emplacement "file://c:\_Projets-CPP2008\DeviceFramework\DeviceFramework\DeviceManager\src\Debug\BuildLog.htm"
DeviceManager - 2 erreur(s), 0 avertissement(s)
*/
run();
};

~QMTPulsar();

//! return owner device
MTDevice* getDevice() const {
return _device ;
}

//! activate (if true) mechanism
virtual void activate(
bool active) {
_active = active;
if (!QThread::isRunning())
run();
};
};

#endif