CCF
Loading...
Searching...
No Matches
proxy.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include <memory>
6#include <uv.h>
7
8namespace asynchost
9{
10 template <typename T>
11 class proxy_ptr;
12
13 template <typename T>
15 {
16 private:
17 // Use a raw pointer, such that the libuv object is only deleted after
18 // closing.
19 friend class proxy_ptr<T>;
20 T* raw;
21
22 public:
23 template <typename... Args>
24 close_ptr(Args&&... args)
25 {
26 raw = new T(std::forward<Args>(args)...);
27 }
28 close_ptr(T* that) : raw(that) {}
29
31 {
32 if (raw != nullptr)
33 {
34 raw->close();
35 }
36 }
37
39 {
40 return raw;
41 }
42
44 {
45 return std::exchange(raw, nullptr);
46 }
47 };
48
49 template <typename T>
51 {
52 private:
53 std::shared_ptr<close_ptr<T>> internal;
54
55 public:
56 proxy_ptr(proxy_ptr<T>& that) : internal(that.internal) {}
57 proxy_ptr(const proxy_ptr<T>& that) : internal(that.internal) {}
58 proxy_ptr(proxy_ptr<T>&& that) : internal(std::move(that.internal)) {}
59 proxy_ptr(std::nullptr_t that) : internal(that) {}
60 proxy_ptr(T* that) : internal(std::make_shared<close_ptr<T>>(that)) {}
61
62 template <typename... Args>
63 proxy_ptr(Args&&... args) :
64 internal(std::make_shared<close_ptr<T>>(std::forward<Args>(args)...))
65 {}
66
68 {
69 return internal.get()->raw;
70 }
71
72 proxy_ptr<T>& operator=(const proxy_ptr<T>& that) = default;
73
74 bool is_null()
75 {
76 return internal == nullptr;
77 }
78 };
79
80 template <typename handle_type>
82 {
83 protected:
84 handle_type uv_handle;
85
89
90 virtual ~with_uv_handle() = default;
91
92 protected:
93 template <typename T>
94 friend class close_ptr;
95
96 void close()
97 {
98 if (!uv_is_closing((uv_handle_t*)&uv_handle))
99 {
100 uv_close((uv_handle_t*)&uv_handle, on_close);
101 }
102 }
103
104 private:
105 static void on_close(uv_handle_t* handle)
106 {
107 static_cast<with_uv_handle<handle_type>*>(handle->data)->on_close();
108 }
109
110 void on_close()
111 {
112 // We are being notified asynchronously that libuv has finished closing
113 // our handle.
114 delete this;
115 }
116 };
117}
Definition proxy.h:15
close_ptr(Args &&... args)
Definition proxy.h:24
T * operator->()
Definition proxy.h:38
~close_ptr()
Definition proxy.h:30
T * release()
Definition proxy.h:43
close_ptr(T *that)
Definition proxy.h:28
Definition proxy.h:51
proxy_ptr(const proxy_ptr< T > &that)
Definition proxy.h:57
proxy_ptr(proxy_ptr< T > &&that)
Definition proxy.h:58
bool is_null()
Definition proxy.h:74
proxy_ptr(T *that)
Definition proxy.h:60
proxy_ptr(Args &&... args)
Definition proxy.h:63
proxy_ptr(proxy_ptr< T > &that)
Definition proxy.h:56
proxy_ptr< T > & operator=(const proxy_ptr< T > &that)=default
T * operator->()
Definition proxy.h:67
proxy_ptr(std::nullptr_t that)
Definition proxy.h:59
Definition proxy.h:82
with_uv_handle(with_uv_handle< handle_type > &&that)=delete
void close()
Definition proxy.h:96
virtual ~with_uv_handle()=default
with_uv_handle(const with_uv_handle< handle_type > &that)=delete
handle_type uv_handle
Definition proxy.h:84
with_uv_handle()
Definition proxy.h:86
Definition after_io.h:8
STL namespace.