10#include "collection.hpp"
11#include "collection_adapter.hpp"
12#include "compiler_utils.hpp"
48 template <
typename TElement>
60 template <
typename TContainer>
62 TElement, std::remove_cv_t<std::remove_reference_t<TContainer>>,
63 std::enable_if_t<true>>;
67 storage.borrowed.data =
nullptr;
68 storage.borrowed.num_instances = 0;
76 typename = std::enable_if_t<
88 switch (other.ownership) {
90 storage.borrowed = other.storage.borrowed;
95 new (&storage.vector_owned) std::vector<TElement>(other.storage.vector_owned);
100 assert(
false &&
"unreachable");
126 RR_WITH_MAYBE_UNINITIALIZED_DISABLED(this->
swap(other);)
141 new (&storage.vector_owned) std::vector<TElement>(
data);
153 template <
typename T>
156 sizeof(T) ==
sizeof(TElement),
157 "T & TElement are not binary compatible: Size mismatch."
160 alignof(T) <=
alignof(TElement),
161 "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."
166 batch.storage.borrowed.data =
reinterpret_cast<const TElement*
>(
data);
167 batch.storage.borrowed.num_instances = num_instances;
182 return borrow(
reinterpret_cast<const TElement*
>(
data), num_instances);
192 template <
typename T>
205 new (&batch.storage.vector_owned) std::vector<TElement>(std::move(
data));
213 std::vector<TElement> elements;
214 elements.emplace_back(std::move(
data));
221 std::vector<TElement> elements = {
data};
228 switch (this->ownership) {
230 switch (other.ownership) {
232 std::swap(this->storage.borrowed, other.storage.borrowed);
236 auto this_borrowed_data_old = this->storage.borrowed;
237 new (&this->storage.vector_owned)
238 std::vector<TElement>(std::move(other.storage.vector_owned));
239 other.storage.borrowed = this_borrowed_data_old;
244 assert(
false &&
"unreachable");
250 switch (other.ownership) {
252 auto other_borrowed_data_old = other.storage.borrowed;
253 new (&other.storage.vector_owned)
254 std::vector<TElement>(std::move(this->storage.vector_owned));
255 this->storage.borrowed = other_borrowed_data_old;
260 std::swap(storage.vector_owned, other.storage.vector_owned);
264 assert(
false &&
"unreachable");
270 assert(
false &&
"unreachable");
273 std::swap(ownership, other.ownership);
282 storage.vector_owned.~vector();
286 assert(
false &&
"unreachable");
294 return storage.borrowed.num_instances;
297 return storage.vector_owned.size();
300 assert(
false &&
"unreachable");
309 return storage.borrowed.num_instances == 0;
312 return storage.vector_owned.empty();
315 assert(
false &&
"unreachable");
330 return storage.borrowed.data;
333 return storage.vector_owned.data();
336 assert(
false &&
"unreachable");
354 const TElement*
end()
const {
373 std::vector<TElement> result;
374 result.reserve(
size());
375 result.insert(result.end(),
begin(),
end());
385 std::vector<TElement> result;
386 result.reserve(
size());
387 result.insert(result.end(),
begin(),
end());
392 return std::move(storage.vector_owned);
396 assert(
false &&
"unreachable");
398 return std::vector<TElement>();
406 reinterpret_cast<const uint8_t*
>(
data()),
407 size() *
sizeof(TElement)
412 auto ptr =
reinterpret_cast<const uint8_t*
>(
data());
413 auto num_bytes =
size() *
sizeof(TElement);
415 std::vector<uint8_t>(ptr, ptr + num_bytes)
420 assert(
false &&
"unreachable");
426 template <
typename T>
427 union CollectionStorage {
430 size_t num_instances;
433 std::vector<T> vector_owned;
435 CollectionStorage() {
436 std::memset(
reinterpret_cast<void*
>(
this), 0,
sizeof(CollectionStorage));
439 ~CollectionStorage() {}
443 CollectionStorage<TElement> storage;
460 template <
typename TElement>
472 template <
typename TElement>
480 template <
typename TElement>
486 template <
typename TElement>
495#include "collection_adapter_builtins.hpp"
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:49
const TElement & operator[](size_t i) const
Random read access to the underlying data.
Definition collection.hpp:359
static Collection< TElement > take_ownership(TElement &&data)
Takes ownership of a single element, moving it into the collection.
Definition collection.hpp:211
static Collection< TElement > borrow(const T *data, size_t num_instances=1)
Borrows binary compatible data into the collection from a typed pointer.
Definition collection.hpp:154
static Collection< TElement > take_ownership(std::vector< TElement > &&data)
Takes ownership of a temporary std::vector, moving it into the collection.
Definition collection.hpp:200
bool empty() const
Returns true if the collection is empty.
Definition collection.hpp:306
Collection(Collection< TElement > &&other)
Move constructor.
Definition collection.hpp:115
static Collection borrow(const std::vector< T > &data)
Borrows binary compatible data into the collection from a vector.
Definition collection.hpp:193
Collection()
Creates a new empty collection.
Definition collection.hpp:66
CollectionOwnership get_ownership() const
Returns the data ownership of collection.
Definition collection.hpp:367
const TElement * end() const
TODO(andreas): Return proper iterator.
Definition collection.hpp:354
void operator=(const Collection< TElement > &other)
Copy assignment.
Definition collection.hpp:109
const TElement * begin() const
TODO(andreas): Return proper iterator.
Definition collection.hpp:349
void swap(Collection< TElement > &other)
Swaps the content of this collection with another.
Definition collection.hpp:226
TElement value_type
Type of the elements in the collection.
Definition collection.hpp:54
static Collection< TElement > take_ownership(const TElement &data)
Takes ownership of a single element, copying it into the collection.
Definition collection.hpp:219
size_t size() const
Returns the number of instances in this collection.
Definition collection.hpp:291
void operator=(Collection< TElement > &&other)
Move assignment.
Definition collection.hpp:120
static Collection borrow(const void *data, size_t num_instances=1)
Borrows binary compatible data into the collection from an untyped pointer.
Definition collection.hpp:181
Collection(TContainer &&input)
Construct using a CollectionAdapter for the given input type.
Definition collection.hpp:79
Collection(const Collection< TElement > &other)
Copy constructor.
Definition collection.hpp:87
Collection(std::initializer_list< TElement > data)
Construct from a initializer list of elements that are compatible with TElement.
Definition collection.hpp:137
std::vector< TElement > to_vector() const &
Copies the data into a new std::vector.
Definition collection.hpp:372
Collection< uint8_t > to_uint8() const
Reinterpret this collection as a collection of bytes.
Definition collection.hpp:402
std::vector< TElement > to_vector() &&
Copies the data into a new std::vector.
Definition collection.hpp:382
const TElement * data() const
Returns a raw pointer to the underlying data.
Definition collection.hpp:327
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:22
Collection< TElement > take_ownership(std::vector< TElement > data)
Takes ownership of a temporary std::vector, moving it into the collection.
Definition collection.hpp:481
CollectionOwnership
Type of ownership of a collection's data.
Definition collection.hpp:18
@ 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.
Collection< TElement > borrow(const TElement *data, size_t num_instances=1)
Borrows binary data into a Collection from a pointer.
Definition collection.hpp:461
The rerun::CollectionAdapter trait is responsible for mapping an input argument to a rerun::Collectio...
Definition collection_adapter.hpp:25