Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
VariantAs.hpp
00001 // ArduinoJson - arduinojson.org 00002 // Copyright Benoit Blanchon 2014-2021 00003 // MIT License 00004 00005 #pragma once 00006 00007 #include <ArduinoJson/Strings/IsWriteableString.hpp> 00008 #include <ArduinoJson/Variant/VariantData.hpp> 00009 00010 namespace ARDUINOJSON_NAMESPACE { 00011 00012 class ArrayRef; 00013 class ArrayConstRef; 00014 class ObjectRef; 00015 class ObjectConstRef; 00016 class VariantRef; 00017 class VariantConstRef; 00018 00019 // A metafunction that returns the type of the value returned by 00020 // VariantRef::as<T>() 00021 template <typename T> 00022 struct VariantAs { 00023 typedef T type; 00024 }; 00025 00026 template <> 00027 struct VariantAs<char*> { 00028 typedef const char* type; 00029 }; 00030 00031 // A metafunction that returns the type of the value returned by 00032 // VariantRef::as<T>() 00033 template <typename T> 00034 struct VariantConstAs { 00035 typedef typename VariantAs<T>::type type; 00036 }; 00037 00038 template <> 00039 struct VariantConstAs<VariantRef> { 00040 typedef VariantConstRef type; 00041 }; 00042 00043 template <> 00044 struct VariantConstAs<ObjectRef> { 00045 typedef ObjectConstRef type; 00046 }; 00047 00048 template <> 00049 struct VariantConstAs<ArrayRef> { 00050 typedef ArrayConstRef type; 00051 }; 00052 00053 // --- 00054 00055 template <typename T> 00056 inline typename enable_if<is_integral<T>::value && !is_same<bool, T>::value, 00057 T>::type 00058 variantAs(const VariantData* data) { 00059 ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T); 00060 return data != 0 ? data->asIntegral<T>() : T(0); 00061 } 00062 00063 template <typename T> 00064 inline typename enable_if<is_enum<T>::value, T>::type variantAs( 00065 const VariantData* data) { 00066 return data != 0 ? static_cast<T>(data->asIntegral<int>()) : T(); 00067 } 00068 00069 template <typename T> 00070 inline typename enable_if<is_same<T, bool>::value, T>::type variantAs( 00071 const VariantData* data) { 00072 return data != 0 ? data->asBoolean() : false; 00073 } 00074 00075 template <typename T> 00076 inline typename enable_if<is_floating_point<T>::value, T>::type variantAs( 00077 const VariantData* data) { 00078 return data != 0 ? data->asFloat<T>() : T(0); 00079 } 00080 00081 template <typename T> 00082 inline typename enable_if<is_same<T, const char*>::value || 00083 is_same<T, char*>::value, 00084 const char*>::type 00085 variantAs(const VariantData* data) { 00086 return data != 0 ? data->asString() : 0; 00087 } 00088 00089 template <typename T> 00090 T variantAs(VariantData* data, MemoryPool*) { 00091 // By default use the read-only conversion. 00092 // There are specializations for 00093 // - ArrayRef 00094 return variantAs<T>(data); 00095 } 00096 00097 template <typename T> 00098 inline typename enable_if<is_same<ArrayConstRef, T>::value, T>::type variantAs( 00099 const VariantData* data); 00100 00101 template <typename T> 00102 inline typename enable_if<is_same<ObjectConstRef, T>::value, T>::type variantAs( 00103 const VariantData* data); 00104 00105 template <typename T> 00106 inline typename enable_if<is_same<VariantConstRef, T>::value, T>::type 00107 variantAs(const VariantData* data); 00108 00109 template <typename T> 00110 inline typename enable_if<IsWriteableString<T>::value, T>::type variantAs( 00111 const VariantData* data); 00112 00113 } // namespace ARDUINOJSON_NAMESPACE
Generated on Wed Jul 13 2022 01:10:37 by
1.7.2