Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Ref.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_REF_H
22 #define GF_REF_H
23 
24 #include <memory>
25 #include <type_traits>
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31 
38  template<typename T>
39  class Ref {
40  public:
46  Ref(T& ref) noexcept
47  : m_ptr(std::addressof(ref))
48  {
49 
50  }
51 
55  Ref(T&&) = delete;
56 
60  Ref(const Ref& other) noexcept = default;
61 
65  template<typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
66  Ref(const Ref<U>& other) noexcept
67  : m_ptr(std::addressof(other.get()))
68  {
69  }
70 
74  Ref& operator=(const Ref& other) noexcept = default;
75 
79  template<typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
80  Ref& operator=(const Ref<U>& other) noexcept {
81  m_ptr = std::addressof(other.get());
82  }
83 
89  T& get() const noexcept {
90  return *m_ptr;
91  }
92 
98  operator T& () const noexcept {
99  return *m_ptr;
100  }
101 
102  private:
103  T *m_ptr;
104  };
105 
112  template<typename T>
113  constexpr bool operator==(Ref<T> lhs, Ref<T> rhs) noexcept {
114  return std::addressof(lhs.get()) == std::addressof(rhs.get());
115  }
116 
123  template<typename T>
124  constexpr bool operator==(Ref<T> lhs, const T& rhs) noexcept {
125  return std::addressof(lhs.get()) == std::addressof(rhs);
126  }
127 
134  template<typename T>
135  constexpr bool operator==(const T& lhs, Ref<T> rhs) noexcept {
136  return std::addressof(lhs) == std::addressof(rhs.get());
137  }
138 
143  template<typename T>
144  Ref<T> ref(T& object) {
145  return Ref<T>(object);
146  }
147 
148 #ifndef DOXYGEN_SHOULD_SKIP_THIS
149  template<typename T>
150  Ref<T> ref(const T&&) = delete;
151 #endif
152 
153 #ifndef DOXYGEN_SHOULD_SKIP_THIS
154 }
155 #endif
156 }
157 
158 #endif // GF_REF_H
constexpr bool operator==(const T &lhs, Ref< T > rhs) noexcept
Referece comparison.
Definition: Ref.h:135
Ref & operator=(const Ref< U > &other) noexcept
Copy assignment.
Definition: Ref.h:80
Ref(const Ref< U > &other) noexcept
Copy constructor.
Definition: Ref.h:66
T & get() const noexcept
Get the reference.
Definition: Ref.h:89
The namespace for gf classes.
Definition: Action.h:35
constexpr bool operator==(Ref< T > lhs, const T &rhs) noexcept
Referece comparison.
Definition: Ref.h:124
Ref< T > ref(T &object)
Reference creation.
Definition: Ref.h:144
constexpr bool operator==(Ref< T > lhs, Ref< T > rhs) noexcept
Referece comparison.
Definition: Ref.h:113
A reference wrapper.
Definition: Ref.h:39
Ref(T &ref) noexcept
Constructor with a lvalue reference.
Definition: Ref.h:46