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:
Qt Code:
  1. #ifndef MY_CLASS_H
  2. #define MY_CLASS_H
  3.  
  4. #include <QObject>
  5. #include <QThread>
  6. #include <QIODevice>
  7.  
  8. class MyClass : public QThread
  9. {
  10. Q_OBJECT
  11.  
  12. public:
  13.  
  14. explicit MyClass(QIODevice* inputDevice = 0, QObject *parent = 0);
  15. ~MyClass();
  16.  
  17. some members ...
  18.  
  19. private:
  20.  
  21. template <class T>
  22. T* device_cast(QIODevice* device);
  23.  
  24. QIODevice* m_pInputDevice;
  25. QIODevice* m_pForwardDevice;
  26.  
  27. some other members ...
  28.  
  29. };
  30.  
  31. #endif // MY_CLASS_H
To copy to clipboard, switch view to plain text mode 

.cpp file:
Qt Code:
  1. #include "MyClass.h"
  2. #include <SerialIODevice.h>
  3. //#include <SslIODevice.h>
  4.  
  5. MyClass::MyClass(QIODevice* inputDevice, QObject *parent) :
  6. QThread(parent), m_pInputDevice(inputDevice), m_pForwardDevice(NULL), m_bForwardData(false)
  7. {
  8. }
  9.  
  10. MyClass::~MyClass()
  11. {
  12. if(m_pInputDevice && device_cast(m_pInputDevice)->isOpen())
  13. device_cast(m_pInputDevice)->close();
  14.  
  15. if(m_pForwardDevice && device_cast(m_pForwardDevice)->isOpen())
  16. device_cast(m_pForwardDevice)->close();
  17. }
  18.  
  19. // use case example
  20. void MyClass::sendData(quint8 id, QByteArray data)
  21. {
  22. if (!m_pInputDevice)
  23. {
  24. emit error("Input device not set!");
  25. return;
  26. }
  27.  
  28. if (!device_cast(m_pInputDevice)->isOpen())
  29. {
  30. emit error("Input device is not open");
  31. return;
  32. }
  33.  
  34. if (device_cast(m_pInputDevice)->openMode() == QIODevice::ReadOnly)
  35. {
  36. emit error("Input device is in read only mode!");
  37. return;
  38. }
  39.  
  40. // some processing...
  41.  
  42. if(device_cast(m_pInputDevice)->write(data) == -1)
  43. emit error("Data output failed.");
  44. }
  45.  
  46. template <class T>
  47. T* MyClass::device_cast(QIODevice* device)
  48. {
  49. if(qobject_cast<SerialIODevice*>(device))
  50. return qobject_cast<SerialIODevice*>(device);
  51. else
  52. return device;
  53. }
To copy to clipboard, switch view to plain text mode