Commit a60a620b authored by Mark Olesen's avatar Mark Olesen
Browse files

updated xfer class

parent 7b5f45b4
......@@ -93,9 +93,9 @@ CompactListList<T>::CompactListList(const UList<label>& rowSizes, const T& t)
template<class T>
CompactListList<T>::CompactListList(xfer<CompactListList<T> >& lst)
CompactListList<T>::CompactListList(const xfer<CompactListList<T> >& lst)
{
transfer(lst.ref());
transfer(*lst);
}
template<class T>
......
......@@ -104,7 +104,7 @@ public:
CompactListList(const UList<label>& rowSizes, const T&);
//- Construct by transferring the parameter contents
CompactListList(xfer<CompactListList<T> >&);
CompactListList(const xfer<CompactListList<T> >&);
//- Construct as copy or re-use as specified.
CompactListList(CompactListList<T>&, bool reUse);
......
......@@ -127,9 +127,9 @@ Foam::List<T>::List(const List<T>& a)
// Construct by transferring the parameter contents
template<class T>
Foam::List<T>::List(xfer<List<T> >& lst)
Foam::List<T>::List(const xfer<List<T> >& lst)
{
transfer(lst.ref());
transfer(*lst);
}
......
......@@ -95,7 +95,7 @@ public:
List(const List<T>&);
//- Construct by transferring the parameter contents
List(xfer<List<T> >&);
List(const xfer<List<T> >&);
//- Construct as copy or re-use as specified.
List(List<T>&, bool reUse);
......
......@@ -55,9 +55,9 @@ PackedList<nBits>::PackedList(const PackedList<nBits>& PList)
template<int nBits>
PackedList<nBits>::PackedList(xfer<PackedList<nBits> >& lst)
PackedList<nBits>::PackedList(const xfer<PackedList<nBits> >& lst)
{
transfer(lst.ref());
transfer(*lst);
}
......
......@@ -135,7 +135,7 @@ public:
PackedList(const PackedList<nBits>& PList);
//- Construct by transferring the parameter contents
PackedList(xfer<PackedList<nBits> >&);
PackedList(const xfer<PackedList<nBits> >&);
//- Construct from labelList.
PackedList(const labelList&);
......
......@@ -72,9 +72,9 @@ Foam::PtrList<T>::PtrList(const PtrList<T>& a, const CloneArg& cloneArg)
template<class T>
Foam::PtrList<T>::PtrList(xfer<PtrList<T> >& lst)
Foam::PtrList<T>::PtrList(const xfer<PtrList<T> >& lst)
{
transfer(lst.ref());
transfer(*lst);
}
......
......@@ -130,7 +130,7 @@ public:
PtrList(const PtrList<T>&, const CloneArg&);
//- Construct by transferring the parameter contents
PtrList(xfer<PtrList<T> >&);
PtrList(const xfer<PtrList<T> >&);
//- Construct as copy or re-use as specified.
PtrList(PtrList<T>&, bool reUse);
......
......@@ -51,9 +51,9 @@ UPtrList<T>::UPtrList(const label s)
template<class T>
UPtrList<T>::UPtrList(xfer<UPtrList<T> >& lst)
UPtrList<T>::UPtrList(const xfer<UPtrList<T> >& lst)
{
transfer(lst.ref());
transfer(*lst);
}
......
......@@ -111,7 +111,7 @@ public:
explicit UPtrList(const label);
//- Construct by transferring the parameter contents
UPtrList(xfer<UPtrList<T> >&);
UPtrList(const xfer<UPtrList<T> >&);
//- Construct as copy or re-use as specified.
UPtrList(UPtrList<T>&, bool reUse);
......
......@@ -26,11 +26,15 @@ Class
Foam::xfer
Description
A simple container for objects of type \<T\> that can be used for
transferring the contents rather than copying them.
A simple container that can be used to transfer the contents of objects
of type \<T\> of rather than copying them.
The wrapped object of type \<T\> must implement a transfer() method.
Note
The macro xferTmp(T,arg) can be used as a workaround for passing
temporaries to copy-constructors.
SourceFiles
xferI.H
......@@ -44,18 +48,8 @@ SourceFiles
namespace Foam
{
class Istream;
class Ostream;
// Forward declaration of friend functions and operators
template<class T> class xfer;
template<class T> Istream& operator>>(Istream&, xfer<T>&);
template<class T> Ostream& operator<<(Ostream&, const xfer<T>&);
/*---------------------------------------------------------------------------*\
Class xfer Declaration
Class xfer Declaration
\*---------------------------------------------------------------------------*/
template<class T>
......@@ -63,81 +57,40 @@ class xfer
{
// Private data
//- Reference to the underlying datatype
T& ref_;
// Private Member Functions
//- Disallow default bitwise copy construct
xfer(const xfer<T>&);
//- Disallow default bitwise assignment
void operator=(const xfer<T>&);
//- Pointer to temporary object
mutable T* ptr_;
public:
// Constructors
//- Construct null, delaying the transfer to later
//- Null constructor, transfer later by assignment
inline xfer();
//- Construct by transferring the parameter into this object
//- Construct by transferring the parameter contents into the object
inline xfer(T&);
//- Construct by transferring the parameter into this object
inline xfer(xfer<T>&);
//- Construct by transferring the parameter contents into the object
inline xfer(const xfer<T>&);
// Destructor
inline ~xfer();
// Member Functions
//- Access to the underlying datatype
inline T& ref();
//- Transfer the contents of the parameter into this object
inline void transfer(T&);
//- Transfer the contents of the parameter into this object
inline void transfer(xfer<T>&);
//- Transfer the contents from this object to the parameter
inline void yield(T&);
//- Transfer the contents from this object to the parameter
inline void yield(xfer<T>&);
// Member Operators
//- Transfer the contents into this object
//- Transfer the contents into the object
inline void operator=(T&);
//- Transfer the contents into this object
inline void operator=(xfer<T>&);
//- Transfer the contents into the object
inline void operator=(const xfer<T>&);
//- Return a non-const reference to const object
// Useful for read-constructors where the argument is temporary
inline xfer<T>& operator()() const;
// @sa xferTmp macro for an alternative for copy-constructors
inline const xfer<T>& operator()() const;
// Istream operator
//- Read from Istream, discarding current contents
friend Istream& operator>>
#ifndef __CINT__
<T>
#endif
(Istream&, xfer<T>&);
// Ostream operator
//- Write to Ostream
friend Ostream& operator<<
#ifndef __CINT__
<T>
#endif
(Ostream&, const xfer<T>&);
//- Reference to the underlying datatype
inline T& operator*() const;
};
......@@ -146,6 +99,27 @@ public:
} // End namespace Foam
/**
* @def xferTmp(T,arg)
* Construct a temporary and return a const reference to an xfer of
* type \<T\> and return a const reference.
*
* Useful for copy-constructors where the argument is temporary.
* This is a workaround for a template resolution problem.
*
* @par Example Use
* @code
* List<label> a;
* ...
* List<label> b(xferTmp(List<label>, a));
*
* @endcode
* @sa xfer class
*/
#define xferTmp(T,arg) \
(static_cast<const Foam::xfer< T >&>(Foam::xfer< T >(arg)()))
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "xferI.H"
......
......@@ -27,125 +27,71 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T>
Foam::xfer<T>::xfer()
inline Foam::xfer<T>::xfer()
:
ref_()
ptr_(new T)
{}
template<class T>
Foam::xfer<T>::xfer(T& t)
inline Foam::xfer<T>::xfer(T& t)
:
ref_()
ptr_(new T)
{
transfer(t);
ptr_->transfer(t);
}
template<class T>
Foam::xfer<T>::xfer(xfer<T>& t)
inline Foam::xfer<T>::xfer(const xfer<T>& t)
:
ref_()
ptr_(new T)
{
transfer(t);
ptr_->transfer(*(t.ptr_));
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class T>
Foam::xfer<T>::~xfer()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
T& Foam::xfer<T>::ref()
{
return ref_;
}
template<class T>
void Foam::xfer<T>::transfer(T& t)
inline Foam::xfer<T>::~xfer()
{
ref_.transfer(t);
}
template<class T>
void Foam::xfer<T>::transfer(xfer<T>& t)
{
ref_.transfer(t.ref_);
}
template<class T>
void Foam::xfer<T>::yield(T& t)
{
t.transfer(ref_);
}
template<class T>
void Foam::xfer<T>::yield(xfer<T>& t)
{
t.ref_.transfer(ref_);
delete ptr_;
ptr_ = 0;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>
void Foam::xfer<T>::operator=(T& t)
inline void Foam::xfer<T>::operator=(T& t)
{
transfer(t);
ptr_->transfer(t);
}
template<class T>
void Foam::xfer<T>::operator=(xfer<T>& t)
inline void Foam::xfer<T>::operator=(const xfer<T>& t)
{
if (this == &t)
// silently ignore copy to self
if (this != &t)
{
FatalErrorIn
(
"Foam::xfer<T>::operator="
"(const Foam::xfer<T>&)"
) << "Attempted assignment to self"
<< abort(FatalError);
ptr_->transfer(*(t.ptr_));
}
transfer(t);
}
template<class T>
Foam::xfer<T>& Foam::xfer<T>::operator()() const
{
return const_cast<xfer<T>&>(*this);
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
template<class T>
Foam::Istream& Foam::operator>>(Istream& is, xfer<T>& t)
inline const Foam::xfer<T>& Foam::xfer<T>::operator()() const
{
is >> t.ref_;
return is;
return *this;
}
template<class T>
Foam::Ostream& Foam::operator<<(Ostream& os, const xfer<T>& t)
inline T& Foam::xfer<T>::operator*() const
{
os << t.ref_;
return os;
return *ptr_;
}
// ************************************************************************* //
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment