uvw 2.12.1
Loading...
Searching...
No Matches
underlying_type.hpp
1#ifndef UVW_UNDERLYING_TYPE_INCLUDE_H
2#define UVW_UNDERLYING_TYPE_INCLUDE_H
3
4#include <memory>
5#include <type_traits>
6#include <utility>
7#include "loop.h"
8
9namespace uvw {
10
16template<typename T, typename U>
18 template<typename, typename>
19 friend class UnderlyingType;
20
21protected:
22 struct ConstructorAccess {
23 explicit ConstructorAccess(int) {}
24 };
25
26 template<typename R = U>
27 auto get() noexcept {
28 return reinterpret_cast<R *>(&resource);
29 }
30
31 template<typename R = U>
32 auto get() const noexcept {
33 return reinterpret_cast<const R *>(&resource);
34 }
35
36 template<typename R, typename... P>
37 auto get(UnderlyingType<P...> &other) noexcept {
38 return reinterpret_cast<R *>(&other.resource);
39 }
40
41public:
42 explicit UnderlyingType(ConstructorAccess, std::shared_ptr<Loop> ref) noexcept
43 : pLoop{std::move(ref)}, resource{} {}
44
45 UnderlyingType(const UnderlyingType &) = delete;
46 UnderlyingType(UnderlyingType &&) = delete;
47
48 virtual ~UnderlyingType() {
49 static_assert(std::is_base_of_v<UnderlyingType<T, U>, T>);
50 }
51
52 UnderlyingType &operator=(const UnderlyingType &) = delete;
53 UnderlyingType &operator=(UnderlyingType &&) = delete;
54
60 template<typename... Args>
61 static std::shared_ptr<T> create(Args &&...args) {
62 return std::make_shared<T>(ConstructorAccess{0}, std::forward<Args>(args)...);
63 }
64
69 Loop &loop() const noexcept {
70 return *pLoop;
71 }
72
88 const U *raw() const noexcept {
89 return &resource;
90 }
91
107 U *raw() noexcept {
108 return const_cast<U *>(const_cast<const UnderlyingType *>(this)->raw());
109 }
110
111private:
112 std::shared_ptr<Loop> pLoop;
113 U resource;
114};
115
116} // namespace uvw
117
118#endif // UVW_UNDERLYING_TYPE_INCLUDE_H
The Loop class.
Definition loop.h:57
Wrapper class for underlying types.
Loop & loop() const noexcept
Gets the loop from which the resource was originated.
const U * raw() const noexcept
Gets the underlying raw data structure.
static std::shared_ptr< T > create(Args &&...args)
Creates a new resource of the given type.
U * raw() noexcept
Gets the underlying raw data structure.
uvw default namespace.
Definition async.h:8