mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 14:21:16 +00:00
Rename SharedArray to SharedVector
This commit is contained in:
parent
0d2d48be4f
commit
dfa25b96f7
15 changed files with 122 additions and 121 deletions
|
@ -409,7 +409,7 @@ struct IntModel : Model<int>
|
|||
int row_data(int value) const override { return value; }
|
||||
};
|
||||
|
||||
/// A Model backed by a SharedArray
|
||||
/// A Model backed by a SharedVector
|
||||
template<typename ModelData>
|
||||
class VectorModel : public Model<ModelData>
|
||||
{
|
||||
|
|
|
@ -56,10 +56,10 @@ public:
|
|||
|
||||
|
||||
private:
|
||||
static SharedArray<PathElement> elements_from_array(const PathElement *firstElement,
|
||||
static SharedVector<PathElement> elements_from_array(const PathElement *firstElement,
|
||||
size_t count)
|
||||
{
|
||||
SharedArray<PathElement> tmp;
|
||||
SharedVector<PathElement> tmp;
|
||||
sixtyfps_new_path_elements(&tmp, firstElement, count);
|
||||
return tmp;
|
||||
}
|
||||
|
@ -69,8 +69,8 @@ private:
|
|||
const Point *firstCoordinate,
|
||||
size_t coordinate_count)
|
||||
{
|
||||
SharedArray<PathEvent> events;
|
||||
SharedArray<Point> coordinates;
|
||||
SharedVector<PathEvent> events;
|
||||
SharedVector<Point> coordinates;
|
||||
sixtyfps_new_path_events(&events, &coordinates, firstEvent, event_count, firstCoordinate,
|
||||
coordinate_count);
|
||||
return Data::Events(events, coordinates);
|
||||
|
|
|
@ -8,32 +8,32 @@
|
|||
Please contact info@sixtyfps.io for more information.
|
||||
LICENSE END */
|
||||
#pragma once
|
||||
#include "sixtyfps_sharedarray_internal.h"
|
||||
#include "sixtyfps_sharedvector_internal.h"
|
||||
#include <atomic>
|
||||
#include <algorithm>
|
||||
|
||||
namespace sixtyfps {
|
||||
|
||||
template<typename T>
|
||||
struct SharedArray
|
||||
struct SharedVector
|
||||
{
|
||||
SharedArray()
|
||||
: inner(const_cast<SharedArrayHeader*>(reinterpret_cast<const SharedArrayHeader*>(
|
||||
cbindgen_private::sixtyfps_shared_array_empty())))
|
||||
SharedVector()
|
||||
: inner(const_cast<SharedVectorHeader*>(reinterpret_cast<const SharedVectorHeader*>(
|
||||
cbindgen_private::sixtyfps_shared_vector_empty())))
|
||||
{ }
|
||||
|
||||
SharedArray(const SharedArray &other)
|
||||
SharedVector(const SharedVector &other)
|
||||
: inner(other.inner)
|
||||
{
|
||||
if (inner->refcount > 0) {
|
||||
++inner->refcount;
|
||||
}
|
||||
}
|
||||
~SharedArray()
|
||||
~SharedVector()
|
||||
{
|
||||
drop();
|
||||
}
|
||||
SharedArray &operator=(const SharedArray &other)
|
||||
SharedVector &operator=(const SharedVector &other)
|
||||
{
|
||||
if (other.inner == inner) { return *this; }
|
||||
drop();
|
||||
|
@ -43,7 +43,7 @@ struct SharedArray
|
|||
}
|
||||
return *this;
|
||||
}
|
||||
SharedArray &operator=(SharedArray &&other)
|
||||
SharedVector &operator=(SharedVector &&other)
|
||||
{
|
||||
std::swap(inner, other.inner);
|
||||
return *this;
|
||||
|
@ -96,12 +96,12 @@ struct SharedArray
|
|||
inner->size++;
|
||||
}
|
||||
|
||||
friend bool operator==(const SharedArray &a, const SharedArray &b) {
|
||||
friend bool operator==(const SharedVector &a, const SharedVector &b) {
|
||||
if (a.size() != a.size())
|
||||
return false;
|
||||
return std::equal(a.cbegin(), a.cend(), b.cbegin());
|
||||
}
|
||||
friend bool operator!=(const SharedArray &a, const SharedArray &b) {
|
||||
friend bool operator!=(const SharedVector &a, const SharedVector &b) {
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ private:
|
|||
if (inner->refcount == 1 && expected_capacity <= inner->capacity) {
|
||||
return;
|
||||
}
|
||||
auto new_array = SharedArray::with_capacity(expected_capacity);
|
||||
auto new_array = SharedVector::with_capacity(expected_capacity);
|
||||
auto old_data = reinterpret_cast<const T *>(inner + 1);
|
||||
auto new_data = reinterpret_cast<T *>(new_array.inner + 1);
|
||||
for (std::size_t i = 0; i < inner->size; ++i) {
|
||||
|
@ -127,30 +127,30 @@ private:
|
|||
for (auto it = b; it < e; ++it) {
|
||||
it->~T();
|
||||
}
|
||||
cbindgen_private::sixtyfps_shared_array_free(
|
||||
cbindgen_private::sixtyfps_shared_vector_free(
|
||||
reinterpret_cast<uint8_t *>(inner),
|
||||
sizeof(SharedArrayHeader) + inner->capacity * sizeof(T),
|
||||
alignof(SharedArrayHeader));
|
||||
sizeof(SharedVectorHeader) + inner->capacity * sizeof(T),
|
||||
alignof(SharedVectorHeader));
|
||||
}
|
||||
}
|
||||
|
||||
static SharedArray with_capacity(std::size_t capacity) {
|
||||
auto mem = cbindgen_private::sixtyfps_shared_array_allocate(
|
||||
sizeof(SharedArrayHeader) + capacity * sizeof(T),
|
||||
alignof(SharedArrayHeader));
|
||||
return SharedArray(new (mem) SharedArrayHeader{ {1}, 0, capacity });
|
||||
static SharedVector with_capacity(std::size_t capacity) {
|
||||
auto mem = cbindgen_private::sixtyfps_shared_vector_allocate(
|
||||
sizeof(SharedVectorHeader) + capacity * sizeof(T),
|
||||
alignof(SharedVectorHeader));
|
||||
return SharedVector(new (mem) SharedVectorHeader{ {1}, 0, capacity });
|
||||
}
|
||||
|
||||
#if !defined(DOXYGEN)
|
||||
// Unfortunately, this cannot be generated by cbindgen because std::atomic is not understood
|
||||
struct SharedArrayHeader {
|
||||
struct SharedVectorHeader {
|
||||
std::atomic<std::intptr_t> refcount;
|
||||
std::size_t size;
|
||||
std::size_t capacity;
|
||||
};
|
||||
static_assert(alignof(T) <= alignof(SharedArrayHeader), "Not yet supported because we would need to add padding");
|
||||
SharedArrayHeader *inner;
|
||||
explicit SharedArray(SharedArrayHeader *inner) : inner(inner) {}
|
||||
static_assert(alignof(T) <= alignof(SharedVectorHeader), "Not yet supported because we would need to add padding");
|
||||
SharedVectorHeader *inner;
|
||||
explicit SharedVector(SharedVectorHeader *inner) : inner(inner) {}
|
||||
#endif
|
||||
};
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue