Rerun C++ SDK
Loading...
Searching...
No Matches
data_cell.hpp
1#pragma once
2
3#include <memory> // shared_ptr
4
5#include "collection.hpp"
6#include "component_type.hpp"
7#include "error.hpp"
8#include "loggable.hpp"
9
10namespace arrow {
11 class Array;
12 class DataType;
13} // namespace arrow
14
15struct rr_data_cell;
16
17namespace rerun {
18 /// Arrow-encoded data of a single batch components for a single entity.
19 ///
20 /// Note that the DataCell doesn't own `datatype` and `component_name`.
21 struct DataCell {
22 /// How many instances of the component were serialized in this data cell.
23 ///
24 /// TODO(andreas): Just like in Rust, make this part of `AsComponents`.
25 /// This will requiring inlining some things on RecordingStream and have some refactor ripples.
26 /// But it's worth keeping the language bindings more similar!
28
29 /// Arrow-encoded data of the component instances.
30 std::shared_ptr<arrow::Array> array;
31
32 /// The type of the component instances in array.
34
35 public:
36 /// Creates a new data cell from a collection of component instances.
37 ///
38 /// Automatically registers the component type the first time this type is encountered.
39 template <typename T>
41 static_assert(
42 rerun::is_loggable<T>,
43 "The given type does not implement the rerun::Loggable trait."
44 );
45
46 // Register type, only done once per type (but error check happens every time).
50 RR_RETURN_NOT_OK(component_type.error);
51
52 /// TODO(#4257) should take a rerun::Collection instead of pointer and size.
53 auto array = Loggable<T>::to_arrow(components.data(), components.size());
54 RR_RETURN_NOT_OK(array.error);
55
56 DataCell cell;
57 cell.num_instances = components.size();
58 cell.array = std::move(array.value);
59 cell.component_type = component_type.value;
60 return cell;
61 }
62
63 /// Creates a new data cell from a single component instance.
64 ///
65 /// Automatically registers the component type the first time this type is encountered.
66 template <typename T>
67 static Result<DataCell> from_loggable(const T& component) {
68 // Collection adapter will automatically borrow for single elements, but let's do this explicitly, avoiding the extra hoop.
69 const auto collection = Collection<T>::borrow(&component, 1);
70 return from_loggable(collection);
71 }
72
73 /// To rerun C API data cell.
74 ///
75 /// The resulting `rr_data_cell` keeps the `arrow::Array` alive until it is released.
76 Error to_c_ffi_struct(rr_data_cell& out_cell) const;
77 };
78} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:47
static Collection< TElement > borrow(const T *data, size_t num_instances)
Borrows binary compatible data into the collection.
Definition collection.hpp:149
size_t size() const
Returns the number of instances in this collection.
Definition collection.hpp:254
const TElement * data() const
Returns a raw pointer to the underlying data.
Definition collection.hpp:282
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:87
A class for representing either a usable value, or an error.
Definition result.hpp:14
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:20
uint32_t ComponentTypeHandle
Handle to a registered component types.
Definition component_type.hpp:14
A type of component that can be registered.
Definition component_type.hpp:19
Result< ComponentTypeHandle > register_component() const
Registers a component type with the SDK.
Arrow-encoded data of a single batch components for a single entity.
Definition data_cell.hpp:21
Error to_c_ffi_struct(rr_data_cell &out_cell) const
To rerun C API data cell.
size_t num_instances
How many instances of the component were serialized in this data cell.
Definition data_cell.hpp:27
ComponentTypeHandle component_type
The type of the component instances in array.
Definition data_cell.hpp:33
static Result< DataCell > from_loggable(const rerun::Collection< T > &components)
Creates a new data cell from a collection of component instances.
Definition data_cell.hpp:40
std::shared_ptr< arrow::Array > array
Arrow-encoded data of the component instances.
Definition data_cell.hpp:30
static Result< DataCell > from_loggable(const T &component)
Creates a new data cell from a single component instance.
Definition data_cell.hpp:67
The Loggable trait is used by all built-in implementation of rerun::AsComponents to serialize a colle...
Definition loggable.hpp:11