[iostreams and serialization] serialize via iostreams
I'm trying to use a combination of the iostreams library and the serialization library in order to be able to serialise objects and send them over a socket. I've written a class called "SocketDevice" which implements a bidirectional iostreams instance, and I'm trying to use this with serialization, in particular the binary_oarchive and binary_iarchive archive models. The problem is that when I do this, the code ends up calling a copy constructor (which doesn't exist) on my SocketDevice class. However I don't want to implement a copy constructor for this class as it doesn't make sense (as it manages a socket and a buffer for that socket, etc). The interface of my SocketDevice class looks like this: class SocketDevice : public boost::iostreams::deviceboost::iostreams::bidirectional { public: typedef char char_type; SocketDevice (int sock); ~SocketDevice (); std::streamsize read (char *s, std::streamsize n); std::streamsize write (const char *s, std::streamsize n); private: const int sock; char *buffer; size_t bufferSize; }; and I'm trying to do the object sending/receiving via template methods, eg: template<class C> void sendObject (const C& data, int sock) { boost::iostreams::stream_buffer<SocketDevice> buf (sock); std::ostream s (&buf); boost::archive::binary_oarchive oa (s); oa << data; } The copy constructor appears to be synthesised when the 'buf' object is instantiated in the above template method. My guess is that I'm doing something wrong, but I can't see what. I've also tried splitting my SocketDevice class from a bidirectional one to a pair of input & output ones, but with the same result. I've also tried making sendObject() a normal (non-template) method, this makes no difference. I realise that there are issues with trying to combine socket communications with object serialisation, but I'm still curious as to why I have this copy constructor issue.
Pooyan McSporran wrote:
I'm trying to use a combination of the iostreams library and the serialization library in order to be able to serialise objects and send them over a socket.
Sounds reasonable.
I've written a class called "SocketDevice" which implements a bidirectional iostreams instance, and I'm trying to use this with serialization, in particular the binary_oarchive and binary_iarchive archive models.
The problem is that when I do this, the code ends up calling a copy constructor (which doesn't exist) on my SocketDevice class. However I don't want to implement a copy constructor for this class as it doesn't make sense (as it manages a socket and a buffer for that socket, etc).
Currently, except for standard streams and stream buffers, which are
given special treatment, Boost.Iostreams fitlers and devices must be
copy constructible. (See, e.g., the documentation for the template
parameter T here:
http://www.boost.org/libs/iostreams/doc/index.html?path=3.3)
The standard way to make a device copy constructible is to use
shared_ptr; you can see examples by looking at the implementation of
file, file_descriptor, and mapped_file.
I'm considering adding a storage policy parameter to the stream and
stream buffer templates to allow them to store a reference or an owned
pointer to a filter or device; this would allow you to do something like
this:
SocketDevice dev(sock);
stream_buffer
On 16/01/2008, Jonathan Turkanis
The problem is that when I do this, the code ends up calling a copy constructor (which doesn't exist) on my SocketDevice class. However I don't want to implement a copy constructor for this class as it doesn't make sense (as it manages a socket and a buffer for that socket, etc).
Currently, except for standard streams and stream buffers, which are given special treatment, Boost.Iostreams fitlers and devices must be copy constructible. (See, e.g., the documentation for the template parameter T here: http://www.boost.org/libs/iostreams/doc/index.html?path=3.3)
Thanks for the information, that clarifies things for me.
participants (2)
-
Jonathan Turkanis
-
Pooyan McSporran