Skip to content

Commit c1c1d89

Browse files
committed
Simplify macro, absorb existence of parenthesis on type name
1 parent 2538638 commit c1c1d89

4 files changed

Lines changed: 71 additions & 37 deletions

File tree

include/iris/pp/arg.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef IRIS_ZZ_PP_ARG_HPP
2+
#define IRIS_ZZ_PP_ARG_HPP
3+
4+
// SPDX-License-Identifier: MIT
5+
6+
#include <iris/pp/if.hpp>
7+
8+
#define IRIS_PP_IDENTITY(x) x
9+
10+
#define IRIS_ZZ_PP_EXPAND_I(...) __VA_ARGS__
11+
#define IRIS_PP_EXPAND(...) IRIS_ZZ_PP_EXPAND_I(__VA_ARGS__)
12+
13+
#define IRIS_ZZ_PP_PROBE() ~, 1
14+
15+
#define IRIS_ZZ_PP_SECOND(a, b, ...) b
16+
#define IRIS_ZZ_PP_IS_PROBE(...) IRIS_ZZ_PP_SECOND(__VA_ARGS__, 0)
17+
#define IRIS_ZZ_PP_IS_PAREN_PROBE(...) IRIS_ZZ_PP_PROBE()
18+
19+
#define IRIS_PP_IS_PAREN(x) IRIS_ZZ_PP_IS_PROBE(IRIS_ZZ_PP_IS_PAREN_PROBE x)
20+
21+
#define IRIS_PP_UNPAREN(x) IRIS_PP_EXPAND x
22+
#define IRIS_PP_UNPAREN_IF_PAREN(x) \
23+
IRIS_PP_IF(IRIS_PP_IS_PAREN(x), IRIS_PP_UNPAREN, IRIS_PP_IDENTITY) (x)
24+
25+
#endif

include/iris/pp/cat.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
// SPDX-License-Identifier: MIT
55

6-
#define IRIS_PP_CAT_I(a, b) a##b
7-
#define IRIS_PP_CAT(a, b) IRIS_PP_CAT_I(a, b)
6+
#define IRIS_ZZ_PP_CAT_I(a, b) a##b
7+
#define IRIS_PP_CAT(a, b) IRIS_ZZ_PP_CAT_I(a, b)
8+
9+
#define IRIS_ZZ_PP_CAT_ONLY_TWO_I(a, b, ...) a##b
10+
#define IRIS_PP_CAT_ONLY_TWO(a, b, ...) IRIS_ZZ_PP_CAT_ONLY_TWO_I(a, b)
811

912
#endif

include/iris/sfield/adapt.hpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <iris/pp/tuple.hpp>
1313
#include <iris/pp/stringize.hpp>
1414
#include <iris/pp/cat.hpp>
15+
#include <iris/pp/arg.hpp>
1516

1617
#include <string>
1718
#include <string_view>
@@ -58,22 +59,20 @@ struct field_definition
5859
} // detail
5960

6061

61-
#define IRIS_ZZ_SFIELD_EXPAND_TYPE(...) __VA_ARGS__
62-
6362
#define IRIS_ZZ_SFIELD_DATA_MEMBER_NAME(field_name) field_name ## _
6463

65-
#define IRIS_ZZ_SFIELD_DATA_MEMBER(paren_type, field_name, ...) \
64+
#define IRIS_ZZ_SFIELD_DATA_MEMBER(maybe_paren_type, field_name, ...) \
6665
private: \
67-
IRIS_ZZ_SFIELD_EXPAND_TYPE paren_type IRIS_ZZ_SFIELD_DATA_MEMBER_NAME(field_name){__VA_ARGS__};
66+
IRIS_PP_UNPAREN_IF_PAREN(maybe_paren_type) IRIS_ZZ_SFIELD_DATA_MEMBER_NAME(field_name){__VA_ARGS__};
6867

69-
#define IRIS_ZZ_SFIELD_GETTER(paren_type, field_name) \
68+
#define IRIS_ZZ_SFIELD_GETTER(maybe_paren_type, field_name) \
7069
public: \
71-
[[nodiscard]] constexpr iris::sfield::detail::getter_return_t<IRIS_ZZ_SFIELD_EXPAND_TYPE paren_type> get_ ## field_name () const noexcept \
70+
[[nodiscard]] constexpr iris::sfield::detail::getter_return_t<IRIS_PP_UNPAREN_IF_PAREN(maybe_paren_type)> get_ ## field_name () const noexcept \
7271
{ \
7372
return IRIS_ZZ_SFIELD_DATA_MEMBER_NAME(field_name); \
7473
} \
7574

76-
#define IRIS_ZZ_SFIELD_GETTER_BOOL(paren_type, field_name) \
75+
#define IRIS_ZZ_SFIELD_GETTER_BOOL(maybe_paren_type, field_name) \
7776
public: \
7877
[[nodiscard]] constexpr bool is_ ## field_name () const noexcept \
7978
{ \
@@ -86,42 +85,48 @@ struct field_definition
8685
return IRIS_ZZ_SFIELD_DATA_MEMBER_NAME(field_name); \
8786
}
8887

89-
#define IRIS_ZZ_SFIELD_SETTER(paren_type, field_name) \
88+
#define IRIS_ZZ_SFIELD_SETTER(maybe_paren_type, field_name) \
9089
public: \
91-
constexpr void set_ ## field_name (iris::sfield::detail::setter_param_t<IRIS_ZZ_SFIELD_EXPAND_TYPE paren_type> new_value) \
92-
noexcept(std::is_nothrow_copy_assignable_v<IRIS_ZZ_SFIELD_EXPAND_TYPE paren_type>) \
90+
constexpr void set_ ## field_name (iris::sfield::detail::setter_param_t<IRIS_PP_UNPAREN_IF_PAREN(maybe_paren_type)> new_value) \
91+
noexcept(std::is_nothrow_copy_assignable_v<IRIS_PP_UNPAREN_IF_PAREN(maybe_paren_type)>) \
9392
{ \
9493
IRIS_ZZ_SFIELD_DATA_MEMBER_NAME(field_name) = new_value; \
9594
}
9695

