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<OFstream> 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.
This commit is contained in:
Mark Olesen 2016-09-28 11:26:42 +02:00
parent bbec683e42
commit 6d7ff59fc8
2 changed files with 25 additions and 2 deletions

View File

@ -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

View File

@ -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<T>::clear()
}
template<class T>
inline T& Foam::autoPtr<T>::refOrNull()
{
return *ptr_;
}
template<class T>
inline const T& Foam::autoPtr<T>::refOrNull() const
{
return *ptr_;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>