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 struct should be explicitly specialized with a
15// void operator()(const SdkType&, common::v1::ApiType*) const
16// to provide API/ABI insulated proto conversion
17template <typename SdkType>
19
20// This struct should be explicitly specialized with a
21// SdkType operator()(const ProtoType*) const
22// to provided API/ABI insulated construction from proto
23template <typename ProtoType>
25
26// This is a helper type trait for deducing corresponding API types from a to_proto specialization.
27// We use boost::callable_traits to generate a tuple of the arguments to the to_proto call operator,
28// of which the last entry (mp_back) is a pointer to the API type.
29template <typename Callable>
30using ProtoArgType = std::remove_pointer_t<
31 boost::mp11::mp_back<boost::callable_traits::args_t<Callable, boost::mp11::mp_list>>>;
32
33} // namespace proto_convert_details
34
37template <typename SdkType,
38 typename = decltype(sizeof(proto_convert_details::to_proto_impl<SdkType>))>
39auto to_proto(const SdkType& t) { // NOLINT(misc-no-recursion)
40 namespace pcd = proto_convert_details;
41 using ProtoReturnType = pcd::ProtoArgType<pcd::to_proto_impl<SdkType>>;
42
43 ProtoReturnType ret;
44 pcd::to_proto_impl<SdkType>{}(t, &ret);
45
46 return ret;
47}
48
52template <typename ApiType,
53 typename = decltype(sizeof(proto_convert_details::from_proto_impl<ApiType>))>
54auto from_proto(const ApiType& proto) { // NOLINT(misc-no-recursion)
55 return proto_convert_details::from_proto_impl<ApiType>{}(&proto);
56}
57
60template <typename SdkType>
61using EquivalentApiType =
62 proto_convert_details::ProtoArgType<proto_convert_details::to_proto_impl<SdkType>>;
63
66template <typename ApiType>
67using EquivalentSdkType =
68 boost::callable_traits::return_type_t<proto_convert_details::from_proto_impl<ApiType>>;
69
70} // namespace sdk
71} // namespace viam