97-
#define IRIS_SFIELD_GET(paren_type, field_name, ...) \
98-
IRIS_ZZ_SFIELD_DATA_MEMBER(paren_type, field_name, __VA_ARGS__) \
99-
IRIS_ZZ_SFIELD_GETTER(paren_type, field_name)
100-
101-
#define IRIS_SFIELD_GET_SET(paren_type, field_name, ...) \
102-
IRIS_ZZ_SFIELD_DATA_MEMBER(paren_type, field_name, __VA_ARGS__) \
103-
IRIS_ZZ_SFIELD_GETTER(paren_type, field_name) \
104-
IRIS_ZZ_SFIELD_SETTER(paren_type, field_name)
105-
106-
#define IRIS_SFIELD(paren_type, field_name, ...) \
107-
IRIS_SFIELD_GET_SET(paren_type, field_name, __VA_ARGS__)
108-
10996

11097
// Default value for bool field is mandatory because it is error prone if omitted
111-
#define IRIS_SFIELD_BOOL_GET(paren_type, field_name, default_value) \
112-
IRIS_ZZ_SFIELD_DATA_MEMBER(paren_type, field_name, default_value) \
113-
IRIS_ZZ_SFIELD_GETTER_BOOL(paren_type, field_name)
98+
#define IRIS_ZZ_SFIELD_BOOL_GET(maybe_paren_type, field_name, default_value) \
99+
IRIS_ZZ_SFIELD_DATA_MEMBER(maybe_paren_type, field_name, default_value) \
100+
IRIS_ZZ_SFIELD_GETTER_BOOL(maybe_paren_type, field_name)
114101

115102
// Default value for bool field is mandatory because it is error prone if omitted
116-
#define IRIS_SFIELD_BOOL_GET_SET(paren_type, field_name, default_value) \
117-
IRIS_ZZ_SFIELD_DATA_MEMBER(paren_type, field_name, default_value) \
118-
IRIS_ZZ_SFIELD_GETTER_BOOL(paren_type, field_name) \
119-
IRIS_ZZ_SFIELD_SETTER(paren_type, field_name)
103+
#define IRIS_ZZ_SFIELD_BOOL_GET_SET(maybe_paren_type, field_name, default_value) \
104+
IRIS_ZZ_SFIELD_DATA_MEMBER(maybe_paren_type, field_name, default_value) \
105+
IRIS_ZZ_SFIELD_GETTER_BOOL(maybe_paren_type, field_name) \
106+
IRIS_ZZ_SFIELD_SETTER(maybe_paren_type, field_name)
120107

121108
// Default value for bool field is mandatory because it is error prone if omitted
122-
#define IRIS_SFIELD_BOOL(paren_type, field_name, default_value) \
123-
IRIS_SFIELD_BOOL_GET_SET(paren_type, field_name, default_value)
109+
#define IRIS_ZZ_SFIELD_BOOL(maybe_paren_type, field_name, default_value) \
110+
IRIS_ZZ_SFIELD_BOOL_GET_SET(maybe_paren_type, field_name, default_value)
111+
112+
113+
#define IRIS_SFIELD_GET(maybe_paren_type, field_name, ...) \
114+
IRIS_ZZ_SFIELD_DATA_MEMBER(maybe_paren_type, field_name, __VA_ARGS__) \
115+
IRIS_ZZ_SFIELD_GETTER(maybe_paren_type, field_name)
116+
117+
#define IRIS_SFIELD_GET_SET(maybe_paren_type, field_name, ...) \
118+
IRIS_ZZ_SFIELD_DATA_MEMBER(maybe_paren_type, field_name, __VA_ARGS__) \
119+
IRIS_ZZ_SFIELD_GETTER(maybe_paren_type, field_name) \
120+
IRIS_ZZ_SFIELD_SETTER(maybe_paren_type, field_name)
121+
122+
#define IRIS_ZZ_SFIELD_TYPE_IS_bool (bool)
124123

124+
#define IRIS_SFIELD(maybe_paren_type, field_name, ...) \
125+
IRIS_PP_IF( \
126+
IRIS_PP_IS_PAREN( IRIS_PP_CAT_ONLY_TWO(IRIS_ZZ_SFIELD_TYPE_IS_, IRIS_PP_UNPAREN_IF_PAREN(maybe_paren_type)) ), \
127+
IRIS_ZZ_SFIELD_BOOL_GET_SET, \
128+
IRIS_SFIELD_GET_SET \
129+
) (maybe_paren_type, field_name, __VA_ARGS__)
125130

126131
#define IRIS_SFIELD_CLASS(class_name) \
127132
template<class Class> \

test/sfield.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ struct MyData
132132
{
133133
IRIS_SFIELD_CLASS(MyData)
134134

135-
IRIS_SFIELD((std::string), name)
136-
IRIS_SFIELD((std::string), text, "empty text")
137-
IRIS_SFIELD((int), age)
138-
IRIS_SFIELD_BOOL((bool), enabled, true)
135+
IRIS_SFIELD(std::string, name)
136+
IRIS_SFIELD(std::string, text, "empty text")
137+
IRIS_SFIELD((std::pair<int, int>), pair)
138+
IRIS_SFIELD(int, age)
139+
IRIS_SFIELD(bool, enabled, true)
139140
};
140141

141142
IRIS_SFIELD_ADAPT(

0 commit comments

Comments
 (0)