source: rtos_arduino/trunk/arduino_lib/libraries/ArduinoJson/include/ArduinoJson/JsonVariantBase.hpp@ 209

Last change on this file since 209 was 209, checked in by ertl-honda, 8 years ago

BlueMix用のフィアルを追加

File size: 4.4 KB
Line 
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
13namespace ArduinoJson {
14
15// Forward declarations.
16class JsonArraySubscript;
17template <typename TKey>
18class JsonObjectSubscript;
19
20template <typename TImpl>
21class 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
74template <typename TImpl, typename TComparand>
75inline bool operator==(const JsonVariantBase<TImpl> &left, TComparand right) {
76 return left.template as<TComparand>() == right;
77}
78
79template <typename TImpl, typename TComparand>
80inline bool operator==(TComparand left, const JsonVariantBase<TImpl> &right) {
81 return left == right.template as<TComparand>();
82}
83
84template <typename TImpl, typename TComparand>
85inline bool operator!=(const JsonVariantBase<TImpl> &left, TComparand right) {
86 return left.template as<TComparand>() != right;
87}
88
89template <typename TImpl, typename TComparand>
90inline bool operator!=(TComparand left, const JsonVariantBase<TImpl> &right) {
91 return left != right.template as<TComparand>();
92}
93
94template <typename TImpl, typename TComparand>
95inline bool operator<=(const JsonVariantBase<TImpl> &left, TComparand right) {
96 return left.template as<TComparand>() <= right;
97}
98
99template <typename TImpl, typename TComparand>
100inline bool operator<=(TComparand left, const JsonVariantBase<TImpl> &right) {
101 return left <= right.template as<TComparand>();
102}
103
104template <typename TImpl, typename TComparand>
105inline bool operator>=(const JsonVariantBase<TImpl> &left, TComparand right) {
106 return left.template as<TComparand>() >= right;
107}
108
109template <typename TImpl, typename TComparand>
110inline bool operator>=(TComparand left, const JsonVariantBase<TImpl> &right) {
111 return left >= right.template as<TComparand>();
112}
113
114template <typename TImpl, typename TComparand>
115inline bool operator<(const JsonVariantBase<TImpl> &left, TComparand right) {
116 return left.template as<TComparand>() < right;
117}
118
119template <typename TImpl, typename TComparand>
120inline bool operator<(TComparand left, const JsonVariantBase<TImpl> &right) {
121 return left < right.template as<TComparand>();
122}
123
124template <typename TImpl, typename TComparand>
125inline bool operator>(const JsonVariantBase<TImpl> &left, TComparand right) {
126 return left.template as<TComparand>() > right;
127}
128
129template <typename TImpl, typename TComparand>
130inline bool operator>(TComparand left, const JsonVariantBase<TImpl> &right) {
131 return left > right.template as<TComparand>();
132}
133}
Note: See TracBrowser for help on using the repository browser.