Skip to content

Commit 5b64398

Browse files
committed
move typed_array from visitor to cursor
1 parent 4ab1609 commit 5b64398

6 files changed

Lines changed: 37 additions & 239 deletions

File tree

include/jsoncons/reflect/decode_traits.hpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ struct decode_traits<T,
422422
if (cursor.is_typed_array())
423423
{
424424
read_typed_array(cursor.typed_array(), cursor.element_type(), v);
425+
cursor.clear_typed_array();
425426
}
426427
else
427428
{
@@ -484,20 +485,16 @@ struct decode_traits<T,
484485
{
485486
std::error_code ec;
486487

487-
cursor.array_expected(ec);
488-
if (JSONCONS_UNLIKELY(ec))
489-
{
490-
return result_type(jsoncons::unexpect, ec, cursor.line(), cursor.column());
491-
}
492-
if (cursor.is_typed_array())
488+
if (cursor.current().event_type() == staj_events::begin_array)
493489
{
494-
std::cout << "IS TYPED ARRAY 3\n";
495-
}
496-
switch (cursor.current().event_type())
497-
{
498-
case staj_events::begin_array:
490+
T v = jsoncons::make_obj_using_allocator<T>(aset.get_allocator());
491+
if (cursor.is_typed_array())
492+
{
493+
read_typed_array(cursor.typed_array(), cursor.element_type(), v);
494+
cursor.clear_typed_array();
495+
}
496+
else
499497
{
500-
T v = jsoncons::make_obj_using_allocator<T>(aset.get_allocator());
501498
if (cursor.current().size() > 0)
502499
{
503500
reserve_storage(typename std::integral_constant<bool, ext_traits::has_reserve<T>::value>::type(), v, cursor.current().size());
@@ -518,12 +515,12 @@ struct decode_traits<T,
518515
{
519516
return result_type{jsoncons::unexpect, conv_errc::not_vector, cursor.line(), cursor.column()};
520517
}
521-
return result_type{std::move(v)};
522-
}
523-
default:
524-
{
525-
return result_type(jsoncons::unexpect, conv_errc::not_vector, cursor.line(), cursor.column());
526518
}
519+
return result_type{std::move(v)};
520+
}
521+
else
522+
{
523+
return result_type(jsoncons::unexpect, conv_errc::not_vector, cursor.line(), cursor.column());
527524
}
528525
}
529526

include/jsoncons/staj_cursor.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,14 @@ class basic_staj_cursor
315315
return typed_array_element_type{};
316316
}
317317

318-
virtual jsoncons::span<uint8_t> typed_array()
318+
virtual jsoncons::span<uint8_t> typed_array() const
319319
{
320320
return jsoncons::span<uint8_t>{};
321321
}
322+
323+
virtual void clear_typed_array()
324+
{
325+
}
322326
};
323327

324328
template <typename CharT>

include/jsoncons/typed_array.hpp

Lines changed: 3 additions & 214 deletions
Original file line numberDiff line numberDiff line change
@@ -24,222 +24,11 @@
2424

