Skip to content

Commit 6c2a161

Browse files
committed
Replace std::enable_if with concept constraints
1 parent e43fea6 commit 6c2a161

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

include/graphqlservice/GraphQLClient.h

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ concept InputVariableClass = std::is_class_v<Type> && !ScalarVariableClass<Type>
8383
template <TypeModifier... Other>
8484
concept OnlyNoneModifiers = (... && (Other == TypeModifier::None));
8585

86+
// Test if the next modifier is Nullable.
87+
template <TypeModifier Modifier>
88+
concept NullableModifier = Modifier == TypeModifier::Nullable;
89+
90+
// Test if the next modifier is List.
91+
template <TypeModifier Modifier>
92+
concept ListModifier = Modifier == TypeModifier::List;
93+
8694
// Special-case an innermost nullable INPUT_OBJECT type.
8795
template <typename Type, TypeModifier... Other>
8896
concept InputVariableUniquePtr = InputVariableClass<Type> && OnlyNoneModifiers<Other...>;
@@ -114,20 +122,18 @@ struct ModifiedVariable
114122

115123
// Peel off the none modifier. If it's included, it should always be last in the list.
116124
template <TypeModifier Modifier = TypeModifier::None, TypeModifier... Other>
117-
[[nodiscard]] static inline
118-
typename std::enable_if_t<TypeModifier::None == Modifier && sizeof...(Other) == 0,
119-
response::Value>
120-
serialize(Type&& value)
125+
[[nodiscard]] static inline response::Value serialize(
126+
Type&& value) requires OnlyNoneModifiers<Modifier, Other...>
121127
{
122128
// Just call through to the non-template method without the modifiers.
123129
return serialize(std::move(value));
124130
}
125131

