xfer class modifications:

- removed operator* in favour of operator() for consistency with tmp
    class. The previous use of operator() for const casting didn't work
    anyhow due to template confusion.
  - added xferCopy(), xferMove() and xferTmp() template functions instead
  - preliminary changes to IOobjects and Fields for xfer
This commit is contained in:
Mark Olesen
2008-10-17 18:27:11 +02:00
parent 5cca2643f7
commit 91cb6d2912
24 changed files with 229 additions and 66 deletions

View File

@ -52,35 +52,35 @@ int main(int argc, char *argv[])
Info<< "lstA: " << lstA << endl;
Info<< "lstC: " << lstC << endl;
xfer<List<label> > xA(lstA, true);
xfer<List<label> > xA = xferMove(lstA);
xfer<List<label> > xB;
List<label> lstB( xA );
Info<< "xA: " << *xA << endl;
Info<< "xB: " << *xB << endl;
Info<< "xA: " << xA() << endl;
Info<< "xB: " << xB() << endl;
Info<< "lstA: " << lstA << endl;
Info<< "lstB: " << lstB << endl;
Info<< "lstC: " << lstC << endl;
xA = lstB;
Info<< "xA: " << *xA << endl;
Info<< "xB: " << *xB << endl;
Info<< "xA: " << xA() << endl;
Info<< "xB: " << xB() << endl;
Info<< "lstA: " << lstA << endl;
Info<< "lstB: " << lstB << endl;
Info<< "lstC: " << lstC << endl;
xB = xA;
List<label> lstD( xferCopy(List<label>, lstC) );
List<label> lstE( xferMove(List<label>, lstC) );
List<label> lstD(xferCopy(lstC));
List<label> lstE(xferMove(lstC));
// this must be empty
List<label> lstF( xferCopy(List<label>, lstC) );
List<label> lstF = xferCopy(lstC);
Info<< "xA: " << *xA << endl;
Info<< "xB: " << *xB << endl;
Info<< "xA: " << xA() << endl;
Info<< "xB: " << xB() << endl;
Info<< "lstA: " << lstA << endl;
Info<< "lstB: " << lstB << endl;
Info<< "lstC: " << lstC << endl;
@ -88,12 +88,12 @@ int main(int argc, char *argv[])
Info<< "lstE: " << lstE << endl;
Info<< "lstF: " << lstF << endl;
Info<< "xB size: " << *xB << endl;
Info<< "xB size: " << xB->size() << endl;
// clear the underlying List
xB->clear();
Info<< "xB size: " << *xB << endl;
Info<< "xB size: " << xB->size() << endl;
return 0;
}

View File

@ -93,7 +93,7 @@ HashTable<T, Key, Hash>::HashTable(const xfer<HashTable<T, Key, Hash> >& ht)
endIter_(*this, NULL, 0),
endConstIter_(*this, NULL, 0)
{
transfer(*ht);
transfer(ht());
}

View File

@ -90,7 +90,7 @@ StaticHashTable<T, Key, Hash>::StaticHashTable
endIter_(*this, 0, 0),
endConstIter_(*this, 0, 0)
{
transfer(*ht);
transfer(ht());
}

View File

@ -95,7 +95,7 @@ CompactListList<T>::CompactListList(const UList<label>& rowSizes, const T& t)
template<class T>
CompactListList<T>::CompactListList(const xfer<CompactListList<T> >& lst)
{
transfer(*lst);
transfer(lst());
}
template<class T>

View File

@ -129,7 +129,7 @@ Foam::List<T>::List(const List<T>& a)
template<class T>
Foam::List<T>::List(const xfer<List<T> >& lst)
{
transfer(*lst);
transfer(lst());
}

View File

@ -57,7 +57,7 @@ PackedList<nBits>::PackedList(const PackedList<nBits>& PList)
template<int nBits>
PackedList<nBits>::PackedList(const xfer<PackedList<nBits> >& lst)
{
transfer(*lst);
transfer(lst());
}

View File

@ -74,7 +74,7 @@ Foam::PtrList<T>::PtrList(const PtrList<T>& a, const CloneArg& cloneArg)
template<class T>
Foam::PtrList<T>::PtrList(const xfer<PtrList<T> >& lst)
{
transfer(*lst);
transfer(lst());
}

View File

@ -45,9 +45,9 @@ template <class Type>
Foam::SortableList<Type>::SortableList(const xfer<List<Type> >& values)
:
List<Type>(),
indices_((*values).size())
indices_(values->size())
{
List<Type>::transfer(*values);
List<Type>::transfer(values());
sort();
}

View File

@ -53,7 +53,7 @@ UPtrList<T>::UPtrList(const label s)
template<class T>
UPtrList<T>::UPtrList(const xfer<UPtrList<T> >& lst)
{
transfer(*lst);
transfer(lst());
}

View File

