explicitly forbid copying of fdstreams; implemented move for fdstreams
This commit is contained in:
parent
f7fb1db4a8
commit
5f3364e805
45
fdstream.cpp
45
fdstream.cpp
@ -9,6 +9,8 @@
|
|||||||
#include "libscio.hpp"
|
#include "libscio.hpp"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
// fdostream --------------------------------------------------------------
|
||||||
|
|
||||||
sc::io::fdostream::~fdostream() {
|
sc::io::fdostream::~fdostream() {
|
||||||
close();
|
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() {
|
sc::io::fdistream::~fdistream() {
|
||||||
close();
|
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() {
|
sc::io::fdstream::~fdstream() {
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
@ -41,3 +73,16 @@ void sc::io::fdstream::close() {
|
|||||||
_iobuf.fd(-1);
|
_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;
|
||||||
|
}
|
||||||
|
31
libscio.hpp
31
libscio.hpp
@ -53,9 +53,18 @@ namespace sc {
|
|||||||
|
|
||||||
class fdostream : public std::ostream {
|
class fdostream : public std::ostream {
|
||||||
public:
|
public:
|
||||||
fdostream(int fd) : std::ostream(&_outbuf), _outbuf(fd) {}
|
|
||||||
fdostream() : std::ostream(&_outbuf), _outbuf(-1) {}
|
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();
|
virtual ~fdostream();
|
||||||
|
|
||||||
int fd() const { return _outbuf.fd(); }
|
int fd() const { return _outbuf.fd(); }
|
||||||
@ -70,9 +79,18 @@ namespace sc {
|
|||||||
|
|
||||||
class fdistream : public std::istream {
|
class fdistream : public std::istream {
|
||||||
public:
|
public:
|
||||||
fdistream(int fd) : std::istream(&_inbuf), _inbuf(fd) {}
|
|
||||||
fdistream() : std::istream(&_inbuf), _inbuf(-1) {}
|
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();
|
virtual ~fdistream();
|
||||||
|
|
||||||
int fd() const { return _inbuf.fd(); }
|
int fd() const { return _inbuf.fd(); }
|
||||||
@ -90,6 +108,15 @@ namespace sc {
|
|||||||
fdstream() : std::iostream(&_iobuf) {}
|
fdstream() : std::iostream(&_iobuf) {}
|
||||||
fdstream(int fd) : std::iostream(&_iobuf), _iobuf(fd) {}
|
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();
|
virtual ~fdstream();
|
||||||
|
|
||||||
int fd() const { return _iobuf.fd(); }
|
int fd() const { return _iobuf.fd(); }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user