B
Barzo
Hi,
I need to create a class to read/write from a Serial port and I need a
suggestion about implementing events.
I've implemented the observer pattern with an interface more or less
like this:
class ISerialDevEvents
{
virtual void DataArrived(void) = 0;
}
class SerialDev
{
public:
SerialDev(ISerialDevEvents *handler);
int Open()
{
/* Create thread and Run it ( Reading() ) */
}
protected:
ISerialDevEvents _handler;
private:
Reading()
{
do while (!StopRequest)
{
....
if (eventHandler != 0)
_handler->DataArrived(void);
}
}
};
class User : public CThreadEventHandler
{
main()
{
SerialDev Dev;
Dev.Open(this);
}
virtual void DataArrived()
{
Dev.GetData();
}
};
But with this method a user class has to implements all virtual
methods of its interface also if it doesn't use it.
What about if, instead of an interface, I made the open() method to
accept a pointer to member function?
template<class R, class T>
Open( R (T::*pmf)() )
If the classes lives in separated threads is it legal?
PS [OT]: Someone knows if the Boost::serial_port is able to manage PIN
like DTR, RTS etc..?
Tnx,
Daniele.
I need to create a class to read/write from a Serial port and I need a
suggestion about implementing events.
I've implemented the observer pattern with an interface more or less
like this:
class ISerialDevEvents
{
virtual void DataArrived(void) = 0;
}
class SerialDev
{
public:
SerialDev(ISerialDevEvents *handler);
int Open()
{
/* Create thread and Run it ( Reading() ) */
}
protected:
ISerialDevEvents _handler;
private:
Reading()
{
do while (!StopRequest)
{
....
if (eventHandler != 0)
_handler->DataArrived(void);
}
}
};
class User : public CThreadEventHandler
{
main()
{
SerialDev Dev;
Dev.Open(this);
}
virtual void DataArrived()
{
Dev.GetData();
}
};
But with this method a user class has to implements all virtual
methods of its interface also if it doesn't use it.
What about if, instead of an interface, I made the open() method to
accept a pointer to member function?
template<class R, class T>
Open( R (T::*pmf)() )
If the classes lives in separated threads is it legal?
PS [OT]: Someone knows if the Boost::serial_port is able to manage PIN
like DTR, RTS etc..?
Tnx,
Daniele.