Public Member Functions | Static Public Member Functions | List of all members
Xfer< T > Class Template Reference

A simple container for copying or transferring objects of type <T>. More...

Public Member Functions

 Xfer (T *=0)
 Store object pointer and manage its deletion. More...
 
 Xfer (T &, bool allowTransfer=false)
 Construct by copying or by transferring the parameter contents. More...
 
 Xfer (const T &)
 Construct by copying the parameter contents. More...
 
 Xfer (const Xfer< T > &)
 Construct by transferring the contents. More...
 
 ~Xfer ()
 Destructor. More...
 
void operator= (T &)
 Transfer the contents into the object. More...
 
void operator= (const Xfer< T > &)
 Transfer the contents into the object. More...
 
Toperator() () const
 Reference to the underlying datatype. More...
 
Toperator-> () const
 Pointer to the underlying datatype. More...
 

Static Public Member Functions

static const Xfer< T > & null ()
 Return a null object reference. More...
 

Detailed Description

template<class T>
class Foam::Xfer< T >

A simple container for copying or transferring objects of type <T>.

The wrapped object of type <T> 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<T>::transfer(List<T>&) method could result in some allocated memory becoming inaccessible, the xferMoveTo() function should be used to invoke the correct List<T>::transfer(DynamicList<T>&) method.

DynamicList<label> dynLst;
...
labelList plainLst( xferMoveTo<labelList>(dynLst) );

Of course, since this example is a very common operation, the DynamicList::xfer() method transfers to a plain List anyhow. It would thus be simpler (and clearer) just to use the following code:

DynamicList<label> dynLst;
...
labelList plainLst(dynLst.xfer());
See also
xferCopy, xferCopyTo, xferMove, xferMoveTo, xferTmp
Source files

Definition at line 85 of file Xfer.H.

Constructor & Destructor Documentation

◆ Xfer() [1/4]

Xfer ( T p = 0)
inlineexplicit

Store object pointer and manage its deletion.

Can also be used later to transfer by assignment

Definition at line 40 of file XferI.H.

◆ Xfer() [2/4]

Xfer ( T t,
bool  allowTransfer = false 
)
inlineexplicit

Construct by copying or by transferring the parameter contents.

Definition at line 47 of file XferI.H.

◆ Xfer() [3/4]

Xfer ( const T t)
inlineexplicit

Construct by copying the parameter contents.

Definition at line 63 of file XferI.H.

◆ Xfer() [4/4]

Xfer ( const Xfer< T > &  t)
inline

Construct by transferring the contents.

Definition at line 72 of file XferI.H.

◆ ~Xfer()

~Xfer ( )
inline

Destructor.

Definition at line 83 of file XferI.H.

Member Function Documentation

◆ null()

const Foam::Xfer< T > & null ( )
inlinestatic

Return a null object reference.

Definition at line 31 of file XferI.H.

◆ operator=() [1/2]

void operator= ( T t)
inline

Transfer the contents into the object.

Definition at line 96 of file XferI.H.

◆ operator=() [2/2]

void operator= ( const Xfer< T > &  t)
inline

Transfer the contents into the object.

Definition at line 103 of file XferI.H.

◆ operator()()

T & operator() ( ) const
inline

Reference to the underlying datatype.

Definition at line 114 of file XferI.H.

◆ operator->()

T * operator-> ( ) const
inline

Pointer to the underlying datatype.

Definition at line 121 of file XferI.H.

References tmp< T >::isTmp(), Foam::T(), Foam::xferCopy(), Foam::xferCopyTo(), Foam::xferMove(), Foam::xferMoveTo(), and Foam::xferTmp().

Here is the call graph for this function:

The documentation for this class was generated from the following files: