JsonVariantBase.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright Benoit Blanchon 2014-2015
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #pragma once
  7. #include "Internals/ForceInline.hpp"
  8. #include "JsonObjectKey.hpp"
  9. namespace ArduinoJson {
  10. // Forward declarations.
  11. class JsonArraySubscript;
  12. class JsonObjectSubscript;
  13. template <typename TImpl>
  14. class JsonVariantBase {
  15. public:
  16. // Gets the variant as a boolean value.
  17. // Returns false if the variant is not a boolean value.
  18. FORCE_INLINE operator bool() const { return as<bool>(); }
  19. // Gets the variant as a floating-point value.
  20. // Returns 0.0 if the variant is not a floating-point value
  21. FORCE_INLINE operator double() const { return as<double>(); }
  22. FORCE_INLINE operator float() const { return as<float>(); }
  23. // Gets the variant as an integer value.
  24. // Returns 0 if the variant is not an integer value.
  25. FORCE_INLINE operator signed long() const { return as<signed long>(); }
  26. FORCE_INLINE operator signed char() const { return as<signed char>(); }
  27. FORCE_INLINE operator signed int() const { return as<signed int>(); }
  28. FORCE_INLINE operator signed short() const { return as<signed short>(); }
  29. FORCE_INLINE operator unsigned char() const { return as<unsigned char>(); }
  30. FORCE_INLINE operator unsigned int() const { return as<unsigned int>(); }
  31. FORCE_INLINE operator unsigned long() const { return as<unsigned long>(); }
  32. FORCE_INLINE operator unsigned short() const { return as<unsigned short>(); }
  33. // Gets the variant as a string.
  34. // Returns NULL if variant is not a string.
  35. FORCE_INLINE operator const char *() const { return as<const char *>(); }
  36. FORCE_INLINE const char *asString() const { return as<const char *>(); }
  37. FORCE_INLINE operator String() const { return as<String>(); }
  38. // Gets the variant as an array.
  39. // Returns a reference to the JsonArray or JsonArray::invalid() if the
  40. // variant
  41. // is not an array.
  42. FORCE_INLINE operator JsonArray &() const { return as<JsonArray &>(); }
  43. FORCE_INLINE JsonArray &asArray() const { return as<JsonArray &>(); }
  44. // Gets the variant as an object.
  45. // Returns a reference to the JsonObject or JsonObject::invalid() if the
  46. // variant is not an object.
  47. FORCE_INLINE operator JsonObject &() const { return as<JsonObject &>(); }
  48. FORCE_INLINE JsonObject &asObject() const { return as<JsonObject &>(); }
  49. template <typename T>
  50. FORCE_INLINE const T as() const {
  51. return impl()->template as<T>();
  52. }
  53. // Mimics an array or an object.
  54. // Returns the size of the array or object if the variant has that type.
  55. // Returns 0 if the variant is neither an array nor an object
  56. size_t size() const { return asArray().size() + asObject().size(); }
  57. // Mimics an array.
  58. // Returns the element at specified index if the variant is an array.
  59. // Returns JsonVariant::invalid() if the variant is not an array.
  60. FORCE_INLINE const JsonArraySubscript operator[](int index) const;
  61. // Mimics an object.
  62. // Returns the value associated with the specified key if the variant is
  63. // an object.
  64. // Return JsonVariant::invalid() if the variant is not an object.
  65. FORCE_INLINE const JsonObjectSubscript operator[](const char *key) const;
  66. FORCE_INLINE const JsonObjectSubscript operator[](const String &key) const;
  67. private:
  68. const TImpl *impl() const { return static_cast<const TImpl *>(this); }
  69. };
  70. template <typename TImpl, typename TComparand>
  71. inline bool operator==(const JsonVariantBase<TImpl> &left, TComparand right) {
  72. return left.template as<TComparand>() == right;
  73. }
  74. template <typename TImpl, typename TComparand>
  75. inline bool operator==(TComparand left, const JsonVariantBase<TImpl> &right) {
  76. return left == right.template as<TComparand>();
  77. }
  78. template <typename TImpl, typename TComparand>
  79. inline bool operator!=(const JsonVariantBase<TImpl> &left, TComparand right) {
  80. return left.template as<TComparand>() != right;
  81. }
  82. template <typename TImpl, typename TComparand>
  83. inline bool operator!=(TComparand left, const JsonVariantBase<TImpl> &right) {
  84. return left != right.template as<TComparand>();
  85. }
  86. template <typename TImpl, typename TComparand>
  87. inline bool operator<=(const JsonVariantBase<TImpl> &left, TComparand right) {
  88. return left.template as<TComparand>() <= right;
  89. }
  90. template <typename TImpl, typename TComparand>
  91. inline bool operator<=(TComparand left, const JsonVariantBase<TImpl> &right) {
  92. return left <= right.template as<TComparand>();
  93. }
  94. template <typename TImpl, typename TComparand>
  95. inline bool operator>=(const JsonVariantBase<TImpl> &left, TComparand right) {
  96. return left.template as<TComparand>() >= right;
  97. }
  98. template <typename TImpl, typename TComparand>
  99. inline bool operator>=(TComparand left, const JsonVariantBase<TImpl> &right) {
  100. return left >= right.template as<TComparand>();
  101. }
  102. template <typename TImpl, typename TComparand>
  103. inline bool operator<(const JsonVariantBase<TImpl> &left, TComparand right) {
  104. return left.template as<TComparand>() < right;
  105. }
  106. template <typename TImpl, typename TComparand>
  107. inline bool operator<(TComparand left, const JsonVariantBase<TImpl> &right) {
  108. return left < right.template as<TComparand>();
  109. }
  110. template <typename TImpl, typename TComparand>
  111. inline bool operator>(const JsonVariantBase<TImpl> &left, TComparand right) {
  112. return left.template as<TComparand>() > right;
  113. }
  114. template <typename TImpl, typename TComparand>
  115. inline bool operator>(TComparand left, const JsonVariantBase<TImpl> &right) {
  116. return left > right.template as<TComparand>();
  117. }
  118. }