From 27fce4fa4e451214a638aa38a048e542458a978f Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Mon, 6 Oct 2008 11:06:44 +0200 Subject: [PATCH] added draft of xfer<T> class - A simple container for objects of type \<T\> that can be used for transferring the contents rather than copying them. - The wrapped object of type \<T\> must implement a transfer() method. --- src/OpenFOAM/containers/misc/xfer.H | 157 +++++++++++++++++++++++++++ src/OpenFOAM/containers/misc/xferI.H | 151 ++++++++++++++++++++++++++ 2 files changed, 308 insertions(+) create mode 100644 src/OpenFOAM/containers/misc/xfer.H create mode 100644 src/OpenFOAM/containers/misc/xferI.H diff --git a/src/OpenFOAM/containers/misc/xfer.H b/src/OpenFOAM/containers/misc/xfer.H new file mode 100644 index 00000000000..41de52a43d8 --- /dev/null +++ b/src/OpenFOAM/containers/misc/xfer.H @@ -0,0 +1,157 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Class + Foam::xfer + +Description + A simple container for objects of type \<T\> that can be used for + transferring the contents rather than copying them. + + The wrapped object of type \<T\> must implement a transfer() method. + +SourceFiles + xferI.H + +\*---------------------------------------------------------------------------*/ + +#ifndef xfer_H +#define xfer_H + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +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 +\*---------------------------------------------------------------------------*/ + +template<class T> +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>&); + +public: + + // Constructors + + //- Construct null, delaying the transfer to later + inline xfer(); + + //- Construct by transferring the parameter into this object + inline xfer(T&); + + //- Construct by transferring the parameter into this object + inline xfer(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 + inline void operator=(T&); + + //- Transfer the contents into this object + inline void operator=(xfer<T>&); + + //- Return a non-const reference to const object + // Useful for read-constructors where the argument is temporary + inline 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>&); + +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "xferI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/misc/xferI.H b/src/OpenFOAM/containers/misc/xferI.H new file mode 100644 index 00000000000..a1bd4cd7f3e --- /dev/null +++ b/src/OpenFOAM/containers/misc/xferI.H @@ -0,0 +1,151 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template<class T> +Foam::xfer<T>::xfer() +: + ref_() +{} + + +template<class T> +Foam::xfer<T>::xfer(T& t) +: + ref_() +{ + transfer(t); +} + + +template<class T> +Foam::xfer<T>::xfer(xfer<T>& t) +: + ref_() +{ + transfer(t); +} + + + +// * * * * * * * * * * * * * * * * 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) +{ + 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_); +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + + +template<class T> +void Foam::xfer<T>::operator=(T& t) +{ + transfer(t); +} + + +template<class T> +void Foam::xfer<T>::operator=(xfer<T>& t) +{ + if (this == &t) + { + FatalErrorIn + ( + "Foam::xfer<T>::operator=" + "(const Foam::xfer<T>&)" + ) << "Attempted assignment to self" + << abort(FatalError); + } + + 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) +{ + is >> t.ref_; + return is; +} + + +template<class T> +Foam::Ostream& Foam::operator<<(Ostream& os, const xfer<T>& t) +{ + os << t.ref_; + return os; +} + +// ************************************************************************* // -- GitLab