Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
default_init_ptr.h
1 #pragma once
2 
3 #include <cstddef>
4 
5 namespace smlt {
6 
7 template<typename T>
9  /* This class exists to protect against forgetting to initialize a
10  * an object pointer. It could theoretically cause a performance issue
11  * but no more so than any other smart pointer, and hopefully the compiler
12  * will be smart enough to optimise any cost away.
13  *
14  * If this does cause a performance hit on a particular platform
15  * (Dreamcast, I'm looking at you!) then it can always be typedef'd away
16  */
17 private:
18  T* ptr_ = nullptr;
19 
20 public:
21  typedef T element_type;
22 
23  default_init_ptr(): ptr_(nullptr) {}
24  default_init_ptr(std::nullptr_t): ptr_(nullptr) {}
25  default_init_ptr(const default_init_ptr<T>&) = default;
26  default_init_ptr<T>& operator=(const default_init_ptr<T>&) = default;
27  default_init_ptr<T>& operator=(std::nullptr_t) {
28  ptr_ = nullptr;
29  return *this;
30  }
31 
32  default_init_ptr(T* p):
33  ptr_(p) {}
34 
35  default_init_ptr<T>& operator=(T* p) {
36  ptr_ = p;
37  return *this;
38  }
39 
40  T* operator->() const {
41  return ptr_;
42  }
43 
44  T& operator*() const {
45  return *ptr_;
46  }
47 
48  T& operator[](std::size_t i) const {
49  return ptr_[i];
50  }
51 
52  operator T*() const {
53  return ptr_;
54  }
55 };
56 
57 
58 }
smlt
Definition: animation.cpp:25
smlt::default_init_ptr
Definition: default_init_ptr.h:8