Skip to content

Commit

Permalink
use k_ for enum names and add explicit numerical values
Browse files Browse the repository at this point in the history
  • Loading branch information
lia-viam committed Sep 20, 2024
1 parent 31116da commit f5e94cf
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/viam/sdk/common/proto_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ ProtoValue::Kind ProtoValue::kind() const {
}

bool ProtoValue::is_null() const {
return kind() == Kind::null;
return kind() == Kind::k_null;
}

template <typename T>
Expand Down
24 changes: 16 additions & 8 deletions src/viam/sdk/common/proto_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ struct all_moves_noexcept
class ProtoValue {
public:
/// @brief Type discriminator constants for possible values stored in a ProtoValue.
enum Kind { null, bool_, int_, double_, string, list, struct_ };
enum Kind {
k_null = 0,
k_bool = 1,
k_double = 2,
k_string = 3,
k_list = 4,
k_struct = 5,
k_int = 6
};

/// @brief Construct a null object.
ProtoValue() noexcept;
Expand Down Expand Up @@ -355,37 +363,37 @@ using KindConstant = std::integral_constant<ProtoValue::Kind, k>;

template <>
struct kind<std::nullptr_t> {
using type = KindConstant<ProtoValue::Kind::null>;
using type = KindConstant<ProtoValue::Kind::k_null>;
};

template <>
struct kind<bool> {
using type = KindConstant<ProtoValue::Kind::bool_>;
using type = KindConstant<ProtoValue::Kind::k_bool>;
};

template <>
struct kind<int> {
using type = KindConstant<ProtoValue::Kind::int_>;
using type = KindConstant<ProtoValue::Kind::k_int>;
};

template <>
struct kind<double> {
using type = KindConstant<ProtoValue::Kind::double_>;
using type = KindConstant<ProtoValue::Kind::k_double>;
};

template <>
struct kind<std::string> {
using type = KindConstant<ProtoValue::Kind::string>;
using type = KindConstant<ProtoValue::Kind::k_string>;
};

template <>
struct kind<ProtoList> {
using type = KindConstant<ProtoValue::Kind::list>;
using type = KindConstant<ProtoValue::Kind::k_list>;
};

template <>
struct kind<ProtoStruct> {
using type = KindConstant<ProtoValue::Kind::struct_>;
using type = KindConstant<ProtoValue::Kind::k_struct>;
};

} // namespace proto_value_details
Expand Down
42 changes: 21 additions & 21 deletions src/viam/sdk/common/proto_value_visit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ template <typename Visitor>
auto visit(Visitor&& visitor, ProtoValue& value)
-> decltype(std::forward<Visitor>(visitor)(std::declval<std::nullptr_t&>())) {
switch (value.kind()) {
case ProtoValue::Kind::bool_:
case ProtoValue::Kind::k_bool:
return std::forward<Visitor>(visitor)(value.get_unchecked<bool>());
case ProtoValue::Kind::int_:
case ProtoValue::Kind::k_int:
return std::forward<Visitor>(visitor)(value.get_unchecked<int>());
case ProtoValue::Kind::double_:
case ProtoValue::Kind::k_double:
return std::forward<Visitor>(visitor)(value.get_unchecked<double>());
case ProtoValue::Kind::string:
case ProtoValue::Kind::k_string:
return std::forward<Visitor>(visitor)(value.get_unchecked<std::string>());
case ProtoValue::Kind::list:
case ProtoValue::Kind::k_list:
return std::forward<Visitor>(visitor)(value.get_unchecked<ProtoList>());
case ProtoValue::Kind::struct_:
case ProtoValue::Kind::k_struct:
return std::forward<Visitor>(visitor)(value.get_unchecked<ProtoStruct>());
case ProtoValue::Kind::null: {
case ProtoValue::Kind::k_null: {
auto np = nullptr;
return std::forward<Visitor>(visitor)(np);
}
Expand All @@ -45,19 +45,19 @@ template <typename Visitor>
auto visit(Visitor&& visitor, const ProtoValue& value)
-> decltype(std::forward<Visitor>(visitor)(std::declval<const std::nullptr_t&>())) {
switch (value.kind()) {
case ProtoValue::Kind::bool_:
case ProtoValue::Kind::k_bool:
return std::forward<Visitor>(visitor)(value.get_unchecked<bool>());
case ProtoValue::Kind::int_:
case ProtoValue::Kind::k_int:
return std::forward<Visitor>(visitor)(value.get_unchecked<int>());
case ProtoValue::Kind::double_:
case ProtoValue::Kind::k_double:
return std::forward<Visitor>(visitor)(value.get_unchecked<double>());
case ProtoValue::Kind::string:
case ProtoValue::Kind::k_string:
return std::forward<Visitor>(visitor)(value.get_unchecked<std::string>());
case ProtoValue::Kind::list:
case ProtoValue::Kind::k_list:
return std::forward<Visitor>(visitor)(value.get_unchecked<ProtoList>());
case ProtoValue::Kind::struct_:
case ProtoValue::Kind::k_struct:
return std::forward<Visitor>(visitor)(value.get_unchecked<ProtoStruct>());
case ProtoValue::Kind::null: {
case ProtoValue::Kind::k_null: {
const auto np = nullptr;
return std::forward<Visitor>(visitor)(np);
}
Expand All @@ -68,19 +68,19 @@ template <typename Visitor>
auto visit(Visitor&& visitor, ProtoValue&& value)
-> decltype(std::forward<Visitor>(visitor)(std::declval<std::nullptr_t&&>())) {
switch (value.kind()) {
case ProtoValue::Kind::bool_:
case ProtoValue::Kind::k_bool:
return std::forward<Visitor>(visitor)(std::move(value.get_unchecked<bool>()));
case ProtoValue::Kind::int_:
case ProtoValue::Kind::k_int:
return std::forward<Visitor>(visitor)(std::move(value.get_unchecked<int>()));
case ProtoValue::Kind::double_:
case ProtoValue::Kind::k_double:
return std::forward<Visitor>(visitor)(std::move(value.get_unchecked<double>()));
case ProtoValue::Kind::string:
case ProtoValue::Kind::k_string:
return std::forward<Visitor>(visitor)(std::move(value.get_unchecked<std::string>()));
case ProtoValue::Kind::list:
case ProtoValue::Kind::k_list:
return std::forward<Visitor>(visitor)(std::move(value.get_unchecked<ProtoList>()));
case ProtoValue::Kind::struct_:
case ProtoValue::Kind::k_struct:
return std::forward<Visitor>(visitor)(std::move(value.get_unchecked<ProtoStruct>()));
case ProtoValue::Kind::null:
case ProtoValue::Kind::k_null:
return std::forward<Visitor>(visitor)(std::nullptr_t());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/viam/sdk/tests/test_proto_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ BOOST_AUTO_TEST_CASE(test_object_equality) {
{
ProtoValue i5(5);
auto int_roundtrip = ProtoValue::from_proto(to_proto(i5));
BOOST_CHECK(i5.kind() == ProtoValue::Kind::int_);
BOOST_CHECK(i5.kind() == ProtoValue::Kind::k_int);
BOOST_CHECK(i5.is_a<int>());

BOOST_CHECK(int_roundtrip.kind() == ProtoValue::Kind::double_);
BOOST_CHECK(int_roundtrip.kind() == ProtoValue::Kind::k_double);
BOOST_CHECK(int_roundtrip.is_a<double>());

BOOST_CHECK(!(i5 == int_roundtrip));
Expand Down
28 changes: 14 additions & 14 deletions src/viam/sdk/tests/test_proto_value_visit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,30 @@ struct rvalue_visitor {
// reference. Thus we spell out the overloads.

bool operator()(std::nullptr_t&&) && {
return kind == ProtoValue::Kind::null;
return kind == ProtoValue::Kind::k_null;
}
bool operator()(bool&&) && {
return kind == ProtoValue::Kind::bool_;
return kind == ProtoValue::Kind::k_bool;
}

bool operator()(int&&) && {
return kind == ProtoValue::Kind::int_;
return kind == ProtoValue::Kind::k_int;
}

bool operator()(double&&) && {
return kind == ProtoValue::Kind::double_;
return kind == ProtoValue::Kind::k_double;
}

bool operator()(std::string&&) && {
return kind == ProtoValue::Kind::string;
return kind == ProtoValue::Kind::k_string;
}

bool operator()(ProtoList&&) && {
return kind == ProtoValue::Kind::list;
return kind == ProtoValue::Kind::k_list;
}

bool operator()(ProtoStruct&&) && {
return kind == ProtoValue::Kind::struct_;
return kind == ProtoValue::Kind::k_struct;
}

bool operator()(...) && {
Expand All @@ -131,13 +131,13 @@ struct rvalue_visitor {

BOOST_AUTO_TEST_CASE(test_visitor) {
auto test_cases = std::make_tuple(
std::make_pair(ProtoValue::Kind::null, nullptr),
std::make_pair(ProtoValue::Kind::bool_, true),
std::make_pair(ProtoValue::Kind::int_, 5),
std::make_pair(ProtoValue::Kind::double_, 12.345),
std::make_pair(ProtoValue::Kind::string, "meow"),
std::make_pair(ProtoValue::Kind::list, ProtoList({{ProtoValue(1), ProtoValue("woof")}})),
std::make_pair(ProtoValue::Kind::struct_, ProtoStruct({{"string", "str"}, {"int", 5}})));
std::make_pair(ProtoValue::Kind::k_null, nullptr),
std::make_pair(ProtoValue::Kind::k_bool, true),
std::make_pair(ProtoValue::Kind::k_int, 5),
std::make_pair(ProtoValue::Kind::k_double, 12.345),
std::make_pair(ProtoValue::Kind::k_string, "meow"),
std::make_pair(ProtoValue::Kind::k_list, ProtoList({{ProtoValue(1), ProtoValue("woof")}})),
std::make_pair(ProtoValue::Kind::k_struct, ProtoStruct({{"string", "str"}, {"int", 5}})));

using visitors = mp_list<const_visitor, mutable_visitor, by_value_visitor, rvalue_visitor>;

Expand Down

0 comments on commit f5e94cf

Please sign in to comment.