Rerun C++ SDK
Loading...
Searching...
No Matches
as_components.hpp
1#pragma once
2
3#include "collection.hpp"
4#include "component_batch.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<ComponentBatch>> serialize(
44 const Collection<TComponent>& components
45 ) {
46 auto batch_result = ComponentBatch::from_loggable<TComponent>(components);
47 RR_RETURN_NOT_OK(batch_result.error);
48
49 return Result<std::vector<ComponentBatch>>({std::move(batch_result.value)});
50 }
51 };
52
53 /// `AsComponents` for a `std::vector` of types implementing the `rerun::Loggable` trait.
54 template <typename TComponent>
55 struct AsComponents<std::vector<TComponent>> {
56 static Result<std::vector<ComponentBatch>> serialize(
57 const std::vector<TComponent>& components
58 ) {
59 return AsComponents<Collection<TComponent>>::serialize(components);
60 }
61 };
62
63 /// AsComponents for `std::initializer_list`
64 template <typename TComponent>
65 struct AsComponents<std::initializer_list<TComponent>> {
66 static Result<std::vector<ComponentBatch>> serialize(
67 std::initializer_list<TComponent> components
68 ) {
69 return AsComponents<Collection<TComponent>>::serialize(components);
70 }
71 };
72
73 /// `AsComponents` for an `std::array` of types implementing the `rerun::Loggable` trait.
74 template <typename TComponent, size_t NumInstances>
75 struct AsComponents<std::array<TComponent, NumInstances>> {
76 static Result<std::vector<ComponentBatch>> serialize(
77 const std::array<TComponent, NumInstances>& components
78 ) {
79 return AsComponents<Collection<TComponent>>::serialize(components);
80 }
81 };
82
83 /// `AsComponents` for an c-array of types implementing the `rerun::Loggable` trait.
84 template <typename TComponent, size_t NumInstances>
85 struct AsComponents<TComponent[NumInstances]> {
86 static Result<std::vector<ComponentBatch>> serialize(const TComponent (&components
87 )[NumInstances]) {
88 return AsComponents<Collection<TComponent>>::serialize(components);
89 }
90 };
91
92 /// `AsComponents` for single indicator components.
93 template <const char Name[]>
94 struct AsComponents<components::IndicatorComponent<Name>> {
95 static Result<std::vector<ComponentBatch>> serialize(
96 const components::IndicatorComponent<Name>& indicator
97 ) {
98 return AsComponents<Collection<components::IndicatorComponent<Name>>>::serialize(
99 indicator
100 );
101 }
102 };
103
104 /// \endcond
105} // namespace rerun
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:22