Skip to content

Commit ca80d26

Browse files
committed
Split the union into separate members for default initialization
1 parent aff8ad0 commit ca80d26

File tree

2 files changed

+21
-87
lines changed

2 files changed

+21
-87
lines changed

GraphQLResponse.cpp

Lines changed: 15 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ namespace response {
1111

1212
Value::Value(Type type /*= Type::Null*/)
1313
: _type(type)
14-
, _boolean(false)
15-
, _int(0)
16-
, _float(0.0)
1714
{
1815
switch (type)
1916
{
@@ -43,37 +40,25 @@ Value::Value(Type type /*= Type::Null*/)
4340
Value::Value(StringType&& value)
4441
: _type(Type::String)
4542
, _string(new StringType(std::move(value)))
46-
, _boolean(false)
47-
, _int(0)
48-
, _float(0.0)
4943
{
5044
}
5145

5246
Value::Value(BooleanType value)
5347
: _type(Type::Boolean)
54-
, _boolean(false)
55-
, _int(0)
56-
, _float(0.0)
48+
, _boolean(value)
5749
{
58-
_boolean = value;
5950
}
6051

6152
Value::Value(IntType value)
6253
: _type(Type::Int)
63-
, _boolean(false)
64-
, _int(0)
65-
, _float(0.0)
54+
, _int(value)
6655
{
67-
_int = value;
6856
}
6957

7058
Value::Value(FloatType value)
7159
: _type(Type::Float)
72-
, _boolean(false)
73-
, _int(0)
74-
, _float(0.0)
60+
, _float(value)
7561
{
76-
_float = value;
7762
}
7863

7964
Value::Value(Value&& other) noexcept
@@ -83,28 +68,10 @@ Value::Value(Value&& other) noexcept
8368
, _list(std::move(other._list))
8469
, _string(std::move(other._string))
8570
, _scalar(std::move(other._scalar))
86-
, _boolean(false)
87-
, _int(0)
88-
, _float(0.0)
71+
, _boolean(other._boolean)
72+
, _int(other._int)
73+
, _float(other._float)
8974
{
90-
switch (_type)
91-
{
92-
case Type::Boolean:
93-
_boolean = other._boolean;
94-
break;
95-
96-
case Type::Int:
97-
_int = other._int;
98-
break;
99-
100-
case Type::Float:
101-
_float = other._float;
102-
break;
103-
104-
default:
105-
break;
106-
}
107-
10875
const_cast<Type&>(other._type) = Type::Null;
10976
other._boolean = false;
11077
other._int = 0;
@@ -113,9 +80,9 @@ Value::Value(Value&& other) noexcept
11380

11481
Value::Value(const Value& other)
11582
: _type(other._type)
116-
, _boolean(false)
117-
, _int(0)
118-
, _float(0.0)
83+
, _boolean(other._boolean)
84+
, _int(other._int)
85+
, _float(other._float)
11986
{
12087
switch (_type)
12188
{
@@ -133,18 +100,6 @@ Value::Value(const Value& other)
133100
_string.reset(new StringType(*other._string));
134101
break;
135102

136-
case Type::Boolean:
137-
_boolean = other._boolean;
138-
break;
139-
140-
case Type::Int:
141-
_int = other._int;
142-
break;
143-
144-
case Type::Float:
145-
_float = other._float;
146-
break;
147-
148103
case Type::Scalar:
149104
_scalar.reset(new Value(*other._scalar));
150105
break;
@@ -164,30 +119,12 @@ Value& Value::operator=(Value&& rhs) noexcept
164119
_list = std::move(rhs._list);
165120
_string = std::move(rhs._string);
166121
_scalar = std::move(rhs._scalar);
167-
_boolean = false;
168-
_int = 0;
169-
_float = 0.0;
170-
171-
switch (_type)
172-
{
173-
case Type::Boolean:
174-
_boolean = rhs._boolean;
175-
rhs._boolean = false;
176-
break;
177-
178-
case Type::Int:
179-
_int = rhs._int;
180-
rhs._int = 0;
181-
break;
182-
183-
case Type::Float:
184-
_float = rhs._float;
185-
rhs._float = 0.0;
186-
break;
187-
188-
default:
189-
break;
190-
}
122+
_boolean = rhs._boolean;
123+
rhs._boolean = false;
124+
_int = rhs._int;
125+
rhs._int = 0;
126+
_float = rhs._float;
127+
rhs._float = 0.0;
191128

192129
return *this;
193130
}

include/graphqlservice/GraphQLResponse.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,14 @@ struct Value
9595
// Type::String or Type::EnumValue
9696
std::unique_ptr<StringType> _string;
9797

98-
union
99-
{
100-
// Type::Boolean
101-
BooleanType _boolean;
98+
// Type::Boolean
99+
BooleanType _boolean = false;
102100

103-
// Type::Int
104-
IntType _int;
101+
// Type::Int
102+
IntType _int = 0;
105103

106-
// Type::Float
107-
FloatType _float;
108-
};
104+
// Type::Float
105+
FloatType _float = 0.0;
109106

110107
// Type::Scalar
111108
std::unique_ptr<ScalarType> _scalar;

0 commit comments

Comments
 (0)