Midnight Graphics
Create Fast and Simple Graphics in C++
TypedHandle.hpp
1 #pragma once
2 
3 #include <functional>
4 
5 namespace mn::Utility
6 {
7  template<typename Type, typename Underlying>
8  struct TypedHandle
9  {
10  TypedHandle() : value{0}
11  { }
12 
13  TypedHandle(Underlying U) : value(U)
14  { }
15 
16  Underlying operator*() const
17  {
18  return this->get();
19  }
20 
21  Underlying get() const
22  {
23  return value;
24  }
25 
26  template<typename A>
27  A as() const
28  {
29  return static_cast<A>(value);
30  }
31 
32  // bug: this allows us to convert to other handles too, we need to disallow this
33  template<typename A>
34  operator A() const
35  {
36  return static_cast<A>(value);
37  }
38 
39  template<typename A>
40  void destroy(std::function<void(A)> d)
41  {
42  if (value)
43  {
44  d(static_cast<A>(value));
45  value = 0;
46  }
47  }
48 
49  private:
50  Underlying value;
51  };
52 }
Definition: TypedHandle.hpp:9