diff options
author | David Shah <dave@ds0.me> | 2019-08-07 10:40:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-07 10:40:25 +0100 |
commit | eaef3b600e2e8c4633db25edd4ad017b59a1cc79 (patch) | |
tree | 66a6e88614d99dfdf8c15ac3a1d1a13339784f9c /common/util.h | |
parent | 1ecf271cb32f9f78ea082788c6534f2523144d01 (diff) | |
parent | 8eef6ac55e48aa181d1e268fcec004a58f9d8db4 (diff) | |
download | nextpnr-eaef3b600e2e8c4633db25edd4ad017b59a1cc79.tar.gz nextpnr-eaef3b600e2e8c4633db25edd4ad017b59a1cc79.tar.bz2 nextpnr-eaef3b600e2e8c4633db25edd4ad017b59a1cc79.zip |
Merge pull request #306 from YosysHQ/dave/jsonfix
Major improvements to constants/Property
Diffstat (limited to 'common/util.h')
-rw-r--r-- | common/util.h | 24 |
1 files changed, 24 insertions, 0 deletions
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 <typename KeyType> +std::string str_or_default(const std::unordered_map<KeyType, Property> &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 <typename Container, typename KeyType> int int_or_default(const Container &ct, const KeyType &key, int def = 0) @@ -62,6 +72,20 @@ template <typename Container, typename KeyType> int int_or_default(const Contain return std::stoi(found->second); }; +template <typename KeyType> +int int_or_default(const std::unordered_map<KeyType, Property> &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 <typename Container, typename KeyType> bool bool_or_default(const Container &ct, const KeyType &key, bool def = false) |