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);
123 RR_WITH_MAYBE_UNINITIALIZED_DISABLED(this->
swap(other);)
138 new (&storage.vector_owned) std::vector<TElement>(
data);
150 template <
typename T>
153 sizeof(T) ==
sizeof(TElement),
154 "T & TElement are not binary compatible: Size mismatch."
157 alignof(T) <=
alignof(TElement),
158 "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."
163 batch.storage.borrowed.data =
reinterpret_cast<const TElement*
>(
data);
164 batch.storage.borrowed.num_instances = num_instances;
179 return borrow(
reinterpret_cast<const TElement*
>(
data), num_instances);
189 template <
typename T>
202 new (&batch.storage.vector_owned) std::vector<TElement>(std::move(
data));
210 std::vector<TElement> elements;
211 elements.emplace_back(std::move(
data));
218 std::vector<TElement> elements = {
data};
225 switch (this->ownership) {
227 switch (other.ownership) {
229 std::swap(this->storage.borrowed, other.storage.borrowed);
233 auto this_borrowed_data_old = this->storage.borrowed;
234 new (&this->storage.vector_owned)
235 std::vector<TElement>(std::move(other.storage.vector_owned));
236 other.storage.borrowed = this_borrowed_data_old;
244 switch (other.ownership) {
246 auto other_borrowed_data_old = other.storage.borrowed;
247 new (&other.storage.vector_owned)
248 std::vector<TElement>(std::move(this->storage.vector_owned));
249 this->storage.borrowed = other_borrowed_data_old;
254 std::swap(storage.vector_owned, other.storage.vector_owned);
261 std::swap(ownership, other.ownership);
269 storage.vector_owned.~vector();
278 return storage.borrowed.num_instances;
280 return storage.vector_owned.size();
289 return storage.borrowed.num_instances == 0;
291 return storage.vector_owned.empty();
306 return storage.borrowed.data;
308 return storage.vector_owned.data();
326 const TElement*
end()
const {
345 std::vector<TElement> result;
346 result.reserve(
size());
347 result.insert(result.end(),
begin(),
end());
357 std::vector<TElement> result;
358 result.reserve(
size());
359 result.insert(result.end(),
begin(),
end());
363 return std::move(storage.vector_owned);
366 return std::vector<TElement>();
374 reinterpret_cast<const uint8_t*
>(
data()),
375 size() *
sizeof(TElement)
379 auto ptr =
reinterpret_cast<const uint8_t*
>(
data());
380 auto num_bytes =
size() *
sizeof(TElement);
382 std::vector<uint8_t>(ptr, ptr + num_bytes)
390 template <
typename T>
391 union CollectionStorage {
394 size_t num_instances;
397 std::vector<T> vector_owned;
399 CollectionStorage() {
400 std::memset(
reinterpret_cast<void*
>(
this), 0,
sizeof(CollectionStorage));
403 ~CollectionStorage() {}
407 CollectionStorage<TElement> storage;
424 template <
typename TElement>
436 template <
typename TElement>
444 template <
typename TElement>
450 template <
typename TElement>
459#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:331
static Collection< TElement > take_ownership(TElement &&data)
Takes ownership of a single element, moving it into the collection.
Definition collection.hpp:208
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:151
static Collection< TElement > take_ownership(std::vector< TElement > &&data)
Takes ownership of a temporary std::vector, moving it into the collection.
Definition collection.hpp:197
bool empty() const
Returns true if the collection is empty.
Definition collection.hpp:286
Collection(Collection< TElement > &&other)
Move constructor.
Definition collection.hpp:112
static Collection borrow(const std::vector< T > &data)
Borrows binary compatible data into the collection from a vector.
Definition collection.hpp:190
Collection()
Creates a new empty collection.
Definition collection.hpp:66
CollectionOwnership get_ownership() const
Returns the data ownership of collection.
Definition collection.hpp:339
const TElement * end() const
TODO(andreas): Return proper iterator.
Definition collection.hpp:326
void operator=(const Collection< TElement > &other)
Copy assignment.
Definition collection.hpp:106
const TElement * begin() const
TODO(andreas): Return proper iterator.
Definition collection.hpp:321
void swap(Collection< TElement > &other)
Swaps the content of this collection with another.
Definition collection.hpp:223
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:216
size_t size() const
Returns the number of instances in this collection.
Definition collection.hpp:275
void operator=(Collection< TElement > &&other)
Move assignment.
Definition collection.hpp:117
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:178
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:134
std::vector< TElement > to_vector() const &
Copies the data into a new std::vector.
Definition collection.hpp:344
Collection< uint8_t > to_uint8() const
Reinterpret this collection as a collection of bytes.
Definition collection.hpp:370
std::vector< TElement > to_vector() &&
Copies the data into a new std::vector.
Definition collection.hpp:354
const TElement * data() const
Returns a raw pointer to the underlying data.
Definition collection.hpp:303
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:445
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:425
The rerun::CollectionAdapter trait is responsible for mapping an input argument to a rerun::Collectio...
Definition collection_adapter.hpp:25