split pointer and object operations

This commit is contained in:
numzero 2025-11-15 18:22:02 +03:00
parent 4e4c4493f9
commit d92d75a4d1
3 changed files with 30 additions and 15 deletions

View File

@ -3,13 +3,15 @@
#include <utility> #include <utility>
#include <stdexcept> #include <stdexcept>
namespace ffi {
extern "C" Core* rt4_viewport_create(xcb_connection_t* connection, std::uint32_t window); 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_destroy(Core* viewport);
extern "C" void rt4_viewport_configure(Core* viewport, std::uint32_t width, std::uint32_t height); extern "C" void rt4_viewport_configure(Core* viewport, std::uint32_t width, std::uint32_t height);
extern "C" void rt4_viewport_redraw(Core* viewport); extern "C" void rt4_viewport_redraw(Core* viewport);
}
BoxCore::BoxCore(BoxCore&& b) 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) if (!window)
throw std::logic_error("attempt to use a null window"); throw std::logic_error("attempt to use a null window");
BoxCore out; BoxCore out;
out.ptr = rt4_viewport_create(connection, window); out.ptr.ptr = ffi::rt4_viewport_create(connection, window);
return out; return out;
} }
void BoxCore::reset() { void BoxCore::reset() {
auto viewport = std::exchange(ptr, nullptr); auto viewport = std::exchange(ptr, {});
if (viewport) if (viewport)
rt4_viewport_destroy(viewport); ffi::rt4_viewport_destroy(viewport.use());
} }
Core* BoxCore::use() const { ffi::Core* MutCore::use() const {
if (!ptr) if (!ptr)
throw std::logic_error("attempt to use a null Core"); throw std::logic_error("attempt to use a null Core");
return ptr; 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); rt4_viewport_configure(use(), width, height);
} }
void BoxCore::redraw() { void MutCore::redraw() const {
rt4_viewport_redraw(use()); rt4_viewport_redraw(use());
} }

View File

@ -4,7 +4,23 @@
struct xcb_connection_t; struct xcb_connection_t;
namespace ffi {
struct Core; 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 { class BoxCore {
public: public:
@ -15,16 +31,13 @@ public:
BoxCore& operator= (BoxCore&&); BoxCore& operator= (BoxCore&&);
~BoxCore(); ~BoxCore();
explicit operator bool() const { return ptr; } explicit operator bool() const { return !!ptr; }
const MutCore* operator->() const { return &ptr; }
void reset(); void reset();
static BoxCore from_xcb(xcb_connection_t* connection, std::uint32_t window); 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: private:
Core* ptr = nullptr; MutCore ptr;
Core* use() const;
}; };

View File

@ -30,7 +30,7 @@ bool Viewport::event(QEvent* event) {
void Viewport::paintEvent(QPaintEvent* event) { void Viewport::paintEvent(QPaintEvent* event) {
if (!core) if (!core)
recreate(); recreate();
core.redraw(); core->redraw();
} }
void Viewport::resizeEvent(QResizeEvent* event) { void Viewport::resizeEvent(QResizeEvent* event) {
@ -63,5 +63,5 @@ void Viewport::recreate() try {
void Viewport::updateSize() { void Viewport::updateSize() {
const QSize device_size = size() * devicePixelRatio(); const QSize device_size = size() * devicePixelRatio();
core.configure(device_size.width(), device_size.height()); core->configure(device_size.width(), device_size.height());
} }