Rerun C++ SDK
Loading...
Searching...
No Matches
component_batch.hpp
1#pragma once
2
3#include <memory> // shared_ptr
4#include <optional>
5
6#include "collection.hpp"
7#include "component_type.hpp"
8#include "error.hpp"
9#include "loggable.hpp"
10
11namespace arrow {
12 class Array;
13 class DataType;
14} // namespace arrow
15
16struct rr_component_batch;
17
18namespace rerun {
19 /// Arrow-encoded data of a single batch components for a single entity.
20 ///
21 /// Note that this doesn't own `datatype` and `component_name`.
23 /// Arrow-encoded data of the component instances.
24 std::shared_ptr<arrow::Array> array;
25
26 /// The type of the component instances in array.
28
29 public:
30 /// Creates a new component batch from a collection of component instances.
31 ///
32 /// Automatically registers the component type the first time this type is encountered.
33 template <typename T>
35 static_assert(
36 rerun::is_loggable<T>,
37 "The given type does not implement the rerun::Loggable trait."
38 );
39
40 // Register type, only done once per type (but error check happens every time).
44 RR_RETURN_NOT_OK(component_type.error);
45
46 /// TODO(#4257) should take a rerun::Collection instead of pointer and size.
47 auto array = Loggable<T>::to_arrow(components.data(), components.size());
48 RR_RETURN_NOT_OK(array.error);
49
50 ComponentBatch component_batch;
51 component_batch.array = std::move(array.value);
52 component_batch.component_type = component_type.value;
53 return component_batch;
54 }
55
56 /// Creates a new component batch from a single component instance.
57 ///
58 /// Automatically registers the component type the first time this type is encountered.
59 template <typename T>
60 static Result<ComponentBatch> from_loggable(const T& component) {
61 // Collection adapter will automatically borrow for single elements, but let's do this explicitly, avoiding the extra hoop.
62 const auto collection = Collection<T>::borrow(&component, 1);
63 return from_loggable(collection);
64 }
65
66 /// Creates a new data cell from a single optional component instance.
67 ///
68 /// None is represented as a data cell with 0 instances.
69 ///
70 /// Automatically registers the component type the first time this type is encountered.
71 template <typename T>
72 static Result<ComponentBatch> from_loggable(const std::optional<T>& component) {
73 if (component.has_value()) {
74 return from_loggable(component.value());
75 } else {
77 }
78 }
79
80 /// Creates a new data cell from an optional collection of component instances.
81 ///
82 /// None is represented as a data cell with 0 instances.
83 ///
84 /// Automatically registers the component type the first time this type is encountered.
85 template <typename T>
87 const std::optional<rerun::Collection<T>>& components
88 ) {
89 if (components.has_value()) {
90 return from_loggable(components.value());
91 } else {
93 }
94 }
95
96 /// To rerun C API component batch.
97 ///
98 /// The resulting `rr_component_batch` keeps the `arrow::Array` alive until it is released.
99 Error to_c_ffi_struct(rr_component_batch& out_component_batch) const;
100 };
101} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:49
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
size_t size() const
Returns the number of instances in this collection.
Definition collection.hpp:291
const TElement * data() const
Returns a raw pointer to the underlying data.
Definition collection.hpp:327
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:95
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:22
uint32_t ComponentTypeHandle
Handle to a registered component types.
Definition component_type.hpp:14
Arrow-encoded data of a single batch components for a single entity.
Definition component_batch.hpp:22
static Result< ComponentBatch > from_loggable(const rerun::Collection< T > &components)
Creates a new component batch from a collection of component instances.
Definition component_batch.hpp:34
static Result< ComponentBatch > from_loggable(const T &component)
Creates a new component batch from a single component instance.
Definition component_batch.hpp:60
static Result< ComponentBatch > from_loggable(const std::optional< rerun::Collection< T > > &components)
Creates a new data cell from an optional collection of component instances.
Definition component_batch.hpp:86
Error to_c_ffi_struct(rr_component_batch &out_component_batch) const
To rerun C API component batch.
ComponentTypeHandle component_type
The type of the component instances in array.
Definition component_batch.hpp:27
std::shared_ptr< arrow::Array > array
Arrow-encoded data of the component instances.
Definition component_batch.hpp:24
static Result< ComponentBatch > from_loggable(const std::optional< T > &component)
Creates a new data cell from a single optional component instance.
Definition component_batch.hpp:72
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.
The Loggable trait is used by all built-in implementation of rerun::AsComponents to serialize a colle...
Definition loggable.hpp:11