Skip to content

Commit 2317389

Browse files
authored
Merge pull request #70 from wravery/master
Use type traits to clarify which types to use with Value::set, get, and release
2 parents e19db51 + 4165251 commit 2317389

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

include/graphqlservice/GraphQLResponse.h

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,63 @@ using FloatType = double;
3737
using ScalarType = Value;
3838
using IdType = std::vector<uint8_t>;
3939

40+
template <typename ValueType>
41+
struct ValueTypeTraits
42+
{
43+
// Set by r-value reference, get by const reference, and release by value. The only types
44+
// that actually support all 3 methods are StringType and ScalarType, everything else
45+
// overrides some subset of these types with a template specialization.
46+
using set_type = ValueType &&;
47+
using get_type = const ValueType &;
48+
using release_type = ValueType;
49+
};
50+
51+
template <>
52+
struct ValueTypeTraits<MapType>
53+
{
54+
// Get by const reference and release by value.
55+
using get_type = const MapType &;
56+
using release_type = MapType;
57+
};
58+
59+
template <>
60+
struct ValueTypeTraits<ListType>
61+
{
62+
// Get by const reference and release by value.
63+
using get_type = const ListType &;
64+
using release_type = ListType;
65+
};
66+
67+
template <>
68+
struct ValueTypeTraits<BooleanType>
69+
{
70+
// Set and get by value.
71+
using set_type = BooleanType;
72+
using get_type = BooleanType;
73+
};
74+
75+
template <>
76+
struct ValueTypeTraits<IntType>
77+
{
78+
// Set and get by value.
79+
using set_type = IntType;
80+
using get_type = IntType;
81+
};
82+
83+
template <>
84+
struct ValueTypeTraits<FloatType>
85+
{
86+
// Set and get by value.
87+
using set_type = FloatType;
88+
using get_type = FloatType;
89+
};
90+
91+
template <>
92+
struct ValueTypeTraits<IdType>
93+
{
94+
// ID values are represented as a String, there's no separate handling of this type.
95+
};
96+
4097
struct TypedData;
4198

4299
// Represent a discriminated union of GraphQL response value types.
@@ -86,15 +143,15 @@ struct Value
86143

87144
// Specialized for all single-value Types.
88145
template <typename ValueType>
89-
void set(ValueType&& value);
146+
void set(typename ValueTypeTraits<ValueType>::set_type value);
90147

91148
// Specialized for all Types.
92149
template <typename ValueType>
93-
ValueType get() const;
150+
typename ValueTypeTraits<ValueType>::get_type get() const;
94151

95152
// Specialized for all Types which allocate extra memory.
96153
template <typename ValueType>
97-
ValueType release();
154+
typename ValueTypeTraits<ValueType>::release_type release();
98155

99156
private:
100157
const Type _type;

src/GraphQLResponse.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ void Value::set<StringType>(StringType&& value)
347347
}
348348

349349
template <>
350-
void Value::set<BooleanType>(BooleanType&& value)
350+
void Value::set<BooleanType>(BooleanType value)
351351
{
352352
if (type() != Type::Boolean)
353353
{
@@ -358,7 +358,7 @@ void Value::set<BooleanType>(BooleanType&& value)
358358
}
359359

360360
template <>
361-
void Value::set<IntType>(IntType&& value)
361+
void Value::set<IntType>(IntType value)
362362
{
363363
if (type() != Type::Int)
364364
{
@@ -369,7 +369,7 @@ void Value::set<IntType>(IntType&& value)
369369
}
370370

371371
template <>
372-
void Value::set<FloatType>(FloatType&& value)
372+
void Value::set<FloatType>(FloatType value)
373373
{
374374
if (type() != Type::Float)
375375
{

0 commit comments

Comments
 (0)