Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ElementProxy.hpp Source File

ElementProxy.hpp

00001 // ArduinoJson - arduinojson.org
00002 // Copyright Benoit Blanchon 2014-2021
00003 // MIT License
00004 
00005 #pragma once
00006 
00007 #include <ArduinoJson/Configuration.hpp>
00008 #include <ArduinoJson/Variant/VariantOperators.hpp>
00009 #include <ArduinoJson/Variant/VariantShortcuts.hpp>
00010 #include <ArduinoJson/Variant/VariantTo.hpp>
00011 
00012 #ifdef _MSC_VER
00013 #pragma warning(push)
00014 #pragma warning(disable : 4522)
00015 #endif
00016 
00017 namespace ARDUINOJSON_NAMESPACE {
00018 
00019 template <typename TArray>
00020 class ElementProxy : public VariantOperators<ElementProxy<TArray> >,
00021                      public VariantShortcuts<ElementProxy<TArray> >,
00022                      public Visitable,
00023                      public VariantTag {
00024   typedef ElementProxy<TArray> this_type;
00025 
00026  public:
00027   typedef VariantRef variant_type;
00028 
00029   FORCE_INLINE ElementProxy(TArray array, size_t index)
00030       : _array(array), _index(index) {}
00031 
00032   FORCE_INLINE ElementProxy(const ElementProxy& src)
00033       : _array(src._array), _index(src._index) {}
00034 
00035   FORCE_INLINE this_type& operator=(const this_type& src) {
00036     getOrAddUpstreamElement().set(src.as<VariantConstRef>());
00037     return *this;
00038   }
00039 
00040   // Replaces the value
00041   //
00042   // operator=(const TValue&)
00043   // TValue = bool, long, int, short, float, double, serialized, VariantRef,
00044   //          std::string, String, ArrayRef, ObjectRef
00045   template <typename T>
00046   FORCE_INLINE this_type& operator=(const T& src) {
00047     getOrAddUpstreamElement().set(src);
00048     return *this;
00049   }
00050   //
00051   // operator=(TValue)
00052   // TValue = char*, const char*, const __FlashStringHelper*
00053   template <typename T>
00054   FORCE_INLINE this_type& operator=(T* src) {
00055     getOrAddUpstreamElement().set(src);
00056     return *this;
00057   }
00058 
00059   FORCE_INLINE void clear() const {
00060     getUpstreamElement().clear();
00061   }
00062 
00063   FORCE_INLINE bool isNull() const {
00064     return getUpstreamElement().isNull();
00065   }
00066 
00067   template <typename T>
00068   FORCE_INLINE typename VariantAs<T>::type as() const {
00069     return getUpstreamElement().template as<T>();
00070   }
00071 
00072   template <typename T>
00073   FORCE_INLINE operator T() const {
00074     return getUpstreamElement();
00075   }
00076 
00077   template <typename T>
00078   FORCE_INLINE bool is() const {
00079     return getUpstreamElement().template is<T>();
00080   }
00081 
00082   template <typename T>
00083   FORCE_INLINE typename VariantTo<T>::type to() const {
00084     return getOrAddUpstreamElement().template to<T>();
00085   }
00086 
00087   // Replaces the value
00088   //
00089   // bool set(const TValue&)
00090   // TValue = bool, long, int, short, float, double, serialized, VariantRef,
00091   //          std::string, String, ArrayRef, ObjectRef
00092   template <typename TValue>
00093   FORCE_INLINE bool set(const TValue& value) const {
00094     return getOrAddUpstreamElement().set(value);
00095   }
00096   //
00097   // bool set(TValue)
00098   // TValue = char*, const char*, const __FlashStringHelper*
00099   template <typename TValue>
00100   FORCE_INLINE bool set(TValue* value) const {
00101     return getOrAddUpstreamElement().set(value);
00102   }
00103 
00104   template <typename TVisitor>
00105   typename TVisitor::result_type accept(TVisitor& visitor) const {
00106     return getUpstreamElement().accept(visitor);
00107   }
00108 
00109   FORCE_INLINE size_t size() const {
00110     return getUpstreamElement().size();
00111   }
00112 
00113   template <typename TNestedKey>
00114   VariantRef getMember(TNestedKey* key) const {
00115     return getUpstreamElement().getMember(key);
00116   }
00117 
00118   template <typename TNestedKey>
00119   VariantRef getMember(const TNestedKey& key) const {
00120     return getUpstreamElement().getMember(key);
00121   }
00122 
00123   template <typename TNestedKey>
00124   VariantRef getOrAddMember(TNestedKey* key) const {
00125     return getOrAddUpstreamElement().getOrAddMember(key);
00126   }
00127 
00128   template <typename TNestedKey>
00129   VariantRef getOrAddMember(const TNestedKey& key) const {
00130     return getOrAddUpstreamElement().getOrAddMember(key);
00131   }
00132 
00133   VariantRef addElement() const {
00134     return getOrAddUpstreamElement().addElement();
00135   }
00136 
00137   VariantRef getElement(size_t index) const {
00138     return getOrAddUpstreamElement().getElement(index);
00139   }
00140 
00141   VariantRef getOrAddElement(size_t index) const {
00142     return getOrAddUpstreamElement().getOrAddElement(index);
00143   }
00144 
00145   FORCE_INLINE void remove(size_t index) const {
00146     getUpstreamElement().remove(index);
00147   }
00148   // remove(char*) const
00149   // remove(const char*) const
00150   // remove(const __FlashStringHelper*) const
00151   template <typename TChar>
00152   FORCE_INLINE typename enable_if<IsString<TChar*>::value>::type remove(
00153       TChar* key) const {
00154     getUpstreamElement().remove(key);
00155   }
00156   // remove(const std::string&) const
00157   // remove(const String&) const
00158   template <typename TString>
00159   FORCE_INLINE typename enable_if<IsString<TString>::value>::type remove(
00160       const TString& key) const {
00161     getUpstreamElement().remove(key);
00162   }
00163 
00164  private:
00165   FORCE_INLINE VariantRef getUpstreamElement() const {
00166     return _array.getElement(_index);
00167   }
00168 
00169   FORCE_INLINE VariantRef getOrAddUpstreamElement() const {
00170     return _array.getOrAddElement(_index);
00171   }
00172 
00173   TArray _array;
00174   const size_t _index;
00175 };
00176 
00177 }  // namespace ARDUINOJSON_NAMESPACE
00178 
00179 #ifdef _MSC_VER
00180 #pragma warning(pop)
00181 #endif