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 "../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 };
41
42 /// \private
43 union TensorBufferData {
44 /// 8bit unsigned integer.
46
47 /// 16bit unsigned integer.
49
50 /// 32bit unsigned integer.
52
53 /// 64bit unsigned integer.
55
56 /// 8bit signed integer.
58
59 /// 16bit signed integer.
61
62 /// 32bit signed integer.
64
65 /// 64bit signed integer.
67
68 /// 16bit IEEE-754 floating point, also known as `half`.
70
71 /// 32bit IEEE-754 floating point, also known as `float` or `single`.
73
74 /// 64bit IEEE-754 floating point, also known as `double`.
76
77 TensorBufferData() {
78 std::memset(reinterpret_cast<void*>(this), 0, sizeof(TensorBufferData));
79 }
80
81 ~TensorBufferData() {}
82
83 void swap(TensorBufferData& other) noexcept {
84 // This bitwise swap would fail for self-referential types, but we don't have any of those.
85 char temp[sizeof(TensorBufferData)];
86 void* otherbytes = reinterpret_cast<void*>(&other);
87 void* thisbytes = reinterpret_cast<void*>(this);
88 std::memcpy(temp, thisbytes, sizeof(TensorBufferData));
89 std::memcpy(thisbytes, otherbytes, sizeof(TensorBufferData));
90 std::memcpy(otherbytes, temp, sizeof(TensorBufferData));
91 }
92 };
93 } // namespace detail
94
95 /// **Datatype**: The underlying storage for `archetypes::Tensor`.
96 ///
97 /// Tensor elements are stored in a contiguous buffer of a single type.
98 struct TensorBuffer {
99 TensorBuffer() : _tag(detail::TensorBufferTag::None) {}
100
101 /// Copy constructor
102 TensorBuffer(const TensorBuffer& other) : _tag(other._tag) {
103 switch (other._tag) {
104 case detail::TensorBufferTag::U8: {
105 using TypeAlias = rerun::Collection<uint8_t>;
106 new (&_data.u8) TypeAlias(other._data.u8);
107 } break;
108 case detail::TensorBufferTag::U16: {
109 using TypeAlias = rerun::Collection<uint16_t>;
110 new (&_data.u16) TypeAlias(other._data.u16);
111 } break;
112 case detail::TensorBufferTag::U32: {
113 using TypeAlias = rerun::Collection<uint32_t>;
114 new (&_data.u32) TypeAlias(other._data.u32);
115 } break;
116 case detail::TensorBufferTag::U64: {
117 using TypeAlias = rerun::Collection<uint64_t>;
118 new (&_data.u64) TypeAlias(other._data.u64);
119 } break;
120 case detail::TensorBufferTag::I8: {
121 using TypeAlias = rerun::Collection<int8_t>;
122 new (&_data.i8) TypeAlias(other._data.i8);
123 } break;
124 case detail::TensorBufferTag::I16: {
125 using TypeAlias = rerun::Collection<int16_t>;
126 new (&_data.i16) TypeAlias(other._data.i16);
127 } break;
128 case detail::TensorBufferTag::I32: {
129 using TypeAlias = rerun::Collection<int32_t>;
130 new (&_data.i32) TypeAlias(other._data.i32);
131 } break;
132 case detail::TensorBufferTag::I64: {
133 using TypeAlias = rerun::Collection<int64_t>;
134 new (&_data.i64) TypeAlias(other._data.i64);
135 } break;
136 case detail::TensorBufferTag::F16: {
137 using TypeAlias = rerun::Collection<rerun::half>;
138 new (&_data.f16) TypeAlias(other._data.f16);
139 } break;
140 case detail::TensorBufferTag::F32: {
141 using TypeAlias = rerun::Collection<float>;
142 new (&_data.f32) TypeAlias(other._data.f32);
143 } break;
144 case detail::TensorBufferTag::F64: {
145 using TypeAlias = rerun::Collection<double>;
146 new (&_data.f64) TypeAlias(other._data.f64);
147 } break;
148 case detail::TensorBufferTag::None: {
149 } break;
150 default:
151 assert(false && "unreachable");
152 }
153 }
154
155 TensorBuffer& operator=(const TensorBuffer& other) noexcept {
156 TensorBuffer tmp(other);
157 this->swap(tmp);
158 return *this;
159 }
160
161 TensorBuffer(TensorBuffer&& other) noexcept : TensorBuffer() {
162 this->swap(other);
163 }
164
165 TensorBuffer& operator=(TensorBuffer&& other) noexcept {
166 this->swap(other);
167 return *this;
168 }
169
170 ~TensorBuffer() {
171 switch (this->_tag) {
172 case detail::TensorBufferTag::None: {
173 // Nothing to destroy
174 } break;
175 case detail::TensorBufferTag::U8: {
176 using TypeAlias = rerun::Collection<uint8_t>;
177 _data.u8.~TypeAlias();
178 } break;
179 case detail::TensorBufferTag::U16: {
180 using TypeAlias = rerun::Collection<uint16_t>;
181 _data.u16.~TypeAlias();
182 } break;
183 case detail::TensorBufferTag::U32: {
184 using TypeAlias = rerun::Collection<uint32_t>;
185 _data.u32.~TypeAlias();
186 } break;
187 case detail::TensorBufferTag::U64: {
188 using TypeAlias = rerun::Collection<uint64_t>;
189 _data.u64.~TypeAlias();
190 } break;
191 case detail::TensorBufferTag::I8: {
192 using TypeAlias = rerun::Collection<int8_t>;
193 _data.i8.~TypeAlias();
194 } break;
195 case detail::TensorBufferTag::I16: {
196 using TypeAlias = rerun::Collection<int16_t>;
197 _data.i16.~TypeAlias();
198 } break;
199 case detail::TensorBufferTag::I32: {
200 using TypeAlias = rerun::Collection<int32_t>;
201 _data.i32.~TypeAlias();
202 } break;
203 case detail::TensorBufferTag::I64: {
204 using TypeAlias = rerun::Collection<int64_t>;
205 _data.i64.~TypeAlias();
206 } break;
207 case detail::TensorBufferTag::F16: {
208 using TypeAlias = rerun::Collection<rerun::half>;
209 _data.f16.~TypeAlias();
210 } break;
211 case detail::TensorBufferTag::F32: {
212 using TypeAlias = rerun::Collection<float>;
213 _data.f32.~TypeAlias();
214 } break;
215 case detail::TensorBufferTag::F64: {
216 using TypeAlias = rerun::Collection<double>;
217 _data.f64.~TypeAlias();
218 } break;
219 default:
220 assert(false && "unreachable");
221 }
222 }
223
224 public: // START of extensions from tensor_buffer_ext.cpp:
225 /// Construct a `TensorBuffer` from any container type that has a `value_type` member and for which a
226 /// `rerun::ContainerAdapter` exists.
227 ///
228 /// This constructor assumes the type of tensor buffer you want to use is defined by `TContainer::value_type`
229 /// and then forwards the argument as-is to the appropriate `rerun::Container` constructor.
230 /// \see rerun::ContainerAdapter, rerun::Container
231 template <typename TContainer, typename value_type = traits::value_type_of_t<TContainer>>
232 TensorBuffer(TContainer&& container)
233 : TensorBuffer(Collection<value_type>(std::forward<TContainer>(container))) {}
234
235 /// Number of elements in the buffer.
236 ///
237 /// You may NOT call this for NV12 or YUY2.
238 size_t num_elems() const;
239
240 // END of extensions from tensor_buffer_ext.cpp, start of generated code:
241
242 void swap(TensorBuffer& other) noexcept {
243 std::swap(this->_tag, other._tag);
244 this->_data.swap(other._data);
245 }
246
247 /// 8bit unsigned integer.
249 *this = TensorBuffer::u8(std::move(u8));
250 }
251
252 /// 16bit unsigned integer.
254 *this = TensorBuffer::u16(std::move(u16));
255 }
256
257 /// 32bit unsigned integer.
259 *this = TensorBuffer::u32(std::move(u32));
260 }
261
262 /// 64bit unsigned integer.
264 *this = TensorBuffer::u64(std::move(u64));
265 }
266
267 /// 8bit signed integer.
269 *this = TensorBuffer::i8(std::move(i8));
270 }
271
272 /// 16bit signed integer.
274 *this = TensorBuffer::i16(std::move(i16));
275 }
276
277 /// 32bit signed integer.
279 *this = TensorBuffer::i32(std::move(i32));
280 }
281
282 /// 64bit signed integer.
284 *this = TensorBuffer::i64(std::move(i64));
285 }
286
287 /// 16bit IEEE-754 floating point, also known as `half`.
289 *this = TensorBuffer::f16(std::move(f16));
290 }
291
292 /// 32bit IEEE-754 floating point, also known as `float` or `single`.
294 *this = TensorBuffer::f32(std::move(f32));
295 }
296
297 /// 64bit IEEE-754 floating point, also known as `double`.
299 *this = TensorBuffer::f64(std::move(f64));
300 }
301
302 /// 8bit unsigned integer.
304 TensorBuffer self;
305 self._tag = detail::TensorBufferTag::U8;
306 new (&self._data.u8) rerun::Collection<uint8_t>(std::move(u8));
307 return self;
308 }
309
310 /// 16bit unsigned integer.
312 TensorBuffer self;
313 self._tag = detail::TensorBufferTag::U16;
314 new (&self._data.u16) rerun::Collection<uint16_t>(std::move(u16));
315 return self;
316 }
317
318 /// 32bit unsigned integer.
320 TensorBuffer self;
321 self._tag = detail::TensorBufferTag::U32;
322 new (&self._data.u32) rerun::Collection<uint32_t>(std::move(u32));
323 return self;
324 }
325
326 /// 64bit unsigned integer.
328 TensorBuffer self;
329 self._tag = detail::TensorBufferTag::U64;
330 new (&self._data.u64) rerun::Collection<uint64_t>(std::move(u64));
331 return self;
332 }
333
334 /// 8bit signed integer.
336 TensorBuffer self;
337 self._tag = detail::TensorBufferTag::I8;
338 new (&self._data.i8) rerun::Collection<int8_t>(std::move(i8));
339 return self;
340 }
341
342 /// 16bit signed integer.
344 TensorBuffer self;
345 self._tag = detail::TensorBufferTag::I16;
346 new (&self._data.i16) rerun::Collection<int16_t>(std::move(i16));
347 return self;
348 }
349
350 /// 32bit signed integer.
352 TensorBuffer self;
353 self._tag = detail::TensorBufferTag::I32;
354 new (&self._data.i32) rerun::Collection<int32_t>(std::move(i32));
355 return self;
356 }
357
358 /// 64bit signed integer.
360 TensorBuffer self;
361 self._tag = detail::TensorBufferTag::I64;
362 new (&self._data.i64) rerun::Collection<int64_t>(std::move(i64));
363 return self;
364 }
365
366 /// 16bit IEEE-754 floating point, also known as `half`.
368 TensorBuffer self;
369 self._tag = detail::TensorBufferTag::F16;
370 new (&self._data.f16) rerun::Collection<rerun::half>(std::move(f16));
371 return self;
372 }
373
374 /// 32bit IEEE-754 floating point, also known as `float` or `single`.
376 TensorBuffer self;
377 self._tag = detail::TensorBufferTag::F32;
378 new (&self._data.f32) rerun::Collection<float>(std::move(f32));
379 return self;
380 }
381
382 /// 64bit IEEE-754 floating point, also known as `double`.
384 TensorBuffer self;
385 self._tag = detail::TensorBufferTag::F64;
386 new (&self._data.f64) rerun::Collection<double>(std::move(f64));
387 return self;
388 }
389
390 /// Return a pointer to u8 if the union is in that state, otherwise `nullptr`.
392 if (_tag == detail::TensorBufferTag::U8) {
393 return &_data.u8;
394 } else {
395 return nullptr;
396 }
397 }
398
399 /// Return a pointer to u16 if the union is in that state, otherwise `nullptr`.
401 if (_tag == detail::TensorBufferTag::U16) {
402 return &_data.u16;
403 } else {
404 return nullptr;
405 }
406 }
407
408 /// Return a pointer to u32 if the union is in that state, otherwise `nullptr`.
410 if (_tag == detail::TensorBufferTag::U32) {
411 return &_data.u32;
412 } else {
413 return nullptr;
414 }
415 }
416
417 /// Return a pointer to u64 if the union is in that state, otherwise `nullptr`.
419 if (_tag == detail::TensorBufferTag::U64) {
420 return &_data.u64;
421 } else {
422 return nullptr;
423 }
424 }
425
426 /// Return a pointer to i8 if the union is in that state, otherwise `nullptr`.
428 if (_tag == detail::TensorBufferTag::I8) {
429 return &_data.i8;
430 } else {
431 return nullptr;
432 }
433 }
434
435 /// Return a pointer to i16 if the union is in that state, otherwise `nullptr`.
437 if (_tag == detail::TensorBufferTag::I16) {
438 return &_data.i16;
439 } else {
440 return nullptr;
441 }
442 }
443
444 /// Return a pointer to i32 if the union is in that state, otherwise `nullptr`.
446 if (_tag == detail::TensorBufferTag::I32) {
447 return &_data.i32;
448 } else {
449 return nullptr;
450 }
451 }
452
453 /// Return a pointer to i64 if the union is in that state, otherwise `nullptr`.
455 if (_tag == detail::TensorBufferTag::I64) {
456 return &_data.i64;
457 } else {
458 return nullptr;
459 }
460 }
461
462 /// Return a pointer to f16 if the union is in that state, otherwise `nullptr`.
464 if (_tag == detail::TensorBufferTag::F16) {
465 return &_data.f16;
466 } else {
467 return nullptr;
468 }
469 }
470
471 /// Return a pointer to f32 if the union is in that state, otherwise `nullptr`.
473 if (_tag == detail::TensorBufferTag::F32) {
474 return &_data.f32;
475 } else {
476 return nullptr;
477 }
478 }
479
480 /// Return a pointer to f64 if the union is in that state, otherwise `nullptr`.
482 if (_tag == detail::TensorBufferTag::F64) {
483 return &_data.f64;
484 } else {
485 return nullptr;
486 }
487 }
488
489 /// \private
490 const detail::TensorBufferData& get_union_data() const {
491 return _data;
492 }
493
494 /// \private
495 detail::TensorBufferTag get_union_tag() const {
496 return _tag;
497 }
498
499 private:
500 detail::TensorBufferTag _tag;
501 detail::TensorBufferData _data;
502 };
503} // namespace rerun::datatypes
504
505namespace rerun {
506 template <typename T>
507 struct Loggable;
508
509 /// \private
510 template <>
511 struct Loggable<datatypes::TensorBuffer> {
512 static constexpr const char Name[] = "rerun.datatypes.TensorBuffer";
513
514 /// Returns the arrow data type this type corresponds to.
515 static const std::shared_ptr<arrow::DataType>& arrow_datatype();
516
517 /// Serializes an array of `rerun::datatypes::TensorBuffer` into an arrow array.
518 static Result<std::shared_ptr<arrow::Array>> to_arrow(
519 const datatypes::TensorBuffer* instances, size_t num_instances
520 );
521
522 /// Fills an arrow array builder with an array of this type.
523 static rerun::Error fill_arrow_array_builder(
524 arrow::DenseUnionBuilder* builder, const datatypes::TensorBuffer* elements,
525 size_t num_elements
526 );
527 };
528} // 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:82
@ 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:22
Datatype: The underlying storage for archetypes::Tensor.
Definition tensor_buffer.hpp:98
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:400
TensorBuffer(const TensorBuffer &other)
Copy constructor.
Definition tensor_buffer.hpp:102
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:418
size_t num_elems() const
Number of elements in the buffer.
TensorBuffer(rerun::Collection< int32_t > i32)
32bit signed integer.
Definition tensor_buffer.hpp:278
static TensorBuffer f32(rerun::Collection< float > f32)
32bit IEEE-754 floating point, also known as float or single.
Definition tensor_buffer.hpp:375
TensorBuffer(rerun::Collection< uint32_t > u32)
32bit unsigned integer.
Definition tensor_buffer.hpp:258
static TensorBuffer u64(rerun::Collection< uint64_t > u64)
64bit unsigned integer.
Definition tensor_buffer.hpp:327
TensorBuffer(rerun::Collection< int16_t > i16)
16bit signed integer.
Definition tensor_buffer.hpp:273
TensorBuffer(rerun::Collection< double > f64)
64bit IEEE-754 floating point, also known as double.
Definition tensor_buffer.hpp:298
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:409
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:463
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:391
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:481
static TensorBuffer i32(rerun::Collection< int32_t > i32)
32bit signed integer.
Definition tensor_buffer.hpp:351
static TensorBuffer i64(rerun::Collection< int64_t > i64)
64bit signed integer.
Definition tensor_buffer.hpp:359
static TensorBuffer f16(rerun::Collection< rerun::half > f16)
16bit IEEE-754 floating point, also known as half.
Definition tensor_buffer.hpp:367
TensorBuffer(rerun::Collection< int64_t > i64)
64bit signed integer.
Definition tensor_buffer.hpp:283
TensorBuffer(rerun::Collection< uint16_t > u16)
16bit unsigned integer.
Definition tensor_buffer.hpp:253
static TensorBuffer u16(rerun::Collection< uint16_t > u16)
16bit unsigned integer.
Definition tensor_buffer.hpp:311
TensorBuffer(rerun::Collection< int8_t > i8)
8bit signed integer.
Definition tensor_buffer.hpp:268
static TensorBuffer i8(rerun::Collection< int8_t > i8)
8bit signed integer.
Definition tensor_buffer.hpp:335
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:436
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:472
static TensorBuffer u8(rerun::Collection< uint8_t > u8)
8bit unsigned integer.
Definition tensor_buffer.hpp:303
static TensorBuffer i16(rerun::Collection< int16_t > i16)
16bit signed integer.
Definition tensor_buffer.hpp:343
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:427
TensorBuffer(rerun::Collection< uint8_t > u8)
8bit unsigned integer.
Definition tensor_buffer.hpp:248
static TensorBuffer u32(rerun::Collection< uint32_t > u32)
32bit unsigned integer.
Definition tensor_buffer.hpp:319
TensorBuffer(rerun::Collection< uint64_t > u64)
64bit unsigned integer.
Definition tensor_buffer.hpp:263
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:232
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:454
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:445
static TensorBuffer f64(rerun::Collection< double > f64)
64bit IEEE-754 floating point, also known as double.
Definition tensor_buffer.hpp:383
TensorBuffer(rerun::Collection< float > f32)
32bit IEEE-754 floating point, also known as float or single.
Definition tensor_buffer.hpp:293
TensorBuffer(rerun::Collection< rerun::half > f16)
16bit IEEE-754 floating point, also known as half.
Definition tensor_buffer.hpp:288