SyDEVS  v0.6.7
Multiscale Simulation and Systems Modeling Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pointer.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef SYDEVS_POINTER_H_
3 #define SYDEVS_POINTER_H_
4 
5 #include <memory>
6 
7 namespace sydevs {
8 
9 
27 class pointer
28 {
29 public:
33  pointer() noexcept;
34 
38  explicit pointer(std::nullptr_t X) noexcept;
39 
43  template<typename T>
44  explicit pointer(T* ptr);
45 
46  pointer(const pointer&) noexcept = default;
47  pointer& operator=(const pointer&) noexcept = default;
48  pointer(pointer&&) noexcept = default;
49  pointer& operator=(pointer&&) noexcept = default;
50  ~pointer() = default;
51 
52  void reset() noexcept;
53 
54  template<typename T>
55  void reset(T* ptr);
56 
57  template<typename T>
58  T& dereference() const;
59 
60  explicit operator bool() const noexcept;
61 
62 private:
63  std::shared_ptr<void> ptr_;
64 };
65 
66 
67 inline pointer::pointer() noexcept
68  : ptr_()
69 {
70 }
71 
72 
73 inline pointer::pointer(std::nullptr_t X) noexcept
74  : ptr_(X)
75 {
76 }
77 
78 
79 template<typename T>
80 inline pointer::pointer(T* ptr)
81  : ptr_(ptr)
82 {
83 }
84 
85 
86 inline void pointer::reset() noexcept
87 {
88  ptr_.reset();
89 }
90 
91 
92 template<typename T>
93 inline void pointer::reset(T* ptr)
94 {
95  ptr_.reset(ptr);
96 }
97 
98 
99 template<typename T>
100 inline T& pointer::dereference() const
101 {
102  if (!ptr_) throw std::logic_error("Attempt to dereference null pointer object");
103  return *static_cast<T*>(ptr_.get());
104 }
105 
106 
107 inline pointer::operator bool() const noexcept
108 {
109  return bool(ptr_);
110 }
111 
112 
113 } // namespace
114 
115 #endif
T & dereference() const
Returns the referenced data, casting it to type T.
Definition: pointer.h:100
pointer() noexcept
Constructs a null pointer instance.
Definition: pointer.h:67
pointer & operator=(const pointer &) noexcept=default
Copy assignment.
~pointer()=default
Destructor.
void reset() noexcept
Modifies the pointer to reference nullptr.
Definition: pointer.h:86
A data type which represents a pointer to anything.
Definition: pointer.h:27