@ -87,6 +87,25 @@ Foam::IOField<Type>::IOField(const IOobject& io, const Field<Type>& f)
}
template<class Type>
Foam::IOField<Type>::IOField(const IOobject& io, const xfer<Field<Type> >& f)
:
regIOobject(io)
{
Field<Type>::transfer(f());
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readStream(typeName) >> *this;
close();
}
}
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
template<class Type>

View File

@ -65,11 +65,14 @@ public:
//- Construct from IOobject
IOField(const IOobject&);
//- Construct from IOobject and size (does not set values)
IOField(const IOobject&, const label size);
//- Construct from components
IOField(const IOobject&, const Field<Type>&);
//- Construct from IOobject and size (does not set values)
IOField(const IOobject&, const label size);
//- Construct by transferring the Field contents
IOField(const IOobject&, const xfer<Field<Type> >&);
// Destructor

View File

@ -87,6 +87,25 @@ Foam::IOList<T>::IOList(const IOobject& io, const List<T>& list)
}
template<class T>
Foam::IOList<T>::IOList(const IOobject& io, const xfer<List<T> >& list)
:
regIOobject(io)
{
List<T>::transfer(list());
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readStream(typeName) >> *this;
close();
}
}
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
template<class T>

View File

@ -72,6 +72,9 @@ public:
//- Construct from IOobject and a List
IOList(const IOobject&, const List<T>&);
//- Construct by transferring the List contents
IOList(const IOobject&, const xfer<List<T> >&);
// Destructor

View File

@ -86,6 +86,25 @@ Foam::IOMap<T>::IOMap(const IOobject& io, const Map<T>& map)
}
template<class T>
Foam::IOMap<T>::IOMap(const IOobject& io, const xfer<Map<T> >& map)
:
regIOobject(io)
{
Map<T>::transfer(map());
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readStream(typeName) >> *this;
close();
}
}
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
template<class T>

View File

@ -66,11 +66,14 @@ public:
//- Construct from IOobject
IOMap(const IOobject&);
//- Construct from IOobject and size of Map
IOMap(const IOobject&, const label);
//- Construct from IOobject and a Map
IOMap(const IOobject&, const Map<T>&);
//- Construct from IOobject and size of Map
IOMap(const IOobject&, const label);
//- Construct by transferring the Map contents
IOMap(const IOobject&, const xfer<Map<T> >&);
// Destructor

View File

@ -84,6 +84,25 @@ Foam::IOPtrList<T>::IOPtrList(const IOobject& io, const PtrList<T>& list)
}
template<class T>
Foam::IOPtrList<T>::IOPtrList(const IOobject& io, const xfer<PtrList<T> >& list)
:
regIOobject(io)
{
PtrList<T>::transfer(list());
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
PtrList<T>::read(readStream(typeName), INew<T>());
close();
}
}
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
template<class T>

View File

@ -73,6 +73,9 @@ public:
//- Construct from IOobject and a PtrList
IOPtrList(const IOobject&, const PtrList<T>&);
//- Construct by transferring the PtrList contents
IOPtrList(const IOobject&, const xfer<PtrList<T> >&);
// Destructor

View File

@ -138,6 +138,19 @@ DimensionedField<Type, GeoMesh>::DimensionedField
{}
template<class Type, class GeoMesh>
DimensionedField<Type, GeoMesh>::DimensionedField
(
const xfer<DimensionedField<Type, GeoMesh> >& df
)
:
regIOobject(df(), true),
Field<Type>(df),
mesh_(df->mesh_),
dimensions_(df->dimensions_)
{}
#ifdef ConstructFromTmp
template<class Type, class GeoMesh>
DimensionedField<Type, GeoMesh>::DimensionedField
@ -202,6 +215,20 @@ DimensionedField<Type, GeoMesh>::DimensionedField
{}
template<class Type, class GeoMesh>
DimensionedField<Type, GeoMesh>::DimensionedField
(
const word& newName,
const xfer<DimensionedField<Type, GeoMesh> >& df
)
:
regIOobject(IOobject(newName, df->time().timeName(), df->db())),
Field<Type>(df),
mesh_(df->mesh_),
dimensions_(df->dimensions_)
{}
#ifdef ConstructFromTmp
template<class Type, class GeoMesh>
DimensionedField<Type, GeoMesh>::DimensionedField

View File

@ -156,6 +156,12 @@ public:
bool reUse
);
//- Construct by transferring the DimensionedField
DimensionedField
(
const xfer<DimensionedField<Type, GeoMesh> >&
);
//- Construct as copy of tmp<DimensionedField> deleting argument
# ifdef ConstructFromTmp
DimensionedField
@ -186,6 +192,13 @@ public:
bool reUse
);
//- Construct by transferring the DimensionedField with a new name
DimensionedField
(
const word& newName,
const xfer<DimensionedField<Type, GeoMesh> >&
);
//- Construct as copy resetting name
# ifdef ConstructFromTmp
DimensionedField

View File

@ -37,6 +37,7 @@ SourceFiles
#define FieldField_H
#include "tmp.H"
#include "xfer.H"
#include "PtrList.H"
#include "scalar.H"
#include "direction.H"

