forked from ifsmirnov/jngen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant_array.h
67 lines (53 loc) · 1.55 KB
/
variant_array.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
#include "array.h"
#include "repr.h"
#include "variant.h"
#include <iterator>
#include <vector>
#include <type_traits>
namespace jngen {
template<typename ... Args>
class VariantArray : public GenericArray<Variant<Args...>> {
public:
using Base = GenericArray<Variant<Args...>>;
using BaseVariant = Variant<Args...>;
using Base::Base;
VariantArray() {}
/* implicit */ VariantArray(const Base& base) :
Base(base)
{ }
template<typename T, typename = typename std::enable_if<
BaseVariant::template hasType<T>()>::type>
VariantArray(const std::vector<T>& other) {
std::copy(other.begin(), other.end(), std::back_inserter(*this));
}
template<typename T, typename = typename std::enable_if<
BaseVariant::template hasType<T>()>::type>
VariantArray(std::vector<T>&& other) {
std::move(other.begin(), other.end(), std::back_inserter(*this));
GenericArray<T>().swap(other);
}
template<typename T, typename = typename std::enable_if<
BaseVariant::template hasType<T>()>::type>
operator GenericArray<T>() const
{
return GenericArray<T>(this->begin(), this->end());
}
bool hasNonEmpty() const {
for (const auto& x: *this) {
if (!x.empty()) {
return true;
}
}
return false;
}
int anyType() const {
for (const auto& x: *this) {
if (!x.empty()) {
return x.type();
}
}
return 0;
}
};
} // namespace jngen