SPH
SafePtr.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 
11 
12 class SafePtrException : public std::exception {
13 public:
14  virtual const char* what() const noexcept {
15  return "Dereferencing nullptr";
16  }
17 };
18 
19 template <typename T>
20 class SafePtr : public AutoPtr<T> {
21 public:
22  INLINE T& operator*() const {
23  if (!ptr) {
24  throw SafePtrException();
25  }
26  return *ptr;
27  }
28 
29  INLINE T* operator->() const {
30  if (!ptr) {
31  throw SafePtrException();
32  }
33  return ptr;
34  }
35 
36  template <typename... TArgs>
37  INLINE decltype(auto) operator()(TArgs&&... args) const {
38  if (!ptr) {
39  throw SafePtrException();
40  }
41  return (*ptr)(std::forward<TArgs>(args)...);
42  }
43 };
44 
Simplified implementation of std::unique_ptr, using only default deleter.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Wrapper of pointer that deletes the resource from destructor.
Definition: AutoPtr.h:15
virtual const char * what() const noexcept
Definition: SafePtr.h:14
INLINE T * operator->() const
Definition: SafePtr.h:29
INLINE T & operator*() const
Definition: SafePtr.h:22