From cd2894a089b4321a1ce576b1fec103a587c72324 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 26 Oct 2023 17:08:55 -0400 Subject: cmUVHandlePtr: Conversions to raw pointers are const --- Source/cmUVHandlePtr.cxx | 4 ++-- Source/cmUVHandlePtr.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/cmUVHandlePtr.cxx b/Source/cmUVHandlePtr.cxx index 34e6a70..fd109a5 100644 --- a/Source/cmUVHandlePtr.cxx +++ b/Source/cmUVHandlePtr.cxx @@ -44,7 +44,7 @@ void uv_loop_ptr::reset() this->loop.reset(); } -uv_loop_ptr::operator uv_loop_t*() +uv_loop_ptr::operator uv_loop_t*() const { return this->loop.get(); } @@ -103,7 +103,7 @@ void uv_handle_ptr_base_::reset() } template -uv_handle_ptr_base_::operator uv_handle_t*() +uv_handle_ptr_base_::operator uv_handle_t*() const { return reinterpret_cast(this->handle.get()); } diff --git a/Source/cmUVHandlePtr.h b/Source/cmUVHandlePtr.h index 027d690..afb7658 100644 --- a/Source/cmUVHandlePtr.h +++ b/Source/cmUVHandlePtr.h @@ -61,7 +61,7 @@ public: * Allow less verbose calling of uv_loop_* functions * @return reinterpreted handle */ - operator uv_loop_t*(); + operator uv_loop_t*() const; uv_loop_t* get() const; uv_loop_t* operator->() const noexcept; @@ -139,7 +139,7 @@ public: * Allow less verbose calling of uv_handle_* functions * @return reinterpreted handle */ - operator uv_handle_t*(); + operator uv_handle_t*() const; T* get() const; T* operator->() const noexcept; -- cgit v0.12 From 17690558c3ef6e6db850aa9f2993ea07467fe536 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 26 Oct 2023 17:08:55 -0400 Subject: cmUVHandlePtr: Add explicit conversion to bool --- Source/cmUVHandlePtr.cxx | 6 ++++++ Source/cmUVHandlePtr.h | 2 ++ Tests/CMakeLib/testUVRAII.cxx | 6 +++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Source/cmUVHandlePtr.cxx b/Source/cmUVHandlePtr.cxx index fd109a5..0e169df 100644 --- a/Source/cmUVHandlePtr.cxx +++ b/Source/cmUVHandlePtr.cxx @@ -97,6 +97,12 @@ void uv_handle_ptr_base_::allocate(void* data) } template +uv_handle_ptr_base_::operator bool() const +{ + return this->handle.get(); +} + +template void uv_handle_ptr_base_::reset() { this->handle.reset(); diff --git a/Source/cmUVHandlePtr.h b/Source/cmUVHandlePtr.h index afb7658..f5e2097 100644 --- a/Source/cmUVHandlePtr.h +++ b/Source/cmUVHandlePtr.h @@ -130,6 +130,8 @@ public: uv_handle_ptr_base_(std::nullptr_t) {} ~uv_handle_ptr_base_() { this->reset(); } + explicit operator bool() const; + /** * Properly close the handle if needed and sets the inner handle to nullptr */ diff --git a/Tests/CMakeLib/testUVRAII.cxx b/Tests/CMakeLib/testUVRAII.cxx index 0bdd44c..ce11761 100644 --- a/Tests/CMakeLib/testUVRAII.cxx +++ b/Tests/CMakeLib/testUVRAII.cxx @@ -37,7 +37,7 @@ static bool testAsyncShutdown() return false; } - if (signal.get()) { + if (signal) { std::cerr << "Loop exited with signal not being cleaned up" << std::endl; return false; } @@ -125,13 +125,13 @@ static bool testCrossAssignment() pipe.init(Loop, 0); cm::uv_stream_ptr stream = std::move(pipe); - if (pipe.get()) { + if (pipe) { std::cerr << "Move should be sure to invalidate the previous ptr" << std::endl; return false; } cm::uv_handle_ptr handle = std::move(stream); - if (stream.get()) { + if (stream) { std::cerr << "Move should be sure to invalidate the previous ptr" << std::endl; return false; -- cgit v0.12 From 70d88a536135067bff693de5d6c96d866dc5b00e Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 6 Nov 2023 15:00:28 -0500 Subject: cmUVHandlePtr: Add uv_idle_ptr Wrap a `uv_idle_t` handle. --- Source/cmUVHandlePtr.cxx | 8 ++++++++ Source/cmUVHandlePtr.h | 9 +++++++++ Tests/CMakeLib/testUVRAII.cxx | 26 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/Source/cmUVHandlePtr.cxx b/Source/cmUVHandlePtr.cxx index 0e169df..951ef3b 100644 --- a/Source/cmUVHandlePtr.cxx +++ b/Source/cmUVHandlePtr.cxx @@ -254,12 +254,20 @@ int uv_tty_ptr::init(uv_loop_t& loop, int fd, int readable, void* data) } #endif +int uv_idle_ptr::init(uv_loop_t& loop, void* data) +{ + this->allocate(data); + return uv_idle_init(&loop, *this); +} + template class uv_handle_ptr_base_; #define UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(NAME) \ template class uv_handle_ptr_base_; \ template class uv_handle_ptr_; +UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(idle) + UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(signal) UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(pipe) diff --git a/Source/cmUVHandlePtr.h b/Source/cmUVHandlePtr.h index f5e2097..e1bf0d0 100644 --- a/Source/cmUVHandlePtr.h +++ b/Source/cmUVHandlePtr.h @@ -196,6 +196,13 @@ public: void send(); }; +struct uv_idle_ptr : public uv_handle_ptr_ +{ + CM_INHERIT_CTOR(uv_idle_ptr, uv_handle_ptr_, ); + + int init(uv_loop_t& loop, void* data = nullptr); +}; + struct uv_signal_ptr : public uv_handle_ptr_ { CM_INHERIT_CTOR(uv_signal_ptr, uv_handle_ptr_, ); @@ -255,6 +262,8 @@ extern template class uv_handle_ptr_base_; UV_HANDLE_PTR_INSTANTIATE_EXTERN(async) +UV_HANDLE_PTR_INSTANTIATE_EXTERN(idle) + UV_HANDLE_PTR_INSTANTIATE_EXTERN(signal) UV_HANDLE_PTR_INSTANTIATE_EXTERN(pipe) diff --git a/Tests/CMakeLib/testUVRAII.cxx b/Tests/CMakeLib/testUVRAII.cxx index ce11761..1b08778 100644 --- a/Tests/CMakeLib/testUVRAII.cxx +++ b/Tests/CMakeLib/testUVRAII.cxx @@ -162,6 +162,7 @@ static bool testAllMoves() uv_async_ptr _13; uv_signal_ptr _14; uv_handle_ptr _15; + uv_idle_ptr _16; }; allTypes a; @@ -218,6 +219,30 @@ static bool testLoopDestructor() return true; } +static bool testIdle() +{ + bool idled = false; + + cm::uv_loop_ptr loop; + loop.init(); + + cm::uv_idle_ptr idle; + idle.init(*loop, &idled); + uv_idle_start(idle, [](uv_idle_t* handle) { + auto idledPtr = static_cast(handle->data); + *idledPtr = true; + uv_idle_stop(handle); + }); + uv_run(loop, UV_RUN_DEFAULT); + + if (!idled) { + std::cerr << "uv_idle_ptr did not trigger callback" << std::endl; + return false; + } + + return true; +} + int testUVRAII(int, char** const) { if (!testAsyncShutdown()) { @@ -230,5 +255,6 @@ int testUVRAII(int, char** const) passed = testAllMoves() && passed; passed = testLoopReset() && passed; passed = testLoopDestructor() && passed; + passed = testIdle() && passed; return passed ? 0 : -1; } -- cgit v0.12