aboutsummaryrefslogtreecommitdiff
path: root/include/jsoncons/typed_array_view.hpp
blob: d1b49062ebd33c5be67e4153b2e36bac3ed44d0f (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
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
// Copyright 2018 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// See https://github.com/danielaparker/jsoncons for latest version

#ifndef JSONCONS_TYPED_ARRAY_VIEW_HPP
#define JSONCONS_TYPED_ARRAY_VIEW_HPP

#include <memory> // std::allocator
#include <string>
#include <stdexcept>
#include <system_error>
#include <ios>
#include <type_traits> // std::enable_if
#include <array> // std::array
#include <functional> // std::function
#include <jsoncons/json_exception.hpp>
#include <jsoncons/json_visitor.hpp>
#include <jsoncons/bigint.hpp>
#include <jsoncons/json_parser.hpp>
#include <jsoncons/ser_context.hpp>
#include <jsoncons/sink.hpp>
#include <jsoncons/detail/write_number.hpp>
#include <jsoncons/json_type_traits.hpp>
#include <jsoncons/converter.hpp>

namespace jsoncons {

    struct uint8_array_arg_t {explicit uint8_array_arg_t() = default; };
    constexpr uint8_array_arg_t uint8_array_arg = uint8_array_arg_t();
    struct uint16_array_arg_t {explicit uint16_array_arg_t() = default; };
    struct uint32_array_arg_t {explicit uint32_array_arg_t() = default; };
    constexpr uint32_array_arg_t uint32_array_arg = uint32_array_arg_t();
    struct uint64_array_arg_t {explicit uint64_array_arg_t() = default; };
    constexpr uint64_array_arg_t uint64_array_arg = uint64_array_arg_t();
    struct int8_array_arg_t {explicit int8_array_arg_t() = default; };
    constexpr int8_array_arg_t int8_array_arg = int8_array_arg_t();
    struct int16_array_arg_t {explicit int16_array_arg_t() = default; };
    constexpr int16_array_arg_t int16_array_arg = int16_array_arg_t();
    struct int32_array_arg_t {explicit int32_array_arg_t() = default; };
    constexpr int32_array_arg_t int32_array_arg = int32_array_arg_t();
    struct int64_array_arg_t {explicit int64_array_arg_t() = default; };
    constexpr int64_array_arg_t int64_array_arg = int64_array_arg_t();
    constexpr uint16_array_arg_t uint16_array_arg = uint16_array_arg_t();
    struct half_array_arg_t {explicit half_array_arg_t() = default; };
    constexpr half_array_arg_t half_array_arg = half_array_arg_t();
    struct float_array_arg_t {explicit float_array_arg_t() = default; };
    constexpr float_array_arg_t float_array_arg = float_array_arg_t();
    struct double_array_arg_t {explicit double_array_arg_t() = default; };
    constexpr double_array_arg_t double_array_arg = double_array_arg_t();
    struct float128_array_arg_t {explicit float128_array_arg_t() = default; };
    constexpr float128_array_arg_t float128_array_arg = float128_array_arg_t();

    enum class typed_array_type{uint8_value=1,uint16_value,uint32_value,uint64_value,
                                int8_value,int16_value,int32_value,int64_value, 
                                half_value, float_value,double_value};

    class typed_array_view
    {
        typed_array_type type_;
        union 
        {
            const uint8_t* uint8_data_;
            const uint16_t* uint16_data_;
            const uint32_t* uint32_data_;
            const uint64_t* uint64_data_;
            const int8_t* int8_data_;
            const int16_t* int16_data_;
            const int32_t* int32_data_;
            const int64_t* int64_data_;
            const float* float_data_;
            const double* double_data_;
        } data_;
        std::size_t size_;
    public:

        typed_array_view()
            : type_(), data_(), size_(0)
        {
        }

        typed_array_view(const typed_array_view& other)
            : type_(other.type_), data_(other.data_), size_(other.size())
        {
        }

        typed_array_view(typed_array_view&& other) noexcept
        {
            swap(*this,other);
        }

        typed_array_view(const uint8_t* data, std::size_t size)
            : type_(typed_array_type::uint8_value), size_(size)
        {
            data_.uint8_data_ = data;
        }

        typed_array_view(const uint16_t* data, std::size_t size)
            : type_(typed_array_type::uint16_value), size_(size)
        {
            data_.uint16_data_ = data;
        }

        typed_array_view(const uint32_t* data, std::size_t size)
            : type_(typed_array_type::uint32_value), size_(size)
        {
            data_.uint32_data_ = data;
        }

        typed_array_view(const uint64_t* data, std::size_t size)
            : type_(typed_array_type::uint64_value), size_(size)
        {
            data_.uint64_data_ = data;
        }

        typed_array_view(const int8_t* data, std::size_t size)
            : type_(typed_array_type::int8_value), size_(size)
        {
            data_.int8_data_ = data;
        }

        typed_array_view(const int16_t* data, std::size_t size)
            : type_(typed_array_type::int16_value), size_(size)
        {
            data_.int16_data_ = data;
        }

        typed_array_view(const int32_t* data, std::size_t size)
            : type_(typed_array_type::int32_value), size_(size)
        {
            data_.int32_data_ = data;
        }

        typed_array_view(const int64_t* data, std::size_t size)
            : type_(typed_array_type::int64_value), size_(size)
        {
            data_.int64_data_ = data;
        }

        typed_array_view(half_array_arg_t, const uint16_t* data, std::size_t size)
            : type_(typed_array_type::half_value), size_(size)
        {
            data_.uint16_data_ = data;
        }

        typed_array_view(const float* data, std::size_t size)
            : type_(typed_array_type::float_value), size_(size)
        {
            data_.float_data_ = data;
        }

        typed_array_view(const double* data, std::size_t size)
            : type_(typed_array_type::double_value), size_(size)
        {
            data_.double_data_ = data;
        }

        typed_array_view& operator=(const typed_array_view& other)
        {
            typed_array_view temp(other);
            swap(*this,temp);
            return *this;
        }

        typed_array_type type() const {return type_;}

        std::size_t size() const
        {
            return size_;
        }

        jsoncons::span<const uint8_t> data(uint8_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::uint8_value);
            return jsoncons::span<const uint8_t>(data_.uint8_data_, size_);
        }

        jsoncons::span<const uint16_t> data(uint16_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::uint16_value);
            return jsoncons::span<const uint16_t>(data_.uint16_data_, size_);
        }

        jsoncons::span<const uint32_t> data(uint32_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::uint32_value);
            return jsoncons::span<const uint32_t>(data_.uint32_data_, size_);
        }

        jsoncons::span<const uint64_t> data(uint64_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::uint64_value);
            return jsoncons::span<const uint64_t>(data_.uint64_data_, size_);
        }

        jsoncons::span<const int8_t> data(int8_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::int8_value);
            return jsoncons::span<const int8_t>(data_.int8_data_, size_);
        }

        jsoncons::span<const int16_t> data(int16_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::int16_value);
            return jsoncons::span<const int16_t>(data_.int16_data_, size_);
        }

        jsoncons::span<const int32_t> data(int32_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::int32_value);
            return jsoncons::span<const int32_t>(data_.int32_data_, size_);
        }

        jsoncons::span<const int64_t> data(int64_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::int64_value);
            return jsoncons::span<const int64_t>(data_.int64_data_, size_);
        }

        jsoncons::span<const uint16_t> data(half_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::half_value);
            return jsoncons::span<const uint16_t>(data_.uint16_data_, size_);
        }

        jsoncons::span<const float> data(float_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::float_value);
            return jsoncons::span<const float>(data_.float_data_, size_);
        }

        jsoncons::span<const double> data(double_array_arg_t) const
        {
            JSONCONS_ASSERT(type_ == typed_array_type::double_value);
            return jsoncons::span<const double>(data_.double_data_, size_);
        }

        friend void swap(typed_array_view& a, typed_array_view& b) noexcept
        {
            std::swap(a.data_,b.data_);
            std::swap(a.type_,b.type_);
            std::swap(a.size_,b.size_);
        }
    };

} // namespace jsoncons

#endif