126132
// Peel off nullable modifiers.
127133
template <TypeModifier Modifier, TypeModifier... Other>
128-
[[nodiscard]] static inline
129-
typename std::enable_if_t<TypeModifier::Nullable == Modifier, response::Value>
130-
serialize(typename VariableTraits<Type, Modifier, Other...>::type&& nullableValue)
134+
[[nodiscard]] static inline response::Value serialize(
135+
typename VariableTraits<Type, Modifier, Other...>::type&& nullableValue) requires
136+
NullableModifier<Modifier>
131137
{
132138
response::Value result;
133139

@@ -142,9 +148,9 @@ struct ModifiedVariable
142148

143149
// Peel off list modifiers.
144150
template <TypeModifier Modifier, TypeModifier... Other>
145-
[[nodiscard]] static inline
146-
typename std::enable_if_t<TypeModifier::List == Modifier, response::Value>
147-
serialize(typename VariableTraits<Type, Modifier, Other...>::type&& listValue)
151+
[[nodiscard]] static inline response::Value serialize(
152+
typename VariableTraits<Type, Modifier, Other...>::type&& listValue) requires
153+
ListModifier<Modifier>
148154
{
149155
response::Value result { response::Type::List };
150156

@@ -159,19 +165,18 @@ struct ModifiedVariable
159165

160166
// Peel off the none modifier. If it's included, it should always be last in the list.
161167
template <TypeModifier Modifier = TypeModifier::None, TypeModifier... Other>
162-
[[nodiscard]] static inline
163-
typename std::enable_if_t<TypeModifier::None == Modifier && sizeof...(Other) == 0, Type>
164-
duplicate(const Type& value)
168+
[[nodiscard]] static inline Type duplicate(
169+
const Type& value) requires OnlyNoneModifiers<Modifier, Other...>
165170
{
166171
// Just copy the value.
167172
return Type { value };
168173
}
169174

170175
// Peel off nullable modifiers.
171176
template <TypeModifier Modifier, TypeModifier... Other>
172-
[[nodiscard]] static inline typename std::enable_if_t<TypeModifier::Nullable == Modifier,
173-
typename VariableTraits<Type, Modifier, Other...>::type>
174-
duplicate(const typename VariableTraits<Type, Modifier, Other...>::type& nullableValue)
177+
[[nodiscard]] static inline typename VariableTraits<Type, Modifier, Other...>::type duplicate(
178+
const typename VariableTraits<Type, Modifier, Other...>::type& nullableValue) requires
179+
NullableModifier<Modifier>
175180
{
176181
typename VariableTraits<Type, Modifier, Other...>::type result {};
177182

@@ -193,9 +198,9 @@ struct ModifiedVariable
193198

194199
// Peel off list modifiers.
195200
template <TypeModifier Modifier, TypeModifier... Other>
196-
[[nodiscard]] static inline typename std::enable_if_t<TypeModifier::List == Modifier,
197-
typename VariableTraits<Type, Modifier, Other...>::type>
198-
duplicate(const typename VariableTraits<Type, Modifier, Other...>::type& listValue)
201+
[[nodiscard]] static inline typename VariableTraits<Type, Modifier, Other...>::type duplicate(
202+
const typename VariableTraits<Type, Modifier, Other...>::type& listValue) requires
203+
ListModifier<Modifier>
199204
{
200205
typename VariableTraits<Type, Modifier, Other...>::type result(listValue.size());
201206

@@ -259,18 +264,16 @@ struct ModifiedResponse
259264

260265
// Peel off the none modifier. If it's included, it should always be last in the list.
261266
template <TypeModifier Modifier = TypeModifier::None, TypeModifier... Other>
262-
[[nodiscard]] static inline
263-
typename std::enable_if_t<TypeModifier::None == Modifier && sizeof...(Other) == 0, Type>
264-
parse(response::Value&& response)
267+
[[nodiscard]] static inline Type parse(
268+
response::Value&& response) requires OnlyNoneModifiers<Modifier, Other...>
265269
{
266270
return parse(std::move(response));
267271
}
268272

269273
// Peel off nullable modifiers.
270274
template <TypeModifier Modifier, TypeModifier... Other>
271-
[[nodiscard]] static inline typename std::enable_if_t<TypeModifier::Nullable == Modifier,
272-
std::optional<typename ResponseTraits<Type, Other...>::type>>
273-
parse(response::Value&& response)
275+
[[nodiscard]] static inline std::optional<typename ResponseTraits<Type, Other...>::type> parse(
276+
response::Value&& response) requires NullableModifier<Modifier>
274277
{
275278
if (response.type() == response::Type::Null)
276279
{
@@ -283,9 +286,8 @@ struct ModifiedResponse
283286

284287
// Peel off list modifiers.
285288
template <TypeModifier Modifier, TypeModifier... Other>
286-
[[nodiscard]] static inline typename std::enable_if_t<TypeModifier::List == Modifier,
287-
std::vector<typename ResponseTraits<Type, Other...>::type>>
288-
parse(response::Value&& response)
289+
[[nodiscard]] static inline std::vector<typename ResponseTraits<Type, Other...>::type> parse(
290+
response::Value&& response) requires ListModifier<Modifier>
289291
{
290292
std::vector<typename ResponseTraits<Type, Other...>::type> result;
291293

include/graphqlservice/GraphQLResponse.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ struct ValueTypeTraits<FloatType>
209209
using get_type = FloatType;
210210
};
211211

212+
template <typename ValueType>
213+
concept ValueTypeMatches = std::is_same_v<std::decay_t<ValueType>, ValueType>;
214+
212215
// Represent a discriminated union of GraphQL response value types.
213216
struct [[nodiscard]] Value
214217
{
@@ -265,15 +268,13 @@ struct [[nodiscard]] Value
265268

266269
// Specialized for all single-value Types.
267270
template <typename ValueType>
268-
void set(typename std::enable_if_t<std::is_same_v<std::decay_t<ValueType>, ValueType>,
269-
typename ValueTypeTraits<ValueType>::set_type>
270-
value);
271+
void set(
272+
typename ValueTypeTraits<ValueType>::set_type value) requires ValueTypeMatches<ValueType>;
271273

272274
// Specialized for all Types.
273275
template <typename ValueType>
274-
[[nodiscard]] typename std::enable_if_t<std::is_same_v<std::decay_t<ValueType>, ValueType>,
275-
typename ValueTypeTraits<ValueType>::get_type>
276-
get() const;
276+
[[nodiscard]] typename ValueTypeTraits<ValueType>::get_type get() const requires
277+
ValueTypeMatches<ValueType>;
277278

278279
// Specialized for all Types which allocate extra memory.
279280
template <typename ValueType>

0 commit comments

Comments
 (0)