Commit 21557091 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: explicit convert to bool operator for autoPtr

- behaves the same as the valid() method, but can be queried directly
  like a normal raw pointer and as per std::unique_ptr.
  Eg,

      autoPtr<T> ptr = ...

      if (ptr) ...
parent ff26b96a
...@@ -143,7 +143,7 @@ int main(int argc, char *argv[]) ...@@ -143,7 +143,7 @@ int main(int argc, char *argv[])
Info<<"now have valid=" << Switch(list.valid()).c_str(); Info<<"now have valid=" << Switch(list.valid()).c_str();
if (list.valid()) if (list)
{ {
Info<< nl Info<< nl
<< flatOutput(*list) << " @ " << long(list->cdata()) << flatOutput(*list) << " @ " << long(list->cdata())
...@@ -153,6 +153,22 @@ int main(int argc, char *argv[]) ...@@ -153,6 +153,22 @@ int main(int argc, char *argv[])
{ {
Info<< nl; Info<< nl;
} }
// These should fail to compile
#if 0
label val0 = 0;
if (true)
{
val0 = list;
}
label val1 = 10;
if (val1 == list)
{
}
#endif
} }
// Memory transfer // Memory transfer
......
...@@ -234,6 +234,8 @@ public: ...@@ -234,6 +234,8 @@ public:
operator const T&() const = delete; operator const T&() const = delete;
#endif #endif
//- True if the managed pointer is non-null
explicit inline operator bool() const noexcept;
//- Transfer object ownership from parameter //- Transfer object ownership from parameter
inline void operator=(autoPtr<T>&& ap) noexcept; inline void operator=(autoPtr<T>&& ap) noexcept;
......
...@@ -260,6 +260,13 @@ inline const T& Foam::autoPtr<T>::operator()() const ...@@ -260,6 +260,13 @@ inline const T& Foam::autoPtr<T>::operator()() const
} }
template<class T>
inline Foam::autoPtr<T>::operator bool() const noexcept
{
return ptr_;
}
template<class T> template<class T>
inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept
{ {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment