Rerun C++ SDK
Loading...
Searching...
No Matches
as_components.hpp
1#pragma once
2
3#include "collection.hpp"
4#include "data_cell.hpp"
5#include "indicator_component.hpp"
6#include "loggable.hpp"
7
8namespace rerun {
9 /// The AsComponents trait is used to convert a type into a list of serialized component.
10 ///
11 /// It is implemented for various built-in types as well as collections of components.
12 /// You can build your own archetypes by implementing this trait.
13 /// Anything that implements `AsComponents` can be logged to a recording stream.
14 template <typename T>
15 struct AsComponents {
16 /// \private
17 /// `NoAsComponentsFor` always evaluates to false, but in a way that requires template instantiation.
18 template <typename T2>
19 struct NoAsComponentsFor : std::false_type {};
20
21 // TODO(andreas): This should also mention an example of how to implement this.
22 static_assert(
23 NoAsComponentsFor<T>::value,
24 "AsComponents is not implemented for this type. "
25 "It is implemented for all built-in archetypes as well as std::vector, std::array, and "
26 "c-arrays of components. "
27 "You can add your own implementation by specializing AsComponents<T> for your type T."
28 );
29
30 // TODO(andreas): List methods that the trait should implement.
31 };
32
33 // Documenting the builtin generic `AsComponents` impls is too much clutter for the doc class overview.
34 /// \cond private
35
36 /// `AsComponents` for a Collection of types implementing the `rerun::Loggable` trait.
37 template <typename TComponent>
38 struct AsComponents<Collection<TComponent>> {
39 static_assert(
40 is_loggable<TComponent>, "The given type does not implement the rerun::Loggable trait."
41 );
42
43 static Result<std::vector<DataCell>> serialize(const Collection<TComponent>& components) {
44 auto cell_result = DataCell::from_loggable<TComponent>(components);
45 RR_RETURN_NOT_OK(cell_result.error);
46
47 return Result<std::vector<DataCell>>({std::move(cell_result.value)});
48 }
49 };
50
51 /// `AsComponents` for a `std::vector` of types implementing the `rerun::Loggable` trait.
52 template <typename TComponent>
53 struct AsComponents<std::vector<TComponent>> {
54 static Result<std::vector<DataCell>> serialize(const std::vector<TComponent>& components) {
55 return AsComponents<Collection<TComponent>>::serialize(components);
56 }
57 };
58
59 /// AsComponents for `std::initializer_list`
60 template <typename TComponent>
61 struct AsComponents<std::initializer_list<TComponent>> {
62 static Result<std::vector<DataCell>> serialize(std::initializer_list<TComponent> components
63 ) {
64 return AsComponents<Collection<TComponent>>::serialize(components);
65 }
66 };
67
68 /// `AsComponents` for an `std::array` of types implementing the `rerun::Loggable` trait.
69 template <typename TComponent, size_t NumInstances>
70 struct AsComponents<std::array<TComponent, NumInstances>> {
71 static Result<std::vector<DataCell>> serialize(
72 const std::array<TComponent, NumInstances>& components
73 ) {
74 return AsComponents<Collection<TComponent>>::serialize(components);
75 }
76 };
77
78 /// `AsComponents` for an c-array of types implementing the `rerun::Loggable` trait.
79 template <typename TComponent, size_t NumInstances>
80 struct AsComponents<TComponent[NumInstances]> {
81 static Result<std::vector<DataCell>> serialize(const TComponent (&components)[NumInstances]
82 ) {
83 return AsComponents<Collection<TComponent>>::serialize(components);
84 }
85 };
86
87 /// `AsComponents` for single indicator components.
88 template <const char Name[]>
89 struct AsComponents<components::IndicatorComponent<Name>> {
90 static Result<std::vector<DataCell>> serialize(
91 const components::IndicatorComponent<Name>& indicator
92 ) {
93 return AsComponents<Collection<components::IndicatorComponent<Name>>>::serialize(
94 indicator
95 );
96 }
97 };
98
99 /// \endcond
100} // namespace rerun
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:20