9#include "collection.hpp"
10#include "collection_adapter.hpp"
11#include "compiler_utils.hpp"
46 template <
typename TElement>
58 template <
typename TContainer>
60 TElement, std::remove_cv_t<std::remove_reference_t<TContainer>>,
61 std::enable_if_t<true>>;
65 storage.borrowed.data =
nullptr;
66 storage.borrowed.num_instances = 0;
74 typename = std::enable_if_t<
86 switch (other.ownership) {
88 storage.borrowed = other.storage.borrowed;
93 new (&storage.vector_owned) std::vector<TElement>(other.storage.vector_owned);
121 RERUN_WITH_MAYBE_UNINITIALIZED_DISABLED(this->
swap(other);)
136 new (&storage.vector_owned) std::vector<TElement>(
data);
148 template <
typename T>
151 sizeof(T) ==
sizeof(TElement),
152 "T & TElement are not binary compatible: Size mismatch."
155 alignof(T) <=
alignof(TElement),
156 "T & TElement are not binary compatible: TElement has a higher alignment requirement than T. This implies that pointers to T may not have the alignment needed to access TElement."
161 batch.storage.borrowed.data =
reinterpret_cast<const TElement*
>(
data);
162 batch.storage.borrowed.num_instances = num_instances;
177 return borrow(
reinterpret_cast<const TElement*
>(
data), num_instances);
188 new (&batch.storage.vector_owned) std::vector<TElement>(std::move(
data));
196 std::vector<TElement> elements;
197 elements.emplace_back(std::move(
data));
204 switch (this->ownership) {
206 switch (other.ownership) {
208 std::swap(this->storage.borrowed, other.storage.borrowed);
212 auto this_borrowed_data_old = this->storage.borrowed;
213 new (&this->storage.vector_owned)
214 std::vector<TElement>(std::move(other.storage.vector_owned));
215 other.storage.borrowed = this_borrowed_data_old;
223 switch (other.ownership) {
225 auto other_borrowed_data_old = other.storage.borrowed;
226 new (&other.storage.vector_owned)
227 std::vector<TElement>(std::move(this->storage.vector_owned));
228 this->storage.borrowed = other_borrowed_data_old;
233 std::swap(storage.vector_owned, other.storage.vector_owned);
240 std::swap(ownership, other.ownership);
248 storage.vector_owned.~vector();
257 return storage.borrowed.num_instances;
259 return storage.vector_owned.size();
268 return storage.borrowed.num_instances == 0;
270 return storage.vector_owned.empty();
285 return storage.borrowed.data;
287 return storage.vector_owned.data();
305 const TElement*
end()
const {
325 std::vector<TElement> result;
326 result.reserve(
size());
327 result.insert(result.end(),
begin(),
end());
332 template <
typename T>
333 union CollectionStorage {
336 size_t num_instances;
339 std::vector<T> vector_owned;
341 CollectionStorage() {
342 std::memset(
reinterpret_cast<void*
>(
this), 0,
sizeof(CollectionStorage));
345 ~CollectionStorage() {}
349 CollectionStorage<TElement> storage;
356#include "collection_adapter_builtins.hpp"
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:47
const TElement & operator[](size_t i) const
Random read access to the underlying data.
Definition collection.hpp:310
static Collection< TElement > take_ownership(TElement &&data)
Takes ownership of a single element, moving it into the collection.
Definition collection.hpp:194
static Collection< TElement > take_ownership(std::vector< TElement > &&data)
Takes ownership of a temporary std::vector, moving it into the collection.
Definition collection.hpp:183
bool empty() const
Returns true if the collection is empty.
Definition collection.hpp:265
Collection(Collection< TElement > &&other)
Move constructor.
Definition collection.hpp:110
Collection()
Creates a new empty collection.
Definition collection.hpp:64
CollectionOwnership get_ownership() const
Returns the data ownership of collection.
Definition collection.hpp:318
const TElement * end() const
TODO(andreas): Return proper iterator.
Definition collection.hpp:305
void operator=(const Collection< TElement > &other)
Copy assignment.
Definition collection.hpp:104
static Collection borrow(const void *data, size_t num_instances)
Borrows binary compatible data into the collection.
Definition collection.hpp:176
const TElement * begin() const
TODO(andreas): Return proper iterator.
Definition collection.hpp:300
static Collection< TElement > borrow(const T *data, size_t num_instances)
Borrows binary compatible data into the collection.
Definition collection.hpp:149
void swap(Collection< TElement > &other)
Swaps the content of this collection with another.
Definition collection.hpp:202
TElement value_type
Type of the elements in the collection.
Definition collection.hpp:52
size_t size() const
Returns the number of instances in this collection.
Definition collection.hpp:254
void operator=(Collection< TElement > &&other)
Move assignment.
Definition collection.hpp:115
Collection(TContainer &&input)
Construct using a CollectionAdapter for the given input type.
Definition collection.hpp:77
Collection(const Collection< TElement > &other)
Copy constructor.
Definition collection.hpp:85
Collection(std::initializer_list< TElement > data)
Construct from a initializer list of elements that are compatible with TElement.
Definition collection.hpp:132
std::vector< TElement > to_vector() const
Copies the data into a new std::vector.
Definition collection.hpp:323
const TElement * data() const
Returns a raw pointer to the underlying data.
Definition collection.hpp:282
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:20
CollectionOwnership
Type of ownership of a collection's data.
Definition collection.hpp:17
@ Borrowed
The collection does not own the data and only has a pointer and a size.
@ VectorOwned
The collection batch owns the data via an std::vector.
The rerun::CollectionAdapter trait is responsible for mapping an input argument to a rerun::Collectio...
Definition collection_adapter.hpp:25