From 93a0d2456086b2c0e4a50d6ecc43ab028895bcad Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Thu, 9 Aug 2018 18:39:10 +0200 Subject: Use settings for placer1 and router1 --- common/settings.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 common/settings.h (limited to 'common/settings.h') diff --git a/common/settings.h b/common/settings.h new file mode 100644 index 00000000..c7ef2016 --- /dev/null +++ b/common/settings.h @@ -0,0 +1,57 @@ +/* + * nextpnr -- Next Generation Place and Route + * + * Copyright (C) 2018 Miodrag Milanovic + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ +#ifndef SETTINGS_H +#define SETTINGS_H + +#include +#include "nextpnr.h" + +NEXTPNR_NAMESPACE_BEGIN + +class Settings +{ + public: + explicit Settings(Context *ctx) : ctx(ctx) {} + + template T get(const char *name, T defaultValue) + { + IdString id = ctx->id(name); + if (ctx->settings.find(id) != ctx->settings.end()) + return boost::lexical_cast(ctx->settings[id]); + ctx->settings.emplace(id, std::to_string(defaultValue)); + return defaultValue; + } + + template void set(const char *name, T value) + { + IdString id = ctx->id(name); + if (ctx->settings.find(id) != ctx->settings.end()) { + ctx->settings[id] = value; + return; + } + ctx->settings.emplace(id, std::to_string(value)); + } + + private: + Context *ctx; +}; + +NEXTPNR_NAMESPACE_END + +#endif // SETTINGS_H -- cgit v1.2.3 From b400cd8d7326ac798c9da76de3c2a11f2d96b6a7 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Sat, 11 Aug 2018 13:04:51 +0200 Subject: Read settings and check validity --- common/settings.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'common/settings.h') diff --git a/common/settings.h b/common/settings.h index c7ef2016..8ced6560 100644 --- a/common/settings.h +++ b/common/settings.h @@ -20,6 +20,7 @@ #define SETTINGS_H #include +#include "log.h" #include "nextpnr.h" NEXTPNR_NAMESPACE_BEGIN @@ -31,10 +32,15 @@ class Settings template T get(const char *name, T defaultValue) { - IdString id = ctx->id(name); - if (ctx->settings.find(id) != ctx->settings.end()) - return boost::lexical_cast(ctx->settings[id]); - ctx->settings.emplace(id, std::to_string(defaultValue)); + try { + IdString id = ctx->id(name); + if (ctx->settings.find(id) != ctx->settings.end()) { + return boost::lexical_cast(ctx->settings[id]); + } + ctx->settings.emplace(id, std::to_string(defaultValue)); + } catch (boost::bad_lexical_cast &) { + log_error("Problem reading setting %s, using default value\n", name); + } return defaultValue; } -- cgit v1.2.3 From eaf824ca73241014a280b1bd787d8f83e772dd05 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Sun, 12 Aug 2018 10:02:32 +0200 Subject: Use emplace result for get,set of settings --- common/settings.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'common/settings.h') diff --git a/common/settings.h b/common/settings.h index 8ced6560..e1f1166a 100644 --- a/common/settings.h +++ b/common/settings.h @@ -34,10 +34,11 @@ class Settings { try { IdString id = ctx->id(name); - if (ctx->settings.find(id) != ctx->settings.end()) { - return boost::lexical_cast(ctx->settings[id]); + auto pair = ctx->settings.emplace(id, std::to_string(defaultValue)); + if (!pair.second) { + return boost::lexical_cast(pair.first->second); } - ctx->settings.emplace(id, std::to_string(defaultValue)); + } catch (boost::bad_lexical_cast &) { log_error("Problem reading setting %s, using default value\n", name); } @@ -47,11 +48,10 @@ class Settings template void set(const char *name, T value) { IdString id = ctx->id(name); - if (ctx->settings.find(id) != ctx->settings.end()) { - ctx->settings[id] = value; - return; - } - ctx->settings.emplace(id, std::to_string(value)); + auto pair = ctx->settings.emplace(id, std::to_string(value)); + if (!pair.second) { + ctx->settings[pair.first->first] = value; + } } private: -- cgit v1.2.3