Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/viam/sdk/common/client_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ClientHelper {
debug_key_ = *value.get<std::string>();
}

proto_convert_details::to_proto<ProtoStruct>{}(extra, request_.mutable_extra());
proto_convert_details::to_proto_impl<ProtoStruct>{}(extra, request_.mutable_extra());
return with(std::forward<RequestSetupCallable>(rsc));
}

Expand Down
4 changes: 2 additions & 2 deletions src/viam/sdk/common/linear_algebra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ Vector3& Vector3::set_z(double z) {

namespace proto_convert_details {

void to_proto<Vector3>::operator()(const Vector3& self, common::v1::Vector3* proto) const {
void to_proto_impl<Vector3>::operator()(const Vector3& self, common::v1::Vector3* proto) const {
proto->set_x(self.x());
proto->set_y(self.y());
proto->set_z(self.z());
}

Vector3 from_proto<common::v1::Vector3>::operator()(const common::v1::Vector3* proto) const {
Vector3 from_proto_impl<common::v1::Vector3>::operator()(const common::v1::Vector3* proto) const {
return {proto->x(), proto->y(), proto->z()};
}

Expand Down
4 changes: 2 additions & 2 deletions src/viam/sdk/common/linear_algebra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ struct Vector3 {
namespace proto_convert_details {

template <>
struct to_proto<Vector3> {
struct to_proto_impl<Vector3> {
void operator()(const Vector3&, common::v1::Vector3*) const;
};

template <>
struct from_proto<common::v1::Vector3> {
struct from_proto_impl<common::v1::Vector3> {
Vector3 operator()(const common::v1::Vector3*) const;
};

Expand Down
14 changes: 7 additions & 7 deletions src/viam/sdk/common/pose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ std::ostream& operator<<(std::ostream& os, const pose_in_frame& v) {

namespace proto_convert_details {

void to_proto<pose>::operator()(const pose& self, common::v1::Pose* proto) const {
void to_proto_impl<pose>::operator()(const pose& self, common::v1::Pose* proto) const {
proto->set_x(self.coordinates.x);
proto->set_y(self.coordinates.y);
proto->set_z(self.coordinates.z);
Expand All @@ -26,7 +26,7 @@ void to_proto<pose>::operator()(const pose& self, common::v1::Pose* proto) const
proto->set_theta(self.theta);
}

pose from_proto<common::v1::Pose>::operator()(const common::v1::Pose* proto) const {
pose from_proto_impl<common::v1::Pose>::operator()(const common::v1::Pose* proto) const {
pose pose;
pose.coordinates.x = proto->x();
pose.coordinates.y = proto->y();
Expand All @@ -39,19 +39,19 @@ pose from_proto<common::v1::Pose>::operator()(const common::v1::Pose* proto) con
return pose;
}

void to_proto<pose_in_frame>::operator()(const pose_in_frame& self,
common::v1::PoseInFrame* pif) const {
void to_proto_impl<pose_in_frame>::operator()(const pose_in_frame& self,
common::v1::PoseInFrame* pif) const {
*(pif->mutable_reference_frame()) = self.reference_frame;
common::v1::Pose proto_pose;
to_proto<pose>{}(self.pose, &proto_pose);
to_proto_impl<pose>{}(self.pose, &proto_pose);
*(pif->mutable_pose()) = std::move(proto_pose);
};

pose_in_frame from_proto<common::v1::PoseInFrame>::operator()(
pose_in_frame from_proto_impl<common::v1::PoseInFrame>::operator()(
const common::v1::PoseInFrame* proto) const {
pose_in_frame pif;
pif.reference_frame = proto->reference_frame();
pif.pose = from_proto<common::v1::Pose>{}(&(proto->pose()));
pif.pose = from_proto_impl<common::v1::Pose>{}(&(proto->pose()));

return pif;
}
Expand Down
8 changes: 4 additions & 4 deletions src/viam/sdk/common/pose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ struct pose_in_frame {
namespace proto_convert_details {

template <>
struct to_proto<pose> {
struct to_proto_impl<pose> {
void operator()(const pose&, common::v1::Pose*) const;
};

template <>
struct from_proto<common::v1::Pose> {
struct from_proto_impl<common::v1::Pose> {
pose operator()(const common::v1::Pose*) const;
};

template <>
struct to_proto<pose_in_frame> {
struct to_proto_impl<pose_in_frame> {
void operator()(const pose_in_frame&, common::v1::PoseInFrame*) const;
};

template <>
struct from_proto<common::v1::PoseInFrame> {
struct from_proto_impl<common::v1::PoseInFrame> {
pose_in_frame operator()(const common::v1::PoseInFrame*) const;
};

Expand Down
45 changes: 16 additions & 29 deletions src/viam/sdk/common/private/repeated_ptr_convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,29 @@ namespace viam {
namespace sdk {
namespace impl {

struct to_repeated_field_ {
template <typename T, typename = EquivalentApiType<T>>
auto operator()(const std::vector<T>& v) const {
::google::protobuf::RepeatedPtrField<EquivalentApiType<T>> result;
result.Reserve(v.size());
template <typename T, typename = EquivalentApiType<T>>
auto to_repeated_field(const std::vector<T>& v) {
::google::protobuf::RepeatedPtrField<EquivalentApiType<T>> result;
result.Reserve(v.size());

for (const auto& elem : v) {
*(result.Add()) = v2::to_proto(elem);
}

return result;
for (const auto& elem : v) {
*(result.Add()) = to_proto(elem);
}
};

struct from_repeated_field_ {
template <typename T, typename = EquivalentSdkType<T>>
auto operator()(const ::google::protobuf::RepeatedPtrField<T>& v) const {
std::vector<EquivalentSdkType<T>> result;
result.reserve(v.size());
return result;
}

for (const auto& elem : v) {
result.push_back(v2::from_proto(elem));
}
template <typename T, typename = EquivalentSdkType<T>>
auto from_repeated_field(const ::google::protobuf::RepeatedPtrField<T>& v) {
std::vector<EquivalentSdkType<T>> result;
result.reserve(v.size());

return result;
for (const auto& elem : v) {
result.push_back(from_proto(elem));
}
};

namespace {

constexpr auto& to_repeated_field = proto_convert_details::static_const<to_repeated_field_>::value;

constexpr auto& from_repeated_field =
proto_convert_details::static_const<from_repeated_field_>::value;

} // namespace
return result;
}

} // namespace impl
} // namespace sdk
Expand Down
2 changes: 1 addition & 1 deletion src/viam/sdk/common/private/service_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ServiceHelper : public ServiceHelperBase {
}

auto getExtra() const {
return request_->has_extra() ? v2::from_proto(request_->extra()) : ProtoStruct{};
return request_->has_extra() ? from_proto(request_->extra()) : ProtoStruct{};
}

private:
Expand Down
80 changes: 26 additions & 54 deletions src/viam/sdk/common/proto_convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,17 @@ namespace sdk {

namespace proto_convert_details {

// This is copied from range-v3 to allow the definition of callable object instances without
// ODR/linkage issues. It is obviated in C++17 and onwards by constexpr inline.
template <typename T>
struct static_const {
static constexpr const T value{};
};

template <typename T>
constexpr const T static_const<T>::value;

// This struct should be explicitly specialized with a
// void operator()(const SdkType&, common::v1::ApiType*) const
// to provide API/ABI insulated proto conversion
template <typename SdkType>
struct to_proto;
struct to_proto_impl;

// This struct should be explicitly specialized with a
// SdkType operator()(const ProtoType*) const
// to provided API/ABI insulated construction from proto
template <typename ProtoType>
struct from_proto;
struct from_proto_impl;

// This is a helper type trait for deducing corresponding API types from a to_proto specialization.
// We use boost::callable_traits to generate a tuple of the arguments to the to_proto call operator,
Expand All @@ -40,60 +30,42 @@ template <typename Callable>
using ProtoArgType = std::remove_pointer_t<
boost::mp11::mp_back<boost::callable_traits::args_t<Callable, boost::mp11::mp_list>>>;

// Implementation struct for the omni-to_proto callable defined below.
struct to_proto_impl {
template <typename SdkType>
auto operator()(const SdkType& t) const {
using ProtoReturnType = ProtoArgType<to_proto<SdkType>>;

ProtoReturnType ret;
to_proto<SdkType>{}(t, &ret);

return ret;
}
};

// Implementation struct for the omni-from_proto callable defined below.
struct from_proto_impl {
template <typename ProtoType>
auto operator()(const ProtoType& proto) const { // NOLINT(misc-no-recursion)
return from_proto<ProtoType>{}(&proto);
}
};

} // namespace proto_convert_details

namespace v2 {

namespace {

/// @brief Function object implementing conversion from an SDK type to an API type.
/// This callable works for any type with a proto_convert_details::to_proto specialization as
/// described above.
constexpr auto& to_proto =
proto_convert_details::static_const<proto_convert_details::to_proto_impl>::value;

/// @brief Function object implementing conversion from an API type to an SDK type.
/// This callable works for any type with a proto_convert_details::from_proto specialization as
/// described above.
constexpr auto& from_proto =
proto_convert_details::static_const<proto_convert_details::from_proto_impl>::value;

} // namespace

} // namespace v2
/// @brief Convert an SDK type to its corresponding API type.
/// @remark Only participates in overload resolution if to_proto_impl<SdkType> has been specialized.
template <typename SdkType,
typename = decltype(sizeof(proto_convert_details::to_proto_impl<SdkType>))>
auto to_proto(const SdkType& t) { // NOLINT(misc-no-recursion)
namespace pcd = proto_convert_details;
using ProtoReturnType = pcd::ProtoArgType<pcd::to_proto_impl<SdkType>>;

ProtoReturnType ret;
pcd::to_proto_impl<SdkType>{}(t, &ret);

return ret;
}

/// @brief Convert an API type to its corresponding SDK type.
/// @remark Only participates in overload resolution if from_proto_impl<ApiType> has been
/// specialized.
template <typename ApiType,
typename = decltype(sizeof(proto_convert_details::from_proto_impl<ApiType>))>
auto from_proto(const ApiType& proto) { // NOLINT(misc-no-recursion)
return proto_convert_details::from_proto_impl<ApiType>{}(&proto);
}

/// @brief Type alias for the API type corresponding to a given SDK type.
/// This is the return type of calling to_proto on an instance of SdkType.
template <typename SdkType>
using EquivalentApiType =
proto_convert_details::ProtoArgType<proto_convert_details::to_proto<SdkType>>;
proto_convert_details::ProtoArgType<proto_convert_details::to_proto_impl<SdkType>>;

/// @brief Type alias for the SDK type corresponding to a given API type.
/// This is the return type of calling from_proto on an instance of ApiType.
template <typename ApiType>
using EquivalentSdkType =
boost::callable_traits::return_type_t<proto_convert_details::from_proto<ApiType>>;
boost::callable_traits::return_type_t<proto_convert_details::from_proto_impl<ApiType>>;

} // namespace sdk
} // namespace viam
22 changes: 12 additions & 10 deletions src/viam/sdk/common/proto_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,24 +237,25 @@ void to_value(std::string s, Value* v) {
void to_value(const ProtoList& vec, Value* v) {
::google::protobuf::ListValue l;
for (const auto& val : vec) {
*l.add_values() = v2::to_proto(val);
*l.add_values() = to_proto(val);
}
*(v->mutable_list_value()) = std::move(l);
}

void to_value(const ProtoStruct& m, Value* v) {
*(v->mutable_struct_value()) = v2::to_proto(m);
*(v->mutable_struct_value()) = to_proto(m);
}

} // namespace proto_value_details

namespace proto_convert_details {

void to_proto<ProtoValue>::operator()(const ProtoValue& self, google::protobuf::Value* v) const {
void to_proto_impl<ProtoValue>::operator()(const ProtoValue& self,
google::protobuf::Value* v) const {
self.vtable_.to_value(self.self_.get(), v);
}

ProtoValue from_proto<google::protobuf::Value>::operator()( // NOLINT(misc-no-recursion)
ProtoValue from_proto_impl<google::protobuf::Value>::operator()( // NOLINT(misc-no-recursion)
const google::protobuf::Value* v) const {
switch (v->kind_case()) {
case Value::KindCase::kBoolValue: {
Expand All @@ -270,13 +271,13 @@ ProtoValue from_proto<google::protobuf::Value>::operator()( // NOLINT(misc-no-r
ProtoList vec;
vec.reserve(v->list_value().values_size());
for (const Value& list_val : v->list_value().values()) {
vec.push_back(v2::from_proto(list_val));
vec.push_back(from_proto(list_val));
}

return ProtoValue(std::move(vec));
}
case Value::KindCase::kStructValue: {
return ProtoValue(v2::from_proto(v->struct_value()));
return ProtoValue(from_proto(v->struct_value()));
}
case Value::KindCase::KIND_NOT_SET:
case Value::KindCase::kNullValue:
Expand All @@ -285,19 +286,20 @@ ProtoValue from_proto<google::protobuf::Value>::operator()( // NOLINT(misc-no-r
}
}

void to_proto<ProtoStruct>::operator()(const ProtoStruct& self, google::protobuf::Struct* s) const {
void to_proto_impl<ProtoStruct>::operator()(const ProtoStruct& self,
google::protobuf::Struct* s) const {
for (const auto& kv : self) {
s->mutable_fields()->insert(
google::protobuf::MapPair<std::string, Value>(kv.first, v2::to_proto(kv.second)));
google::protobuf::MapPair<std::string, Value>(kv.first, to_proto(kv.second)));
}
}

ProtoStruct from_proto<google::protobuf::Struct>::operator()( // NOLINT(misc-no-recursion)
ProtoStruct from_proto_impl<google::protobuf::Struct>::operator()( // NOLINT(misc-no-recursion)
const google::protobuf::Struct* s) const {
ProtoStruct result;

for (const auto& val : s->fields()) {
result.emplace(val.first, v2::from_proto(val.second));
result.emplace(val.first, from_proto(val.second));
}

return result;
Expand Down
10 changes: 5 additions & 5 deletions src/viam/sdk/common/proto_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct all_moves_noexcept
/// definition.
class ProtoValue {
public:
friend proto_convert_details::to_proto<ProtoValue>;
friend proto_convert_details::to_proto_impl<ProtoValue>;

/// @brief Type discriminator constants for possible values stored in a ProtoValue.
enum Kind { k_null = 0, k_bool = 1, k_double = 2, k_string = 3, k_list = 4, k_struct = 5 };
Expand Down Expand Up @@ -318,22 +318,22 @@ extern template ProtoStruct&& ProtoValue::get_unchecked<ProtoStruct>() &&;
namespace proto_convert_details {

template <>
struct to_proto<ProtoValue> {
struct to_proto_impl<ProtoValue> {
void operator()(const ProtoValue&, google::protobuf::Value*) const;
};

template <>
struct to_proto<ProtoStruct> {
struct to_proto_impl<ProtoStruct> {
void operator()(const ProtoStruct&, google::protobuf::Struct*) const;
};

template <>
struct from_proto<google::protobuf::Value> {
struct from_proto_impl<google::protobuf::Value> {
ProtoValue operator()(const google::protobuf::Value*) const;
};

template <>
struct from_proto<google::protobuf::Struct> {
struct from_proto_impl<google::protobuf::Struct> {
ProtoStruct operator()(const google::protobuf::Struct*) const;
};

Expand Down
Loading
Loading