View File

@ -155,6 +155,13 @@ Field<Type>::Field(Field<Type>& f, bool reUse)
{}
template<class Type>
Field<Type>::Field(const xfer<Field<Type> >& f)
:
List<Type>(f)
{}
template<class Type>
Field<Type>::Field(const typename Field<Type>::subField& sf)
:
@ -568,6 +575,20 @@ void Field<Type>::replace
}
template<class Type>
void Field<Type>::transfer(Field<Type>& f)
{
List<Type>::transfer(f);
}
template<class Type>
void Field<Type>::transfer(List<Type>& lst)
{
List<Type>::transfer(lst);
}
template<class Type>
tmp<Field<Type> > Field<Type>::T() const
{

View File

@ -43,6 +43,7 @@ SourceFiles
#define Field_H
#include "tmp.H"
#include "xfer.H"
#include "direction.H"
#include "VectorSpace.H"
#include "scalarList.H"
@ -164,6 +165,9 @@ public:
//- Construct as copy or re-use as specified.
Field(Field<Type>&, bool reUse);
//- Construct by transferring the Field contents
Field(const xfer<Field<Type> >&);
//- Construct as copy of subField
Field(const typename Field<Type>::subField&);
@ -293,6 +297,12 @@ public:
//- Replace a component field of the field
void replace(const direction, const cmptType&);
//- Transfer the contents of the argument Field into this Field
void transfer(Field<Type>&);
//- Transfer the contents of the argument List into this Field
void transfer(List<Type>&);
//- Return the field transpose (only defined for second rank tensors)
tmp<Field<Type> > T() const;

View File

@ -39,10 +39,6 @@ Description
The wrapped object of type \<T\> must implement a transfer() method and
an operator=() copy method.
Note
The macros xferCopy(T,arg) and xferMove(T,arg) can be used as
workarounds for passing temporaries to copy-constructors.
SourceFiles
xferI.H
@ -56,6 +52,9 @@ SourceFiles
namespace Foam
{
// Forward declaration of classes
template<class T> class tmp;
/*---------------------------------------------------------------------------*\
Class xfer Declaration
\*---------------------------------------------------------------------------*/
@ -97,12 +96,8 @@ public:
//- Transfer the contents into the object
inline void operator=(const xfer<T>&);
//- Return a non-const reference to const object
// @sa xferCopy, xferMove macros alternatives for copy-constructors
inline const xfer<T>& operator()() const;
//- Reference to the underlying datatype
inline T& operator*() const;
inline T& operator()() const;
//- Pointer to the underlying datatype
inline T* operator->() const;
@ -112,40 +107,54 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
/**
* @def xferCopy(T,arg)
* @fn template<class T> xferCopy(const T&)
* Construct by copying the contents of the @a arg
* and return a const reference to an xfer of type \<T\>
*
* Useful for constructors where the argument is temporary.
* This is a workaround for a template resolution issue.
*
* @sa xferMove and Foam::xfer
* @sa xferMove, xferTmp and Foam::xfer
*/
#define xferCopy(T,arg) \
(static_cast<const Foam::xfer< T >&>(Foam::xfer< T >(arg)()))
template<class T>
Foam::xfer<T> xferCopy(const T& t)
{
return Foam::xfer<T>(t);
}
/**
* @def xferMove(T,arg)
* @fn template<class T> xferMove(T&)
* Construct by transferring the contents of the @a arg
* and return a const reference to an xfer of type \<T\>
*
* Useful for constructors where the argument is temporary.
* This is a workaround for a template resolution issue.
*
* @par Example Use
* @code
* List<label> a;
* ...
* List<label> b(xferMove(List<label>, a));
* List<label> b(xferMove(a));
* @endcode
*
* @sa xferCopy and Foam::xfer
* @sa xferCopy, xferTmp and Foam::xfer
*/
#define xferMove(T,arg) \
(static_cast<const Foam::xfer< T >&>(Foam::xfer< T >(arg, true)()))
template<class T>
Foam::xfer<T> xferMove(T& t)
{
return Foam::xfer<T>(t, true);
}
/**
* @fn template<class T> xferTmp(tmp<T>&)
* Construct by transferring the contents of the @a arg
*
* @sa xferCopy, xferMove and Foam::xfer
*/
template<class T>
Foam::xfer<T> xferTmp(Foam::tmp<T>& tt)
{
return Foam::xfer<T>(tt(), tt.isTmp());
}
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -98,18 +98,12 @@ inline void Foam::xfer<T>::operator=(const xfer<T>& t)
template<class T>
inline const Foam::xfer<T>& Foam::xfer<T>::operator()() const
{
return *this;
}
template<class T>
inline T& Foam::xfer<T>::operator*() const
inline T& Foam::xfer<T>::operator()() const
{
return *ptr_;
}
template<class T>
inline T* Foam::xfer<T>::operator->() const
{