make allocator private

This commit is contained in:
MasterLogick 2023-06-08 20:13:15 +03:00
parent 38ad95e5fe
commit b5eea8f01d
No known key found for this signature in database
GPG Key ID: 8E5B0F7CD118BCBA
7 changed files with 44 additions and 8 deletions

View File

@ -243,8 +243,6 @@ public:
uv_fileno(as_uv_handle(), &fd);
return fd;
}
std::function<void(uv_buf_t *, std::size_t, T &)> allocator = details::default_allocator<T>;
};
} // namespace uvw

View File

@ -157,7 +157,7 @@ public:
#else
using base::base;
#endif
using allocator = std::function<void(uv_buf_t *, std::size_t, T &)>;
/**
* @brief Shutdowns the outgoing (write) side of a duplex stream.
*
@ -459,6 +459,24 @@ public:
size_t write_queue_size() const noexcept {
return uv_stream_get_write_queue_size(as_uv_stream());
}
/**
* @return Allocator for data_event buffers
*/
allocator &get_allocator() {
return readBufferAllocator;
}
/**
* Sets allocator for data_event buffers or resets to default
* @param alloc allocator for data_event buffers
*/
void set_allocator(const allocator &alloc = details::default_allocator<T>) {
readBufferAllocator = alloc;
}
private:
allocator readBufferAllocator = details::default_allocator<T>;
};
} // namespace uvw

View File

@ -213,4 +213,10 @@ UVW_INLINE size_t udp_handle::send_queue_count() const noexcept {
return uv_udp_get_send_queue_count(raw());
}
UVW_INLINE udp_handle::allocator &udp_handle::get_allocator() {
return readBufferAllocator;
}
UVW_INLINE void udp_handle::set_allocator(const udp_handle::allocator &alloc) {
readBufferAllocator = alloc;
}
} // namespace uvw

View File

@ -87,6 +87,7 @@ public:
using udp_flags = details::uvw_udp_flags;
using ipv4 = uvw::ipv4;
using ipv6 = uvw::ipv6;
using allocator = std::function<void(uv_buf_t *, std::size_t, udp_handle &)>;
explicit udp_handle(loop::token token, std::shared_ptr<loop> ref, unsigned int f = {});
@ -533,6 +534,17 @@ public:
*/
size_t send_queue_count() const noexcept;
/**
* @return Allocator for data_event buffers
*/
allocator &get_allocator();
/**
* Sets allocator for data_event buffers or resets to default
* @param alloc allocator for data_event buffers
*/
void set_allocator(const allocator &alloc = details::default_allocator<udp_handle>);
private:
enum {
DEFAULT,
@ -540,6 +552,8 @@ private:
} tag{DEFAULT};
unsigned int flags{};
allocator readBufferAllocator = details::default_allocator<udp_handle>;
};
} // namespace uvw

View File

@ -279,7 +279,7 @@ void default_allocator(uv_buf_t *buf, std::size_t suggested, T &) {
template<class T>
void alloc_callback(uv_handle_t *hndl, std::size_t suggested, uv_buf_t *buf) {
T &ref = *(static_cast<T *>(hndl->data));
ref.allocator(buf, suggested, ref);
(ref.get_allocator())(buf, suggested, ref);
}
sockaddr ip_addr(const char *addr, unsigned int port);

View File

@ -175,9 +175,9 @@ TEST(TCP, CustomAllocator) {
ASSERT_EQ(data, ptr);
sock.close();
});
socket->allocator = [ptr](uv_buf_t *b, size_t suggested, uvw::tcp_handle &h) {
socket->set_allocator([ptr](uv_buf_t *b, size_t suggested, uvw::tcp_handle &h) {
*b = uv_buf_init(ptr, 5);
};
});
ASSERT_EQ(0, handle.accept(*socket));
ASSERT_EQ(0, socket->read());
});

View File

@ -141,9 +141,9 @@ TEST(UDP, CustomAllocator) {
ASSERT_EQ(data, ptr);
handle.close();
});
server->allocator = [ptr](uv_buf_t *b, size_t suggested, uvw::udp_handle &h) {
server->set_allocator([ptr](uv_buf_t *b, size_t suggested, uvw::udp_handle &h) {
*b = uv_buf_init(ptr, 5);
};
});
client->on<uvw::send_event>([](const uvw::send_event &, uvw::udp_handle &handle) {
handle.close();