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 "loggable.hpp"
6
7namespace rerun {
8 /// The AsComponents trait is used to convert a type into a list of component batches.
9 ///
10 /// It is implemented for various built-in types as well as collections of components.
11 /// You can build your own archetypes by implementing this trait.
12 /// Anything that implements `AsComponents` can be logged to a recording stream.
13 template <typename T>
14 struct AsComponents {
15 /// \private
16 /// `NoAsComponentsFor` always evaluates to false, but in a way that requires template instantiation.
17 template <typename T2>
18 struct NoAsComponentsFor : std::false_type {};
19
20 // TODO(andreas): This should also mention an example of how to implement this.
21 static_assert(
22 NoAsComponentsFor<T>::value,
23 "AsComponents is not implemented for this type. "
24 "It is implemented for all built-in archetypes as well as invidiual & collections of `rerun::ComponentBatch`."
25 "You can add your own implementation by specializing AsComponents<T> for your type T."
26 );
27
28 /// Converts the type `T` into a collection of `ComponentBatch`s.
29 static Result<Collection<ComponentBatch>> as_batches(const T& archetype);
30 };
31
32 // Documenting the builtin generic `AsComponents` impls is too much clutter for the doc class overview.
33 /// \cond private
34
35 /// `AsComponents` for a `Collection<ComponentBatch>`.
36 template <>
37 struct AsComponents<Collection<ComponentBatch>> {
38 static Result<Collection<ComponentBatch>> as_batches(Collection<ComponentBatch> components
39 ) {
40 return components;
41 }
42 };
43
44 /// `AsComponents` for a single `ComponentBatch`.
45 template <>
46 struct AsComponents<ComponentBatch> {
47 static Result<Collection<ComponentBatch>> as_batches(ComponentBatch components) {
48 return rerun::take_ownership(std::move(components));
49 }
50 };
51
52 /// `AsComponents` for a `Collection<ComponentBatch>` wrapped in a `Result`, forwarding errors for convenience.
53 template <>
54 struct AsComponents<Result<Collection<ComponentBatch>>> {
55 static Result<Collection<ComponentBatch>> as_batches(
56 Result<Collection<ComponentBatch>> components
57 ) {
58 RR_RETURN_NOT_OK(components.error);
59 return components.value;
60 }
61 };
62
63 /// `AsComponents` for a `Collection<ComponentBatch>` individually wrapped in `Result`, forwarding errors for convenience.
64 template <>
65 struct AsComponents<Collection<Result<ComponentBatch>>> {
66 static Result<Collection<ComponentBatch>> as_batches(
67 Collection<Result<ComponentBatch>> components
68 ) {
69 std::vector<ComponentBatch> result;
70 result.reserve(components.size());
71 for (auto& component : components) {
72 RR_RETURN_NOT_OK(component.error);
73 result.push_back(std::move(component.value));
74 }
75 return rerun::take_ownership(std::move(result));
76 }
77 };
78
79 /// `AsComponents` for a single `ComponentBatch` wrapped in a `Result`, forwarding errors for convenience.
80 template <>
81 struct AsComponents<Result<ComponentBatch>> {
82 static Result<Collection<ComponentBatch>> as_batches(Result<ComponentBatch> components) {
83 RR_RETURN_NOT_OK(components.error);
84 return rerun::take_ownership(std::move(components.value));
85 }
86 };
87
88 /// \endcond
89} // 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