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 component batches.
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 invidiual & collections of `rerun::ComponentBatch`."
26 "You can add your own implementation by specializing AsComponents<T> for your type T."
27 );
28
29 /// Converts the type `T` into a collection of `ComponentBatch`s.
30 static Result<Collection<ComponentBatch>> as_batches(const T& archetype);
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<ComponentBatch>`.
37 template <>
38 struct AsComponents<Collection<ComponentBatch>> {
39 static Result<Collection<ComponentBatch>> as_batches(Collection<ComponentBatch> components
40 ) {
41 return components;
42 }
43 };
44
45 /// `AsComponents` for a single `ComponentBatch`.
46 template <>
47 struct AsComponents<ComponentBatch> {
48 static Result<Collection<ComponentBatch>> as_batches(ComponentBatch components) {
49 return rerun::take_ownership(std::move(components));
50 }
51 };
52
53 /// `AsComponents` for a `Collection<ComponentBatch>` wrapped in a `Result`, forwarding errors for convenience.
54 template <>
55 struct AsComponents<Result<Collection<ComponentBatch>>> {
56 static Result<Collection<ComponentBatch>> as_batches(
57 Result<Collection<ComponentBatch>> components
58 ) {
59 RR_RETURN_NOT_OK(components.error);
60 return components.value;
61 }
62 };
63
64 /// `AsComponents` for a `Collection<ComponentBatch>` individually wrapped in `Result`, forwarding errors for convenience.
65 template <>
66 struct AsComponents<Collection<Result<ComponentBatch>>> {
67 static Result<Collection<ComponentBatch>> as_batches(
68 Collection<Result<ComponentBatch>> components
69 ) {
70 std::vector<ComponentBatch> result;
71 result.reserve(components.size());
72 for (auto& component : components) {
73 RR_RETURN_NOT_OK(component.error);
74 result.push_back(std::move(component.value));
75 }
76 return rerun::take_ownership(std::move(result));
77 }
78 };
79
80 /// `AsComponents` for a single `ComponentBatch` wrapped in a `Result`, forwarding errors for convenience.
81 template <>
82 struct AsComponents<Result<ComponentBatch>> {
83 static Result<Collection<ComponentBatch>> as_batches(Result<ComponentBatch> components) {
84 RR_RETURN_NOT_OK(components.error);
85 return rerun::take_ownership(std::move(components.value));
86 }
87 };
88
89 /// \endcond
90} // namespace rerun
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:23
Collection< TElement > take_ownership(std::vector< TElement > data)
Takes ownership of a temporary std::vector, moving it into the collection.
Definition collection.hpp:482