dtool: add StreamReader/StreamWriter move ctor and assignment ops

This commit is contained in:
rdb 2019-04-29 18:17:54 +02:00
parent 269d3db290
commit 5d6b4f4a77
4 changed files with 57 additions and 2 deletions

View File

@ -43,7 +43,18 @@ StreamReader(const StreamReader &copy) :
}
/**
* The copy constructor does not copy ownership of the stream.
* The move constructor steals ownership of the stream.
*/
INLINE StreamReader::
StreamReader(StreamReader &&from) noexcept :
_in(from._in),
_owns_stream(from._owns_stream)
{
from._owns_stream = false;
}
/**
* The copy assignment operator does not copy ownership of the stream.
*/
INLINE void StreamReader::
operator = (const StreamReader &copy) {
@ -54,6 +65,19 @@ operator = (const StreamReader &copy) {
_owns_stream = false;
}
/**
* The move assignment operator steals ownership of the stream.
*/
INLINE void StreamReader::
operator = (StreamReader &&from) noexcept {
if (_owns_stream) {
delete _in;
}
_in = from._in;
_owns_stream = from._owns_stream;
from._owns_stream = false;
}
/**
*
*/

View File

@ -31,7 +31,9 @@ public:
PUBLISHED:
INLINE explicit StreamReader(std::istream *in, bool owns_stream);
INLINE StreamReader(const StreamReader &copy);
INLINE StreamReader(StreamReader &&from) noexcept;
INLINE void operator = (const StreamReader &copy);
INLINE void operator = (StreamReader &&from) noexcept;
INLINE ~StreamReader();
INLINE std::istream *get_istream() const;

View File

@ -51,7 +51,21 @@ StreamWriter(const StreamWriter &copy) :
}
/**
* The copy constructor does not copy ownership of the stream.
* The move constructor steals ownership of the stream.
*/
INLINE StreamWriter::
StreamWriter(StreamWriter &&from) noexcept :
#ifdef HAVE_PYTHON
softspace(0),
#endif
_out(from._out),
_owns_stream(from._owns_stream)
{
from._owns_stream = false;
}
/**
* The copy assignment operator does not copy ownership of the stream.
*/
INLINE void StreamWriter::
operator = (const StreamWriter &copy) {
@ -62,6 +76,19 @@ operator = (const StreamWriter &copy) {
_owns_stream = false;
}
/**
* The move assignment operator steals ownership of the stream.
*/
INLINE void StreamWriter::
operator = (StreamWriter &&from) noexcept {
if (_owns_stream) {
delete _out;
}
_out = from._out;
_owns_stream = from._owns_stream;
from._owns_stream = false;
}
/**
*
*/

View File

@ -32,7 +32,9 @@ public:
PUBLISHED:
INLINE explicit StreamWriter(std::ostream *out, bool owns_stream);
INLINE StreamWriter(const StreamWriter &copy);
INLINE StreamWriter(StreamWriter &&from) noexcept;
INLINE void operator = (const StreamWriter &copy);
INLINE void operator = (StreamWriter &&from) noexcept;
INLINE ~StreamWriter();
INLINE std::ostream *get_ostream() const;