1 | // Copyright Benoit Blanchon 2014-2016
|
---|
2 | // MIT License
|
---|
3 | //
|
---|
4 | // Arduino JSON library
|
---|
5 | // https://github.com/bblanchon/ArduinoJson
|
---|
6 | // If you like this project, please add a star!
|
---|
7 |
|
---|
8 | #pragma once
|
---|
9 |
|
---|
10 | #include "Internals/ForceInline.hpp"
|
---|
11 | #include "JsonObjectKey.hpp"
|
---|
12 |
|
---|
13 | namespace ArduinoJson {
|
---|
14 |
|
---|
15 | // Forward declarations.
|
---|
16 | class JsonArraySubscript;
|
---|
17 | template <typename TKey>
|
---|
18 | class JsonObjectSubscript;
|
---|
19 |
|
---|
20 | template <typename TImpl>
|
---|
21 | class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
|
---|
22 | public:
|
---|
23 | FORCE_INLINE const char *asString() const { return as<const char *>(); }
|
---|
24 |
|
---|
25 | // Gets the variant as an array.
|
---|
26 | // Returns a reference to the JsonArray or JsonArray::invalid() if the
|
---|
27 | // variant
|
---|
28 | // is not an array.
|
---|
29 | FORCE_INLINE operator JsonArray &() const { return as<JsonArray &>(); }
|
---|
30 | FORCE_INLINE JsonArray &asArray() const { return as<JsonArray &>(); }
|
---|
31 |
|
---|
32 | // Gets the variant as an object.
|
---|
33 | // Returns a reference to the JsonObject or JsonObject::invalid() if the
|
---|
34 | // variant is not an object.
|
---|
35 | FORCE_INLINE operator JsonObject &() const { return as<JsonObject &>(); }
|
---|
36 | FORCE_INLINE JsonObject &asObject() const { return as<JsonObject &>(); }
|
---|
37 |
|
---|
38 | template <typename T>
|
---|
39 | FORCE_INLINE operator T() const {
|
---|
40 | return as<T>();
|
---|
41 | }
|
---|
42 |
|
---|
43 | template <typename T>
|
---|
44 | FORCE_INLINE const T as() const {
|
---|
45 | return impl()->template as<T>();
|
---|
46 | }
|
---|
47 |
|
---|
48 | // Mimics an array or an object.
|
---|
49 | // Returns the size of the array or object if the variant has that type.
|
---|
50 | // Returns 0 if the variant is neither an array nor an object
|
---|
51 | size_t size() const { return asArray().size() + asObject().size(); }
|
---|
52 |
|
---|
53 | // Mimics an array.
|
---|
54 | // Returns the element at specified index if the variant is an array.
|
---|
55 | // Returns JsonVariant::invalid() if the variant is not an array.
|
---|
56 | FORCE_INLINE const JsonArraySubscript operator[](int index) const;
|
---|
57 |
|
---|
58 | // Mimics an object.
|
---|
59 | // Returns the value associated with the specified key if the variant is
|
---|
60 | // an object.
|
---|
61 | // Return JsonVariant::invalid() if the variant is not an object.
|
---|
62 | FORCE_INLINE const JsonObjectSubscript<const char *> operator[](
|
---|
63 | const char *key) const;
|
---|
64 | FORCE_INLINE const JsonObjectSubscript<const String &> operator[](
|
---|
65 | const String &key) const;
|
---|
66 |
|
---|
67 | // Serialize the variant to a JsonWriter
|
---|
68 | void writeTo(Internals::JsonWriter &writer) const;
|
---|
69 |
|
---|
70 | private:
|
---|
71 | const TImpl *impl() const { return static_cast<const TImpl *>(this); }
|
---|
72 | };
|
---|
73 |
|
---|
74 | template <typename TImpl, typename TComparand>
|
---|
75 | inline bool operator==(const JsonVariantBase<TImpl> &left, TComparand right) {
|
---|
76 | return left.template as<TComparand>() == right;
|
---|
77 | }
|
---|
78 |
|
---|
79 | template <typename TImpl, typename TComparand>
|
---|
80 | inline bool operator==(TComparand left, const JsonVariantBase<TImpl> &right) {
|
---|
81 | return left == right.template as<TComparand>();
|
---|
82 | }
|
---|
83 |
|
---|
84 | template <typename TImpl, typename TComparand>
|
---|
85 | inline bool operator!=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
---|
86 | return left.template as<TComparand>() != right;
|
---|
87 | }
|
---|
88 |
|
---|
89 | template <typename TImpl, typename TComparand>
|
---|
90 | inline bool operator!=(TComparand left, const JsonVariantBase<TImpl> &right) {
|
---|
91 | return left != right.template as<TComparand>();
|
---|
92 | }
|
---|
93 |
|
---|
94 | template <typename TImpl, typename TComparand>
|
---|
95 | inline bool operator<=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
---|
96 | return left.template as<TComparand>() <= right;
|
---|
97 | }
|
---|
98 |
|
---|
99 | template <typename TImpl, typename TComparand>
|
---|
100 | inline bool operator<=(TComparand left, const JsonVariantBase<TImpl> &right) {
|
---|
101 | return left <= right.template as<TComparand>();
|
---|
102 | }
|
---|
103 |
|
---|
104 | template <typename TImpl, typename TComparand>
|
---|
105 | inline bool operator>=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
---|
106 | return left.template as<TComparand>() >= right;
|
---|
107 | }
|
---|
108 |
|
---|
109 | template <typename TImpl, typename TComparand>
|
---|
110 | inline bool operator>=(TComparand left, const JsonVariantBase<TImpl> &right) {
|
---|
111 | return left >= right.template as<TComparand>();
|
---|
112 | }
|
---|
113 |
|
---|
114 | template <typename TImpl, typename TComparand>
|
---|
115 | inline bool operator<(const JsonVariantBase<TImpl> &left, TComparand right) {
|
---|
116 | return left.template as<TComparand>() < right;
|
---|
117 | }
|
---|
118 |
|
---|
119 | template <typename TImpl, typename TComparand>
|
---|
120 | inline bool operator<(TComparand left, const JsonVariantBase<TImpl> &right) {
|
---|
121 | return left < right.template as<TComparand>();
|
---|
122 | }
|
---|
123 |
|
---|
124 | template <typename TImpl, typename TComparand>
|
---|
125 | inline bool operator>(const JsonVariantBase<TImpl> &left, TComparand right) {
|
---|
126 | return left.template as<TComparand>() > right;
|
---|
127 | }
|
---|
128 |
|
---|
129 | template <typename TImpl, typename TComparand>
|
---|
130 | inline bool operator>(TComparand left, const JsonVariantBase<TImpl> &right) {
|
---|
131 | return left > right.template as<TComparand>();
|
---|
132 | }
|
---|
133 | }
|
---|