Use properties for settings and save in json

This commit is contained in:
Miodrag Milanovic 2019-06-12 18:34:34 +02:00
parent d9b0bac248
commit 856760599e
7 changed files with 16 additions and 23 deletions

View File

@ -541,7 +541,7 @@ struct BaseCtx
mutable std::vector<const std::string *> *idstring_idx_to_str; mutable std::vector<const std::string *> *idstring_idx_to_str;
// Project settings and config switches // Project settings and config switches
std::unordered_map<IdString, std::string> settings; std::unordered_map<IdString, Property> settings;
// Placed nets and cells. // Placed nets and cells.
std::unordered_map<IdString, std::unique_ptr<NetInfo>> nets; std::unordered_map<IdString, std::unique_ptr<NetInfo>> nets;

View File

@ -74,7 +74,7 @@ void ProjectHandler::save(Context *ctx, std::string filename)
std::string path = "project.settings."; std::string path = "project.settings.";
path += item.first.c_str(ctx); path += item.first.c_str(ctx);
std::replace(path.begin(), path.end(), '/', '.'); std::replace(path.begin(), path.end(), '/', '.');
root.put(path, item.second); root.put(path, item.second.str);
} }
pt::write_json(f, root); pt::write_json(f, root);
} catch (...) { } catch (...) {
@ -88,7 +88,9 @@ void addSettings(Context *ctx, std::string path, pt::ptree sub)
const std::string &key = v.first; const std::string &key = v.first;
const boost::property_tree::ptree &subtree = v.second; const boost::property_tree::ptree &subtree = v.second;
if (subtree.empty()) { if (subtree.empty()) {
ctx->settings.emplace(ctx->id(path + key), subtree.get_value<std::string>().c_str()); Property p;
p.setString(subtree.get_value<std::string>().c_str());
ctx->settings.emplace(ctx->id(path + key), p);
} else { } else {
addSettings(ctx, path + key + "/", subtree); addSettings(ctx, path + key + "/", subtree);
} }

View File

@ -34,10 +34,10 @@ class Settings
{ {
try { try {
IdString id = ctx->id(name); IdString id = ctx->id(name);
auto pair = ctx->settings.emplace(id, std::to_string(defaultValue)); if (ctx->settings.find(id) != ctx->settings.end())
if (!pair.second) { return boost::lexical_cast<T>(ctx->settings.find(id)->second.str);
return boost::lexical_cast<T>(pair.first->second); else
} ctx->settings[ctx->id(name)] = std::to_string(defaultValue);
} catch (boost::bad_lexical_cast &) { } catch (boost::bad_lexical_cast &) {
log_error("Problem reading setting %s, using default value\n", name); log_error("Problem reading setting %s, using default value\n", name);
@ -53,20 +53,12 @@ class Settings
template <typename T> inline void Settings::set(const char *name, T value) template <typename T> inline void Settings::set(const char *name, T value)
{ {
IdString id = ctx->id(name); ctx->settings[ctx->id(name)] = std::to_string(value);
auto pair = ctx->settings.emplace(id, std::to_string(value));
if (!pair.second) {
ctx->settings[pair.first->first] = value;
}
} }
template <> inline void Settings::set<std::string>(const char *name, std::string value) template <> inline void Settings::set<std::string>(const char *name, std::string value)
{ {
IdString id = ctx->id(name); ctx->settings[ctx->id(name)] = value;
auto pair = ctx->settings.emplace(id, value);
if (!pair.second) {
ctx->settings[pair.first->first] = value;
}
} }
NEXTPNR_NAMESPACE_END NEXTPNR_NAMESPACE_END

View File

@ -133,7 +133,7 @@ bool Arch::applyLPF(std::string filename, std::istream &in)
} }
if (!isempty(linebuf)) if (!isempty(linebuf))
log_error("unexpected end of LPF file\n"); log_error("unexpected end of LPF file\n");
settings.emplace(id("input/lpf"), filename); settings[id("input/lpf")] = filename;
return true; return true;
} catch (log_execution_error_exception) { } catch (log_execution_error_exception) {
return false; return false;

View File

@ -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; return true;
} catch (log_execution_error_exception) { } catch (log_execution_error_exception) {
return false; return false;

View File

@ -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_info("Checksum: 0x%08x\n", ctx->checksum());
log_break(); log_break();
ctx->settings.emplace(ctx->id("input/json"), filename); ctx->settings[ctx->id("input/json")] = filename;
ctx->attributesToArchInfo(); ctx->attributesToArchInfo();
return true; return true;
} catch (log_execution_error_exception) { } catch (log_execution_error_exception) {

View File

@ -69,10 +69,9 @@ void write_module(std::ostream &f, Context *ctx)
f << stringf(" %s: {\n", get_string(val->second.str).c_str()); f << stringf(" %s: {\n", get_string(val->second.str).c_str());
else else
f << stringf(" %s: {\n", get_string("top").c_str()); 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); write_parameters(f, ctx, ctx->settings, true);
f << stringf("\n },\n");*/ f << stringf("\n },\n");
f << stringf(" \"attributes\": {"); f << stringf(" \"attributes\": {");
write_parameters(f, ctx, ctx->attrs, true); write_parameters(f, ctx, ctx->attrs, true);
f << stringf("\n },\n"); f << stringf("\n },\n");