uvw  2.2.0
resource.hpp
1 #pragma once
2 
3 
4 #include <memory>
5 #include <utility>
6 #include "emitter.hpp"
7 #include "underlying_type.hpp"
8 
9 
10 namespace uvw {
11 
12 
18 template<typename T, typename U>
19 class Resource: public UnderlyingType<T, U>, public Emitter<T>, public std::enable_shared_from_this<T> {
20 protected:
21  using ConstructorAccess = typename UnderlyingType<T, U>::ConstructorAccess;
22 
23  auto parent() const noexcept {
24  return this->loop().loop.get();
25  }
26 
27  void leak() noexcept {
28  sPtr = this->shared_from_this();
29  }
30 
31  void reset() noexcept {
32  sPtr.reset();
33  }
34 
35  bool self() const noexcept {
36  return static_cast<bool>(sPtr);
37  }
38 
39 public:
40  explicit Resource(ConstructorAccess ca, std::shared_ptr<Loop> ref)
41  : UnderlyingType<T, U>{ca, std::move(ref)},
42  Emitter<T>{},
43  std::enable_shared_from_this<T>{}
44  {
45  this->get()->data = static_cast<T*>(this);
46  }
47 
52  template<typename R = void>
53  std::shared_ptr<R> data() const {
54  return std::static_pointer_cast<R>(userData);
55  }
56 
61  void data(std::shared_ptr<void> uData) {
62  userData = std::move(uData);
63  }
64 
65 private:
66  std::shared_ptr<void> userData{nullptr};
67  std::shared_ptr<void> sPtr{nullptr};
68 };
69 
70 
71 }
Event emitter base class.
Definition: emitter.hpp:87
Wrapper class for underlying types.
Common class for almost all the resources available in uvw.
Definition: resource.hpp:19
Loop & loop() const noexcept
Gets the loop from which the resource was originated.
uvw default namespace.
Definition: async.hpp:11