summaryrefslogtreecommitdiff
path: root/boost/beast/core/detail/variant.hpp
blob: cba6ba63c07a6de38472e602d9431b3cbe1c7589 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_DETAIL_VARIANT_HPP
#define BOOST_BEAST_DETAIL_VARIANT_HPP

#include <boost/beast/core/detail/type_traits.hpp>
#include <boost/assert.hpp>
#include <cstddef>
#include <tuple>
#include <type_traits>

namespace boost {
namespace beast {
namespace detail {

// This simple variant gets the job done without
// causing too much trouble with template depth:
//
// * Always allows an empty state I==0
// * emplace() and get() support 1-based indexes only
// * Basic exception guarantee
// * Max 255 types
//
template<class... TN>
class variant
{
    typename std::aligned_storage<
        max_sizeof<TN...>(),
        max_alignof<TN...>()
    >::type buf_;
    unsigned char i_ = 0;

    template<std::size_t I>
    using type = typename std::tuple_element<
        I , std::tuple<TN...>>::type;

    template<std::size_t I>
    using C = std::integral_constant<std::size_t, I>;

public:
    variant() = default;

    ~variant()
    {
        if(i_)
            destroy(C<0>{});
    }

    // 0 = empty
    unsigned char
    index() const
    {
        return i_;
    }

    // moved-from object becomes empty
    variant(variant&& other)
    {
        i_ = other.move(&buf_, C<0>{});
    }

    variant(variant const& other)
    {
        i_ = other.copy(&buf_, C<0>{});
    }

    // moved-from object becomes empty
    variant& operator=(variant&& other)
    {
        if(i_ != 0)
            destroy(C<0>{});
        i_ = other.move(&buf_, C<0>{});
        return *this;
    }

    variant& operator=(variant const& other)
    {
        if(i_ != 0)
            destroy(C<0>{});
        i_ = other.copy(&buf_, C<0>{});
        return *this;
    }

    template<std::size_t I, class... Args>
    void
    emplace(Args&&... args)
    {
        if(i_ != 0)
            destroy(C<0>{});
        new(&buf_) type<I-1>(
            std::forward<Args>(args)...);
        i_ = I;
    }

    template<std::size_t I>
    type<I-1>&
    get()
    {
        BOOST_ASSERT(i_ == I);
        return *reinterpret_cast<
            type<I-1>*>(&buf_);
    }

    template<std::size_t I>
    type<I-1> const&
    get() const
    {
        BOOST_ASSERT(i_ == I);
        return *reinterpret_cast<
            type<I-1> const*>(&buf_);
    }

    void
    reset()
    {
        if(i_ == 0)
            return;
        destroy(C<0>{});
    }

private:
    void
    destroy(C<sizeof...(TN)>)
    {
        return;
    }

    template<std::size_t I>
    void
    destroy(C<I>)
    {
        if(i_ == I+1)
        {
            using T = type<I>;
            get<I+1>().~T();
            i_ = 0;
            return;
        }
        destroy(C<I+1>{});
    }

    unsigned char
    move(void*, C<sizeof...(TN)>)
    {
        return 0;
    }

    template<std::size_t I>
    unsigned char
    move(void* dest, C<I>)
    {
        if(i_ == I+1)
        {
            using T = type<I>;
            new(dest) T{std::move(get<I+1>())};
            get<I+1>().~T();
            i_ = 0;
            return I+1;
        }
        return move(dest, C<I+1>{});
    }

    unsigned char
    copy(void*, C<sizeof...(TN)>) const
    {
        return 0;
    }

    template<std::size_t I>
    unsigned char
    copy(void* dest, C<I>) const
    {
        if(i_ == I+1)
        {
            using T = type<I>;
            auto const& t = get<I+1>();
            new(dest) T{t};
            return I+1;
        }
        return copy(dest, C<I+1>{});
    }
};

} // detail
} // beast
} // boost

#endif