Rerun C++ SDK
Loading...
Searching...
No Matches
tensor_buffer.hpp
1// DO NOT EDIT! This file was auto-generated by crates/re_types_builder/src/codegen/cpp/mod.rs
2// Based on "crates/re_types/definitions/rerun/datatypes/tensor_buffer.fbs".
3
4#pragma once
5
6#include "../collection.hpp"
7#include "../half.hpp"
8#include "../result.hpp"
9#include "../type_traits.hpp"
10
11#include <cstdint>
12#include <cstring>
13#include <memory>
14#include <new>
15#include <utility>
16
17namespace arrow {
18 class Array;
19 class DataType;
20 class DenseUnionBuilder;
21} // namespace arrow
22
23namespace rerun::datatypes {
24 namespace detail {
25 /// \private
26 enum class TensorBufferTag : uint8_t {
27 /// Having a special empty state makes it possible to implement move-semantics. We need to be able to leave the object in a state which we can run the destructor on.
28 None = 0,
29 U8,
30 U16,
31 U32,
32 U64,
33 I8,
34 I16,
35 I32,
36 I64,
37 F16,
38 F32,
39 F64,
40 JPEG,
41 NV12,
42 };
43
44 /// \private
45 union TensorBufferData {
47
49
51
53
55
57
59
61
63
65
67
69
71
72 TensorBufferData() {
73 std::memset(reinterpret_cast<void*>(this), 0, sizeof(TensorBufferData));
74 }
75
76 ~TensorBufferData() {}
77
78 void swap(TensorBufferData& other) noexcept {
79 // This bitwise swap would fail for self-referential types, but we don't have any of those.
80 char temp[sizeof(TensorBufferData)];
81 void* otherbytes = reinterpret_cast<void*>(&other);
82 void* thisbytes = reinterpret_cast<void*>(this);
83 std::memcpy(temp, thisbytes, sizeof(TensorBufferData));
84 std::memcpy(thisbytes, otherbytes, sizeof(TensorBufferData));
85 std::memcpy(otherbytes, temp, sizeof(TensorBufferData));
86 }
87 };
88 } // namespace detail
89
90 /// **Datatype**: The underlying storage for a `Tensor`.
91 ///
92 /// Tensor elements are stored in a contiguous buffer of a single type.
93 struct TensorBuffer {
94 TensorBuffer() : _tag(detail::TensorBufferTag::None) {}
95
96 /// Copy constructor
97 TensorBuffer(const TensorBuffer& other) : _tag(other._tag) {
98 switch (other._tag) {
99 case detail::TensorBufferTag::U8: {
100 using TypeAlias = rerun::Collection<uint8_t>;
101 new (&_data.u8) TypeAlias(other._data.u8);
102 } break;
103 case detail::TensorBufferTag::U16: {
104 using TypeAlias = rerun::Collection<uint16_t>;
105 new (&_data.u16) TypeAlias(other._data.u16);
106 } break;
107 case detail::TensorBufferTag::U32: {
108 using TypeAlias = rerun::Collection<uint32_t>;
109 new (&_data.u32) TypeAlias(other._data.u32);
110 } break;
111 case detail::TensorBufferTag::U64: {
112 using TypeAlias = rerun::Collection<uint64_t>;
113 new (&_data.u64) TypeAlias(other._data.u64);
114 } break;
115 case detail::TensorBufferTag::I8: {
116 using TypeAlias = rerun::Collection<int8_t>;
117 new (&_data.i8) TypeAlias(other._data.i8);
118 } break;
119 case detail::TensorBufferTag::I16: {
120 using TypeAlias = rerun::Collection<int16_t>;
121 new (&_data.i16) TypeAlias(other._data.i16);
122 } break;
123 case detail::TensorBufferTag::I32: {
124 using TypeAlias = rerun::Collection<int32_t>;
125 new (&_data.i32) TypeAlias(other._data.i32);
126 } break;
127 case detail::TensorBufferTag::I64: {
128 using TypeAlias = rerun::Collection<int64_t>;
129 new (&_data.i64) TypeAlias(other._data.i64);
130 } break;
131 case detail::TensorBufferTag::F16: {
132 using TypeAlias = rerun::Collection<rerun::half>;
133 new (&_data.f16) TypeAlias(other._data.f16);
134 } break;
135 case detail::TensorBufferTag::F32: {
136 using TypeAlias = rerun::Collection<float>;
137 new (&_data.f32) TypeAlias(other._data.f32);
138 } break;
139 case detail::TensorBufferTag::F64: {
140 using TypeAlias = rerun::Collection<double>;
141 new (&_data.f64) TypeAlias(other._data.f64);
142 } break;
143 case detail::TensorBufferTag::JPEG: {
144 using TypeAlias = rerun::Collection<uint8_t>;
145 new (&_data.jpeg) TypeAlias(other._data.jpeg);
146 } break;
147 case detail::TensorBufferTag::NV12: {
148 using TypeAlias = rerun::Collection<uint8_t>;
149 new (&_data.nv12) TypeAlias(other._data.nv12);
150 } break;
151 case detail::TensorBufferTag::None: {
152 } break;
153 }
154 }
155
156 TensorBuffer& operator=(const TensorBuffer& other) noexcept {
157 TensorBuffer tmp(other);
158 this->swap(tmp);
159 return *this;
160 }
161
162 TensorBuffer(TensorBuffer&& other) noexcept : TensorBuffer() {
163 this->swap(other);
164 }
165
166 TensorBuffer& operator=(TensorBuffer&& other) noexcept {
167 this->swap(other);
168 return *this;
169 }
170
171 ~TensorBuffer() {
172 switch (this->_tag) {
173 case detail::TensorBufferTag::None: {
174 // Nothing to destroy
175 } break;
176 case detail::TensorBufferTag::U8: {
177 using TypeAlias = rerun::Collection<uint8_t>;
178 _data.u8.~TypeAlias();
179 } break;
180 case detail::TensorBufferTag::U16: {
181 using TypeAlias = rerun::Collection<uint16_t>;
182 _data.u16.~TypeAlias();
183 } break;
184 case detail::TensorBufferTag::U32: {
185 using TypeAlias = rerun::Collection<uint32_t>;
186 _data.u32.~TypeAlias();
187 } break;
188 case detail::TensorBufferTag::U64: {
189 using TypeAlias = rerun::Collection<uint64_t>;
190 _data.u64.~TypeAlias();
191 } break;
192 case detail::TensorBufferTag::I8: {
193 using TypeAlias = rerun::Collection<int8_t>;
194 _data.i8.~TypeAlias();
195 } break;
196 case detail::TensorBufferTag::I16: {
197 using TypeAlias = rerun::Collection<int16_t>;
198 _data.i16.~TypeAlias();
199 } break;
200 case detail::TensorBufferTag::I32: {
201 using TypeAlias = rerun::Collection<int32_t>;
202 _data.i32.~TypeAlias();
203 } break;
204 case detail::TensorBufferTag::I64: {
205 using TypeAlias = rerun::Collection<int64_t>;
206 _data.i64.~TypeAlias();
207 } break;
208 case detail::TensorBufferTag::F16: {
209 using TypeAlias = rerun::Collection<rerun::half>;
210 _data.f16.~TypeAlias();
211 } break;
212 case detail::TensorBufferTag::F32: {
213 using TypeAlias = rerun::Collection<float>;
214 _data.f32.~TypeAlias();
215 } break;
216 case detail::TensorBufferTag::F64: {
217 using TypeAlias = rerun::Collection<double>;
218 _data.f64.~TypeAlias();
219 } break;
220 case detail::TensorBufferTag::JPEG: {
221 using TypeAlias = rerun::Collection<uint8_t>;
222 _data.jpeg.~TypeAlias();
223 } break;
224 case detail::TensorBufferTag::NV12: {
225 using TypeAlias = rerun::Collection<uint8_t>;
226 _data.nv12.~TypeAlias();
227 } break;
228 }
229 }
230
231 public:
232 // Extensions to generated type defined in 'tensor_buffer_ext.cpp'
233
234 /// Construct a `TensorBuffer` from a `Collection<uint8_t>`.
236
237 /// Construct a `TensorBuffer` from a `Collection<uint16_t>`.
239
240 /// Construct a `TensorBuffer` from a `Collection<uint32_t>`.
242
243 /// Construct a `TensorBuffer` from a `Collection<uint64_t>`.
245
246 /// Construct a `TensorBuffer` from a `Collection<int8_t>`.
248
249 /// Construct a `TensorBuffer` from a `Collection<int16_t>`.
251
252 /// Construct a `TensorBuffer` from a `Collection<int32_t>`.
254
255 /// Construct a `TensorBuffer` from a `Collection<int64_t>`.
257
258 /// Construct a `TensorBuffer` from a `Collection<half>`.
260 : TensorBuffer(TensorBuffer::f16(std::move(f16))) {}
261
262 /// Construct a `TensorBuffer` from a `Collection<float>`.
264
265 /// Construct a `TensorBuffer` from a `Collection<double>`.
267
268 /// Construct a `TensorBuffer` from any container type that has a `value_type` member and for which a
269 /// `rerun::ContainerAdapter` exists.
270 ///
271 /// This constructor assumes the type of tensor buffer you want to use is defined by `TContainer::value_type`
272 /// and then forwards the argument as-is to the appropriate `rerun::Container` constructor.
273 /// \see rerun::ContainerAdapter, rerun::Container
274 template <typename TContainer, typename value_type = traits::value_type_of_t<TContainer>>
275 TensorBuffer(TContainer&& container)
276 : TensorBuffer(Collection<value_type>(std::forward<TContainer>(container))) {}
277
278 /// Number of elements in the buffer.
279 ///
280 /// You may NOT call this for JPEG buffers.
281 size_t num_elems() const;
282
283 void swap(TensorBuffer& other) noexcept {
284 std::swap(this->_tag, other._tag);
285 this->_data.swap(other._data);
286 }
287
289 TensorBuffer self;
290 self._tag = detail::TensorBufferTag::U8;
291 new (&self._data.u8) rerun::Collection<uint8_t>(std::move(u8));
292 return self;
293 }
294
296 TensorBuffer self;
297 self._tag = detail::TensorBufferTag::U16;
298 new (&self._data.u16) rerun::Collection<uint16_t>(std::move(u16));
299 return self;
300 }
301
302 static TensorBuffer u32(rerun::Collection<uint32_t> u32) {
303 TensorBuffer self;
304 self._tag = detail::TensorBufferTag::U32;
305 new (&self._data.u32) rerun::Collection<uint32_t>(std::move(u32));
306 return self;
307 }
308
309 static TensorBuffer u64(rerun::Collection<uint64_t> u64) {
310 TensorBuffer self;
311 self._tag = detail::TensorBufferTag::U64;
312 new (&self._data.u64) rerun::Collection<uint64_t>(std::move(u64));
313 return self;
314 }
315
316 static TensorBuffer i8(rerun::Collection<int8_t> i8) {
317 TensorBuffer self;
318 self._tag = detail::TensorBufferTag::I8;
319 new (&self._data.i8) rerun::Collection<int8_t>(std::move(i8));
320 return self;
321 }
322
323 static TensorBuffer i16(rerun::Collection<int16_t> i16) {
324 TensorBuffer self;
325 self._tag = detail::TensorBufferTag::I16;
326 new (&self._data.i16) rerun::Collection<int16_t>(std::move(i16));
327 return self;
328 }
329
330 static TensorBuffer i32(rerun::Collection<int32_t> i32) {
331 TensorBuffer self;
332 self._tag = detail::TensorBufferTag::I32;
333 new (&self._data.i32) rerun::Collection<int32_t>(std::move(i32));
334 return self;
335 }
336
337 static TensorBuffer i64(rerun::Collection<int64_t> i64) {
338 TensorBuffer self;
339 self._tag = detail::TensorBufferTag::I64;
340 new (&self._data.i64) rerun::Collection<int64_t>(std::move(i64));
341 return self;
342 }
343
344 static TensorBuffer f16(rerun::Collection<rerun::half> f16) {
345 TensorBuffer self;
346 self._tag = detail::TensorBufferTag::F16;
347 new (&self._data.f16) rerun::Collection<rerun::half>(std::move(f16));
348 return self;
349 }
350
351 static TensorBuffer f32(rerun::Collection<float> f32) {
352 TensorBuffer self;
353 self._tag = detail::TensorBufferTag::F32;
354 new (&self._data.f32) rerun::Collection<float>(std::move(f32));
355 return self;
356 }
357
358 static TensorBuffer f64(rerun::Collection<double> f64) {
359 TensorBuffer self;
360 self._tag = detail::TensorBufferTag::F64;
361 new (&self._data.f64) rerun::Collection<double>(std::move(f64));
362 return self;
363 }
364
365 static TensorBuffer jpeg(rerun::Collection<uint8_t> jpeg) {
366 TensorBuffer self;
367 self._tag = detail::TensorBufferTag::JPEG;
368 new (&self._data.jpeg) rerun::Collection<uint8_t>(std::move(jpeg));
369 return self;
370 }
371
372 static TensorBuffer nv12(rerun::Collection<uint8_t> nv12) {
373 TensorBuffer self;
374 self._tag = detail::TensorBufferTag::NV12;
375 new (&self._data.nv12) rerun::Collection<uint8_t>(std::move(nv12));
376 return self;
377 }
378
379 /// Return a pointer to u8 if the union is in that state, otherwise `nullptr`.
381 if (_tag == detail::TensorBufferTag::U8) {
382 return &_data.u8;
383 } else {
384 return nullptr;
385 }
386 }
387
388 /// Return a pointer to u16 if the union is in that state, otherwise `nullptr`.
390 if (_tag == detail::TensorBufferTag::U16) {
391 return &_data.u16;
392 } else {
393 return nullptr;
394 }
395 }
396
397 /// Return a pointer to u32 if the union is in that state, otherwise `nullptr`.
399 if (_tag == detail::TensorBufferTag::U32) {
400 return &_data.u32;
401 } else {
402 return nullptr;
403 }
404 }
405
406 /// Return a pointer to u64 if the union is in that state, otherwise `nullptr`.
408 if (_tag == detail::TensorBufferTag::U64) {
409 return &_data.u64;
410 } else {
411 return nullptr;
412 }
413 }
414
415 /// Return a pointer to i8 if the union is in that state, otherwise `nullptr`.
417 if (_tag == detail::TensorBufferTag::I8) {
418 return &_data.i8;
419 } else {
420 return nullptr;
421 }
422 }
423
424 /// Return a pointer to i16 if the union is in that state, otherwise `nullptr`.
426 if (_tag == detail::TensorBufferTag::I16) {
427 return &_data.i16;
428 } else {
429 return nullptr;
430 }
431 }
432
433 /// Return a pointer to i32 if the union is in that state, otherwise `nullptr`.
435 if (_tag == detail::TensorBufferTag::I32) {
436 return &_data.i32;
437 } else {
438 return nullptr;
439 }
440 }
441
442 /// Return a pointer to i64 if the union is in that state, otherwise `nullptr`.
444 if (_tag == detail::TensorBufferTag::I64) {
445 return &_data.i64;
446 } else {
447 return nullptr;
448 }
449 }
450
451 /// Return a pointer to f16 if the union is in that state, otherwise `nullptr`.
453 if (_tag == detail::TensorBufferTag::F16) {
454 return &_data.f16;
455 } else {
456 return nullptr;
457 }
458 }
459
460 /// Return a pointer to f32 if the union is in that state, otherwise `nullptr`.
462 if (_tag == detail::TensorBufferTag::F32) {
463 return &_data.f32;
464 } else {
465 return nullptr;
466 }
467 }
468
469 /// Return a pointer to f64 if the union is in that state, otherwise `nullptr`.
471 if (_tag == detail::TensorBufferTag::F64) {
472 return &_data.f64;
473 } else {
474 return nullptr;
475 }
476 }
477
478 /// Return a pointer to jpeg if the union is in that state, otherwise `nullptr`.
480 if (_tag == detail::TensorBufferTag::JPEG) {
481 return &_data.jpeg;
482 } else {
483 return nullptr;
484 }
485 }
486
487 /// Return a pointer to nv12 if the union is in that state, otherwise `nullptr`.
489 if (_tag == detail::TensorBufferTag::NV12) {
490 return &_data.nv12;
491 } else {
492 return nullptr;
493 }
494 }
495
496 /// \private
497 const detail::TensorBufferData& get_union_data() const {
498 return _data;
499 }
500
501 /// \private
502 detail::TensorBufferTag get_union_tag() const {
503 return _tag;
504 }
505
506 private:
507 detail::TensorBufferTag _tag;
508 detail::TensorBufferData _data;
509 };
510} // namespace rerun::datatypes
511
512namespace rerun {
513 template <typename T>
514 struct Loggable;
515
516 /// \private
517 template <>
518 struct Loggable<datatypes::TensorBuffer> {
519 static constexpr const char Name[] = "rerun.datatypes.TensorBuffer";
520
521 /// Returns the arrow data type this type corresponds to.
522 static const std::shared_ptr<arrow::DataType>& arrow_datatype();
523
524 /// Fills an arrow array builder with an array of this type.
525 static rerun::Error fill_arrow_array_builder(
526 arrow::DenseUnionBuilder* builder, const datatypes::TensorBuffer* elements,
527 size_t num_elements
528 );
529
530 /// Serializes an array of `rerun::datatypes::TensorBuffer` into an arrow array.
531 static Result<std::shared_ptr<arrow::Array>> to_arrow(
532 const datatypes::TensorBuffer* instances, size_t num_instances
533 );
534 };
535} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:47
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:87
All built-in datatypes. See Types in the Rerun manual.
Definition rerun.hpp:72
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:20
Datatype: The underlying storage for a Tensor.
Definition tensor_buffer.hpp:93
const rerun::Collection< uint16_t > * get_u16() const
Return a pointer to u16 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:389
TensorBuffer(const TensorBuffer &other)
Copy constructor.
Definition tensor_buffer.hpp:97
const rerun::Collection< uint64_t > * get_u64() const
Return a pointer to u64 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:407
TensorBuffer(Collection< int32_t > i32)
Construct a TensorBuffer from a Collection<int32_t>.
Definition tensor_buffer.hpp:253
size_t num_elems() const
Number of elements in the buffer.
TensorBuffer(Collection< uint16_t > u16)
Construct a TensorBuffer from a Collection<uint16_t>.
Definition tensor_buffer.hpp:238
TensorBuffer(Collection< float > f32)
Construct a TensorBuffer from a Collection<float>.
Definition tensor_buffer.hpp:263
TensorBuffer(Collection< rerun::half > f16)
Construct a TensorBuffer from a Collection<half>.
Definition tensor_buffer.hpp:259
const rerun::Collection< uint32_t > * get_u32() const
Return a pointer to u32 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:398
const rerun::Collection< rerun::half > * get_f16() const
Return a pointer to f16 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:452
const rerun::Collection< uint8_t > * get_u8() const
Return a pointer to u8 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:380
const rerun::Collection< double > * get_f64() const
Return a pointer to f64 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:470
const rerun::Collection< uint8_t > * get_nv12() const
Return a pointer to nv12 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:488
TensorBuffer(Collection< uint8_t > u8)
Construct a TensorBuffer from a Collection<uint8_t>.
Definition tensor_buffer.hpp:235
const rerun::Collection< uint8_t > * get_jpeg() const
Return a pointer to jpeg if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:479
TensorBuffer(Collection< int16_t > i16)
Construct a TensorBuffer from a Collection<int16_t>.
Definition tensor_buffer.hpp:250
const rerun::Collection< int16_t > * get_i16() const
Return a pointer to i16 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:425
const rerun::Collection< float > * get_f32() const
Return a pointer to f32 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:461
TensorBuffer(Collection< int64_t > i64)
Construct a TensorBuffer from a Collection<int64_t>.
Definition tensor_buffer.hpp:256
TensorBuffer(Collection< uint32_t > u32)
Construct a TensorBuffer from a Collection<uint32_t>.
Definition tensor_buffer.hpp:241
TensorBuffer(Collection< uint64_t > u64)
Construct a TensorBuffer from a Collection<uint64_t>.
Definition tensor_buffer.hpp:244
TensorBuffer(Collection< int8_t > i8)
Construct a TensorBuffer from a Collection<int8_t>.
Definition tensor_buffer.hpp:247
const rerun::Collection< int8_t > * get_i8() const
Return a pointer to i8 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:416
TensorBuffer(TContainer &&container)
Construct a TensorBuffer from any container type that has a value_type member and for which a rerun::...
Definition tensor_buffer.hpp:275
const rerun::Collection< int64_t > * get_i64() const
Return a pointer to i64 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:443
const rerun::Collection< int32_t > * get_i32() const
Return a pointer to i32 if the union is in that state, otherwise nullptr.
Definition tensor_buffer.hpp:434
TensorBuffer(Collection< double > f64)
Construct a TensorBuffer from a Collection<double>.
Definition tensor_buffer.hpp:266