Skip to content

Commit 2b57b59

Browse files
committed
DPL: move out of line a few methods from Variant
1 parent 387fdc7 commit 2b57b59

2 files changed

Lines changed: 151 additions & 145 deletions

File tree

Framework/Core/include/Framework/Variant.h

Lines changed: 5 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -269,151 +269,11 @@ class Variant
269269
"\n did you accidentally put braces around the default value?");
270270
}
271271

272-
Variant(const Variant& other) : mType(other.mType)
273-
{
274-
// In case this is an array we need to duplicate it to avoid
275-
// double deletion.
276-
switch (mType) {
277-
case variant_trait_v<const char*>:
278-
mSize = other.mSize;
279-
variant_helper<storage_t, const char*>::set(&mStore, other.get<const char*>());
280-
return;
281-
case variant_trait_v<int*>:
282-
mSize = other.mSize;
283-
variant_helper<storage_t, int*>::set(&mStore, other.get<int*>(), mSize);
284-
return;
285-
case variant_trait_v<float*>:
286-
mSize = other.mSize;
287-
variant_helper<storage_t, float*>::set(&mStore, other.get<float*>(), mSize);
288-
return;
289-
case variant_trait_v<double*>:
290-
mSize = other.mSize;
291-
variant_helper<storage_t, double*>::set(&mStore, other.get<double*>(), mSize);
292-
return;
293-
case variant_trait_v<bool*>:
294-
mSize = other.mSize;
295-
variant_helper<storage_t, bool*>::set(&mStore, other.get<bool*>(), mSize);
296-
return;
297-
case variant_trait_v<std::string*>:
298-
mSize = other.mSize;
299-
variant_helper<storage_t, std::string*>::set(&mStore, other.get<std::string*>(), mSize);
300-
return;
301-
default:
302-
mStore = other.mStore;
303-
mSize = other.mSize;
304-
}
305-
}
306-
307-
Variant(Variant&& other) : mType(other.mType)
308-
{
309-
mStore = other.mStore;
310-
mSize = other.mSize;
311-
switch (mType) {
312-
case variant_trait_v<const char*>:
313-
*reinterpret_cast<char**>(&(other.mStore)) = nullptr;
314-
return;
315-
case variant_trait_v<int*>:
316-
*reinterpret_cast<int**>(&(other.mStore)) = nullptr;
317-
return;
318-
case variant_trait_v<float*>:
319-
*reinterpret_cast<float**>(&(other.mStore)) = nullptr;
320-
return;
321-
case variant_trait_v<double*>:
322-
*reinterpret_cast<double**>(&(other.mStore)) = nullptr;
323-
return;
324-
case variant_trait_v<bool*>:
325-
*reinterpret_cast<bool**>(&(other.mStore)) = nullptr;
326-
return;
327-
case variant_trait_v<std::string*>:
328-
*reinterpret_cast<std::string**>(&(other.mStore)) = nullptr;
329-
default:
330-
return;
331-
}
332-
}
333-
334-
~Variant()
335-
{
336-
// In case we allocated an array, we
337-
// should delete it.
338-
switch (mType) {
339-
case variant_trait_v<const char*>:
340-
case variant_trait_v<int*>:
341-
case variant_trait_v<float*>:
342-
case variant_trait_v<double*>:
343-
case variant_trait_v<bool*>:
344-
case variant_trait_v<std::string*>:
345-
if (reinterpret_cast<void**>(&mStore) != nullptr) {
346-
free(*reinterpret_cast<void**>(&mStore));
347-
}
348-
return;
349-
default:
350-
return;
351-
}
352-
}
353-
354-
Variant& operator=(const Variant& other)
355-
{
356-
mSize = other.mSize;
357-
mType = other.mType;
358-
switch (mType) {
359-
case variant_trait_v<const char*>:
360-
variant_helper<storage_t, const char*>::set(&mStore, other.get<const char*>());
361-
return *this;
362-
case variant_trait_v<int*>:
363-
variant_helper<storage_t, int*>::set(&mStore, other.get<int*>(), mSize);
364-
return *this;
365-
case variant_trait_v<float*>:
366-
variant_helper<storage_t, float*>::set(&mStore, other.get<float*>(), mSize);
367-
return *this;
368-
case variant_trait_v<double*>:
369-
variant_helper<storage_t, double*>::set(&mStore, other.get<double*>(), mSize);
370-
return *this;
371-
case variant_trait_v<bool*>:
372-
variant_helper<storage_t, bool*>::set(&mStore, other.get<bool*>(), mSize);
373-
return *this;
374-
case variant_trait_v<std::string*>:
375-
variant_helper<storage_t, std::string*>::set(&mStore, other.get<std::string*>(), mSize);
376-
return *this;
377-
default:
378-
mStore = other.mStore;
379-
return *this;
380-
}
381-
}
382-
383-
Variant& operator=(Variant&& other)
384-
{
385-
mSize = other.mSize;
386-
mType = other.mType;
387-
switch (mType) {
388-
case variant_trait_v<const char*>:
389-
variant_helper<storage_t, const char*>::set(&mStore, other.get<const char*>());
390-
*reinterpret_cast<char**>(&(other.mStore)) = nullptr;
391-
return *this;
392-
case variant_trait_v<int*>:
393-
variant_helper<storage_t, int*>::set(&mStore, other.get<int*>(), mSize);
394-
*reinterpret_cast<int**>(&(other.mStore)) = nullptr;
395-
return *this;
396-
case variant_trait_v<float*>:
397-
variant_helper<storage_t, float*>::set(&mStore, other.get<float*>(), mSize);
398-
*reinterpret_cast<float**>(&(other.mStore)) = nullptr;
399-
return *this;
400-
case variant_trait_v<double*>:
401-
variant_helper<storage_t, double*>::set(&mStore, other.get<double*>(), mSize);
402-
*reinterpret_cast<double**>(&(other.mStore)) = nullptr;
403-
return *this;
404-
case variant_trait_v<bool*>:
405-
variant_helper<storage_t, bool*>::set(&mStore, other.get<bool*>(), mSize);
406-
*reinterpret_cast<bool**>(&(other.mStore)) = nullptr;
407-
return *this;
408-
case variant_trait_v<std::string*>:
409-
variant_helper<storage_t, std::string*>::set(&mStore, other.get<std::string*>(), mSize);
410-
*reinterpret_cast<std::string**>(&(other.mStore)) = nullptr;
411-
return *this;
412-
default:
413-
mStore = other.mStore;
414-
return *this;
415-
}
416-
}
272+
Variant(const Variant& other);
273+
Variant(Variant&& other);
274+
~Variant();
275+
Variant& operator=(const Variant& other);
276+
Variant& operator=(Variant&& other);
417277

