Hello everybody,
I am trying to develop a class which has two QIODevice * members
and forwards data from the input device to the output device.
My problem is that my devices are:
- a SerialIODevice * (derived from QextSerialPort)
- a SslIODevice * (derived from QSslSocket)
- other derived classes to come
when I call open, getChar, write, ... members,
the members of QIODevice are called instead of the members of the derived
classes.
I tried to perform qobject_cast based tests in my code and it works.
However the code becomes very compliated. Now I am trying to develop
a template function which will perform the casting work and return
a pointer to the derived class. As it is the first time I develop template
functions, I have compilation errors I don'manage to solve even after
a lot of googleing. Errors are related to unfound prototype where I use
my template function.
Below are relevant parts of my code. Thanks in advance for helping me.
Regards.
.h file:
#ifndef MY_CLASS_H
#define MY_CLASS_H
#include <QObject>
#include <QThread>
#include <QIODevice>
{
Q_OBJECT
public:
~MyClass();
some members ...
private:
template <class T>
some other members ...
};
#endif // MY_CLASS_H
#ifndef MY_CLASS_H
#define MY_CLASS_H
#include <QObject>
#include <QThread>
#include <QIODevice>
class MyClass : public QThread
{
Q_OBJECT
public:
explicit MyClass(QIODevice* inputDevice = 0, QObject *parent = 0);
~MyClass();
some members ...
private:
template <class T>
T* device_cast(QIODevice* device);
QIODevice* m_pInputDevice;
QIODevice* m_pForwardDevice;
some other members ...
};
#endif // MY_CLASS_H
To copy to clipboard, switch view to plain text mode
.cpp file:
#include "MyClass.h"
#include <SerialIODevice.h>
//#include <SslIODevice.h>
QThread(parent
), m_pInputDevice
(inputDevice
), m_pForwardDevice
(NULL), m_bForwardData
(false) {
}
MyClass::~MyClass()
{
if(m_pInputDevice && device_cast(m_pInputDevice)->isOpen())
device_cast(m_pInputDevice)->close();
if(m_pForwardDevice && device_cast(m_pForwardDevice)->isOpen())
device_cast(m_pForwardDevice)->close();
}
// use case example
void MyClass
::sendData(quint8 id,
QByteArray data
) {
if (!m_pInputDevice)
{
emit error("Input device not set!");
return;
}
if (!device_cast(m_pInputDevice)->isOpen())
{
emit error("Input device is not open");
return;
}
if (device_cast
(m_pInputDevice
)->openMode
() == QIODevice::ReadOnly) {
emit error("Input device is in read only mode!");
return;
}
// some processing...
if(device_cast(m_pInputDevice)->write(data) == -1)
emit error("Data output failed.");
}
template <class T>
{
if(qobject_cast<SerialIODevice*>(device))
return qobject_cast<SerialIODevice*>(device);
else
return device;
}
#include "MyClass.h"
#include <SerialIODevice.h>
//#include <SslIODevice.h>
MyClass::MyClass(QIODevice* inputDevice, QObject *parent) :
QThread(parent), m_pInputDevice(inputDevice), m_pForwardDevice(NULL), m_bForwardData(false)
{
}
MyClass::~MyClass()
{
if(m_pInputDevice && device_cast(m_pInputDevice)->isOpen())
device_cast(m_pInputDevice)->close();
if(m_pForwardDevice && device_cast(m_pForwardDevice)->isOpen())
device_cast(m_pForwardDevice)->close();
}
// use case example
void MyClass::sendData(quint8 id, QByteArray data)
{
if (!m_pInputDevice)
{
emit error("Input device not set!");
return;
}
if (!device_cast(m_pInputDevice)->isOpen())
{
emit error("Input device is not open");
return;
}
if (device_cast(m_pInputDevice)->openMode() == QIODevice::ReadOnly)
{
emit error("Input device is in read only mode!");
return;
}
// some processing...
if(device_cast(m_pInputDevice)->write(data) == -1)
emit error("Data output failed.");
}
template <class T>
T* MyClass::device_cast(QIODevice* device)
{
if(qobject_cast<SerialIODevice*>(device))
return qobject_cast<SerialIODevice*>(device);
else
return device;
}
To copy to clipboard, switch view to plain text mode
Bookmarks