FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::Xfer
00026 
00027 Description
00028     A simple container for copying or transferring objects of type <T>.
00029 
00030     The wrapped object of type <T> must implement a transfer() method and
00031     an operator=() copy method.
00032 
00033     Since it is decided upon construction of the Xfer object whether the
00034     parameter is to be copied or transferred, the contents of the resulting
00035     Xfer object can be transferred unconditionally. This greatly simplifies
00036     defining constructors or methods in other classes with mixed
00037     transfer/copy semantics without requiring 2^N different versions.
00038 
00039     When transferring between dissimilar types, the xferCopyTo() and
00040     xferMoveTo() functions can prove useful. An example is transferring
00041     from a DynamicList to a List.  Since the
00042     List<T>::transfer(List<T>&) method could result in some allocated
00043     memory becoming inaccessible, the xferMoveTo() function should be used to
00044     invoke the correct List<T>::transfer(DynamicList<T>&) method.
00045 
00046     @code
00047         DynamicList<label> dynLst;
00048         ...
00049         labelList plainLst( xferMoveTo<labelList>(dynLst) );
00050     @endcode
00051 
00052     Of course, since this example is a very common operation, the
00053     DynamicList::xfer() method transfers to a plain List anyhow.
00054     It would thus be simpler (and clearer) just to use the following code:
00055 
00056     @code
00057         DynamicList<label> dynLst;
00058         ...
00059         labelList plainLst(dynLst.xfer());
00060     @endcode
00061 
00062 SeeAlso
00063     xferCopy, xferCopyTo, xferMove, xferMoveTo, xferTmp
00064 
00065 SourceFiles
00066     XferI.H
00067 
00068 \*---------------------------------------------------------------------------*/
00069 
00070 #ifndef Xfer_H
00071 #define Xfer_H
00072 
00073 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00074 
00075 namespace Foam
00076 {
00077 
00078 // Forward declaration of classes
00079 template<class T> class tmp;
00080 
00081 /*---------------------------------------------------------------------------*\
00082                            Class Xfer Declaration
00083 \*---------------------------------------------------------------------------*/
00084 
00085 template<class T>
00086 class Xfer
00087 {
00088     // Private data
00089 
00090         //- Pointer to underlying datatype
00091         mutable T* ptr_;
00092 
00093 public:
00094 
00095     // Constructors
00096 
00097         //- Store object pointer and manage its deletion
00098         //  Can also be used later to transfer by assignment
00099         inline explicit Xfer(T* = 0);
00100 
00101         //- Construct by copying or by transferring the parameter contents
00102         inline explicit Xfer(T&, bool allowTransfer=false);
00103 
00104         //- Construct by copying the parameter contents
00105         inline explicit Xfer(const T&);
00106 
00107         //- Construct by transferring the contents
00108         inline Xfer(const Xfer<T>&);
00109 
00110     // Destructor
00111 
00112         inline ~Xfer();
00113 
00114     // Member Functions
00115 
00116         //- Return a null object reference
00117         inline static const Xfer<T>& null();
00118 
00119     // Member Operators
00120 
00121         //- Transfer the contents into the object
00122         inline void operator=(T&);
00123 
00124         //- Transfer the contents into the object
00125         inline void operator=(const Xfer<T>&);
00126 
00127         //- Reference to the underlying datatype
00128         inline T& operator()() const;
00129 
00130         //- Pointer to the underlying datatype
00131         inline T* operator->() const;
00132 
00133 };
00134 
00135 
00136 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00137 
00143 template<class T>
00144 inline Xfer<T> xferCopy(const T&);
00145 
00151 template<class T>
00152 inline Xfer<T> xferMove(T&);
00153 
00154 
00160 template<class T>
00161 inline Xfer<T> xferTmp(Foam::tmp<T>&);
00162 
00163 
00170 template<class To, class From>
00171 inline Xfer<To> xferCopyTo(const From&);
00172 
00173 
00187 template<class To, class From>
00188 inline Xfer<To> xferMoveTo(From&);
00189 
00190 
00191 } // End namespace Foam
00192 
00193 
00194 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00195 
00196 #include "XferI.H"
00197 
00198 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00199 
00200 #endif
00201 
00202 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines