Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
GraphicsHandle.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_GRAPHICS_HANDLE_H
22 #define GF_GRAPHICS_HANDLE_H
23 
24 #include <utility>
25 
26 #include "Portability.h"
27 #include "Types.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
34  enum class GraphicsTag {
35  Buffer,
37  Texture,
38  };
39 
40  template<GraphicsTag Tag>
41  struct GraphicsTrait;
42 
49  template<GraphicsTag Tag>
51  public:
53  : m_name()
54  {
55  GraphicsTrait<Tag>::gen(1, &m_name);
56  }
57 
58  constexpr GraphicsHandle(NoneType) noexcept
59  : m_name(0)
60  {
61  }
62 
63  ~GraphicsHandle() noexcept {
64  if (m_name != 0) {
65  GraphicsTrait<Tag>::del(1, &m_name);
66  }
67  }
68 
69  GraphicsHandle(const GraphicsHandle&) = delete;
70 
71  GraphicsHandle& operator=(const GraphicsHandle&) = delete;
72 
73  GraphicsHandle(GraphicsHandle&& other) noexcept
74  : m_name(std::exchange(other.m_name, 0))
75  {
76  }
77 
79  std::swap(m_name, other.m_name);
80  return *this;
81  }
82 
83  bool isValid() const noexcept {
84  return m_name != 0;
85  }
86 
87  unsigned getName() const noexcept {
88  return m_name;
89  }
90 
91  operator unsigned () const noexcept {
92  return m_name;
93  }
94 
95  private:
96  unsigned m_name;
97  };
98 
99 #ifndef DOXYGEN_SHOULD_SKIP_THIS
100 }
101 #endif
102 }
103 
104 #endif // GF_GRAPHICS_HANDLE_H
GraphicsHandle(GraphicsHandle &&other) noexcept
Definition: GraphicsHandle.h:73
Semantic type to represent "none".
Definition: Types.h:37
Definition: GraphicsHandle.h:41
A GL handle.
Definition: GraphicsHandle.h:50
constexpr GraphicsHandle(NoneType) noexcept
Definition: GraphicsHandle.h:58
bool isValid() const noexcept
Definition: GraphicsHandle.h:83
GraphicsHandle()
Definition: GraphicsHandle.h:52
GraphicsTag
Definition: GraphicsHandle.h:34
The namespace for gf classes.
Definition: Action.h:35
GraphicsHandle & operator=(GraphicsHandle &&other) noexcept
Definition: GraphicsHandle.h:78
unsigned getName() const noexcept
Definition: GraphicsHandle.h:87
~GraphicsHandle() noexcept
Definition: GraphicsHandle.h:63