2525
namespace jsoncons {
2626

27-
struct uint8_array_arg_t {explicit uint8_array_arg_t() = default; };
28-
constexpr uint8_array_arg_t uint8_array_arg = uint8_array_arg_t();
29-
struct uint16_array_arg_t {explicit uint16_array_arg_t() = default; };
30-
struct uint32_array_arg_t {explicit uint32_array_arg_t() = default; };
31-
constexpr uint32_array_arg_t uint32_array_arg = uint32_array_arg_t();
32-
struct uint64_array_arg_t {explicit uint64_array_arg_t() = default; };
33-
constexpr uint64_array_arg_t uint64_array_arg = uint64_array_arg_t();
34-
struct int8_array_arg_t {explicit int8_array_arg_t() = default; };
35-
constexpr int8_array_arg_t int8_array_arg = int8_array_arg_t();
36-
struct int16_array_arg_t {explicit int16_array_arg_t() = default; };
37-
constexpr int16_array_arg_t int16_array_arg = int16_array_arg_t();
38-
struct int32_array_arg_t {explicit int32_array_arg_t() = default; };
39-
constexpr int32_array_arg_t int32_array_arg = int32_array_arg_t();
40-
struct int64_array_arg_t {explicit int64_array_arg_t() = default; };
41-
constexpr int64_array_arg_t int64_array_arg = int64_array_arg_t();
42-
constexpr uint16_array_arg_t uint16_array_arg = uint16_array_arg_t();
43-
struct half_array_arg_t {explicit half_array_arg_t() = default; };
44-
constexpr half_array_arg_t half_array_arg = half_array_arg_t();
45-
struct float_array_arg_t {explicit float_array_arg_t() = default; };
46-
constexpr float_array_arg_t float_array_arg = float_array_arg_t();
47-
struct double_array_arg_t {explicit double_array_arg_t() = default; };
48-
constexpr double_array_arg_t double_array_arg = double_array_arg_t();
49-
struct float128_array_arg_t {explicit float128_array_arg_t() = default; };
50-
constexpr float128_array_arg_t float128_array_arg = float128_array_arg_t();
51-
52-
enum class typed_array_element_type{
27+
enum class typed_array_element_type
28+
{
5329
uint8=1,uint16,uint32,uint64,
5430
int8,int16,int32,int64,
55-
half_float, float32, float64};
56-
57-
class typed_array_view
58-
{
59-
typed_array_element_type type_;
60-
union
61-
{
62-
const uint8_t* uint8_data_;
63-
const uint16_t* uint16_data_;
64-
const uint32_t* uint32_data_;
65-
const uint64_t* uint64_data_;
66-
const int8_t* int8_data_;
67-
const int16_t* int16_data_;
68-
const int32_t* int32_data_;
69-
const int64_t* int64_data_;
70-
const float* float_data_;
71-
const double* double_data_;
72-
} data_;
73-
std::size_t size_{0};
74-
public:
75-
76-
typed_array_view()
77-
: type_(), data_()
78-
{
79-
}
80-
81-
typed_array_view(const typed_array_view& other)
82-
: type_(other.type_), data_(other.data_), size_(other.size())
83-
{
84-
}
85-
86-
typed_array_view(typed_array_view&& other) noexcept
87-
{
88-
swap(*this,other);
89-
}
90-
91-
typed_array_view(const uint8_t* data, std::size_t size)
92-
: type_(typed_array_element_type::uint8), size_(size)
93-
{
94-
data_.uint8_data_ = data;
95-
}
96-
97-
typed_array_view(const uint16_t* data, std::size_t size)
98-
: type_(typed_array_element_type::uint16), size_(size)
99-
{
100-
data_.uint16_data_ = data;
101-
}
102-
103-
typed_array_view(const uint32_t* data, std::size_t size)
104-
: type_(typed_array_element_type::uint32), size_(size)
105-
{
106-
data_.uint32_data_ = data;
107-
}
108-
109-
typed_array_view(const uint64_t* data, std::size_t size)
110-
: type_(typed_array_element_type::uint64), size_(size)
111-
{
112-
data_.uint64_data_ = data;
113-
}
114-
115-
typed_array_view(const int8_t* data, std::size_t size)
116-
: type_(typed_array_element_type::int8), size_(size)
117-
{
118-
data_.int8_data_ = data;
119-
}
120-
121-
typed_array_view(const int16_t* data, std::size_t size)
122-
: type_(typed_array_element_type::int16), size_(size)
123-
{
124-
data_.int16_data_ = data;
125-
}
126-
127-
typed_array_view(const int32_t* data, std::size_t size)
128-
: type_(typed_array_element_type::int32), size_(size)
129-
{
130-
data_.int32_data_ = data;
131-
}
132-
133-
typed_array_view(const int64_t* data, std::size_t size)
134-
: type_(typed_array_element_type::int64), size_(size)
135-
{
136-
data_.int64_data_ = data;
137-
}
138-
139-
typed_array_view(half_array_arg_t, const uint16_t* data, std::size_t size)
140-
: type_(typed_array_element_type::half_float), size_(size)
141-
{
142-
data_.uint16_data_ = data;
143-
}
144-
145-
typed_array_view(const float* data, std::size_t size)
146-
: type_(typed_array_element_type::float32), size_(size)
147-
{
148-
data_.float_data_ = data;
149-
}
150-
151-
typed_array_view(const double* data, std::size_t size)
152-
: type_(typed_array_element_type::float64), size_(size)
153-
{
154-
data_.double_data_ = data;
155-
}
156-
157-
typed_array_view& operator=(const typed_array_view& other)
158-
{
159-
typed_array_view temp(other);
160-
swap(*this,temp);
161-
return *this;
162-
}
163-
164-
typed_array_element_type type() const {return type_;}
165-
166-
std::size_t size() const
167-
{
168-
return size_;
169-
}
170-
171-
jsoncons::span<const uint8_t> data(uint8_array_arg_t) const
172-
{
173-
JSONCONS_ASSERT(type_ == typed_array_element_type::uint8);
174-
return jsoncons::span<const uint8_t>(data_.uint8_data_, size_);
175-
}
176-
177-
jsoncons::span<const uint16_t> data(uint16_array_arg_t) const
178-
{
179-
JSONCONS_ASSERT(type_ == typed_array_element_type::uint16);
180-
return jsoncons::span<const uint16_t>(data_.uint16_data_, size_);
181-
}
182-
183-
jsoncons::span<const uint32_t> data(uint32_array_arg_t) const
184-
{
185-
JSONCONS_ASSERT(type_ == typed_array_element_type::uint32);
186-
return jsoncons::span<const uint32_t>(data_.uint32_data_, size_);
187-
}
188-
189-
jsoncons::span<const uint64_t> data(uint64_array_arg_t) const
190-
{
191-
JSONCONS_ASSERT(type_ == typed_array_element_type::uint64);
192-
return jsoncons::span<const uint64_t>(data_.uint64_data_, size_);
193-
}
194-
195-
jsoncons::span<const int8_t> data(int8_array_arg_t) const
196-
{
197-
JSONCONS_ASSERT(type_ == typed_array_element_type::int8);
198-
return jsoncons::span<const int8_t>(data_.int8_data_, size_);
199-
}
200-
201-
jsoncons::span<const int16_t> data(int16_array_arg_t) const
202-
{
203-
JSONCONS_ASSERT(type_ == typed_array_element_type::int16);
204-
return jsoncons::span<const int16_t>(data_.int16_data_, size_);
205-
}
206-
207-
jsoncons::span<const int32_t> data(int32_array_arg_t) const
208-
{
209-
JSONCONS_ASSERT(type_ == typed_array_element_type::int32);
210-
return jsoncons::span<const int32_t>(data_.int32_data_, size_);
211-
}
212-
213-
jsoncons::span<const int64_t> data(int64_array_arg_t) const
214-
{
215-
JSONCONS_ASSERT(type_ == typed_array_element_type::int64);
216-
return jsoncons::span<const int64_t>(data_.int64_data_, size_);
217-
}
218-
219-
jsoncons::span<const uint16_t> data(half_array_arg_t) const
220-
{
221-
JSONCONS_ASSERT(type_ == typed_array_element_type::half_float);
222-
return jsoncons::span<const uint16_t>(data_.uint16_data_, size_);
223-
}
224-
225-
jsoncons::span<const float> data(float_array_arg_t) const
226-
{
227-
JSONCONS_ASSERT(type_ == typed_array_element_type::float32);
228-
return jsoncons::span<const float>(data_.float_data_, size_);
229-
}
230-
231-
jsoncons::span<const double> data(double_array_arg_t) const
232-
{
233-
JSONCONS_ASSERT(type_ == typed_array_element_type::float64);
234-
return jsoncons::span<const double>(data_.double_data_, size_);
235-
}
236-
237-
friend void swap(typed_array_view& a, typed_array_view& b) noexcept
238-
{
239-
std::swap(a.data_,b.data_);
240-
std::swap(a.type_,b.type_);
241-
std::swap(a.size_,b.size_);
242-
}
31+
half_float, float32, float64
24332
};
24433

24534
template <typename T>

include/jsoncons_ext/cbor/cbor_cursor.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,16 @@ class basic_cbor_cursor : public basic_staj_cursor<char>, private virtual ser_co
173173
return parser_.element_type();
174174
}
175175

176-
jsoncons::span<uint8_t> typed_array() final
176+
jsoncons::span<uint8_t> typed_array() const final
177177
{
178178
return parser_.typed_array();
179179
}
180180

181+
void clear_typed_array() final
182+
{
183+
parser_.clear_typed_array();
184+
}
185+
181186
const staj_event& current() const override
182187
{
183188
return cursor_visitor_.event();

include/jsoncons_ext/cbor/cbor_parser.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,15 @@ class basic_cbor_parser : public ser_context
289289
return element_type_;
290290
}
291291

292-
jsoncons::span<uint8_t> typed_array()
292+
jsoncons::span<uint8_t> typed_array() const
293+
{
294+
return typed_array_;
295+
}
296+
297+
void clear_typed_array()
293298
{
294299
is_typed_array_ = false;
295300
state_stack_.pop_back();
296-
return typed_array_;
297301
}
298302

299303
void parse(item_event_visitor& visitor, std::error_code& ec)

test/cbor/src/cbor_typed_array_tests.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct my_cbor_visitor : public default_json_visitor
5858
JSONCONS_VISITOR_RETURN;
5959
}
6060
};
61-
#if 0
61+
6262
TEST_CASE("cbor multi dim row major cursor tests")
6363
{
6464
SECTION("Tag 86, float64, little endian")
@@ -104,10 +104,9 @@ TEST_CASE("cbor typed array cursor tests")
104104
//}
105105
}
106106
}
107-
#endif
107+
108108
TEST_CASE("cbor typed array tests")
109109
{
110-
#if 0
111110
SECTION("Tag 64 (uint8 Typed Array)")
112111
{
113112
//std::cout << "CBOR typed array Tag 64 (uint8 Typed Array)" << '\n';
@@ -319,7 +318,7 @@ TEST_CASE("cbor typed array tests")
319318
check_native(std::integral_constant<bool, jsoncons::endian::native == jsoncons::endian::little>(),
320319
input, buf);
321320
}
322-
#endif
321+
323322
SECTION("Tag 71 (uint64,little endian)")
324323
{
325324
//std::cout << "CBOR typed array Tag 71 (uint64,little endian)" << '\n';

0 commit comments

Comments
 (0)