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