The Pedigree Project  0.1
Pointers.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef POINTERS_H
21 #define POINTERS_H
22 
23 #include "pedigree/kernel/Log.h"
24 #include "pedigree/kernel/processor/types.h"
25 
26 template <class T>
28 {
29  public:
30  UniqueCommon() : m_Pointer(nullptr)
31  {
32  }
33 
34  virtual ~UniqueCommon()
35  {
36  reset();
37  }
38 
39  T *operator*() const
40  {
41  return get();
42  }
43 
44  operator void *() const
45  {
46  return get();
47  }
48 
49  T *get() const
50  {
51  return m_Pointer;
52  }
53 
54  void reset()
55  {
56  if (m_Pointer)
57  {
58  destroy();
59  m_Pointer = 0;
60  }
61  }
62 
63  NOT_COPYABLE_OR_ASSIGNABLE(UniqueCommon<T>);
64 
65  protected:
66  UniqueCommon(T *p) : m_Pointer(p)
67  {
68  }
69 
70  virtual void destroy()
71  {
72  delete m_Pointer;
73  }
74 
75  void setPointer(T *p)
76  {
77  m_Pointer = p;
78  }
79 
81  void release()
82  {
83  m_Pointer = nullptr;
84  }
85 
86  T *m_Pointer;
87 };
88 
92 template <class T>
93 class UniquePointer : public UniqueCommon<T>
94 {
95  public:
96  UniquePointer() = default;
97 
98  virtual ~UniquePointer()
99  {
100  this->reset();
101  }
102 
103  // move constructor
105  {
106  move_from(pedigree_std::move(p));
107  }
108 
109  // no copy construction permitted
110  NOT_COPYABLE_OR_ASSIGNABLE(UniquePointer<T>);
111 
112  UniquePointer<T> &operator=(UniquePointer<T> &&p)
113  {
114  move_from(pedigree_std::move(p));
115  return *this;
116  }
117 
118  template <class... Args>
119  static UniquePointer<T> allocate(Args &&... args)
120  {
121  return UniquePointer<T>(new T(args...));
122  }
123 
124  private:
125  UniquePointer(T *p) : UniqueCommon<T>(p)
126  {
127  }
128 
129  void move_from(UniquePointer<T> &&p)
130  {
131  T *ptr = p.get();
132  p.release();
133 
134  this->reset();
135  this->setPointer(ptr);
136  }
137 };
138 
140 template <class T>
141 class UniqueArray : public UniqueCommon<T>
142 {
143  public:
144  UniqueArray() = default;
145 
146  virtual ~UniqueArray()
147  {
148  this->reset();
149  }
150 
151  // move constructor
153  {
154  move_from(pedigree_std::move(p));
155  }
156 
157  // no copy construction permitted
158  NOT_COPYABLE_OR_ASSIGNABLE(UniqueArray<T>);
159 
160  UniqueArray<T> &operator=(UniqueArray<T> &&p)
161  {
162  move_from(pedigree_std::move(p));
163  return *this;
164  }
165 
166  static UniqueArray<T> allocate(size_t count)
167  {
168  return UniqueArray<T>(new T[count]);
169  }
170 
171  protected:
172  virtual void destroy() override
173  {
174  T *ptr = this->get();
175  delete[] ptr;
176  }
177 
178  private:
179  UniqueArray(T *p) : UniqueCommon<T>(p)
180  {
181  }
182 
183  void move_from(UniqueArray<T> &&p)
184  {
185  T *ptr = p.get();
186  p.release();
187 
188  this->reset();
189  this->setPointer(ptr);
190  }
191 };
192 
193 #endif
void release()
Definition: Pointers.h:81