Gamedev Framework (gf)  0.12.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 
26 namespace gf {
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
28 inline namespace v1 {
29 #endif
30 
37  template<typename T>
38  class Ref {
39  public:
45  Ref(T& ref) noexcept
46  : m_ptr(std::addressof(ref))
47  {
48 
49  }
50 
54  Ref(T&&) = delete;
55 
59  Ref(const Ref& other) noexcept = default;
60 
64  Ref& operator=(const Ref& other) noexcept = default;
65 
71  T& get() const noexcept {
72  return *m_ptr;
73  }
74 
80  operator T& () const noexcept {
81  return *m_ptr;
82  }
83 
84  private:
85  T *m_ptr;
86  };
87 
94  template<typename T>
95  constexpr bool operator==(Ref<T> lhs, Ref<T> rhs) noexcept {
96  return std::addressof(lhs.get()) == std::addressof(rhs.get());
97  }
98 
105  template<typename T>
106  constexpr bool operator==(Ref<T> lhs, const T& rhs) noexcept {
107  return std::addressof(lhs.get()) == std::addressof(rhs);
108  }
109 
116  template<typename T>
117  constexpr bool operator==(const T& lhs, Ref<T> rhs) noexcept {
118  return std::addressof(lhs) == std::addressof(rhs.get());
119  }
120 
121 #ifndef DOXYGEN_SHOULD_SKIP_THIS
122 }
123 #endif
124 }
125 
126 #endif // GF_REF_H
constexpr bool operator==(const T &lhs, Ref< T > rhs) noexcept
Referece comparison.
Definition: Ref.h:117
The namespace for gf classes.
Definition: Action.h:35
constexpr bool operator==(Ref< T > lhs, const T &rhs) noexcept
Referece comparison.
Definition: Ref.h:106
constexpr bool operator==(Ref< T > lhs, Ref< T > rhs) noexcept
Referece comparison.
Definition: Ref.h:95
A reference wrapper.
Definition: Ref.h:38
Ref(T &ref) noexcept
Constructor with a lvalue reference.
Definition: Ref.h:45