From 6d7ff59fc87933c1be10b36c9732a16c02e22fd2 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 28 Sep 2016 11:26:42 +0200 Subject: [PATCH] ENH: provide refOrNull method for autoPtr. - Normally use '()' to deference. This has extra safety and issues a fatal error if the underlying pointer is not valid. However, in some cases we are happy with getting a null reference. The refOrNull() method returns the reference without any checking. Usage example: autoPtr osPtr; if (Pstream::master()) { osPtr.reset(new OFstream(...)); } writeViaMaster(osPtr.refOrNull()); - The writeViaMaster() call takes an OFstream reference, but this is only used directly on the master. The slaves will pass things through to the master. --- src/OpenFOAM/memory/autoPtr/autoPtr.H | 11 ++++++++++- src/OpenFOAM/memory/autoPtr/autoPtrI.H | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/OpenFOAM/memory/autoPtr/autoPtr.H b/src/OpenFOAM/memory/autoPtr/autoPtr.H index 748975b5cf..a8802f23a9 100644 --- a/src/OpenFOAM/memory/autoPtr/autoPtr.H +++ b/src/OpenFOAM/memory/autoPtr/autoPtr.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -104,6 +104,15 @@ public: inline void clear(); + // Access + + //- Return reference, without checking pointer validity. + inline T& refOrNull(); + + //- Return const reference, without checking pointer validity. + inline const T& refOrNull() const; + + // Member operators //- Return reference to the object data diff --git a/src/OpenFOAM/memory/autoPtr/autoPtrI.H b/src/OpenFOAM/memory/autoPtr/autoPtrI.H index ef199ca20c..7440bdce69 100644 --- a/src/OpenFOAM/memory/autoPtr/autoPtrI.H +++ b/src/OpenFOAM/memory/autoPtr/autoPtrI.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -129,6 +129,20 @@ inline void Foam::autoPtr::clear() } +template +inline T& Foam::autoPtr::refOrNull() +{ + return *ptr_; +} + + +template +inline const T& Foam::autoPtr::refOrNull() const +{ + return *ptr_; +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template