SyDEVS  v0.7
Simulation-based analysis of complex systems involving people, devices, physical elements, and dynamic environments.
pointer.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef SYDEVS_POINTER_H_
3 #define SYDEVS_POINTER_H_
4 
6 #include <memory>
7 
8 namespace sydevs {
9 
10 
28 class pointer
29 {
30 public:
34  pointer() noexcept;
35 
39  explicit pointer(std::nullptr_t X) noexcept;
40 
44  template<typename T>
45  explicit pointer(T* ptr);
46 
47  pointer(const pointer&) noexcept = default;
48  pointer& operator=(const pointer&) noexcept = default;
49  pointer(pointer&&) noexcept = default;
50  pointer& operator=(pointer&&) noexcept = default;
51  ~pointer() = default;
52 
53  void reset() noexcept;
54 
55  template<typename T>
56  void reset(T* ptr);
57 
58  template<typename T>
59  T& dereference() const;
60 
61  explicit operator bool() const noexcept;
62 
63 private:
64  std::shared_ptr<void> ptr_;
65 };
66 
67 
68 inline pointer::pointer() noexcept
69  : ptr_()
70 {
71 }
72 
73 
74 inline pointer::pointer(std::nullptr_t X) noexcept
75  : ptr_(X)
76 {
77 }
78 
79 
80 template<typename T>
81 inline pointer::pointer(T* ptr)
82  : ptr_(ptr)
83 {
84 }
85 
86 
87 inline void pointer::reset() noexcept
88 {
89  ptr_.reset();
90 }
91 
92 
93 template<typename T>
94 inline void pointer::reset(T* ptr)
95 {
96  ptr_.reset(ptr);
97 }
98 
99 
100 template<typename T>
101 inline T& pointer::dereference() const
102 {
103  if (!ptr_) throw std::logic_error("Attempt to dereference null pointer object");
104  return *static_cast<T*>(ptr_.get());
105 }
106 
107 
108 inline pointer::operator bool() const noexcept
109 {
110  return bool(ptr_);
111 }
112 
113 
114 } // namespace
115 
116 #endif
A data type which represents a pointer to anything.
Definition: pointer.h:29
pointer(const pointer &) noexcept=default
Copy constructor.
void reset() noexcept
Modifies the pointer to reference nullptr.
Definition: pointer.h:87
pointer & operator=(const pointer &) noexcept=default
Copy assignment.
pointer(pointer &&) noexcept=default
Move constructor.
pointer() noexcept
Constructs a null pointer instance.
Definition: pointer.h:68
T & dereference() const
Returns the referenced data, casting it to type T.
Definition: pointer.h:101
Definition: arraynd.h:1211
Definition: arraynd.h:8