mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-26 08:47:31 +02:00
118 lines
3.0 KiB
C++
118 lines
3.0 KiB
C++
// ArduinoJson - arduinojson.org
|
|
// Copyright Benoit Blanchon 2014-2018
|
|
// MIT License
|
|
|
|
#pragma once
|
|
|
|
#include "Configuration.hpp"
|
|
#include "JsonVariantBase.hpp"
|
|
|
|
#ifdef _MSC_VER
|
|
#pragma warning(push)
|
|
#pragma warning(disable : 4522)
|
|
#endif
|
|
|
|
namespace ArduinoJson {
|
|
namespace Internals {
|
|
class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
|
|
public:
|
|
FORCE_INLINE JsonArraySubscript(JsonArray array, size_t index)
|
|
: _array(array), _index(index) {}
|
|
|
|
FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& src) {
|
|
_array.set(_index, src.as<JsonVariant>());
|
|
return *this;
|
|
}
|
|
|
|
// Replaces the value
|
|
//
|
|
// operator=(const TValue&)
|
|
// TValue = bool, long, int, short, float, double, serialized, JsonVariant,
|
|
// std::string, String, JsonArray, JsonObject
|
|
template <typename T>
|
|
FORCE_INLINE JsonArraySubscript& operator=(const T& src) {
|
|
_array.set(_index, src);
|
|
return *this;
|
|
}
|
|
//
|
|
// operator=(TValue)
|
|
// TValue = char*, const char*, const FlashStringHelper*
|
|
template <typename T>
|
|
FORCE_INLINE JsonArraySubscript& operator=(T* src) {
|
|
_array.set(_index, src);
|
|
return *this;
|
|
}
|
|
|
|
FORCE_INLINE bool isNull() const {
|
|
return _index >= _array.size();
|
|
}
|
|
|
|
template <typename T>
|
|
FORCE_INLINE typename JsonVariantAs<T>::type as() const {
|
|
return _array.get<T>(_index);
|
|
}
|
|
|
|
template <typename T>
|
|
FORCE_INLINE bool is() const {
|
|
return _array.is<T>(_index);
|
|
}
|
|
|
|
template <typename T>
|
|
FORCE_INLINE typename JsonVariantTo<T>::type to() {
|
|
return _array.get<JsonVariant>(_index).to<T>();
|
|
}
|
|
|
|
// Replaces the value
|
|
//
|
|
// bool set(const TValue&)
|
|
// TValue = bool, long, int, short, float, double, serialized, JsonVariant,
|
|
// std::string, String, JsonArray, JsonObject
|
|
template <typename TValue>
|
|
FORCE_INLINE bool set(const TValue& value) {
|
|
return _array.set(_index, value);
|
|
}
|
|
//
|
|
// bool set(TValue)
|
|
// TValue = char*, const char*, const FlashStringHelper*
|
|
template <typename TValue>
|
|
FORCE_INLINE bool set(TValue* value) {
|
|
return _array.set(_index, value);
|
|
}
|
|
|
|
template <typename Visitor>
|
|
void accept(Visitor& visitor) const {
|
|
return _array.get<JsonVariant>(_index).accept(visitor);
|
|
}
|
|
|
|
private:
|
|
JsonArray _array;
|
|
const size_t _index;
|
|
};
|
|
|
|
template <typename TImpl>
|
|
inline JsonArraySubscript JsonVariantSubscripts<TImpl>::operator[](
|
|
size_t index) {
|
|
return impl()->template as<JsonArray>()[index];
|
|
}
|
|
|
|
template <typename TImpl>
|
|
inline const JsonArraySubscript JsonVariantSubscripts<TImpl>::operator[](
|
|
size_t index) const {
|
|
return impl()->template as<JsonArray>()[index];
|
|
}
|
|
} // namespace Internals
|
|
|
|
inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) {
|
|
return Internals::JsonArraySubscript(*this, index);
|
|
}
|
|
|
|
inline const Internals::JsonArraySubscript JsonArray::operator[](
|
|
size_t index) const {
|
|
return Internals::JsonArraySubscript(*this, index);
|
|
}
|
|
} // namespace ArduinoJson
|
|
|
|
#ifdef _MSC_VER
|
|
#pragma warning(pop)
|
|
#endif
|