From 60d17345640fff9296754c008fe8aa3c7d2a7e4f Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 23 Oct 2008 09:56:21 +0200 Subject: [PATCH] add xferCopyTo and xferMoveTo functions --- applications/test/xfer/xferListTest.C | 16 ++--- src/OpenFOAM/memory/xfer/xfer.H | 97 ++++++++++++++++----------- src/OpenFOAM/memory/xfer/xferI.H | 44 +++++++++++- 3 files changed, 109 insertions(+), 48 deletions(-) diff --git a/applications/test/xfer/xferListTest.C b/applications/test/xfer/xferListTest.C index 019ca3bfd1..6f2af0932a 100644 --- a/applications/test/xfer/xferListTest.C +++ b/applications/test/xfer/xferListTest.C @@ -38,6 +38,8 @@ Description using namespace Foam; + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: @@ -104,21 +106,17 @@ int main(int argc, char *argv[]) } face f1(dl); + face f2(xferCopy(dl)); Info<< "dl[" << dl.size() << "/" << dl.allocSize() << "] " << dl << endl; Info<< "f1: " << f1 << endl; - - // note: the allocated size will be wrong, but we can at least avoid - // wasting memory by using a shrink - - face f2(xferMove(dl.shrink())); - Info<< "dl[" << dl.size() << "/" << dl.allocSize() << "] " << dl << endl; Info<< "f2: " << f2 << endl; - dl.clearStorage(); - + // note: using xferMoveTo to ensure the correct transfer() method is called + face f3( xferMoveTo(dl) ); Info<< "dl[" << dl.size() << "/" << dl.allocSize() << "] " << dl << endl; - + Info<< "f3: " << f3 << endl; + return 0; } diff --git a/src/OpenFOAM/memory/xfer/xfer.H b/src/OpenFOAM/memory/xfer/xfer.H index e13782c9f4..cec4624c13 100644 --- a/src/OpenFOAM/memory/xfer/xfer.H +++ b/src/OpenFOAM/memory/xfer/xfer.H @@ -26,19 +26,33 @@ Class Foam::xfer Description - A simple container that can be used to copy or transfer the contents - of objects of type \. - - Since it is decided upon construction of the xfer object whether the - parameter is to be copied or transferred, the contents of the resulting - object can be transferred unconditionally. - - This greatly simplifies defining the constructors for other classes - with mixed transfer/copy semantics. + A simple container for copying or transferring objects of type \. The wrapped object of type \ must implement a transfer() method and an operator=() copy method. + Since it is decided upon construction of the xfer object whether the + parameter is to be copied or transferred, the contents of the resulting + xfer object can be transferred unconditionally. This greatly simplifies + defining constructors or methods in other classes with mixed + transfer/copy semantics without requiring 2^N different versions. + + When transferring between dissimilar types, the xferCopyTo() and + xferMoveTo() functions can prove useful. An example is transferring + from a DynamicList to a List. Since the + List\::transfer(List\&) method could result in some allocated + memory becoming inaccessible, the xferMoveTo() function can be used to + invoke the correct List\::transfer(DynamicList\&) method. + + @code + DynamicList