Skip to content

Commit

Permalink
style: introduce consistent syle for Macro_Args - make them pop
Browse files Browse the repository at this point in the history
  • Loading branch information
strasdat committed Sep 24, 2023
1 parent 47457f7 commit e06e5b3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 66 deletions.
84 changes: 42 additions & 42 deletions cpp/farm_ng/core/proto_conv/std/conv_impl_macro.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -26,58 +26,58 @@
namespace farm_ng {

#define FARM_CONV_IMPL_OPTIONAL(Proto_Type_, Cpp_Type_) \
template <> \
template <> \
auto fromProt<Proto_Type_>(Proto_Type_ const& proto) \
-> Expected<std::optional<Cpp_Type_>> { \
std::optional<Cpp_Type_> v; \
if (proto.has_value()) { \
v = proto.value(); \
} else { \
v = std::nullopt; \
} \
return v; \
} \
\
template <> \
-> Expected<std::optional<Cpp_Type_>> { \
std::optional<Cpp_Type_> v; \
if (proto.has_value()) { \
v = proto.value(); \
} else { \
v = std::nullopt; \
} \
return v; \
} \
\
template <> \
auto toProt<std::optional<Cpp_Type_>>(std::optional<Cpp_Type_> const& v) \
-> Proto_Type_ { \
Proto_Type_ proto; \
if (v.has_value()) { \
proto.set_value(v.value()); \
proto.set_has_value(true); \
} else { \
proto.set_has_value(false); \
} \
return proto; \
-> Proto_Type_ { \
Proto_Type_ proto; \
if (v.has_value()) { \
proto.set_value(v.value()); \
proto.set_has_value(true); \
} else { \
proto.set_has_value(false); \
} \
return proto; \
}

// Proxy for FARM_CONV_IMPL_OPTIONAL to make it callable from
// FARM_PP_SEQ_FOR_EACH.
#define FARM_CONV_IMPL_OPTIONAL_ONE_ITER(D0, D1, Proto_Cpp_Pair) \
FARM_CONV_IMPL_OPTIONAL( \
FARM_PP_TUPLE_ELEM(0, Proto_Cpp_Pair), \
FARM_PP_TUPLE_ELEM(1, Proto_Cpp_Pair))
#define FARM_CONV_IMPL_OPTIONAL_ONE_ITER(D0_, D1_, Proto_Cpp_Pair_) \
FARM_CONV_IMPL_OPTIONAL( \
FARM_PP_TUPLE_ELEM(0, Proto_Cpp_Pair_), \
FARM_PP_TUPLE_ELEM(1, Proto_Cpp_Pair_))

#define FARM_CONV_IMPL_REPEATED(Proto_Type_, Cpp_Type_) \
template <> \
template <> \
auto fromProt<Proto_Type_>(Proto_Type_ const& proto) \
-> Expected<std::deque<Cpp_Type_>> { \
std::deque<Cpp_Type_> v; \
for (int i = 0; i < proto.value_size(); ++i) { \
FARM_TRY(auto, value, fromProt(proto.value(i))); \
v.push_back(value); \
} \
return v; \
} \
\
template <> \
-> Expected<std::deque<Cpp_Type_>> { \
std::deque<Cpp_Type_> v; \
for (int i = 0; i < proto.value_size(); ++i) { \
FARM_TRY(auto, value, fromProt(proto.value(i))); \
v.push_back(value); \
} \
return v; \
} \
\
template <> \
auto toProt<std::deque<Cpp_Type_>>(std::deque<Cpp_Type_> const& v) \
-> Proto_Type_ { \
Proto_Type_ proto; \
for (auto const& value : v) { \
*proto.add_value() = toProt(value); \
} \
return proto; \
-> Proto_Type_ { \
Proto_Type_ proto; \
for (auto const& value : v) { \
*proto.add_value() = toProt(value); \
} \
return proto; \
}

} // namespace farm_ng
6 changes: 3 additions & 3 deletions cpp/farm_ng/core/proto_conv/struct/conv_impl_macro.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
//
// Output: static_assert(TCpp::kFieldNames[i] == "name0");
#define FARM_STRUCT_DETAILS_ASSERT_FIELD_NAME( \
Dummy_, Field_Names_, Index_, Type_Name_Init) \
Dummy_, Field_Names_, Index_, Type_Name_Init_) \
static_assert( \
Field_Names_[Index_] == \
FARM_PP_STRINGIZE(FARM_PP_TUPLE_ELEM(0, Type_Name_Init)), \
FARM_PP_STRINGIZE(FARM_PP_TUPLE_ELEM(0, Type_Name_Init_)), \
"Field #" FARM_PP_STRINGIZE(Index_) " in FARM_PROTO_CONV_IMPL is `" \
FARM_PP_STRINGIZE(FARM_PP_TUPLE_ELEM(0, Type_Name_Init)) "` but expected `" \
FARM_PP_STRINGIZE(FARM_PP_TUPLE_ELEM(0, Type_Name_Init_)) "` but expected `" \
FARM_PP_STRINGIZE(Field_Names_[Index_]) "`.");
// Given a sequence of triplets, create a static_assert which checks that the
Expand Down
15 changes: 8 additions & 7 deletions cpp/farm_ng/core/struct/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
// Input: (Type0, name0, {init0})
// Output: Type name {init};
//
#define FARM_STRUCT_DETAILS_FIELD_DECLARATION(Dummy1_, Dummy2, Type_Name_Init) \
FARM_PP_TUPLE_ELEM(0, Type_Name_Init) \
FARM_PP_TUPLE_ELEM(1, Type_Name_Init) FARM_PP_TUPLE_ELEM(2, Type_Name_Init);
#define FARM_STRUCT_DETAILS_FIELD_DECLARATION( \
Dummy1_, Dummy2_, Type_Name_Init_) \
FARM_PP_TUPLE_ELEM(0, Type_Name_Init_) \
FARM_PP_TUPLE_ELEM(1, Type_Name_Init_) FARM_PP_TUPLE_ELEM(2, Type_Name_Init_);

