Rerun C++ SDK
Loading...
Searching...
No Matches
component_batch.hpp
1#pragma once
2
3#include <memory> // shared_ptr
4#include <optional>
5#include <unordered_map>
6
7#include "collection.hpp"
8#include "component_descriptor.hpp"
9#include "component_type.hpp"
10#include "error.hpp"
11#include "loggable.hpp"
12
13namespace arrow {
14 class Array;
15 class DataType;
16} // namespace arrow
17
18struct rr_component_batch;
19
20namespace rerun {
21 /// Arrow-encoded data of a single batch components for a single entity.
22 ///
23 /// Note that this doesn't own `datatype` and `component_name`.
25 /// Arrow-encoded data of the component instances.
26 std::shared_ptr<arrow::Array> array;
27
28 /// The type of the component instances in array.
30
31 public:
32 /// Creates a new component batch from a collection of component instances.
33 ///
34 /// Automatically registers the component type the first time this type is encountered.
35 template <typename T>
37 return from_loggable(components, Loggable<T>::Descriptor);
38 }
39
40 /// Creates a new component batch from a collection of component instances.
41 ///
42 /// Automatically registers the component type the first time this type is encountered.
43 template <typename T>
45 const rerun::Collection<T>& components, const ComponentDescriptor& descriptor
46 ) {
47 static_assert(
48 rerun::is_loggable<T>,
49 "The given type does not implement the rerun::Loggable trait."
50 );
51
52 // The template is over the `Loggable` itself, but a single `Loggable` might have any number of
53 // descriptors/tags associated with it, therefore a static `ComponentTypeHandle` is not enough,
54 // we need a map.
55 static std::unordered_map<ComponentDescriptorHash, ComponentTypeHandle>
56 comp_types_per_descr;
57
58 ComponentTypeHandle comp_type_handle;
59
60 auto descr_hash = descriptor.hashed();
61
62 auto search = comp_types_per_descr.find(descr_hash);
63 if (search != comp_types_per_descr.end()) {
64 comp_type_handle = search->second;
65 } else {
66 auto comp_type = ComponentType(descriptor, Loggable<T>::arrow_datatype());
67
68 const Result<ComponentTypeHandle> comp_type_handle_result =
69 comp_type.register_component();
70 RR_RETURN_NOT_OK(comp_type_handle_result.error);
71
72 comp_type_handle = comp_type_handle_result.value;
73 comp_types_per_descr.insert({descr_hash, comp_type_handle});
74 }
75
76 /// TODO(#4257) should take a rerun::Collection instead of pointer and size.
77 auto array = Loggable<T>::to_arrow(components.data(), components.size());
78 RR_RETURN_NOT_OK(array.error);
79
80 ComponentBatch component_batch;
81 component_batch.array = std::move(array.value);
82 component_batch.component_type = comp_type_handle;
83 return component_batch;
84 }
85
86 /// Creates a new component batch from a single component instance.
87 ///
88 /// Automatically registers the component type the first time this type is encountered.
89 template <typename T>
90 static Result<ComponentBatch> from_loggable(const T& component) {
91 // Collection adapter will automatically borrow for single elements, but let's do this explicitly, avoiding the extra hoop.
92 const auto collection = Collection<T>::borrow(&component, 1);
93 return from_loggable(collection);
94 }
95
96 /// Creates a new component batch from a single component instance.
97 ///
98 /// Automatically registers the component type the first time this type is encountered.
99 template <typename T>
101 const T& component, const ComponentDescriptor& descriptor
102 ) {
103 // Collection adapter will automatically borrow for single elements, but let's do this explicitly, avoiding the extra hoop.
104 const auto collection = Collection<T>::borrow(&component, 1);
105 return from_loggable(collection, descriptor);
106 }
107
108 /// Creates a new data cell from a single optional component instance.
109 ///
110 /// None is represented as a data cell with 0 instances.
111 ///
112 /// Automatically registers the component type the first time this type is encountered.
113 template <typename T>
114 static Result<ComponentBatch> from_loggable(const std::optional<T>& component) {
115 if (component.has_value()) {
116 return from_loggable(component.value());
117 } else {
119 }
120 }
121
122 /// Creates a new data cell from a single optional component instance.
123 ///
124 /// None is represented as a data cell with 0 instances.
125 ///
126 /// Automatically registers the component type the first time this type is encountered.
127 template <typename T>
129 const std::optional<T>& component, const ComponentDescriptor& descriptor
130 ) {
131 if (component.has_value()) {
132 return from_loggable(component.value(), descriptor);
133 } else {
134 return from_loggable(Collection<T>(), descriptor);
135 }
136 }
137
138 /// Creates a new data cell from an optional collection of component instances.
139 ///
140 /// None is represented as a data cell with 0 instances.
141 ///
142 /// Automatically registers the component type the first time this type is encountered.
143 template <typename T>
145 const std::optional<rerun::Collection<T>>& components
146 ) {
147 if (components.has_value()) {
148 return from_loggable(components.value());
149 } else {
151 }
152 }
153
154 /// Creates a new data cell from an optional collection of component instances.
155 ///
156 /// None is represented as a data cell with 0 instances.
157 ///
158 /// Automatically registers the component type the first time this type is encountered.
159 template <typename T>
161 const std::optional<rerun::Collection<T>>& components,
162 const ComponentDescriptor& descriptor
163 ) {
164 if (components.has_value()) {
165 return from_loggable(components.value(), descriptor);
166 } else {
167 return from_loggable(Collection<T>(), descriptor);
168 }
169 }
170
171 /// To rerun C API component batch.
172 ///
173 /// The resulting `rr_component_batch` keeps the `arrow::Array` alive until it is released.
174 Error to_c_ffi_struct(rr_component_batch& out_component_batch) const;
175 };
176} // 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:23
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:24
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:36
static Result< ComponentBatch > from_loggable(const T &component)
Creates a new component batch from a single component instance.
Definition component_batch.hpp:90
static Result< ComponentBatch > from_loggable(const T &component, const ComponentDescriptor &descriptor)
Creates a new component batch from a single component instance.
Definition component_batch.hpp:100
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:144
Error to_c_ffi_struct(rr_component_batch &out_component_batch) const
To rerun C API component batch.
static Result< ComponentBatch > from_loggable(const rerun::Collection< T > &components, const ComponentDescriptor &descriptor)
Creates a new component batch from a collection of component instances.
Definition component_batch.hpp:44
static Result< ComponentBatch > from_loggable(const std::optional< rerun::Collection< T > > &components, const ComponentDescriptor &descriptor)
Creates a new data cell from an optional collection of component instances.
Definition component_batch.hpp:160
ComponentTypeHandle component_type
The type of the component instances in array.
Definition component_batch.hpp:29
static Result< ComponentBatch > from_loggable(const std::optional< T > &component, const ComponentDescriptor &descriptor)
Creates a new data cell from a single optional component instance.
Definition component_batch.hpp:128
std::shared_ptr< arrow::Array > array
Arrow-encoded data of the component instances.
Definition component_batch.hpp:26
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:114
A ComponentDescriptor fully describes the semantics of a column of data.
Definition component_descriptor.hpp:13
A type of component that can be registered.
Definition component_type.hpp:19
The Loggable trait is used by all built-in implementation of rerun::AsComponents to serialize a colle...
Definition loggable.hpp:11