Serializing file descriptors

#include <x/serialize.H>

typedef x::serialize<> serialize_impl;

std::stringstream sb;

std::ostreambuf_iterator<char> ofs_iter=sb.rdbuf();

serialize_impl::iterator<std::ostreambuf_iterator<char> >
    o_iter(ofs_iter);

x::fd fd(x::fd::open("filename", O_WRONLY | O_CREAT | O_TRUNC, 0666));

o_iter(fd);

File descriptors implement a serialization method. The file descriptor serialization method serializes the contents of the file descriptor. Serialization is implemented for regular files only, and the file descriptor must be positioned at the beginning of the file before serializing or deserializing it.

When passing a file descriptor reference to a deserialization iterator, if the file descriptor is already open, the contents of the file get deserialized into the opened file. Otherwise, a temporary file gets created and opened. This functionality is implemented in the default traits class.