Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
json_array.h
1#ifndef SOURCEMETA_CORE_JSON_ARRAY_H_
2#define SOURCEMETA_CORE_JSON_ARRAY_H_
3
4#include <initializer_list> // std::initializer_list
5#include <vector> // std::vector
6
7namespace sourcemeta::core {
8
11template <typename Value> class JSONArray {
12public:
13 // Constructors
15 using Container =
16 std::vector<Value, typename Value::template Allocator<Value>>;
17 JSONArray() : data{} {}
19 JSONArray(std::initializer_list<Value> values) : data{values} {}
20
21 // Operators
22 // We cannot default given that this class references
23 // a JSON "value" as an incomplete type
24 auto operator<(const JSONArray<Value> &other) const noexcept -> bool {
25 return this->data < other.data;
26 }
27 auto operator<=(const JSONArray<Value> &other) const noexcept -> bool {
28 return this->data <= other.data;
29 }
30 auto operator>(const JSONArray<Value> &other) const noexcept -> bool {
31 return this->data > other.data;
32 }
33 auto operator>=(const JSONArray<Value> &other) const noexcept -> bool {
34 return this->data >= other.data;
35 }
36 auto operator==(const JSONArray<Value> &other) const noexcept -> bool {
37 return this->data == other.data;
38 }
39 auto operator!=(const JSONArray<Value> &other) const noexcept -> bool {
40 return this->data != other.data;
41 }
42
43 // Member types
44 using value_type = typename Container::value_type;
45 using allocator_type = typename Container::allocator_type;
46 using size_type = typename Container::size_type;
47 using difference_type = typename Container::difference_type;
48 using reference = typename Container::reference;
49 using const_reference = typename Container::const_reference;
50 using pointer = typename Container::pointer;
51 using const_pointer = typename Container::const_pointer;
52 using iterator = typename Container::iterator;
53 using const_iterator = typename Container::const_iterator;
54 using reverse_iterator = typename Container::reverse_iterator;
55 using const_reverse_iterator = typename Container::const_reverse_iterator;
56
58 auto begin() noexcept -> iterator { return this->data.begin(); }
60 auto end() noexcept -> iterator { return this->data.end(); }
62 [[nodiscard]] auto begin() const noexcept -> const_iterator {
63 return this->data.begin();
64 }
66 [[nodiscard]] auto end() const noexcept -> const_iterator {
67 return this->data.end();
68 }
70 [[nodiscard]] auto cbegin() const noexcept -> const_iterator {
71 return this->data.cbegin();
72 }
74 [[nodiscard]] auto cend() const noexcept -> const_iterator {
75 return this->data.cend();
76 }
78 auto rbegin() noexcept -> reverse_iterator { return this->data.rbegin(); }
80 auto rend() noexcept -> reverse_iterator { return this->data.rend(); }
82 [[nodiscard]] auto rbegin() const noexcept -> const_reverse_iterator {
83 return this->data.rbegin();
84 }
86 [[nodiscard]] auto rend() const noexcept -> const_reverse_iterator {
87 return this->data.rend();
88 }
90 [[nodiscard]] auto crbegin() const noexcept -> const_reverse_iterator {
91 return this->data.crbegin();
92 }
94 [[nodiscard]] auto crend() const noexcept -> const_reverse_iterator {
95 return this->data.crend();
96 }
97
99 [[nodiscard]] auto size() const noexcept -> size_type {
100 return this->data.size();
101 }
102
104 auto reserve(const size_type capacity) -> void {
105 this->data.reserve(capacity);
106 }
107
108private:
109 friend Value;
110// Exporting symbols that depends on the standard C++ library is considered
111// safe.
112// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
113#if defined(_MSC_VER)
114#pragma warning(disable : 4251)
115#endif
116 Container data;
117#if defined(_MSC_VER)
118#pragma warning(default : 4251)
119#endif
120};
121
122} // namespace sourcemeta::core
123
124#endif
std::vector< Value, typename Value::template Allocator< Value > > Container
The underlying container type that holds the array elements.
Definition json_array.h:15
auto reserve(const size_type capacity) -> void
Reserve capacity for a given number of elements.
Definition json_array.h:104
JSONArray(std::initializer_list< Value > values)
Construct an array from a list of values.
Definition json_array.h:19
auto size() const noexcept -> size_type
Get array size.
Definition json_array.h:99
Definition json_array.h:11