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 raw(new T(
26 std::forward<Args>(args)...)) // NOLINT(cppcoreguidelines-owning-memory)
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) noexcept : internal(std::move(that.internal))
59 {}
60 proxy_ptr(std::nullptr_t that) : internal(that) {}
61 proxy_ptr(T* that) : internal(std::make_shared<close_ptr<T>>(that)) {}
62
63 template <typename... Args>
64 proxy_ptr(Args&&... args) :
65 internal(std::make_shared<close_ptr<T>>(std::forward<Args>(args)...))
66 {}
67
69 {
70 return internal.get()->raw;
71 }
72
73 proxy_ptr<T>& operator=(const proxy_ptr<T>& that) = default;
74
75 bool is_null()
76 {
77 return internal == nullptr;
78 }
79 };
80
81 // NOLINTBEGIN(cppcoreguidelines-virtual-class-destructor)
82 template <typename handle_type>
84 {
85 public:
88
89 protected:
90 handle_type uv_handle{};
91
92 with_uv_handle() = default;
93 virtual ~with_uv_handle() = default;
94
95 template <typename T>
96 friend class close_ptr;
97
98 void close()
99 {
100 if (uv_is_closing(reinterpret_cast<uv_handle_t*>(&uv_handle)) == 0)
101 {
102 uv_close(reinterpret_cast<uv_handle_t*>(&uv_handle), on_close);
103 }
104 }
105
106 private:
107 static void on_close(uv_handle_t* handle)
108 {
109 static_cast<with_uv_handle<handle_type>*>(handle->data)->on_close();
110 }
111
112 void on_close()
113 {
114 // We are being notified asynchronously that libuv has finished closing
115 // our handle.
116 delete this;
117 }
118 };
119 // NOLINTEND(cppcoreguidelines-virtual-class-destructor)
120}
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
bool is_null()
Definition proxy.h:75
proxy_ptr(T *that)
Definition proxy.h:61
proxy_ptr(proxy_ptr< T > &&that) noexcept
Definition proxy.h:58
proxy_ptr(Args &&... args)
Definition proxy.h:64
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:68
proxy_ptr(std::nullptr_t that)
Definition proxy.h:60
Definition proxy.h:84
with_uv_handle(with_uv_handle< handle_type > &&that)=delete
void close()
Definition proxy.h:98
virtual ~with_uv_handle()=default
with_uv_handle(const with_uv_handle< handle_type > &that)=delete
handle_type uv_handle
Definition proxy.h:90
Definition after_io.h:8
STL namespace.