OGLplus  (0.59.0) a C++ wrapper for rendering APIs

handle.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_HANDLE_HPP
10 #define EAGINE_HANDLE_HPP
11 
12 #include "span.hpp"
13 #include "wrapping_container.hpp"
14 #include <array>
15 #include <vector>
16 
17 namespace eagine {
18 //------------------------------------------------------------------------------
25 template <typename Tag, typename Handle, Handle invalid = ~Handle(0)>
26 class basic_handle {
27 public:
29  using tag_type = Tag;
30 
32  using handle_type = Handle;
33 
34  ~basic_handle() noexcept = default;
35 
38  constexpr basic_handle() noexcept = default;
39 
41  constexpr basic_handle(basic_handle&& tmp) noexcept
42  : _name{tmp._release()} {}
43 
45  auto operator=(basic_handle&& tmp) noexcept -> auto& {
46  _name = tmp._release();
47  return *this;
48  }
49 
51  constexpr basic_handle(const basic_handle&) noexcept = default;
52 
54  auto operator=(const basic_handle&) noexcept -> basic_handle& = default;
55 
57  explicit constexpr basic_handle(Handle name) noexcept
58  : _name{name} {}
59 
61  constexpr auto is_valid() const noexcept -> bool {
62  return _name != invalid;
63  }
64 
67  explicit constexpr operator bool() const noexcept {
68  return is_valid();
69  }
70 
72  explicit constexpr operator Handle() const noexcept {
73  return _name;
74  }
75 
76  struct transform {
77  constexpr auto operator()(Handle hndl) const noexcept {
79  }
80  };
81 
82 protected:
83  auto _release() noexcept {
84  return std::exchange(_name, invalid);
85  }
86 
87 private:
88  Handle _name{invalid};
89 };
90 //------------------------------------------------------------------------------
97 template <typename Tag, typename Handle, Handle invalid = ~Handle(0)>
98 class basic_owned_handle : public basic_handle<Tag, Handle, invalid> {
100 
101 public:
102  ~basic_owned_handle() noexcept = default;
103 
106  constexpr basic_owned_handle() noexcept = default;
107 
109  constexpr basic_owned_handle(basic_owned_handle&& tmp) noexcept
110  : base{tmp.release()} {}
111 
113  auto operator=(basic_owned_handle&& tmp) noexcept -> auto& {
114  *static_cast<base*>(this) = static_cast<base&&>(tmp);
115  return *this;
116  }
117 
119  basic_owned_handle(const basic_owned_handle&) = delete;
120 
122  auto operator=(const basic_owned_handle&) = delete;
123 
125  explicit constexpr basic_owned_handle(base adopted) noexcept
126  : base{adopted} {}
127 
129  explicit constexpr basic_owned_handle(Handle name) noexcept
130  : base{name} {}
131 
134  auto release() noexcept -> Handle {
135  return this->_release();
136  }
137 };
138 //------------------------------------------------------------------------------
139 template <typename BasicHandle, typename Container>
140 class basic_handle_container;
141 
144 template <typename Tag, typename Handle, Handle invalid, typename Container>
145 class basic_handle_container<basic_handle<Tag, Handle, invalid>, Container>
146  : public basic_wrapping_container<
147  Container,
148  basic_handle<Tag, Handle, invalid>,
149  Handle,
150  invalid> {
152  Container,
154  Handle,
155  invalid>;
156 
157 public:
158  using base::base;
159 
161  constexpr auto raw_handles() noexcept {
162  return this->raw_items();
163  }
164 
166  constexpr auto raw_handles() const noexcept {
167  return this->raw_items();
168  }
169 };
170 //------------------------------------------------------------------------------
176 template <typename BasicHandle>
177 using basic_handle_span =
178  basic_handle_container<BasicHandle, span<typename BasicHandle::handle_type>>;
179 
185 template <typename BasicHandle>
186 using basic_handle_view = basic_handle_container<
187  BasicHandle,
188  span<const typename BasicHandle::handle_type>>;
189 
195 template <typename BasicHandle, std::size_t N>
196 using basic_handle_array = basic_handle_container<
197  BasicHandle,
198  std::array<typename BasicHandle::handle_type, N>>;
199 
205 template <typename BasicHandle>
206 using basic_handle_vector = basic_handle_container<
207  BasicHandle,
208  std::vector<typename BasicHandle::handle_type>>;
209 //------------------------------------------------------------------------------
210 } // namespace eagine
211 
212 #endif // EAGINE_HANDLE_HPP
constexpr basic_owned_handle() noexcept=default
Default constructor.
auto release() noexcept -> Handle
Releases the underlying handle value.
Definition: handle.hpp:134
Common code is placed in this namespace.
Definition: eagine.hpp:21
constexpr basic_owned_handle(base adopted) noexcept
Constructor adopting a non-owning handle wrapper.
Definition: handle.hpp:125
auto operator=(basic_handle &&tmp) noexcept -> auto &
Move assignment operator.
Definition: handle.hpp:45
constexpr auto raw_handles() noexcept
Returns the raw handles.
Definition: handle.hpp:161
constexpr basic_owned_handle(basic_owned_handle &&tmp) noexcept
Move constructor.
Definition: handle.hpp:109
basic_handle_container< BasicHandle, std::vector< typename BasicHandle::handle_type > > basic_handle_vector
Alias for basic handle container based on std::vector.
Definition: handle.hpp:208
buffer_tag tag_type
Alias for the tag type.
Definition: handle.hpp:29
Handle handle_type
Alias for the underlying handle type.
Definition: handle.hpp:32
constexpr basic_handle(Handle name) noexcept
Construction from the underlying handle type.
Definition: handle.hpp:57
Non-owning wrapper for C-API opaque handle types.
Definition: handle.hpp:26
constexpr basic_handle() noexcept=default
Default constructor.
auto operator=(basic_owned_handle &&tmp) noexcept -> auto &
Move assignment operator.
Definition: handle.hpp:113
basic_handle_container< BasicHandle, std::array< typename BasicHandle::handle_type, N > > basic_handle_array
Alias for basic handle container based on std::array.
Definition: handle.hpp:198
basic_handle_container< BasicHandle, span< const typename BasicHandle::handle_type > > basic_handle_view
Alias for basic handle container based on const span.
Definition: handle.hpp:188
static auto transform(basic_span< T, P, S > spn, Transform function) -> basic_span< T, P, S >
Transforms the elements of a span with a function.
Definition: span_algo.hpp:572
constexpr auto is_valid() const noexcept -> bool
Indicates if this instance contains a valid handle.
Definition: handle.hpp:61
Owning wrapper for C-API opaque handle types.
Definition: handle.hpp:98
Template used in implementation of containers with wrapped elements.
Definition: wrapping_container.hpp:26
constexpr basic_owned_handle(Handle name) noexcept
Constructor adopting an underlying handle value.
Definition: handle.hpp:129
basic_handle_container< BasicHandle, span< typename BasicHandle::handle_type > > basic_handle_span
Alias for basic handle container based on non-const span.
Definition: handle.hpp:178
constexpr auto raw_handles() const noexcept
Returns the raw handles.
Definition: handle.hpp:166

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).