Browse Source

dtool: add StreamReader/StreamWriter move ctor and assignment ops

rdb 6 years ago
parent
commit
5d6b4f4a77

+ 25 - 1
dtool/src/prc/streamReader.I

@@ -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::
 INLINE void StreamReader::
 operator = (const StreamReader &copy) {
 operator = (const StreamReader &copy) {
@@ -54,6 +65,19 @@ operator = (const StreamReader &copy) {
   _owns_stream = false;
   _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;
+}
+
 /**
 /**
  *
  *
  */
  */

+ 2 - 0
dtool/src/prc/streamReader.h

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

+ 28 - 1
dtool/src/prc/streamWriter.I

@@ -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::
 INLINE void StreamWriter::
 operator = (const StreamWriter &copy) {
 operator = (const StreamWriter &copy) {
@@ -62,6 +76,19 @@ operator = (const StreamWriter &copy) {
   _owns_stream = false;
   _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;
+}
+
 /**
 /**
  *
  *
  */
  */

+ 2 - 0
dtool/src/prc/streamWriter.h

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