Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Handle.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_HANDLE_H
22 #define GF_HANDLE_H
23 
24 #include "Id.h"
25 
26 #include "Log.h"
27 
28 namespace gf {
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 inline namespace v1 {
31 #endif
32 
33  class Handle {
34  public:
35  Handle() = default;
36 
37  explicit Handle(Id id)
38  : m_id(id)
39  {
40  }
41 
42  template<typename T>
43  explicit Handle(T& object)
44  : m_ptr(&object)
45  {
46  }
47 
48  Id asId() const {
49  return m_id;
50  }
51 
52  template<typename T>
53  T& as() {
54  return *static_cast<T*>(m_ptr);
55  }
56 
57  template<typename T>
58  const T& as() const {
59  return *static_cast<const T*>(m_ptr);
60  }
61 
62  private:
63  union {
65  void * m_ptr;
66  };
67  };
68 
69 
70 #ifndef DOXYGEN_SHOULD_SKIP_THIS
71 }
72 #endif
73 }
74 
75 #endif // GF_HANDLE_H
void * m_ptr
Definition: Handle.h:65
Id asId() const
Definition: Handle.h:48
uint64_t Id
An identifier.
Definition: Id.h:39
const T & as() const
Definition: Handle.h:58
Id m_id
Definition: Handle.h:64
The namespace for gf classes.
Definition: Action.h:35
T & as()
Definition: Handle.h:53
Definition: Handle.h:33
Handle(Id id)
Definition: Handle.h:37
Handle(T &object)
Definition: Handle.h:43