[209] | 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 "Configuration.hpp"
|
---|
| 11 | #include "JsonVariantBase.hpp"
|
---|
| 12 |
|
---|
| 13 | #ifdef _MSC_VER
|
---|
| 14 | #pragma warning(push)
|
---|
| 15 | #pragma warning(disable : 4522)
|
---|
| 16 | #endif
|
---|
| 17 |
|
---|
| 18 | namespace ArduinoJson {
|
---|
| 19 | class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
|
---|
| 20 | public:
|
---|
| 21 | FORCE_INLINE JsonArraySubscript(JsonArray& array, size_t index)
|
---|
| 22 | : _array(array), _index(index) {}
|
---|
| 23 |
|
---|
| 24 | JsonArraySubscript& operator=(const JsonArraySubscript& src) {
|
---|
| 25 | _array.set<const JsonVariant&>(_index, src);
|
---|
| 26 | return *this;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | template <typename T>
|
---|
| 30 | typename TypeTraits::EnableIf<JsonArray::CanSet<T&>::value,
|
---|
| 31 | JsonArraySubscript>::type&
|
---|
| 32 | operator=(const T& src) {
|
---|
| 33 | _array.set<T&>(_index, const_cast<T&>(src));
|
---|
| 34 | return *this;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | template <typename T>
|
---|
| 38 | typename TypeTraits::EnableIf<JsonArray::CanSet<T>::value,
|
---|
| 39 | JsonArraySubscript>::type&
|
---|
| 40 | operator=(T src) {
|
---|
| 41 | _array.set<T>(_index, src);
|
---|
| 42 | return *this;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | FORCE_INLINE bool success() const { return _index < _array.size(); }
|
---|
| 46 |
|
---|
| 47 | FORCE_INLINE operator JsonVariant() const { return _array.get(_index); }
|
---|
| 48 |
|
---|
| 49 | template <typename T>
|
---|
| 50 | FORCE_INLINE T as() const {
|
---|
| 51 | return _array.get<T>(_index);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | template <typename T>
|
---|
| 55 | FORCE_INLINE bool is() const {
|
---|
| 56 | return _array.is<T>(_index);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | void writeTo(Internals::JsonWriter& writer) const {
|
---|
| 60 | _array.get(_index).writeTo(writer);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | template <typename TValue>
|
---|
| 64 | void set(TValue value) {
|
---|
| 65 | _array.set(_index, value);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | private:
|
---|
| 69 | JsonArray& _array;
|
---|
| 70 | const size_t _index;
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 | #if ARDUINOJSON_ENABLE_STD_STREAM
|
---|
| 74 | inline std::ostream& operator<<(std::ostream& os,
|
---|
| 75 | const JsonArraySubscript& source) {
|
---|
| 76 | return source.printTo(os);
|
---|
| 77 | }
|
---|
| 78 | #endif
|
---|
| 79 |
|
---|
| 80 | } // namespace ArduinoJson
|
---|
| 81 |
|
---|
| 82 | #ifdef _MSC_VER
|
---|
| 83 | #pragma warning(pop)
|
---|
| 84 | #endif
|
---|