diff options
| author | David Shah <dave@ds0.me> | 2019-08-01 14:28:21 +0100 | 
|---|---|---|
| committer | David Shah <dave@ds0.me> | 2019-08-05 14:52:15 +0100 | 
| commit | 1839a3a770a71c928b92bf876e04728d2649e425 (patch) | |
| tree | 9de012efabb47f066860918b6360f3966b42f4da /common/util.h | |
| parent | 1ecf271cb32f9f78ea082788c6534f2523144d01 (diff) | |
| download | nextpnr-1839a3a770a71c928b92bf876e04728d2649e425.tar.gz nextpnr-1839a3a770a71c928b92bf876e04728d2649e425.tar.bz2 nextpnr-1839a3a770a71c928b92bf876e04728d2649e425.zip  | |
Major Property improvements for common and iCE40
Signed-off-by: David Shah <dave@ds0.me>
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)  | 
