From d92d75a4d1bbbd6893963a845cb9a5a613a4580e Mon Sep 17 00:00:00 2001 From: numzero Date: Sat, 15 Nov 2025 18:22:02 +0300 Subject: [PATCH] split pointer and object operations --- ui/src/api.cxx | 16 +++++++++------- ui/src/api.hxx | 25 +++++++++++++++++++------ ui/src/viewport.cxx | 4 ++-- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/ui/src/api.cxx b/ui/src/api.cxx index d85f970..0c6c40d 100644 --- a/ui/src/api.cxx +++ b/ui/src/api.cxx @@ -3,13 +3,15 @@ #include #include +namespace ffi { extern "C" Core* rt4_viewport_create(xcb_connection_t* connection, std::uint32_t window); extern "C" void rt4_viewport_destroy(Core* viewport); extern "C" void rt4_viewport_configure(Core* viewport, std::uint32_t width, std::uint32_t height); extern "C" void rt4_viewport_redraw(Core* viewport); +} BoxCore::BoxCore(BoxCore&& b) - : ptr(std::exchange(b.ptr, nullptr)) + : ptr(std::exchange(b.ptr, {})) { } @@ -31,26 +33,26 @@ BoxCore BoxCore::from_xcb(xcb_connection_t* connection, std::uint32_t window) { if (!window) throw std::logic_error("attempt to use a null window"); BoxCore out; - out.ptr = rt4_viewport_create(connection, window); + out.ptr.ptr = ffi::rt4_viewport_create(connection, window); return out; } void BoxCore::reset() { - auto viewport = std::exchange(ptr, nullptr); + auto viewport = std::exchange(ptr, {}); if (viewport) - rt4_viewport_destroy(viewport); + ffi::rt4_viewport_destroy(viewport.use()); } -Core* BoxCore::use() const { +ffi::Core* MutCore::use() const { if (!ptr) throw std::logic_error("attempt to use a null Core"); return ptr; } -void BoxCore::configure(std::uint32_t width, std::uint32_t height) { +void MutCore::configure(std::uint32_t width, std::uint32_t height) const { rt4_viewport_configure(use(), width, height); } -void BoxCore::redraw() { +void MutCore::redraw() const { rt4_viewport_redraw(use()); } diff --git a/ui/src/api.hxx b/ui/src/api.hxx index 2b59c32..0055435 100644 --- a/ui/src/api.hxx +++ b/ui/src/api.hxx @@ -4,7 +4,23 @@ struct xcb_connection_t; +namespace ffi { struct Core; +} + +class MutCore { + friend class BoxCore; + +public: + explicit operator bool() const { return ptr; } + + void configure(std::uint32_t width, std::uint32_t height) const; + void redraw() const; + +private: + ffi::Core* ptr = nullptr; + ffi::Core* use() const; +}; class BoxCore { public: @@ -15,16 +31,13 @@ public: BoxCore& operator= (BoxCore&&); ~BoxCore(); - explicit operator bool() const { return ptr; } + explicit operator bool() const { return !!ptr; } + const MutCore* operator->() const { return &ptr; } void reset(); static BoxCore from_xcb(xcb_connection_t* connection, std::uint32_t window); - void configure(std::uint32_t width, std::uint32_t height); - void redraw(); private: - Core* ptr = nullptr; - - Core* use() const; + MutCore ptr; }; diff --git a/ui/src/viewport.cxx b/ui/src/viewport.cxx index ebdcb9d..634b996 100644 --- a/ui/src/viewport.cxx +++ b/ui/src/viewport.cxx @@ -30,7 +30,7 @@ bool Viewport::event(QEvent* event) { void Viewport::paintEvent(QPaintEvent* event) { if (!core) recreate(); - core.redraw(); + core->redraw(); } void Viewport::resizeEvent(QResizeEvent* event) { @@ -63,5 +63,5 @@ void Viewport::recreate() try { void Viewport::updateSize() { const QSize device_size = size() * devicePixelRatio(); - core.configure(device_size.width(), device_size.height()); + core->configure(device_size.width(), device_size.height()); }