// Given a sequence of triplets, create field declarations for
// each triplet.
Expand All @@ -58,8 +59,8 @@
//
// Input: (Type0, name0, {init0})
// Output: , "name0"
#define FARM_STRUCT_DETAILS_FIELD_NAME(Dummy1_, Dummy2, Type_Name_Init) \
, FARM_PP_STRINGIZE(FARM_PP_TUPLE_ELEM(1, Type_Name_Init))
#define FARM_STRUCT_DETAILS_FIELD_NAME(Dummy1_, Dummy2_, Type_Name_Init_) \
, FARM_PP_STRINGIZE(FARM_PP_TUPLE_ELEM(1, Type_Name_Init_))

// Given a sequence of triplets, create a comma separated list of field name
// strings.
Expand All @@ -83,8 +84,8 @@
//
// Input: (Type0, name0, {init0})
// Output: , Type
#define FARM_STRUCT_DETAILS_FIELD_TYPE(Dummy1_, Dummy2, Type_Name_Init) \
, FARM_PP_TUPLE_ELEM(0, Type_Name_Init)
#define FARM_STRUCT_DETAILS_FIELD_TYPE(Dummy1_, Dummy2_, Type_Name_Init_) \
, FARM_PP_TUPLE_ELEM(0, Type_Name_Init_)

// Given a sequence of triplets, create a comma separated list of field types.
//
Expand Down
14 changes: 7 additions & 7 deletions cpp/farm_ng/core/struct/macro_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
//
// Input: (A)
// Output: (((A))
#define FARM_MACROLIB_DETAILS_MAKE_TUPLE(D1, D2, Entity) \
((FARM_PP_REMOVE_PARENS(Entity)))
#define FARM_MACROLIB_DETAILS_MAKE_TUPLE(D1_, D2_, Entity_) \
((FARM_PP_REMOVE_PARENS(Entity_)))

// Given a tuple of entities, which might be tuples, return a sequence of
// tuples.
Expand All @@ -42,8 +42,8 @@
//
// Input: (A, (B,2) , C)
// Output: ((A))((B,2))((C))
#define FARM_MACROLIB_SEQ_OF_TUPLES_FROM_TUPLE(Tuple_Of_Entities) \
FARM_PP_SEQ_FOR_EACH( \
FARM_MACROLIB_DETAILS_MAKE_TUPLE, \
_, \
FARM_PP_TUPLE_TO_SEQ(Tuple_Of_Entities))
#define FARM_MACROLIB_SEQ_OF_TUPLES_FROM_TUPLE(Tuple_Of_Entities_) \
FARM_PP_SEQ_FOR_EACH( \
FARM_MACROLIB_DETAILS_MAKE_TUPLE, \
_, \
FARM_PP_TUPLE_TO_SEQ(Tuple_Of_Entities_))
14 changes: 7 additions & 7 deletions cpp/farm_ng/core/struct/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
//
// This intermediate macro exists since the conversion from a list to a sequence
// is likely somewhat expensive, so we want to do it once and reuse it.
#define FARM_STRUCT_DETAIL_FROM_SEQ(Struct_Name, Num_Fields_, Field_Seq_) \
struct Struct_Name { \
FARM_STRUCT_DETAILS_BASE(Num_Fields_, Field_Seq_) \
#define FARM_STRUCT_DETAIL_FROM_SEQ(Struct_Name_, Num_Fields_, Field_Seq_) \
struct Struct_Name_ { \
FARM_STRUCT_DETAILS_BASE(Num_Fields_, Field_Seq_) \
}

/// Takes in a struct name and a tuple of fields and generates a struct.
#define FARM_STRUCT(Struct_Name, Tuple_Of_Fields_) \
FARM_STRUCT_DETAIL_FROM_SEQ( \
Struct_Name, \
FARM_PP_TUPLE_SIZE(Tuple_Of_Fields_), \
#define FARM_STRUCT(Struct_Name_, Tuple_Of_Fields_) \
FARM_STRUCT_DETAIL_FROM_SEQ( \
Struct_Name_, \
FARM_PP_TUPLE_SIZE(Tuple_Of_Fields_), \
FARM_MACROLIB_SEQ_OF_TUPLES_FROM_TUPLE(Tuple_Of_Fields_))

0 comments on commit e06e5b3

Please sign in to comment.