1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
28 A simple container for copying or transferring objects of type \<T\>.
30 The wrapped object of type \<T\> must implement a transfer() method and
31 an operator=() copy method.
33 Since it is decided upon construction of the Xfer object whether the
34 parameter is to be copied or transferred, the contents of the resulting
35 Xfer object can be transferred unconditionally. This greatly simplifies
36 defining constructors or methods in other classes with mixed
37 transfer/copy semantics without requiring 2^N different versions.
39 When transferring between dissimilar types, the xferCopyTo() and
40 xferMoveTo() functions can prove useful. An example is transferring
41 from a DynamicList to a List. Since the
42 List\<T\>::transfer(List\<T\>&) method could result in some allocated
43 memory becoming inaccessible, the xferMoveTo() function should be used to
44 invoke the correct List\<T\>::transfer(DynamicList\<T\>&) method.
47 DynamicList<label> dynLst;
49 labelList plainLst( xferMoveTo<labelList>(dynLst) );
52 Of course, since this example is a very common operation, the
53 DynamicList::xfer() method transfers to a plain List anyhow.
54 It would thus be simpler (and clearer) just to use the following code:
57 DynamicList<label> dynLst;
59 labelList plainLst(dynLst.xfer());
63 xferCopy, xferCopyTo, xferMove, xferMoveTo, xferTmp
68 \*---------------------------------------------------------------------------*/
73 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
78 // Forward declaration of classes
79 template<class T> class tmp;
81 /*---------------------------------------------------------------------------*\
82 Class Xfer Declaration
83 \*---------------------------------------------------------------------------*/
90 //- Pointer to underlying datatype
97 //- Store object pointer and manage its deletion
98 // Can also be used later to transfer by assignment
99 inline explicit Xfer(T* = 0);
101 //- Construct by copying or by transferring the parameter contents
102 inline explicit Xfer(T&, bool allowTransfer=false);
104 //- Construct by copying the parameter contents
105 inline explicit Xfer(const T&);
107 //- Construct by transferring the contents
108 inline Xfer(const Xfer<T>&);
115 //- Return a null object reference
116 inline static const Xfer<T>& null();
120 //- Transfer the contents into the object
121 inline void operator=(T&);
123 //- Transfer the contents into the object
124 inline void operator=(const Xfer<T>&);
126 //- Reference to the underlying datatype
127 inline T& operator()() const;
129 //- Pointer to the underlying datatype
130 inline T* operator->() const;
135 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
138 * Construct by copying the contents of the \a arg
140 * \sa xferCopyTo, xferMove, xferMoveTo, xferTmp and Foam::Xfer
143 inline Xfer<T> xferCopy(const T&);
146 * Construct by transferring the contents of the \a arg
148 * \sa xferCopy, xferCopyTo, xferMoveTo, xferTmp and Foam::Xfer
151 inline Xfer<T> xferMove(T&);
155 * Construct by transferring the contents of the \a arg
157 * \sa xferCopy, xferCopyTo, xferMove, xferMoveTo and Foam::Xfer
160 inline Xfer<T> xferTmp(Foam::tmp<T>&);
164 * Construct by copying the contents of the \a arg
165 * between dissimilar types
167 * \sa xferCopy, xferMove, xferMoveTo, xferTmp and Foam::Xfer
169 template<class To, class From>
170 inline Xfer<To> xferCopyTo(const From&);
174 * Construct by transferring the contents of the \a arg
175 * between dissimilar types
179 * DynamicList<label> dynLst;
181 * labelList plainLst( xferMoveTo<labelList>(dynLst) );
184 * \sa xferCopy, xferCopyTo, xferMove, xferTmp and Foam::Xfer
186 template<class To, class From>
187 inline Xfer<To> xferMoveTo(From&);
190 } // End namespace Foam
193 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 // ************************************************************************* //