Templated Class as Polymorphic Function Parameter

I apologize if this is a beginner question, but I can't find much on how something like this is supposed to work. I am using boost::iostreams to abstract away a couple of different methods of talking to our hardware devices. So I've created a device for each transport, each of which derive from device<bidirectional>; ip_device, serial_device, and usb_device. I then declare streams for each as stream<ip_device>, etc... Now that I have the streams, I have a class that communicates with the device and I would like to construct or initialize an object of that class with any one of the streams. I have yet to figure out how to form the constructor/function parameter to allow this. I've tried something like this, a stream of the base type of my devices: void init( stream< device<bidirectional> > str); I think something like this may work but I'm not sure if I want to template the function: template< typename T> void init(stream<T> str); A reference to any kind of discussion about using templates in such a way would help me in more than just this instance. Thanks for any input, Josh

Now that I have the streams, I have a class that communicates with the device and I would like to construct or initialize an object of that class with any one of the streams. I have yet to figure out how to form the constructor/function
Joshua Perry <josh <at> 6bit.com> writes: parameter to allow this.
I've tried something like this, a stream of the base type of my devices: void init( stream< device<bidirectional> > str);
Something like this? struct foo { void init(std::iostream& strm) { m_strm = &strm; } private: std::iostream* m_strm; }; Roman Perepelitsa.

Yes, and thats what I am doing now, but it only gives me access as a std::iostream. I would really like access to the other functionality of my templated class. On Jul 8, 2008, at 9:15 AM, Roman Perepelitsa wrote:
Now that I have the streams, I have a class that communicates with the device and I would like to construct or initialize an object of that class with any one of the streams. I have yet to figure out how to form the constructor/function
Joshua Perry <josh <at> 6bit.com> writes: parameter to allow this.
I've tried something like this, a stream of the base type of my devices: void init( stream< device<bidirectional> > str);
Something like this?
struct foo { void init(std::iostream& strm) { m_strm = &strm; } private: std::iostream* m_strm; };
Roman Perepelitsa.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Joshua Perry <josh <at> 6bit.com> writes:
Now that I have the streams, I have a class that communicates with the device and I would like to construct or initialize an object of that class with any one of the streams. I have yet to figure out how to form the constructor/function parameter to allow this.
I've tried something like this, a stream of the base type of my devices: void init( stream< device<bidirectional> > str);
Something like this? <snip>
Yes, and thats what I am doing now, but it only gives me access as a std::iostream. I would really like access to the other functionality of my templated class.
Have you considered a base class with templatized derived classes, like boost::function? If that is what you are after I think it is called 'type erasure'.
participants (3)
-
John Femiani
-
Joshua Perry
-
Roman Perepelitsa