OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Xfer.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::Xfer
00027 
00028 Description
00029     A simple container for copying or transferring objects of type <T>.
00030 
00031     The wrapped object of type <T> must implement a transfer() method and
00032     an operator=() copy method.
00033 
00034     Since it is decided upon construction of the Xfer object whether the
00035     parameter is to be copied or transferred, the contents of the resulting
00036     Xfer object can be transferred unconditionally. This greatly simplifies
00037     defining constructors or methods in other classes with mixed
00038     transfer/copy semantics without requiring 2^N different versions.
00039 
00040     When transferring between dissimilar types, the xferCopyTo() and
00041     xferMoveTo() functions can prove useful. An example is transferring
00042     from a DynamicList to a List.  Since the
00043     List<T>::transfer(List<T>&) method could result in some allocated
00044     memory becoming inaccessible, the xferMoveTo() function should be used to
00045     invoke the correct List<T>::transfer(DynamicList<T>&) method.
00046 
00047     @code
00048         DynamicList<label> dynLst;
00049         ...
00050         labelList plainLst( xferMoveTo<labelList>(dynLst) );
00051     @endcode
00052 
00053     Of course, since this example is a very common operation, the
00054     DynamicList::xfer() method transfers to a plain List anyhow.
00055     It would thus be simpler (and clearer) just to use the following code:
00056 
00057     @code
00058         DynamicList<label> dynLst;
00059         ...
00060         labelList plainLst(dynLst.xfer());
00061     @endcode
00062 
00063 SeeAlso
00064     xferCopy, xferCopyTo, xferMove, xferMoveTo, xferTmp
00065 
00066 SourceFiles
00067     XferI.H
00068 
00069 \*---------------------------------------------------------------------------*/
00070 
00071 #ifndef Xfer_H
00072 #define Xfer_H
00073 
00074 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00075 
00076 namespace Foam
00077 {
00078 
00079 // Forward declaration of classes
00080 template<class T> class tmp;
00081 
00082 /*---------------------------------------------------------------------------*\
00083                            Class Xfer Declaration
00084 \*---------------------------------------------------------------------------*/
00085 
00086 template<class T>
00087 class Xfer
00088 {
00089     // Private data
00090 
00091         //- Pointer to underlying datatype
00092         mutable T* ptr_;
00093 
00094 public:
00095 
00096     // Constructors
00097 
00098         //- Store object pointer and manage its deletion
00099         //  Can also be used later to transfer by assignment
00100         inline explicit Xfer(T* = 0);
00101 
00102         //- Construct by copying or by transferring the parameter contents
00103         inline explicit Xfer(T&, bool allowTransfer=false);
00104 
00105         //- Construct by copying the parameter contents
00106         inline explicit Xfer(const T&);
00107 
00108         //- Construct by transferring the contents
00109         inline Xfer(const Xfer<T>&);
00110 
00111     // Destructor
00112 
00113         inline ~Xfer();
00114 
00115     // Member Functions
00116 
00117         //- Return a null object reference
00118         inline static const Xfer<T>& null();
00119 
00120     // Member Operators
00121 
00122         //- Transfer the contents into the object
00123         inline void operator=(T&);
00124 
00125         //- Transfer the contents into the object
00126         inline void operator=(const Xfer<T>&);
00127 
00128         //- Reference to the underlying datatype
00129         inline T& operator()() const;
00130 
00131         //- Pointer to the underlying datatype
00132         inline T* operator->() const;
00133 
00134 };
00135 
00136 
00137 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00138 
00144 template<class T>
00145 inline Xfer<T> xferCopy(const T&);
00146 
00152 template<class T>
00153 inline Xfer<T> xferMove(T&);
00154 
00155 
00161 template<class T>
00162 inline Xfer<T> xferTmp(Foam::tmp<T>&);
00163 
00164 
00171 template<class To, class From>
00172 inline Xfer<To> xferCopyTo(const From&);
00173 
00174 
00188 template<class To, class From>
00189 inline Xfer<To> xferMoveTo(From&);
00190 
00191 
00192 } // End namespace Foam
00193 
00194 
00195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00196 
00197 #include "XferI.H"
00198 
00199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00200 
00201 #endif
00202 
00203 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd