liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.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/json
//
 
#ifndef BOOST_JSON_DETAIL_VALUE_HPP
#define BOOST_JSON_DETAIL_VALUE_HPP
 
#include <boost/json/kind.hpp>
#include <boost/json/storage_ptr.hpp>
#include <cstdint>
#include <limits>
#include <new>
#include <utility>
 
BOOST_JSON_NS_BEGIN
namespace detail {
 
struct key_t
{
};
 
#if 0
template<class T>
struct to_number_limit
    : std::numeric_limits<T>
{
};
 
template<class T>
struct to_number_limit<T const>
    : to_number_limit<T>
{
};
 
template<>
struct to_number_limit<long long>
{
    static constexpr long long (min)() noexcept
    {
        return -9223372036854774784;
    }
 
    static constexpr long long (max)() noexcept
    {
        return 9223372036854774784;
    }
};
 
template<>
struct to_number_limit<unsigned long long>
{
    static constexpr
    unsigned long long (min)() noexcept
    {
        return 0;
    }
 
    static constexpr
    unsigned long long (max)() noexcept
    {
        return 18446744073709549568ULL;
    }
};
#else
 
template<class T>
class to_number_limit
{
    // unsigned
 
    static constexpr
    double min1(std::false_type)
    {
        return 0.0;
    }
 
    static constexpr
    double max1(std::false_type)
    {
        return max2u(std::integral_constant<
            bool, (std::numeric_limits<T>::max)() ==
            UINT64_MAX>{});
    }
 
    static constexpr
    double max2u(std::false_type)
    {
        return static_cast<double>(
            (std::numeric_limits<T>::max)());
    }
 
    static constexpr
    double max2u(std::true_type)
    {
        return 18446744073709549568.0;
    }
 
    // signed
 
    static constexpr
    double min1(std::true_type)
    {
        return min2s(std::integral_constant<
            bool, (std::numeric_limits<T>::max)() ==
            INT64_MAX>{});
    }
 
    static constexpr
    double min2s(std::false_type)
    {
        return static_cast<double>(
            (std::numeric_limits<T>::min)());
    }
 
    static constexpr
    double min2s(std::true_type)
    {
        return -9223372036854774784.0;
    }
 
    static constexpr
    double max1(std::true_type)
    {
        return max2s(std::integral_constant<
            bool, (std::numeric_limits<T>::max)() ==
            INT64_MAX>{});
    }
 
    static constexpr
    double max2s(std::false_type)
    {
        return static_cast<double>(
            (std::numeric_limits<T>::max)());
    }
 
    static constexpr
    double max2s(std::true_type)
    {
        return 9223372036854774784.0;
    }
 
public:
    static constexpr
    double (min)() noexcept
    {
        return min1(std::is_signed<T>{});
    }
 
    static constexpr
    double (max)() noexcept
    {
        return max1(std::is_signed<T>{});
    }
};
 
#endif
 
struct scalar
{
    storage_ptr sp; // must come first
    kind k;         // must come second
    union
    {
        bool b;
        std::int64_t i;
        std::uint64_t u;
        double d;
    };
 
    explicit
    scalar(storage_ptr sp_ = {}) noexcept
        : sp(std::move(sp_))
        , k(json::kind::null)
    {
    }
 
    explicit
    scalar(bool b_,
        storage_ptr sp_ = {}) noexcept
        : sp(std::move(sp_))
        , k(json::kind::bool_)
        , b(b_)
    {
    }
 
    explicit
    scalar(std::int64_t i_,
        storage_ptr sp_ = {}) noexcept
        : sp(std::move(sp_))
        , k(json::kind::int64)
        , i(i_)
    {
    }
 
    explicit
    scalar(std::uint64_t u_,
        storage_ptr sp_ = {}) noexcept
        : sp(std::move(sp_))
        , k(json::kind::uint64)
        , u(u_)
    {
    }
 
    explicit
    scalar(double d_,
        storage_ptr sp_ = {}) noexcept
        : sp(std::move(sp_))
        , k(json::kind::double_)
        , d(d_)
    {
    }
};
 
struct access
{
    template<class Value, class... Args>
    static
    Value&
    construct_value(Value* p, Args&&... args)
    {
        return *reinterpret_cast<
            Value*>(::new(p) Value(
            std::forward<Args>(args)...));
    }
 
    template<class KeyValuePair, class... Args>
    static
    KeyValuePair&
    construct_key_value_pair(
        KeyValuePair* p, Args&&... args)
    {
        return *reinterpret_cast<
            KeyValuePair*>(::new(p)
                KeyValuePair(
                    std::forward<Args>(args)...));
    }
 
    template<class Value>
    static
    char const*
    release_key(
        Value& jv,
        std::size_t& len) noexcept
    {
        BOOST_ASSERT(jv.is_string());
        jv.str_.sp_.~storage_ptr();
        return jv.str_.impl_.release_key(len);
    }
 
    using index_t = std::uint32_t;
 
    template<class KeyValuePair>
    static
    index_t&
    next(KeyValuePair& e) noexcept
    {
        return e.next_;
    }
 
    template<class KeyValuePair>
    static
    index_t const&
    next(KeyValuePair const& e) noexcept
    {
        return e.next_;
    }
};
 
} // detail
BOOST_JSON_NS_END
 
#endif