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/build/re_type_definitions/rerun/datatypes/tensor_buffer.def.rs".
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 // Substitution fails for `TensorBuffer`, which has no `value_type`, so this cannot hide
233 // copy or move construction.
234 TensorBuffer(TContainer&& container) // NOLINT(bugprone-forwarding-reference-overload)
235 : TensorBuffer(Collection<value_type>(std::forward<TContainer>(container))) {}
236
237 /// Number of elements in the buffer.
238 ///
239 /// You may NOT call this for NV12 or YUY2.
240 size_t num_elems() const;
241
242 // END of extensions from tensor_buffer_ext.cpp, start of generated code:
243
244 void swap(TensorBuffer& other) noexcept {
245 std::swap(this->_tag, other._tag);
246 this->_data.swap(other._data);
247 }
248
249 /// 8bit unsigned integer.
251 *this = TensorBuffer::u8(std::move(u8));
252 }
253
254 /// 16bit unsigned integer.
256 *this = TensorBuffer::u16(std::move(u16));
257 }
258
259 /// 32bit unsigned integer.
261 *this = TensorBuffer::u32(std::move(u32));
262 }
263
264 /// 64bit unsigned integer.
266 *this = TensorBuffer::u64(std::move(u64));
267 }
268
269 /// 8bit signed integer.
271 *this = TensorBuffer::i8(std::move(i8));
272 }
273
274 /// 16bit signed integer.
276 *this = TensorBuffer::i16(std::move(i16));
277 }
278
279 /// 32bit signed integer.
281 *this = TensorBuffer::i32(std::move(i32));
282 }
283
284 /// 64bit signed integer.
286 *this = TensorBuffer::i64(std::move(i64));
287 }
288
289 /// 16bit IEEE-754 floating point, also known as `half`.
291 *this = TensorBuffer::f16(std::move(f16));
292 }
293
294 /// 32bit IEEE-754 floating point, also known as `float` or `single`.
296 *this = TensorBuffer::f32(std::move(f32));
297 }
298
299 /// 64bit IEEE-754 floating point, also known as `double`.
301 *this = TensorBuffer::f64(std::move(f64));
302 }
303
304 /// 8bit unsigned integer.
306 TensorBuffer self;
307 self._tag = detail::TensorBufferTag::U8;
308 new (&self._data.u8) rerun::Collection<uint8_t>(std::move(u8));
309 return self;
310 }
311
312 /// 16bit unsigned integer.
314 TensorBuffer self;
315 self._tag = detail::TensorBufferTag::U16;
316 new (&self._data.u16) rerun::Collection<uint16_t>(std::move(u16));
317 return self;
318 }
319
320 /// 32bit unsigned integer.
322 TensorBuffer self;
323 self._tag = detail::TensorBufferTag::U32;
324 new (&self._data.u32) rerun::Collection<uint32_t>(std::move(u32));
325 return self;
326 }
327
328 /// 64bit unsigned integer.
330 TensorBuffer self;
331 self._tag = detail::TensorBufferTag::U64;
332 new (&self._data.u64) rerun::Collection<uint64_t>(std::move(u64));
333 return self;
334 }
335
336 /// 8bit signed integer.
338 TensorBuffer self;
339 self._tag = detail::TensorBufferTag::I8;
340 new (&self._data.i8) rerun::Collection<int8_t>(std::move(i8));
341 return self;
342 }
343
344 /// 16bit signed integer.
346 TensorBuffer self;
347 self._tag = detail::TensorBufferTag::I16;
348 new (&self._data.i16) rerun::Collection<int16_t>(std::move(i16));
349 return self;
350 }
351
352 /// 32bit signed integer.
354 TensorBuffer self;
355 self._tag = detail::TensorBufferTag::I32;
356 new (&self._data.i32) rerun::Collection<int32_t>(std::move(i32));
357 return self;
358 }
359
360 /// 64bit signed integer.
362 TensorBuffer self;
363 self._tag = detail::TensorBufferTag::I64;
364 new (&self._data.i64) rerun::Collection<int64_t>(std::move(i64));
365 return self;
366 }
367
368 /// 16bit IEEE-754 floating point, also known as `half`.
370 TensorBuffer self;
371 self._tag = detail::TensorBufferTag::F16;
372 new (&self._data.f16) rerun::Collection<rerun::half>(std::move(f16));
373 return self;
374 }
375
376 /// 32bit IEEE-754 floating point, also known as `float` or `single`.
378 TensorBuffer self;
379 self._tag = detail::TensorBufferTag::F32;
380 new (&self._data.f32) rerun::Collection<float>(std::move(f32));
381 return self;
382 }
383
384 /// 64bit IEEE-754 floating point, also known as `double`.
386 TensorBuffer self;
387 self._tag = detail::TensorBufferTag::F64;
388 new (&self._data.f64) rerun::Collection<double>(std::move(f64));
389 return self;
390 }
391
392 /// Return a pointer to u8 if the union is in that state, otherwise `nullptr`.
394 if (_tag == detail::TensorBufferTag::U8) {
395 return &_data.u8;
396 } else {
397 return nullptr;
398 }
399 }
400
401 /// Return a pointer to u16 if the union is in that state, otherwise `nullptr`.
403 if (_tag == detail::TensorBufferTag::U16) {
404 return &_data.u16;
405 } else {
406 return nullptr;
407 }
408 }
409
410 /// Return a pointer to u32 if the union is in that state, otherwise `nullptr`.
412 if (_tag == detail::TensorBufferTag::U32) {
413 return &_data.u32;
414 } else {
415 return nullptr;
416 }
417 }
418
419 /// Return a pointer to u64 if the union is in that state, otherwise `nullptr`.
421 if (_tag == detail::TensorBufferTag::U64) {
422 return &_data.u64;
423 } else {
424 return nullptr;
425 }
426 }
427
428 /// Return a pointer to i8 if the union is in that state, otherwise `nullptr`.
430 if (_tag == detail::TensorBufferTag::I8) {
431 return &_data.i8;
432 } else {
433 return nullptr;
434 }
435 }
436
437 /// Return a pointer to i16 if the union is in that state, otherwise `nullptr`.
439 if (_tag == detail::TensorBufferTag::I16) {
440 return &_data.i16;
441 } else {
442 return nullptr;
443 }
444 }
445
446 /// Return a pointer to i32 if the union is in that state, otherwise `nullptr`.
448 if (_tag == detail::TensorBufferTag::I32) {
449 return &_data.i32;
450 } else {
451 return nullptr;
452 }
453 }
454
455 /// Return a pointer to i64 if the union is in that state, otherwise `nullptr`.
457 if (_tag == detail::TensorBufferTag::I64) {
458 return &_data.i64;
459 } else {
460 return nullptr;
461 }
462 }
463
464 /// Return a pointer to f16 if the union is in that state, otherwise `nullptr`.
466 if (_tag == detail::TensorBufferTag::F16) {
467 return &_data.f16;
468 } else {
469 return nullptr;
470 }
471 }
472
473 /// Return a pointer to f32 if the union is in that state, otherwise `nullptr`.
475 if (_tag == detail::TensorBufferTag::F32) {
476 return &_data.f32;
477 } else {
478 return nullptr;
479 }
480 }
481
482 /// Return a pointer to f64 if the union is in that state, otherwise `nullptr`.
484 if (_tag == detail::TensorBufferTag::F64) {
485 return &_data.f64;
486 } else {
487 return nullptr;
488 }
489 }
490
491 /// \private
492 const detail::TensorBufferData& get_union_data() const {
493 return _data;
494 }
495
496 /// \private
497 detail::TensorBufferTag get_union_tag() const {
498 return _tag;
499 }
500
501 private:
502 detail::TensorBufferTag _tag;
503 detail::TensorBufferData _data;
504 };
505} // namespace rerun::datatypes
506
507namespace rerun {
508 template <typename T>
509 struct Loggable;
510
511 /// \private
512 template <>
513 struct Loggable<datatypes::TensorBuffer> {
514 static constexpr std::string_view ComponentType = "rerun.datatypes.TensorBuffer";
515
516 /// Returns the arrow data type this type corresponds to.
517 static const std::shared_ptr<arrow::DataType>& arrow_datatype();
518
519 /// Serializes an array of `rerun::datatypes::TensorBuffer` into an arrow array.
520 static Result<std::shared_ptr<arrow::Array>> to_arrow(
521 const datatypes::TensorBuffer* instances, size_t num_instances
522 );
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} // 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:103
All built-in datatypes. See Types in the Rerun manual.
Definition rerun.hpp:93
@ 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: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:402
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:420
size_t num_elems() const
Number of elements in the buffer.
TensorBuffer(rerun::Collection< int32_t > i32)
32bit signed integer.
Definition tensor_buffer.hpp:280
static TensorBuffer f32(rerun::Collection< float > f32)
32bit IEEE-754 floating point, also known as float or single.
Definition tensor_buffer.hpp:377
TensorBuffer(rerun::Collection< uint32_t > u32)
32bit unsigned integer.
Definition tensor_buffer.hpp:260
static TensorBuffer u64(rerun::Collection< uint64_t > u64)
64bit unsigned integer.
Definition tensor_buffer.hpp:329
TensorBuffer(rerun::Collection< int16_t > i16)
16bit signed integer.
Definition tensor_buffer.hpp:275
TensorBuffer(rerun::Collection< double > f64)
64bit IEEE-754 floating point, also known as double.
Definition tensor_buffer.hpp:300
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:411
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:465
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:393
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:483
static TensorBuffer i32(rerun::Collection< int32_t > i32)
32bit signed integer.
Definition tensor_buffer.hpp:353
static TensorBuffer i64(rerun::Collection< int64_t > i64)
64bit signed integer.
Definition tensor_buffer.hpp:361
static TensorBuffer f16(rerun::Collection< rerun::half > f16)
16bit IEEE-754 floating point, also known as half.
Definition tensor_buffer.hpp:369
TensorBuffer(rerun::Collection< int64_t > i64)
64bit signed integer.
Definition tensor_buffer.hpp:285
TensorBuffer(rerun::Collection< uint16_t > u16)
16bit unsigned integer.
Definition tensor_buffer.hpp:255
static TensorBuffer u16(rerun::Collection< uint16_t > u16)
16bit unsigned integer.
Definition tensor_buffer.hpp:313
TensorBuffer(rerun::Collection< int8_t > i8)
8bit signed integer.
Definition tensor_buffer.hpp:270
static TensorBuffer i8(rerun::Collection< int8_t > i8)
8bit signed integer.
Definition tensor_buffer.hpp:337
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:438
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:474
static TensorBuffer u8(rerun::Collection< uint8_t > u8)
8bit unsigned integer.
Definition tensor_buffer.hpp:305
static TensorBuffer i16(rerun::Collection< int16_t > i16)
16bit signed integer.
Definition tensor_buffer.hpp:345
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:429
TensorBuffer(rerun::Collection< uint8_t > u8)
8bit unsigned integer.
Definition tensor_buffer.hpp:250
static TensorBuffer u32(rerun::Collection< uint32_t > u32)
32bit unsigned integer.
Definition tensor_buffer.hpp:321
TensorBuffer(rerun::Collection< uint64_t > u64)
64bit unsigned integer.
Definition tensor_buffer.hpp:265
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:234
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:456
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:447
static TensorBuffer f64(rerun::Collection< double > f64)
64bit IEEE-754 floating point, also known as double.
Definition tensor_buffer.hpp:385
TensorBuffer(rerun::Collection< float > f32)
32bit IEEE-754 floating point, also known as float or single.
Definition tensor_buffer.hpp:295
TensorBuffer(rerun::Collection< rerun::half > f16)
16bit IEEE-754 floating point, also known as half.
Definition tensor_buffer.hpp:290