418278
template <typename T>
419279
T get() const

Framework/Core/src/Variant.cxx

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,150 @@ std::string Variant::asString() const
109109
return ss.str();
110110
}
111111

112+
Variant::Variant(const Variant& other) : mType(other.mType)
113+
{
114+
// In case this is an array we need to duplicate it to avoid
115+
// double deletion.
116+
switch (mType) {
117+
case variant_trait_v<const char*>:
118+
mSize = other.mSize;
119+
variant_helper<storage_t, const char*>::set(&mStore, other.get<const char*>());
120+
return;
121+
case variant_trait_v<int*>:
122+
mSize = other.mSize;
123+
variant_helper<storage_t, int*>::set(&mStore, other.get<int*>(), mSize);
124+
return;
125+
case variant_trait_v<float*>:
126+
mSize = other.mSize;
127+
variant_helper<storage_t, float*>::set(&mStore, other.get<float*>(), mSize);
128+
return;
129+
case variant_trait_v<double*>:
130+
mSize = other.mSize;
131+
variant_helper<storage_t, double*>::set(&mStore, other.get<double*>(), mSize);
132+
return;
133+
case variant_trait_v<bool*>:
134+
mSize = other.mSize;
135+
variant_helper<storage_t, bool*>::set(&mStore, other.get<bool*>(), mSize);
136+
return;
137+
case variant_trait_v<std::string*>:
138+
mSize = other.mSize;
139+
variant_helper<storage_t, std::string*>::set(&mStore, other.get<std::string*>(), mSize);
140+
return;
141+
default:
142+
mStore = other.mStore;
143+
mSize = other.mSize;
144+
}
145+
}
146+
147+
Variant::Variant(Variant&& other) : mType(other.mType)
148+
{
149+
mStore = other.mStore;
150+
mSize = other.mSize;
151+
switch (mType) {
152+
case variant_trait_v<const char*>:
153+
*reinterpret_cast<char**>(&(other.mStore)) = nullptr;
154+
return;
155+
case variant_trait_v<int*>:
156+
*reinterpret_cast<int**>(&(other.mStore)) = nullptr;
157+
return;
158+
case variant_trait_v<float*>:
159+
*reinterpret_cast<float**>(&(other.mStore)) = nullptr;
160+
return;
161+
case variant_trait_v<double*>:
162+
*reinterpret_cast<double**>(&(other.mStore)) = nullptr;
163+
return;
164+
case variant_trait_v<bool*>:
165+
*reinterpret_cast<bool**>(&(other.mStore)) = nullptr;
166+
return;
167+
case variant_trait_v<std::string*>:
168+
*reinterpret_cast<std::string**>(&(other.mStore)) = nullptr;
169+
default:
170+
return;
171+
}
172+
}
173+
174+
Variant::~Variant()
175+
{
176+
// In case we allocated an array, we
177+
// should delete it.
178+
switch (mType) {
179+
case variant_trait_v<const char*>:
180+
case variant_trait_v<int*>:
181+
case variant_trait_v<float*>:
182+
case variant_trait_v<double*>:
183+
case variant_trait_v<bool*>:
184+
case variant_trait_v<std::string*>:
185+
if (reinterpret_cast<void**>(&mStore) != nullptr) {
186+
free(*reinterpret_cast<void**>(&mStore));
187+
}
188+
return;
189+
default:
190+
return;
191+
}
192+
}
193+
194+
Variant& Variant::operator=(const Variant& other)
195+
{
196+
mSize = other.mSize;
197+
mType = other.mType;
198+
switch (mType) {
199+
case variant_trait_v<const char*>:
200+
variant_helper<storage_t, const char*>::set(&mStore, other.get<const char*>());
201+
return *this;
202+
case variant_trait_v<int*>:
203+
variant_helper<storage_t, int*>::set(&mStore, other.get<int*>(), mSize);
204+
return *this;
205+
case variant_trait_v<float*>:
206+
variant_helper<storage_t, float*>::set(&mStore, other.get<float*>(), mSize);
207+
return *this;
208+
case variant_trait_v<double*>:
209+
variant_helper<storage_t, double*>::set(&mStore, other.get<double*>(), mSize);
210+
return *this;
211+
case variant_trait_v<bool*>:
212+
variant_helper<storage_t, bool*>::set(&mStore, other.get<bool*>(), mSize);
213+
return *this;
214+
case variant_trait_v<std::string*>:
215+
variant_helper<storage_t, std::string*>::set(&mStore, other.get<std::string*>(), mSize);
216+
return *this;
217+
default:
218+
mStore = other.mStore;
219+
return *this;
220+
}
221+
}
222+
223+
Variant& Variant::operator=(Variant&& other)
224+
{
225+
mSize = other.mSize;
226+
mType = other.mType;
227+
switch (mType) {
228+
case variant_trait_v<const char*>:
229+
variant_helper<storage_t, const char*>::set(&mStore, other.get<const char*>());
230+
*reinterpret_cast<char**>(&(other.mStore)) = nullptr;
231+
return *this;
232+
case variant_trait_v<int*>:
233+
variant_helper<storage_t, int*>::set(&mStore, other.get<int*>(), mSize);
234+
*reinterpret_cast<int**>(&(other.mStore)) = nullptr;
235+
return *this;
236+
case variant_trait_v<float*>:
237+
variant_helper<storage_t, float*>::set(&mStore, other.get<float*>(), mSize);
238+
*reinterpret_cast<float**>(&(other.mStore)) = nullptr;
239+
return *this;
240+
case variant_trait_v<double*>:
241+
variant_helper<storage_t, double*>::set(&mStore, other.get<double*>(), mSize);
242+
*reinterpret_cast<double**>(&(other.mStore)) = nullptr;
243+
return *this;
244+
case variant_trait_v<bool*>:
245+
variant_helper<storage_t, bool*>::set(&mStore, other.get<bool*>(), mSize);
246+
*reinterpret_cast<bool**>(&(other.mStore)) = nullptr;
247+
return *this;
248+
case variant_trait_v<std::string*>:
249+
variant_helper<storage_t, std::string*>::set(&mStore, other.get<std::string*>(), mSize);
250+
*reinterpret_cast<std::string**>(&(other.mStore)) = nullptr;
251+
return *this;
252+
default:
253+
mStore = other.mStore;
254+
return *this;
255+
}
256+
}
257+
112258
} // namespace o2::framework

0 commit comments

Comments
 (0)