diff options
author | Daniel Friesel <daniel.friesel@uos.de> | 2021-05-12 09:12:09 +0200 |
---|---|---|
committer | Daniel Friesel <daniel.friesel@uos.de> | 2021-05-12 09:12:09 +0200 |
commit | 39895a677e5d370824e702cfe90ebc67737b8482 (patch) | |
tree | ff5b4cc9e373d33a795d50d9333e05549bd9f2b8 /include/lib/ArduinoJson/Array/ElementProxy.hpp | |
parent | 42e7fdf01c3a5701bb51e93ad6c650c3dbbc5450 (diff) |
import ArduinoJson 6.18.0
Diffstat (limited to 'include/lib/ArduinoJson/Array/ElementProxy.hpp')
-rw-r--r-- | include/lib/ArduinoJson/Array/ElementProxy.hpp | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/include/lib/ArduinoJson/Array/ElementProxy.hpp b/include/lib/ArduinoJson/Array/ElementProxy.hpp new file mode 100644 index 0000000..c6062e4 --- /dev/null +++ b/include/lib/ArduinoJson/Array/ElementProxy.hpp @@ -0,0 +1,193 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Configuration.hpp> +#include <ArduinoJson/Variant/VariantOperators.hpp> +#include <ArduinoJson/Variant/VariantShortcuts.hpp> +#include <ArduinoJson/Variant/VariantTo.hpp> + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4522) +#endif + +namespace ARDUINOJSON_NAMESPACE { + +template <typename TArray> +class ElementProxy : public VariantOperators<ElementProxy<TArray> >, + public VariantShortcuts<ElementProxy<TArray> >, + public Visitable, + public VariantTag { + typedef ElementProxy<TArray> this_type; + + public: + typedef VariantRef variant_type; + + FORCE_INLINE ElementProxy(TArray array, size_t index) + : _array(array), _index(index) {} + + FORCE_INLINE ElementProxy(const ElementProxy& src) + : _array(src._array), _index(src._index) {} + + FORCE_INLINE this_type& operator=(const this_type& src) { + getOrAddUpstreamElement().set(src.as<VariantConstRef>()); + return *this; + } + + // Replaces the value + // + // operator=(const TValue&) + // TValue = bool, long, int, short, float, double, serialized, VariantRef, + // std::string, String, ArrayRef, ObjectRef + template <typename T> + FORCE_INLINE this_type& operator=(const T& src) { + getOrAddUpstreamElement().set(src); + return *this; + } + // + // operator=(TValue) + // TValue = char*, const char*, const __FlashStringHelper* + template <typename T> + FORCE_INLINE this_type& operator=(T* src) { + getOrAddUpstreamElement().set(src); + return *this; + } + + FORCE_INLINE void clear() const { + getUpstreamElement().clear(); + } + + FORCE_INLINE bool isNull() const { + return getUpstreamElement().isNull(); + } + + template <typename T> + FORCE_INLINE typename enable_if<!is_same<T, char*>::value, T>::type as() + const { + return getUpstreamElement().template as<T>(); + } + + template <typename T> + FORCE_INLINE typename enable_if<is_same<T, char*>::value, const char*>::type + ARDUINOJSON_DEPRECATED("Replace as<char*>() with as<const char*>()") + as() const { + return as<const char*>(); + } + + template <typename T> + FORCE_INLINE operator T() const { + return getUpstreamElement(); + } + + template <typename T> + FORCE_INLINE bool is() const { + return getUpstreamElement().template is<T>(); + } + + template <typename T> + FORCE_INLINE typename VariantTo<T>::type to() const { + return getOrAddUpstreamElement().template to<T>(); + } + + // Replaces the value + // + // bool set(const TValue&) + // TValue = bool, long, int, short, float, double, serialized, VariantRef, + // std::string, String, ArrayRef, ObjectRef + template <typename TValue> + FORCE_INLINE bool set(const TValue& value) const { + return getOrAddUpstreamElement().set(value); + } + // + // bool set(TValue) + // TValue = char*, const char*, const __FlashStringHelper* + template <typename TValue> + FORCE_INLINE bool set(TValue* value) const { + return getOrAddUpstreamElement().set(value); + } + + template <typename TVisitor> + typename TVisitor::result_type accept(TVisitor& visitor) const { + return getUpstreamElement().accept(visitor); + } + + FORCE_INLINE size_t size() const { + return getUpstreamElement().size(); + } + + template <typename TNestedKey> + VariantRef getMember(TNestedKey* key) const { + return getUpstreamElement().getMember(key); + } + + template <typename TNestedKey> + VariantRef getMember(const TNestedKey& key) const { + return getUpstreamElement().getMember(key); + } + + template <typename TNestedKey> + VariantRef getOrAddMember(TNestedKey* key) const { + return getOrAddUpstreamElement().getOrAddMember(key); + } + + template <typename TNestedKey> + VariantRef getOrAddMember(const TNestedKey& key) const { + return getOrAddUpstreamElement().getOrAddMember(key); + } + + VariantRef addElement() const { + return getOrAddUpstreamElement().addElement(); + } + + VariantRef getElement(size_t index) const { + return getOrAddUpstreamElement().getElement(index); + } + + VariantRef getOrAddElement(size_t index) const { + return getOrAddUpstreamElement().getOrAddElement(index); + } + + FORCE_INLINE void remove(size_t index) const { + getUpstreamElement().remove(index); + } + // remove(char*) const + // remove(const char*) const + // remove(const __FlashStringHelper*) const + template <typename TChar> + FORCE_INLINE typename enable_if<IsString<TChar*>::value>::type remove( + TChar* key) const { + getUpstreamElement().remove(key); + } + // remove(const std::string&) const + // remove(const String&) const + template <typename TString> + FORCE_INLINE typename enable_if<IsString<TString>::value>::type remove( + const TString& key) const { + getUpstreamElement().remove(key); + } + + private: + FORCE_INLINE VariantRef getUpstreamElement() const { + return _array.getElement(_index); + } + + FORCE_INLINE VariantRef getOrAddUpstreamElement() const { + return _array.getOrAddElement(_index); + } + + friend bool convertToJson(const this_type& src, VariantRef dst) { + return dst.set(src.getUpstreamElement()); + } + + TArray _array; + const size_t _index; +}; + +} // namespace ARDUINOJSON_NAMESPACE + +#ifdef _MSC_VER +#pragma warning(pop) +#endif |