uvw 3.1.0
Loading...
Searching...
No Matches
resource.hpp
1#ifndef UVW_RESOURCE_INCLUDE_H
2#define UVW_RESOURCE_INCLUDE_H
3
4#include <memory>
5#include <utility>
6#include "config.h"
7#include "emitter.h"
8#include "uv_type.hpp"
9
10namespace uvw {
11
17template<typename T, typename U, typename... E>
18class resource: public uv_type<U>, public emitter<T, E...>, public std::enable_shared_from_this<T> {
19protected:
20 int leak_if(int err) noexcept {
21 if(err == 0) {
22 self_ptr = this->shared_from_this();
23 }
24
25 return err;
26 }
27
28 void self_reset() noexcept {
29 self_ptr.reset();
30 }
31
32 bool has_self() const noexcept {
33 return static_cast<bool>(self_ptr);
34 }
35
36public:
37 explicit resource(loop::token token, std::shared_ptr<loop> ref)
38 : uv_type<U>{token, std::move(ref)} {
39 this->raw()->data = this;
40 }
41
46 template<typename R = void>
47 std::shared_ptr<R> data() const {
48 return std::static_pointer_cast<R>(user_data);
49 }
50
55 void data(std::shared_ptr<void> udata) {
56 user_data = std::move(udata);
57 }
58
59private:
60 std::shared_ptr<void> user_data{nullptr};
61 std::shared_ptr<void> self_ptr{nullptr};
62};
63
64} // namespace uvw
65
66#endif // UVW_RESOURCE_INCLUDE_H
Event emitter base class.
Definition: emitter.h:83
Common class for almost all the resources available in uvw.
Definition: resource.hpp:18
void data(std::shared_ptr< void > udata)
Sets arbitrary data. uvw won't use this field in any case.
Definition: resource.hpp:55
std::shared_ptr< R > data() const
Gets user-defined data. uvw won't use this field in any case.
Definition: resource.hpp:47
uvw default namespace.
Definition: async.h:8
Wrapper class for underlying types.
Definition: uv_type.hpp:18
const U * raw() const noexcept
Gets the underlying raw data structure.
Definition: uv_type.hpp:59