diff --git a/fdstream.cpp b/fdstream.cpp index a12b7b4..0b719a1 100644 --- a/fdstream.cpp +++ b/fdstream.cpp @@ -9,6 +9,8 @@ #include "libscio.hpp" #include +// fdostream -------------------------------------------------------------- + sc::io::fdostream::~fdostream() { close(); } @@ -20,6 +22,21 @@ void sc::io::fdostream::close() { } } +sc::io::fdostream::fdostream(sc::io::fdostream&& other) : std::ostream(&_outbuf) { + _outbuf.fd(other._outbuf.fd()); + other._outbuf.fd(-1); +} + +sc::io::fdostream& sc::io::fdostream::operator=(sc::io::fdostream&& other) { + if (this != &other) { + _outbuf.fd(other._outbuf.fd()); + other._outbuf.fd(-1); + } + return *this; +} + +// fdistream -------------------------------------------------------------- + sc::io::fdistream::~fdistream() { close(); } @@ -31,6 +48,21 @@ void sc::io::fdistream::close() { } } +sc::io::fdistream::fdistream(sc::io::fdistream&& other) : std::istream(&_inbuf) { + _inbuf.fd(other._inbuf.fd()); + other._inbuf.fd(-1); +} + +sc::io::fdistream& sc::io::fdistream::operator=(sc::io::fdistream&& other) { + if (this != &other) { + _inbuf.fd(other._inbuf.fd()); + other._inbuf.fd(-1); + } + return *this; +} + +// fdstream --------------------------------------------------------------- + sc::io::fdstream::~fdstream() { close(); } @@ -41,3 +73,16 @@ void sc::io::fdstream::close() { _iobuf.fd(-1); } } + +sc::io::fdstream::fdstream(sc::io::fdstream&& other) : std::iostream(&_iobuf) { + _iobuf.fd(other._iobuf.fd()); + other._iobuf.fd(-1); +} + +sc::io::fdstream& sc::io::fdstream::operator=(sc::io::fdstream&& other) { + if (this != &other) { + _iobuf.fd(other._iobuf.fd()); + other._iobuf.fd(-1); + } + return *this; +} diff --git a/libscio.hpp b/libscio.hpp index 05ed456..8a98aa2 100644 --- a/libscio.hpp +++ b/libscio.hpp @@ -53,9 +53,18 @@ namespace sc { class fdostream : public std::ostream { public: - fdostream(int fd) : std::ostream(&_outbuf), _outbuf(fd) {} fdostream() : std::ostream(&_outbuf), _outbuf(-1) {} + fdostream(int fd) : std::ostream(&_outbuf), _outbuf(fd) {} + // no copying + fdostream(const fdostream&) = delete; + fdostream& operator=(const fdostream&) = delete; + + // moving allowed + fdostream(fdostream&& other); + fdostream& operator=(fdostream&& other); + + // cleanup: fdostream is RAII class for open file descriptor virtual ~fdostream(); int fd() const { return _outbuf.fd(); } @@ -70,9 +79,18 @@ namespace sc { class fdistream : public std::istream { public: - fdistream(int fd) : std::istream(&_inbuf), _inbuf(fd) {} fdistream() : std::istream(&_inbuf), _inbuf(-1) {} + fdistream(int fd) : std::istream(&_inbuf), _inbuf(fd) {} + // no copying + fdistream(const fdistream&) = delete; + fdistream& operator=(const fdistream&) = delete; + + // moving allowed + fdistream(fdistream&& other); + fdistream& operator=(fdistream&& other); + + // cleanup: fdistream is RAII class for open file descriptor virtual ~fdistream(); int fd() const { return _inbuf.fd(); } @@ -90,6 +108,15 @@ namespace sc { fdstream() : std::iostream(&_iobuf) {} fdstream(int fd) : std::iostream(&_iobuf), _iobuf(fd) {} + // no copying + fdstream(const fdstream&) = delete; + fdstream& operator=(const fdstream&) = delete; + + // moving allowed + fdstream(fdstream&& other); + fdstream& operator=(fdstream&& other); + + // cleanup: fdstream is RAII class for open file descriptor virtual ~fdstream(); int fd() const { return _iobuf.fd(); }