Make option names not dumb and wrong

Originally committed to SVN as r5731.
This commit is contained in:
Thomas Goyne 2011-10-11 00:06:34 +00:00
parent a3d9deddb9
commit 91ab2ee9ba
2 changed files with 9 additions and 28 deletions

View file

@ -40,15 +40,14 @@
namespace agi { namespace agi {
Options::Options(const std::string &file, const std::string& default_config, const OptionSetting setting): Options::Options(const std::string &file, const std::string& default_config, const OptionSetting setting)
config_file(file), config_default(default_config), config_loaded(false), setting(setting) { : config_file(file), config_default(default_config), config_loaded(false), setting(setting) {
LOG_D("agi/options") << "New Options object"; LOG_D("agi/options") << "New Options object";
std::istringstream stream(default_config); std::istringstream stream(default_config);
LoadConfig(stream); LoadConfig(stream);
} }
Options::~Options() { Options::~Options() {
if ((setting & FLUSH_SKIP) != FLUSH_SKIP) { if ((setting & FLUSH_SKIP) != FLUSH_SKIP) {
Flush(); Flush();
} }
@ -114,8 +113,6 @@ void Options::Flush() {
json::Object obj_out; json::Object obj_out;
for (OptionValueMap::const_iterator i = values.begin(); i != values.end(); ++i) { for (OptionValueMap::const_iterator i = values.begin(); i != values.end(); ++i) {
std::string key = i->first.substr(i->first.rfind("/")+1, i->first.size());
switch (i->second->GetType()) { switch (i->second->GetType()) {
case OptionValue::Type_String: case OptionValue::Type_String:
PutOption(obj_out, i->first, (json::String)i->second->GetString()); PutOption(obj_out, i->first, (json::String)i->second->GetString());

View file

@ -32,13 +32,16 @@ namespace agi {
ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name) ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name)
: values(val) : values(val)
, name(member_name + "/") , name(member_name)
{ {
} }
void ConfigVisitor::Visit(const json::Object& object) { void ConfigVisitor::Visit(const json::Object& object) {
json::Object::const_iterator index(object.Begin()), index_end(object.End()); json::Object::const_iterator index(object.Begin()), index_end(object.End());
if (!name.empty())
name += "/";
for (; index != index_end; ++index) { for (; index != index_end; ++index) {
const json::Object::Member& member = *index; const json::Object::Member& member = *index;
const std::string &member_name = member.name; const std::string &member_name = member.name;
@ -50,16 +53,12 @@ void ConfigVisitor::Visit(const json::Object& object) {
} }
} }
void ConfigVisitor::Visit(const json::Array& array) { void ConfigVisitor::Visit(const json::Array& array) {
bool init = false;
OptionValueList *array_list = NULL; OptionValueList *array_list = NULL;
json::Array::const_iterator index(array.Begin()), indexEnd(array.End()); json::Array::const_iterator index(array.Begin()), indexEnd(array.End());
for (; index != indexEnd; ++index) { for (; index != indexEnd; ++index) {
const json::Object& index_array = *index; const json::Object& index_array = *index;
json::Object::const_iterator index_object(index_array.Begin()), index_objectEnd(index_array.End()); json::Object::const_iterator index_object(index_array.Begin()), index_objectEnd(index_array.End());
@ -68,10 +67,9 @@ void ConfigVisitor::Visit(const json::Array& array) {
const json::Object::Member& member = *index_object; const json::Object::Member& member = *index_object;
const std::string& member_name = member.name; const std::string& member_name = member.name;
// This can only happen once since a list must always be of the same // This can only happen once since a list must always be of the same
// type, if we try inserting another type into it we want it to fail. // type, if we try inserting another type into it we want it to fail.
if (!init) { if (!array_list) {
if (member_name == "string") { if (member_name == "string") {
array_list = new OptionValueListString(name); array_list = new OptionValueListString(name);
} else if (member_name == "int") { } else if (member_name == "int") {
@ -85,7 +83,6 @@ void ConfigVisitor::Visit(const json::Array& array) {
} else { } else {
throw OptionJsonValueArray("Array type not handled"); throw OptionJsonValueArray("Array type not handled");
} }
init = true;
} }
try { try {
@ -114,13 +111,10 @@ void ConfigVisitor::Visit(const json::Array& array) {
delete array_list; delete array_list;
throw OptionJsonValueArray("Attempt to insert value into array of wrong type"); throw OptionJsonValueArray("Attempt to insert value into array of wrong type");
} }
} // for index_object } // for index_object
} // for index } // for index
if (array_list) AddOptionValue(array_list); if (array_list) AddOptionValue(array_list);
} }
@ -134,10 +128,8 @@ void ConfigVisitor::Visit(const json::Number& number) {
OptionValue *opt = new OptionValueDouble(name, val); OptionValue *opt = new OptionValueDouble(name, val);
AddOptionValue(opt); AddOptionValue(opt);
} }
} }
void ConfigVisitor::Visit(const json::String& string) { void ConfigVisitor::Visit(const json::String& string) {
OptionValue *opt; OptionValue *opt;
if (string.Value().find("rgb(") == 0) { if (string.Value().find("rgb(") == 0) {
@ -148,29 +140,24 @@ void ConfigVisitor::Visit(const json::String& string) {
AddOptionValue(opt); AddOptionValue(opt);
} }
void ConfigVisitor::Visit(const json::Boolean& boolean) { void ConfigVisitor::Visit(const json::Boolean& boolean) {
OptionValue *opt = new OptionValueBool(name, boolean.Value()); OptionValue *opt = new OptionValueBool(name, boolean.Value());
AddOptionValue(opt); AddOptionValue(opt);
} }
void ConfigVisitor::Visit(const json::Null& null) { void ConfigVisitor::Visit(const json::Null& null) {
throw OptionJsonValueNull("Attempt to read null value"); throw OptionJsonValueNull("Attempt to read null value");
} }
void ConfigVisitor::AddOptionValue(OptionValue* opt) { void ConfigVisitor::AddOptionValue(OptionValue* opt) {
// Corresponding code is in the constuctor.
std::string stripped = name.substr(1, name.rfind("/")-1);
OptionValue *opt_cur; OptionValue *opt_cur;
OptionValueMap::iterator index; OptionValueMap::iterator index;
if ((index = values.find(stripped)) != values.end()) { if ((index = values.find(name)) != values.end()) {
opt_cur = index->second; opt_cur = index->second;
} else { } else {
values.insert(OptionValuePair(stripped, opt)); values.insert(OptionValuePair(name, opt));
return; return;
} }
@ -228,7 +215,6 @@ void ConfigVisitor::AddOptionValue(OptionValue* opt) {
break; break;
} }
case OptionValue::Type_List_Bool: { case OptionValue::Type_List_Bool: {
std::vector<bool> array; std::vector<bool> array;
opt->GetListBool(array); opt->GetListBool(array);
@ -236,7 +222,5 @@ void ConfigVisitor::AddOptionValue(OptionValue* opt) {
break; break;
} }
} }
} }
} // namespace agi } // namespace agi