mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 06:11:16 +00:00
Change C++ API to use the ComponentHandle
This commit is contained in:
parent
8f362ead43
commit
d6a440aa4a
65 changed files with 222 additions and 141 deletions
|
@ -65,9 +65,10 @@ struct VRcInner {
|
|||
template<typename VTable_, typename X_> friend class VRc;
|
||||
template<typename VTable_, typename X_> friend class VWeak;
|
||||
private:
|
||||
const VTable *vtable;
|
||||
int strong_ref;
|
||||
int weak_ref;
|
||||
VRcInner() : layout {} {}
|
||||
const VTable *vtable = &X::component_type;
|
||||
int strong_ref = 1;
|
||||
int weak_ref = 1;
|
||||
union {
|
||||
X data;
|
||||
Layout layout;
|
||||
|
@ -79,17 +80,17 @@ struct Dyn {};
|
|||
template<typename VTable, typename X = Dyn>
|
||||
class VRc {
|
||||
VRcInner<VTable, X> *inner;
|
||||
VRc(VRcInner<VTable, X> inner) : inner(inner) {}
|
||||
VRc(VRcInner<VTable, X> *inner) : inner(inner) {}
|
||||
template<typename VTable_, typename X_> friend class VWeak;
|
||||
public:
|
||||
~VRc() {
|
||||
if (--inner->strong_ref) {
|
||||
Layout layout = inner->vtable->drop_in_place(&inner->data);
|
||||
if (!--inner->strong_ref) {
|
||||
Layout layout = inner->vtable->drop_in_place({inner->vtable, &inner->data});
|
||||
layout.size += sizeof(const VTable *) + 2 * sizeof(int);
|
||||
layout.align = std::max<size_t>(layout.align, alignof(VRcInner<VTable, Dyn>));
|
||||
inner->layout = layout;
|
||||
if (--inner->weak_ref) {
|
||||
inner->vtable->dealloc(layout);
|
||||
if (!--inner->weak_ref) {
|
||||
inner->vtable->dealloc(inner->vtable, reinterpret_cast<uint8_t*>(inner), layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -104,12 +105,16 @@ public:
|
|||
return *this;
|
||||
}
|
||||
template<typename ...Args> static VRc make(Args... args) {
|
||||
return VRc(new VRcInner<VTable, X>{
|
||||
X::component_type, 1, 1, X(args...)
|
||||
});
|
||||
auto mem = ::operator new(sizeof(VRcInner<VTable, X>), static_cast<std::align_val_t>(alignof(VRcInner<VTable, X>)));
|
||||
auto inner = new (mem) VRcInner<VTable, X>;
|
||||
new (&inner->data) X(args...);
|
||||
return VRc(inner);
|
||||
}
|
||||
|
||||
const X* operator->() const {
|
||||
return &inner->data;
|
||||
}
|
||||
const X& operator*() const {
|
||||
return inner->data;
|
||||
}
|
||||
};
|
||||
|
@ -120,8 +125,8 @@ class VWeak {
|
|||
public:
|
||||
VWeak() = default;
|
||||
~VWeak() {
|
||||
if (inner && --inner->weak_ref) {
|
||||
inner->vtable->dealloc(inner->layout);
|
||||
if (inner && !--inner->weak_ref) {
|
||||
inner->vtable->dealloc(inner->vtable, reinterpret_cast<uint8_t*>(inner), inner->layout);
|
||||
}
|
||||
}
|
||||
VWeak(const VWeak &other): inner(other.inner) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue