#ifndef FLUTTER_INAPPWEBVIEW_PLUGIN_UTIL_FLUTTER_H_ #define FLUTTER_INAPPWEBVIEW_PLUGIN_UTIL_FLUTTER_H_ #include #include "map.h" #include "util.h" #include "vector.h" namespace flutter_inappwebview_plugin { static inline flutter::EncodableValue make_fl_value() { return flutter::EncodableValue(); } template static inline flutter::EncodableValue make_fl_value(const T& val) { return flutter::EncodableValue(val); } template static inline flutter::EncodableValue make_fl_value(const T* val) { return val == nullptr ? make_fl_value() : flutter::EncodableValue(val); } template static inline flutter::EncodableValue make_fl_value(const std::vector& vec) { auto encodableList = flutter::EncodableList{}; for (auto const& val : vec) { encodableList.push_back(make_fl_value(val)); } return encodableList; } template static inline flutter::EncodableValue make_fl_value(const std::map& map) { auto encodableMap = flutter::EncodableMap{}; for (auto const& [key, val] : map) { encodableMap.insert({ make_fl_value(key), make_fl_value(val) }); } return encodableMap; } template static inline flutter::EncodableValue make_fl_value(const std::optional& optional) { return optional.has_value() ? make_fl_value(optional.value()) : make_fl_value(); } template static inline flutter::EncodableValue make_fl_value(const std::optional>& optional) { if (!optional.has_value()) { return make_fl_value(); } auto& vecValue = optional.value(); auto encodableList = flutter::EncodableList{}; for (auto const& val : vecValue) { encodableList.push_back(make_fl_value(val)); } return encodableList; } template static inline flutter::EncodableValue make_fl_value(const std::optional>& optional) { if (!optional.has_value()) { return make_fl_value(); } auto& mapValue = optional.value(); auto encodableMap = flutter::EncodableMap{}; for (auto const& [key, val] : mapValue) { encodableMap.insert({ make_fl_value(key), make_fl_value(val) }); } return encodableMap; } static inline bool fl_map_contains(const flutter::EncodableMap& map, const char* key) { return map_contains(map, make_fl_value(key)); } static inline bool fl_map_contains_not_null(const flutter::EncodableMap& map, const char* key) { return fl_map_contains(map, key) && !map.at(make_fl_value(key)).IsNull(); } template::value && !std::is_same::value), bool>::type* = nullptr> static inline T get_fl_map_value(const flutter::EncodableMap& map, const char* key) { return std::get(map.at(make_fl_value(key))); } template::value, bool>::type* = nullptr> static inline int64_t get_fl_map_value(const flutter::EncodableMap& map, const char* key) { return map.at(make_fl_value(key)).LongValue(); } template::value, bool>::type* = nullptr> static inline int64_t get_fl_map_value(const flutter::EncodableMap& map, const char* key) { return map.at(make_fl_value(key)).LongValue(); } template::value && !is_vector::value && !std::is_same::value && !std::is_same::value) || std::is_same::value || std::is_same::value), int>::type* = nullptr> static inline std::optional get_optional_fl_map_value(const flutter::EncodableMap& map, const char* key) { if (fl_map_contains_not_null(map, key)) { auto fl_key = make_fl_value(key); return make_pointer_optional(std::get_if(&map.at(fl_key))); } return std::nullopt; } template::value, bool>::type* = nullptr> static inline std::optional get_optional_fl_map_value(const flutter::EncodableMap& map, const char* key) { if (fl_map_contains_not_null(map, key)) { auto fl_key = make_fl_value(key); return std::make_optional(map.at(fl_key).LongValue()); } return std::nullopt; } template::value, bool>::type* = nullptr> static inline std::optional get_optional_fl_map_value(const flutter::EncodableMap& map, const char* key) { if (fl_map_contains_not_null(map, key)) { auto fl_key = make_fl_value(key); return std::make_optional(map.at(fl_key).LongValue()); } return std::nullopt; } template static inline T get_fl_map_value(const flutter::EncodableMap& map, const char* key, const T& defaultValue) { auto optional = get_optional_fl_map_value(map, key); return !optional.has_value() ? defaultValue : optional.value(); } template::value && !std::is_same::value)>::type* = nullptr> static inline std::optional get_optional_fl_map_value(const flutter::EncodableMap& map, const char* key) { using K = typename T::key_type; using V = typename T::mapped_type; auto flMap = std::get_if(&map.at(make_fl_value(key))); if (flMap) { T mapValue = {}; for (auto itr = flMap->begin(); itr != flMap->end(); itr++) { mapValue.insert({ std::get(itr->first), std::get(itr->second) }); } return make_pointer_optional(&mapValue); } return std::nullopt; } template static inline std::map get_fl_map_value(const flutter::EncodableMap& map, const char* key, const std::map& defaultValue) { auto optional = get_optional_fl_map_value>(map, key); return !optional.has_value() ? defaultValue : optional.value(); } template::value && !std::is_same::value), bool>::type* = nullptr> static inline std::optional get_optional_fl_map_value(const flutter::EncodableMap& map, const char* key) { using V = typename T::value_type; auto flList = std::get_if(&map.at(make_fl_value(key))); if (flList) { T vecValue(flList->size()); for (auto itr = flList->begin(); itr != flList->end(); itr++) { vecValue.push_back(std::get(*itr)); } return make_pointer_optional(&vecValue); } return std::nullopt; } template static inline std::vector get_fl_map_value(const flutter::EncodableMap& map, const char* key, const std::vector& defaultValue) { auto optional = get_optional_fl_map_value>(map, key); return !optional.has_value() ? defaultValue : optional.value(); } } #endif //FLUTTER_INAPPWEBVIEW_PLUGIN_UTIL_FLUTTER_H_