From 1839a3a770a71c928b92bf876e04728d2649e425 Mon Sep 17 00:00:00 2001 From: David Shah Date: Thu, 1 Aug 2019 14:28:21 +0100 Subject: Major Property improvements for common and iCE40 Signed-off-by: David Shah --- common/util.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'common/util.h') diff --git a/common/util.h b/common/util.h index 8f361dc8..81d7e47d 100644 --- a/common/util.h +++ b/common/util.h @@ -51,6 +51,16 @@ std::string str_or_default(const Container &ct, const KeyType &key, std::string return found->second; }; +template +std::string str_or_default(const std::unordered_map &ct, const KeyType &key, std::string def = "") +{ + auto found = ct.find(key); + if (found == ct.end()) + return def; + else + return found->second.as_string(); +}; + // Get a value from a map-style container, converting to int, and returning // default if value is not found template int int_or_default(const Container &ct, const KeyType &key, int def = 0) @@ -62,6 +72,20 @@ template int int_or_default(const Contain return std::stoi(found->second); }; +template +int int_or_default(const std::unordered_map &ct, const KeyType &key, int def = 0) +{ + auto found = ct.find(key); + if (found == ct.end()) + return def; + else { + if (found->second.is_string) + return std::stoi(found->second.as_string()); + else + return found->second.as_int64(); + } +}; + // As above, but convert to bool template bool bool_or_default(const Container &ct, const KeyType &key, bool def = false) -- cgit v1.2.3