Viam C++ SDK current
Loading...
Searching...
No Matches
proto_convert.hpp
1#pragma once
2
3#include <type_traits>
4
5#include <boost/callable_traits/args.hpp>
6#include <boost/callable_traits/return_type.hpp>
7#include <boost/mp11/algorithm.hpp>
8
9namespace viam {
10namespace sdk {
11
12namespace proto_convert_details {
13
14// This is copied from range-v3 to allow the definition of callable object instances without
15// ODR/linkage issues. It is obviated in C++17 and onwards by constexpr inline.
16template <typename T>
18 static constexpr const T value{};
19};
20
21template <typename T>
22constexpr const T static_const<T>::value;
23
24// This struct should be explicitly specialized with a
25// void operator()(const SdkType&, common::v1::ApiType*) const
26// to provide API/ABI insulated proto conversion
27template <typename SdkType>
28struct to_proto;
29
30// This struct should be explicitly specialized with a
31// SdkType operator()(const ProtoType*) const
32// to provided API/ABI insulated construction from proto
33template <typename ProtoType>
35
36// This is a helper type trait for deducing corresponding API types from a to_proto specialization.
37// We use boost::callable_traits to generate a tuple of the arguments to the to_proto call operator,
38// of which the last entry (mp_back) is a pointer to the API type.
39template <typename Callable>
40using ProtoArgType = std::remove_pointer_t<
41 boost::mp11::mp_back<boost::callable_traits::args_t<Callable, boost::mp11::mp_list>>>;
42
43// Implementation struct for the omni-to_proto callable defined below.
45 template <typename SdkType>
46 auto operator()(const SdkType& t) const {
47 using ProtoReturnType = ProtoArgType<to_proto<SdkType>>;
48
49 ProtoReturnType ret;
50 to_proto<SdkType>{}(t, &ret);
51
52 return ret;
53 }
54};
55
56// Implementation struct for the omni-from_proto callable defined below.
58 template <typename ProtoType>
59 auto operator()(const ProtoType& proto) const { // NOLINT(misc-no-recursion)
60 return from_proto<ProtoType>{}(&proto);
61 }
62};
63
64} // namespace proto_convert_details
65
66namespace v2 {
67
68namespace {
69
73constexpr auto& to_proto =
75
79constexpr auto& from_proto =
81
82} // namespace
83
84} // namespace v2
85
88template <typename SdkType>
89using EquivalentApiType =
90 proto_convert_details::ProtoArgType<proto_convert_details::to_proto<SdkType>>;
91
94template <typename ApiType>
95using EquivalentSdkType =
96 boost::callable_traits::return_type_t<proto_convert_details::from_proto<ApiType>>;
97
98} // namespace sdk
99} // namespace viam
Definition proto_convert.hpp:34
Definition proto_convert.hpp:17
Definition proto_convert.hpp:28