farm-ng-core
proto_conv.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
18 
19 namespace farm_ng {
20 
21 /// Trait to associate one concrete Proto type with given C++ type `TCpp`.
22 ///
23 /// Helper trait for the `toProt` function template.
24 template <class TCpp>
25 struct ToProtoTrait;
26 
27 /// Trait to associate one concrete Cpp type with given proto type `TProto`.
28 ///
29 /// Helper trait for the `fromProt` function template.
30 template <class TProto>
32 
33 /// Function template to convert a C++ object to a proto object.
34 ///
35 /// Example:
36 ///
37 /// Eigen::Vector3d v(1, 2, 3);
38 /// proto::Vec3F64 proto = toProt(v);
39 ///
40 /// Note: The return type is deduced from the argument type using the
41 /// `FromProtoTrait` trait.
42 template <class TProto>
43 auto fromProt(TProto const& proto)
45 
46 /// Function template to convert a proto object to a C++ object.
47 ///
48 /// Example:
49 ///
50 /// proto::Vec3F64 proto;
51 /// proto.set_x(1);
52 /// proto.set_y(2);
53 /// proto.set_z(3);
54 /// Eigen::Vector3d v = fromProt(proto);
55 ///
56 /// Note: The return type is deduced from the argument type using the
57 /// `ToProtoTrait` trait.
58 template <class TCpp>
59 auto toProt(TCpp const& cpp) -> typename ToProtoTrait<TCpp>::ProtoType;
60 
61 /// Convenience macro to define the `ToProtoTrait` and `FromProtoTrait` as well
62 /// as the `toProt` and `fromProt` function templates instantiation declarations
63 /// for a given C++ type `TCpp` and a given proto type `TProto`. Hence, this
64 /// macro is typically used in a header file.
65 ///
66 /// Example:
67 ///
68 /// FARM_PROTO_CONV_TRAIT(Eigen::Vector3d, proto::Vec3F64);
69 ///
70 /// produces the following code:
71 ///
72 /// template <>
73 /// struct ToProtoTrait<Eigen::Vector3d> {
74 /// using ProtoType = proto::Vec3F64;
75 /// };
76 ///
77 /// template <>
78 /// struct FromProtoTrait<proto::Vec3F64> {
79 /// using CppType = Eigen::Vector3d;
80 /// };
81 ///
82 /// template <>
83 /// auto fromProt<proto::Vec3F64>(proto::Vec3F64 const& proto)
84 /// -> Expected<Eigen::Vector3d>;
85 ///
86 /// template <>
87 /// auto toProt<Eigen::Vector3d>(Eigen::Vector3d const& cpp)
88 /// -> proto::Vec3F64;
89 ///
90 #define FARM_PROTO_CONV_TRAIT(TCpp, TProto) \
91  template <> \
92  struct ToProtoTrait<TCpp> { \
93  using ProtoType = TProto; \
94  }; \
95  template <> \
96  struct FromProtoTrait<TProto> { \
97  using CppType = TCpp; \
98  }; \
99  template <> \
100  auto fromProt<TProto>(TProto const& proto)->Expected<TCpp>; \
101  \
102  template <> \
103  auto toProt<TCpp>(TCpp const& cpp)->TProto
104 
105 } // namespace farm_ng
farm_ng
Definition: backtrace.cpp:102
farm_ng::toProt
core::proto::Uri toProt(Uri const &uri)
Definition: conv.cpp:27
expected.h
farm_ng::FromProtoTrait
Trait to associate one concrete Cpp type with given proto type TProto.
Definition: proto_conv.h:31
farm_ng::fromProt
auto fromProt(core::proto::Uri const &proto) -> Expected< Uri >
Definition: conv.cpp:22
farm_ng::ToProtoTrait
Trait to associate one concrete Proto type with given C++ type TCpp.
Definition: proto_conv.h:25
farm_ng::Expected
tl::expected< TT, TE > Expected
Definition: expected.h:37