Przeglądaj źródła

Implement "move" equivalent of assignment operator for StrPair

Dmitry-Me 11 lat temu
rodzic
commit
08b40dd8a5
2 zmienionych plików z 29 dodań i 1 usunięć
  1. 24 1
      tinyxml2.cpp
  2. 5 0
      tinyxml2.h

+ 24 - 1
tinyxml2.cpp

@@ -70,6 +70,29 @@ StrPair::~StrPair()
 }
 
 
+void StrPair::TransferTo( StrPair& other )
+{
+    if ( this == &other ) {
+        return;
+    }
+    // This in effect implements the assignment operator by "moving"
+    // ownership (as in auto_ptr).
+
+    TIXMLASSERT( other._flags == 0 );
+    TIXMLASSERT( other._start == 0 );
+    TIXMLASSERT( other._end == 0 );
+
+    other.Reset();
+
+    other._flags = _flags;
+    other._start = _start;
+    other._end = _end;
+
+    _flags = 0;
+    _start = 0;
+    _end = 0;
+}
+
 void StrPair::Reset()
 {
     if ( _flags & NEEDS_DELETE ) {
@@ -824,7 +847,7 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
         // We read the end tag. Return it to the parent.
         if ( ele && ele->ClosingType() == XMLElement::CLOSING ) {
             if ( parentEnd ) {
-                *parentEnd = ele->_value;
+                ele->_value.TransferTo( *parentEnd );
             }
 			node->_memPool->SetTracked();	// created and then immediately deleted.
             DeleteNode( node );

+ 5 - 0
tinyxml2.h

@@ -184,6 +184,8 @@ public:
     char* ParseText( char* in, const char* endTag, int strFlags );
     char* ParseName( char* in );
 
+    void TransferTo( StrPair& other );
+
 private:
     void Reset();
     void CollapseWhitespace();
@@ -197,6 +199,9 @@ private:
     int     _flags;
     char*   _start;
     char*   _end;
+
+    StrPair( const StrPair& other );	// not supported
+    void operator=( StrPair& other );	// not supported, use TransferTo()
 };