From 025eebca13c90124c37431afa0e68700849b9e91 Mon Sep 17 00:00:00 2001 From: mark <mark@opencfd> Date: Mon, 3 Oct 2016 16:38:19 +0200 Subject: [PATCH] ENH: provide direct access to raw pointer/reference from autoPtr (issue #252) All of the access methods for autoPtr include validity checks and will fail if the underlying point is NULL. In some cases, however, we'd like to retain the automatic deletion mechanism, but still address a nullptr. This is mostly for cases in which a file-stream should be allocated, but only on the master process. For these cases we'd still like to pass through and reference the underlying pointer (eg, to obtain the correct method call) without tripping the pointer check mechanism. If we attempt to use the ptr() method, the autoPtr memory management is bypassed and we risk memory leaks. Instead provide an alternative mechanism to obtain the raw underlying pointers/references. Use rawPtr() and rawRef() for these potentially useful, but also potentially dangerous, operations. --- src/OpenFOAM/memory/autoPtr/autoPtr.H | 16 ++++++++++++---- src/OpenFOAM/memory/autoPtr/autoPtrI.H | 18 ++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/OpenFOAM/memory/autoPtr/autoPtr.H b/src/OpenFOAM/memory/autoPtr/autoPtr.H index a8802f23a92..00b8ed7809f 100644 --- a/src/OpenFOAM/memory/autoPtr/autoPtr.H +++ b/src/OpenFOAM/memory/autoPtr/autoPtr.H @@ -106,11 +106,19 @@ public: // Access - //- Return reference, without checking pointer validity. - inline T& refOrNull(); + //- Return the pointer, without nullptr checking. + // Pointer remains under autoPtr management. + inline T* rawPtr(); - //- Return const reference, without checking pointer validity. - inline const T& refOrNull() const; + //- Const access to the pointer, without nullptr checking. + // Pointer remains under autoPtr management. + inline const T* rawPtr() const; + + //- Return the reference, without nullptr checking. + inline T& rawRef(); + + //- Return the const reference, without nullptr checking. + inline const T& rawRef() const; // Member operators diff --git a/src/OpenFOAM/memory/autoPtr/autoPtrI.H b/src/OpenFOAM/memory/autoPtr/autoPtrI.H index 7440bdce690..c1e6b144643 100644 --- a/src/OpenFOAM/memory/autoPtr/autoPtrI.H +++ b/src/OpenFOAM/memory/autoPtr/autoPtrI.H @@ -130,14 +130,28 @@ inline void Foam::autoPtr<T>::clear() template<class T> -inline T& Foam::autoPtr<T>::refOrNull() +inline T* Foam::autoPtr<T>::rawPtr() +{ + return ptr_; +} + + +template<class T> +inline const T* Foam::autoPtr<T>::rawPtr() const +{ + return ptr_; +} + + +template<class T> +inline T& Foam::autoPtr<T>::rawRef() { return *ptr_; } template<class T> -inline const T& Foam::autoPtr<T>::refOrNull() const +inline const T& Foam::autoPtr<T>::rawRef() const { return *ptr_; } -- GitLab