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");
111 if (
this == &other) {
130 RR_WITH_MAYBE_UNINITIALIZED_DISABLED(this->
swap(other);)
145 new (&storage.vector_owned) std::vector<TElement>(
data);
157 template <
typename T>
160 sizeof(T) ==
sizeof(TElement),
161 "T & TElement are not binary compatible: Size mismatch."
164 alignof(T) <=
alignof(TElement),
165 "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."
170 batch.storage.borrowed.data =
reinterpret_cast<const TElement*
>(
data);
171 batch.storage.borrowed.num_instances = num_instances;
186 return borrow(
reinterpret_cast<const TElement*
>(
data), num_instances);
196 template <
typename T>
209 new (&batch.storage.vector_owned) std::vector<TElement>(std::move(
data));
217 std::vector<TElement> elements;
218 elements.emplace_back(std::move(
data));
225 std::vector<TElement> elements = {
data};
232 switch (this->ownership) {
234 switch (other.ownership) {
236 std::swap(this->storage.borrowed, other.storage.borrowed);
240 auto this_borrowed_data_old = this->storage.borrowed;
241 new (&this->storage.vector_owned)
242 std::vector<TElement>(std::move(other.storage.vector_owned));
243 other.storage.borrowed = this_borrowed_data_old;
248 assert(
false &&
"unreachable");
254 switch (other.ownership) {
256 auto other_borrowed_data_old = other.storage.borrowed;
257 new (&other.storage.vector_owned)
258 std::vector<TElement>(std::move(this->storage.vector_owned));
259 this->storage.borrowed = other_borrowed_data_old;
264 std::swap(storage.vector_owned, other.storage.vector_owned);
268 assert(
false &&
"unreachable");
274 assert(
false &&
"unreachable");
277 std::swap(ownership, other.ownership);
286 storage.vector_owned.~vector();
290 assert(
false &&
"unreachable");
298 return storage.borrowed.num_instances;
301 return storage.vector_owned.size();
304 assert(
false &&
"unreachable");
324 return storage.borrowed.data;
327 return storage.vector_owned.data();
330 assert(
false &&
"unreachable");
348 const TElement*
end()
const {
367 return std::vector<TElement>(
begin(),
end());
376 std::vector<TElement> result;
377 result.reserve(
size());
378 result.insert(result.end(),
begin(),
end());
384 return std::move(storage.vector_owned);
388 assert(
false &&
"unreachable");
390 return std::vector<TElement>();
398 reinterpret_cast<const uint8_t*
>(
data()),
399 size() *
sizeof(TElement)
404 auto ptr =
reinterpret_cast<const uint8_t*
>(
data());
405 auto num_bytes =
size() *
sizeof(TElement);
407 std::vector<uint8_t>(ptr, ptr + num_bytes)
412 assert(
false &&
"unreachable");
418 template <
typename T>
419 union CollectionStorage {
422 size_t num_instances;
425 std::vector<T> vector_owned;
427 CollectionStorage() {
428 std::memset(
reinterpret_cast<void*
>(
this), 0,
sizeof(CollectionStorage));
431 ~CollectionStorage() {}
435 CollectionStorage<TElement> storage;
452 template <
typename TElement>
464 template <
typename TElement>
472 template <
typename TElement>
478 template <
typename TElement>
487#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:353
static Collection< TElement > take_ownership(TElement &&data)
Takes ownership of a single element, moving it into the collection.
Definition collection.hpp:215
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:158
static Collection< TElement > take_ownership(std::vector< TElement > &&data)
Takes ownership of a temporary std::vector, moving it into the collection.
Definition collection.hpp:204
bool empty() const
Returns true if the collection is empty.
Definition collection.hpp:310
Collection(Collection< TElement > &&other)
Move constructor.
Definition collection.hpp:119
static Collection borrow(const std::vector< T > &data)
Borrows binary compatible data into the collection from a vector.
Definition collection.hpp:197
Collection()
Creates a new empty collection.
Definition collection.hpp:66
CollectionOwnership get_ownership() const
Returns the data ownership of collection.
Definition collection.hpp:361
const TElement * end() const
TODO(andreas): Return proper iterator.
Definition collection.hpp:348
void operator=(const Collection< TElement > &other)
Copy assignment.
Definition collection.hpp:109
const TElement * begin() const
TODO(andreas): Return proper iterator.
Definition collection.hpp:343
void swap(Collection< TElement > &other)
Swaps the content of this collection with another.
Definition collection.hpp:230
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:223
size_t size() const
Returns the number of instances in this collection.
Definition collection.hpp:295
void operator=(Collection< TElement > &&other)
Move assignment.
Definition collection.hpp:124
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:185
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:141
std::vector< TElement > to_vector() const &
Copies the data into a new std::vector.
Definition collection.hpp:366
Collection< uint8_t > to_uint8() const
Reinterpret this collection as a collection of bytes.
Definition collection.hpp:394
std::vector< TElement > to_vector() &&
Copies the data into a new std::vector.
Definition collection.hpp:373
const TElement * data() const
Returns a raw pointer to the underlying data.
Definition collection.hpp:321
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:23
Collection< TElement > take_ownership(std::vector< TElement > data)
Takes ownership of a temporary std::vector, moving it into the collection.
Definition collection.hpp:473
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:453
The rerun::CollectionAdapter trait is responsible for mapping an input argument to a rerun::Collectio...
Definition collection_adapter.hpp:25