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