ENH: add Xfer rvalue(), valid() methods

- rvalue() is a (transitional) means of converting Xfer content to a
  reference for move construct, move assign semantics.

- valid() method for consistency with autoPtr and tmp classes
This commit is contained in:
Mark Olesen
2018-02-28 09:30:31 +01:00
parent 7e84380783
commit 081783db6c
3 changed files with 53 additions and 7 deletions

View File

@ -79,7 +79,11 @@ class Xfer
public:
typedef T Type;
// STL type definition similar to std::shared_ptr
//- Type of object being managed
typedef T element_type;
// Constructors
@ -107,11 +111,17 @@ public:
// Member Functions
//- Pointer to the underlying object
//- Test for valid pointer and not a nullObject reference.
inline bool valid() const;
//- Pointer to the underlying object.
inline T* get() const;
//- Rvalue reference to the underlying object.
inline T&& rvalue() const;
//- Swaps the managed objects
void swap(Xfer<T>& other) noexcept;
inline void swap(Xfer<T>& other) noexcept;
// Member Operators

View File

@ -39,8 +39,7 @@ template<class T>
template<class... Args>
inline Foam::Xfer<T> Foam::Xfer<T>::New(Args&&... args)
{
T* ptr = new T(std::forward<Args>(args)...);
return Foam::Xfer<T>(ptr);
return Xfer<T>(new T(std::forward<Args>(args)...));
}
@ -69,6 +68,13 @@ inline Foam::Xfer<T>::Xfer(const Xfer<T>& xf)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
inline bool Foam::Xfer<T>::valid() const
{
return (ptr_ && Foam::notNull(ptr_));
}
template<class T>
inline T* Foam::Xfer<T>::get() const
{
@ -76,13 +82,21 @@ inline T* Foam::Xfer<T>::get() const
}
template<class T>
inline T&& Foam::Xfer<T>::rvalue() const
{
// Need check for valid() ?
return std::move(*ptr_);
}
template<class T>
inline void Foam::Xfer<T>::swap(Xfer<T>& other) noexcept
{
// Swap pointers
T* tmp = ptr_;
T* p = ptr_;
ptr_ = other.ptr;
other.ptr_ = tmp;
other.ptr_ = p;
}