From 856760599e51bd4c6da34112c993dc8bfb995f36 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Wed, 12 Jun 2019 18:34:34 +0200 Subject: [PATCH] Use properties for settings and save in json --- common/nextpnr.h | 2 +- common/project.cc | 6 ++++-- common/settings.h | 20 ++++++-------------- ecp5/lpf.cc | 2 +- ice40/pcf.cc | 2 +- json/jsonparse.cc | 2 +- json/jsonwrite.cc | 5 ++--- 7 files changed, 16 insertions(+), 23 deletions(-) diff --git a/common/nextpnr.h b/common/nextpnr.h index 3b17920d..66134456 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -541,7 +541,7 @@ struct BaseCtx mutable std::vector *idstring_idx_to_str; // Project settings and config switches - std::unordered_map settings; + std::unordered_map settings; // Placed nets and cells. std::unordered_map> nets; diff --git a/common/project.cc b/common/project.cc index b0ebe961..3b4e67e4 100644 --- a/common/project.cc +++ b/common/project.cc @@ -74,7 +74,7 @@ void ProjectHandler::save(Context *ctx, std::string filename) std::string path = "project.settings."; path += item.first.c_str(ctx); std::replace(path.begin(), path.end(), '/', '.'); - root.put(path, item.second); + root.put(path, item.second.str); } pt::write_json(f, root); } catch (...) { @@ -88,7 +88,9 @@ void addSettings(Context *ctx, std::string path, pt::ptree sub) const std::string &key = v.first; const boost::property_tree::ptree &subtree = v.second; if (subtree.empty()) { - ctx->settings.emplace(ctx->id(path + key), subtree.get_value().c_str()); + Property p; + p.setString(subtree.get_value().c_str()); + ctx->settings.emplace(ctx->id(path + key), p); } else { addSettings(ctx, path + key + "/", subtree); } diff --git a/common/settings.h b/common/settings.h index b57947c9..9d43c774 100644 --- a/common/settings.h +++ b/common/settings.h @@ -34,10 +34,10 @@ class Settings { try { IdString id = ctx->id(name); - auto pair = ctx->settings.emplace(id, std::to_string(defaultValue)); - if (!pair.second) { - return boost::lexical_cast(pair.first->second); - } + if (ctx->settings.find(id) != ctx->settings.end()) + return boost::lexical_cast(ctx->settings.find(id)->second.str); + else + ctx->settings[ctx->id(name)] = std::to_string(defaultValue); } catch (boost::bad_lexical_cast &) { log_error("Problem reading setting %s, using default value\n", name); @@ -53,20 +53,12 @@ class Settings template inline void Settings::set(const char *name, T value) { - IdString id = ctx->id(name); - auto pair = ctx->settings.emplace(id, std::to_string(value)); - if (!pair.second) { - ctx->settings[pair.first->first] = value; - } + ctx->settings[ctx->id(name)] = std::to_string(value); } template <> inline void Settings::set(const char *name, std::string value) { - IdString id = ctx->id(name); - auto pair = ctx->settings.emplace(id, value); - if (!pair.second) { - ctx->settings[pair.first->first] = value; - } + ctx->settings[ctx->id(name)] = value; } NEXTPNR_NAMESPACE_END diff --git a/ecp5/lpf.cc b/ecp5/lpf.cc index 4ac70fc9..ceb1d7ae 100644 --- a/ecp5/lpf.cc +++ b/ecp5/lpf.cc @@ -133,7 +133,7 @@ bool Arch::applyLPF(std::string filename, std::istream &in) } if (!isempty(linebuf)) log_error("unexpected end of LPF file\n"); - settings.emplace(id("input/lpf"), filename); + settings[id("input/lpf")] = filename; return true; } catch (log_execution_error_exception) { return false; diff --git a/ice40/pcf.cc b/ice40/pcf.cc index a8273dd6..91935bee 100644 --- a/ice40/pcf.cc +++ b/ice40/pcf.cc @@ -116,7 +116,7 @@ bool apply_pcf(Context *ctx, std::string filename, std::istream &in) } } } - ctx->settings.emplace(ctx->id("input/pcf"), filename); + ctx->settings[ctx->id("input/pcf")] = filename; return true; } catch (log_execution_error_exception) { return false; diff --git a/json/jsonparse.cc b/json/jsonparse.cc index a36f891d..f0d0069a 100644 --- a/json/jsonparse.cc +++ b/json/jsonparse.cc @@ -907,7 +907,7 @@ bool parse_json_file(std::istream &f, std::string &filename, Context *ctx) log_info("Checksum: 0x%08x\n", ctx->checksum()); log_break(); - ctx->settings.emplace(ctx->id("input/json"), filename); + ctx->settings[ctx->id("input/json")] = filename; ctx->attributesToArchInfo(); return true; } catch (log_execution_error_exception) { diff --git a/json/jsonwrite.cc b/json/jsonwrite.cc index 19c43d7c..ac54bc4e 100644 --- a/json/jsonwrite.cc +++ b/json/jsonwrite.cc @@ -69,10 +69,9 @@ void write_module(std::ostream &f, Context *ctx) f << stringf(" %s: {\n", get_string(val->second.str).c_str()); else f << stringf(" %s: {\n", get_string("top").c_str()); - // TODO: check if this is better to be separate - /*f << stringf(" \"settings\": {"); + f << stringf(" \"settings\": {"); write_parameters(f, ctx, ctx->settings, true); - f << stringf("\n },\n");*/ + f << stringf("\n },\n"); f << stringf(" \"attributes\": {"); write_parameters(f, ctx, ctx->attrs, true); f << stringf("\n },\n");