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
30 {
31 if (raw != nullptr)
32 {
33 raw->close();
34 }
35 }
36
38 {
39 return raw;
40 }
41
43 {
44 return std::exchange(raw, nullptr);
45 }
46 };
47
48 template <typename T>
50 {
51 private:
52 std::shared_ptr<close_ptr<T>> internal;
53
54 public:
55 proxy_ptr(proxy_ptr<T>& that) : internal(that.internal) {}
56 proxy_ptr(const proxy_ptr<T>& that) : internal(that.internal) {}
57 proxy_ptr(proxy_ptr<T>&& that) : internal(std::move(that.internal)) {}
58 proxy_ptr(std::nullptr_t that) : internal(that) {}
59
60 template <typename... Args>
61 proxy_ptr(Args&&... args) :
62 internal(std::make_shared<close_ptr<T>>(std::forward<Args>(args)...))
63 {}
64
66 {
67 return internal.get()->raw;
68 }
69
70 proxy_ptr<T>& operator=(const proxy_ptr<T>& that) = default;
71
72 bool is_null()
73 {
74 return internal == nullptr;
75 }
76 };
77
78 template <typename handle_type>
80 {
81 protected:
82 handle_type uv_handle;
83
87
88 virtual ~with_uv_handle() = default;
89
90 protected:
91 template <typename T>
92 friend class close_ptr;
93
94 void close()
95 {
96 if (!uv_is_closing((uv_handle_t*)&uv_handle))
97 {
98 uv_close((uv_handle_t*)&uv_handle, on_close);
99 }
100 }
101
102 private:
103 static void on_close(uv_handle_t* handle)
104 {
105 static_cast<with_uv_handle<handle_type>*>(handle->data)->on_close();
106 }
107
108 void on_close()
109 {
110 // We are being notified asynchronously that libuv has finished closing
111 // our handle.
112 delete this;
113 }
114 };
115}
Definition proxy.h:15
close_ptr(Args &&... args)
Definition proxy.h:24
T * operator->()
Definition proxy.h:37
~close_ptr()
Definition proxy.h:29
T * release()
Definition proxy.h:42
Definition proxy.h:50
proxy_ptr(const proxy_ptr< T > &that)
Definition proxy.h:56
proxy_ptr(proxy_ptr< T > &&that)
Definition proxy.h:57
bool is_null()
Definition proxy.h:72
proxy_ptr(Args &&... args)
Definition proxy.h:61
proxy_ptr(proxy_ptr< T > &that)
Definition proxy.h:55
proxy_ptr< T > & operator=(const proxy_ptr< T > &that)=default
T * operator->()
Definition proxy.h:65
proxy_ptr(std::nullptr_t that)
Definition proxy.h:58
Definition proxy.h:80
with_uv_handle(with_uv_handle< handle_type > &&that)=delete
void close()
Definition proxy.h:94
virtual ~with_uv_handle()=default
with_uv_handle(const with_uv_handle< handle_type > &that)=delete
handle_type uv_handle
Definition proxy.h:82
with_uv_handle()
Definition proxy.h:84
Definition after_io.h:8
STL namespace.