This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
unique_ptr.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef DOXYGEN
4 
5 template <class T>
6 struct remove_reference {
7  typedef T type;
8 };
9 template <class T>
10 struct remove_reference<T &> {
11  typedef T type;
12 };
13 template <class T>
14 struct remove_reference<T &&> {
15  typedef T type;
16 };
17 
18 template <class T>
19 typename remove_reference<T>::type &&move(T &&t) {
20  return static_cast<typename remove_reference<T>::type &&>(t);
21 }
22 
23 template <class T, T v>
24 struct integral_constant {
25  constexpr static T value = v;
26 };
27 
28 using true_type = integral_constant<bool, true>;
29 using false_type = integral_constant<bool, false>;
30 
31 template <class T>
32 struct is_lvalue_reference : false_type {};
33 template <class T>
34 struct is_lvalue_reference<T &> : true_type {};
35 
36 template <class T>
37 inline T &&forward(typename remove_reference<T>::type &t) noexcept {
38  return static_cast<T &&>(t);
39 }
40 
41 template <class T>
42 inline T &&forward(typename remove_reference<T>::type &&t) noexcept {
43  static_assert(!is_lvalue_reference<T>::value,
44  "Can not forward an rvalue as an lvalue.");
45  return static_cast<T &&>(t);
46 }
47 
48 #endif
49 
52 
59 template <class T>
60 class unique_ptr {
61  public:
62  unique_ptr() = default;
63  explicit unique_ptr(T *p) : p(p) {}
64  unique_ptr(const unique_ptr &) = delete;
65  template <class U>
67  reset(r.release());
68  }
69 
70  ~unique_ptr() { delete p; }
71 
72  unique_ptr &operator=(const unique_ptr &) = delete;
74  reset(r.release());
75  return *this;
76  }
77 
78  void reset(T *newP = nullptr) {
79  T *old_ptr = p;
80  p = newP;
81  delete old_ptr;
82  }
83 
84  T *release() {
85  T *old_ptr = p;
86  p = nullptr;
87  return old_ptr;
88  }
89 
90  explicit operator bool() const { return p != nullptr; }
91 
92  T *get() { return p; }
93  const T *get() const { return p; }
94 
95  T *operator->() { return p; }
96  const T *operator->() const { return p; }
97  T &operator*() { return *p; }
98  const T &operator*() const { return *p; }
99 
100  private:
101  T *p = nullptr;
102 };
103 
104 template <typename T, typename... Args>
105 unique_ptr<T> make_unique(Args &&... args) {
106  return unique_ptr<T>(new T(forward<Args>(args)...));
107 }
108 
109 /// @}
unique_ptr::operator->
T * operator->()
Definition: unique_ptr.hpp:95
unique_ptr::unique_ptr
unique_ptr(unique_ptr< U > &&r)
Definition: unique_ptr.hpp:66
unique_ptr::~unique_ptr
~unique_ptr()
Definition: unique_ptr.hpp:70
unique_ptr::operator*
const T & operator*() const
Definition: unique_ptr.hpp:98
unique_ptr::unique_ptr
unique_ptr(T *p)
Definition: unique_ptr.hpp:63
unique_ptr::release
T * release()
Definition: unique_ptr.hpp:84
unique_ptr::unique_ptr
unique_ptr()=default
unique_ptr::operator=
unique_ptr< T > & operator=(unique_ptr< T > &&r)
Definition: unique_ptr.hpp:73
unique_ptr::reset
void reset(T *newP=nullptr)
Definition: unique_ptr.hpp:78
unique_ptr::operator=
unique_ptr & operator=(const unique_ptr &)=delete
unique_ptr::operator*
T & operator*()
Definition: unique_ptr.hpp:97
unique_ptr::get
const T * get() const
Definition: unique_ptr.hpp:93
unique_ptr::operator->
const T * operator->() const
Definition: unique_ptr.hpp:96
make_unique
unique_ptr< T > make_unique(Args &&... args)
Definition: unique_ptr.hpp:105
unique_ptr
Very basic smart pointer.
Definition: unique_ptr.hpp:60
unique_ptr::p
T * p
Definition: unique_ptr.hpp:101
unique_ptr::get
T * get()
Definition: unique_ptr.hpp:92