Eliminate a bunch of now unneccesary explicit casts to/from json types

Originally committed to SVN as r5751.
This commit is contained in:
Thomas Goyne 2011-10-17 22:00:28 +00:00
parent b4fa4e6f0c
commit 4e8848c110
5 changed files with 46 additions and 84 deletions

View File

@ -76,8 +76,7 @@ Hotkey::Hotkey(const std::string &file, const std::string &default_config)
json::Object object = hotkey_root; json::Object object = hotkey_root;
for (json::Object::const_iterator index(object.begin()); index != object.end(); index++) { for (json::Object::const_iterator index(object.begin()); index != object.end(); index++) {
const json::Object& obj = index->second; BuildHotkey(index->first, index->second);
BuildHotkey(index->first, obj);
} }
} }
@ -89,18 +88,15 @@ void Hotkey::BuildHotkey(std::string const& context, const json::Object& object)
for (json::Array::const_iterator arr_index(array.begin()); arr_index != array.end(); arr_index++) { for (json::Array::const_iterator arr_index(array.begin()); arr_index != array.end(); arr_index++) {
Combo combo(context, index->first); Combo combo(context, index->first);
const json::Object& obj = *arr_index; const json::Array& arr_mod = (*arr_index)["modifiers"];
const json::Array& arr_mod = obj["modifiers"];
for (json::Array::const_iterator arr_mod_index(arr_mod.begin()); arr_mod_index != arr_mod.end(); arr_mod_index++) { for (json::Array::const_iterator arr_mod_index(arr_mod.begin()); arr_mod_index != arr_mod.end(); arr_mod_index++) {
const json::String& key_mod = *arr_mod_index; combo.KeyInsert(*arr_mod_index);
combo.KeyInsert(key_mod);
} }
combo.KeyInsert(static_cast<const json::String&>(obj["key"]));
combo.KeyInsert((*arr_index)["key"]);
ComboInsert(combo); ComboInsert(combo);
} // for arr_index }
} // for index }
} }
bool Hotkey::Scan(const std::string &context, const std::string &str, bool always, std::string &cmd) const { bool Hotkey::Scan(const std::string &context, const std::string &str, bool always, std::string &cmd) const {
@ -179,11 +175,9 @@ void Hotkey::Flush() {
json::Object hotkey; json::Object hotkey;
hotkey["modifiers"] = modifiers; hotkey["modifiers"] = modifiers;
hotkey["key"] = json::String(combo_map.back()); hotkey["key"] = combo_map.back();
json::Object& context_obj = root[index->second.Context()];
json::Array& combo_array = context_obj[index->second.CmdName()];
json::Array& combo_array = root[index->second.Context()][index->second.CmdName()];
combo_array.push_back(hotkey); combo_array.push_back(hotkey);
} }

View File

@ -102,7 +102,6 @@ LogSink::~LogSink() {
timeval_close.push_back(time_close.tv_usec); timeval_close.push_back(time_close.tv_usec);
root["timeval"]["close"] = timeval_close; root["timeval"]["close"] = timeval_close;
root["log"] = array; root["log"] = array;
json::Writer::Write(root, file.Get()); json::Writer::Write(root, file.Get());

View File

@ -36,10 +36,8 @@ MRUManager::MRUManager(const std::string &config, const std::string &default_con
json::Object::const_iterator index_object(root_new.begin()), index_objectEnd(root_new.end()); json::Object::const_iterator index_object(root_new.begin()), index_objectEnd(root_new.end());
for (; index_object != index_objectEnd; ++index_object) { for (; index_object != index_objectEnd; ++index_object)
const std::string &member_name = index_object->first; Load(index_object->first, index_object->second);
Load(member_name, (json::Array)index_object->second);
}
} }
@ -92,11 +90,7 @@ void MRUManager::Flush() {
for (MRUMap::const_iterator i = mru.begin(); i != mru.end(); ++i) { for (MRUMap::const_iterator i = mru.begin(); i != mru.end(); ++i) {
json::Array &array = out[i->first]; json::Array &array = out[i->first];
const MRUListMap &map_list = i->second; copy(i->second.begin(), i->second.end(), std::back_inserter(array));
for (MRUListMap::const_iterator i_lst = map_list.begin(); i_lst != map_list.end(); ++i_lst) {
array.push_back(json::String(*i_lst));
}
} }
json::Writer::Write(out, io::Save(config_name).Get()); json::Writer::Write(out, io::Save(config_name).Get());
@ -109,16 +103,12 @@ inline void MRUManager::Prune(MRUListMap& map) {
map.resize(std::min<size_t>(16, map.size())); map.resize(std::min<size_t>(16, map.size()));
} }
static json::String cast_str(json::UnknownElement const& e) {
return static_cast<json::String>(e);
}
/// @brief Load MRU Lists. /// @brief Load MRU Lists.
/// @param key List name. /// @param key List name.
/// @param array json::Array of values. /// @param array json::Array of values.
void MRUManager::Load(const std::string &key, const json::Array& array) { void MRUManager::Load(const std::string &key, const json::Array& array) {
try { try {
transform(array.begin(), array.end(), back_inserter(mru[key]), cast_str); copy(array.begin(), array.end(), back_inserter(mru[key]));
} }
catch (json::Exception const&) { catch (json::Exception const&) {
// Out of date MRU file; just discard the data and skip it // Out of date MRU file; just discard the data and skip it

View File

@ -75,7 +75,6 @@ void Options::ConfigUser() {
config_loaded = true; config_loaded = true;
} }
void Options::LoadConfig(std::istream& stream) { void Options::LoadConfig(std::istream& stream) {
/// @todo Store all previously loaded configs in an array for bug report purposes, /// @todo Store all previously loaded configs in an array for bug report purposes,
/// this is just a temp stub. /// this is just a temp stub.
@ -94,9 +93,6 @@ void Options::LoadConfig(std::istream& stream) {
config_root.Accept(config_visitor); config_root.Accept(config_visitor);
} }
OptionValue* Options::Get(const std::string &name) { OptionValue* Options::Get(const std::string &name) {
OptionValueMap::iterator index; OptionValueMap::iterator index;
@ -107,31 +103,29 @@ OptionValue* Options::Get(const std::string &name) {
throw OptionErrorNotFound("Option value not found: " + name); throw OptionErrorNotFound("Option value not found: " + name);
} }
void Options::Flush() { 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) {
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, i->second->GetString());
break; break;
case OptionValue::Type_Int: case OptionValue::Type_Int:
PutOption(obj_out, i->first, (json::Number)(const double)i->second->GetInt()); PutOption(obj_out, i->first, (double)i->second->GetInt());
break; break;
case OptionValue::Type_Double: case OptionValue::Type_Double:
PutOption(obj_out, i->first, (json::Number)i->second->GetDouble()); PutOption(obj_out, i->first, i->second->GetDouble());
break; break;
case OptionValue::Type_Colour: case OptionValue::Type_Colour:
PutOption(obj_out, i->first, (json::String)i->second->GetColour()); PutOption(obj_out, i->first, i->second->GetColour());
break; break;
case OptionValue::Type_Bool: case OptionValue::Type_Bool:
PutOption(obj_out, i->first, (json::Boolean)i->second->GetBool()); PutOption(obj_out, i->first, i->second->GetBool());
break; break;
case OptionValue::Type_List_String: { case OptionValue::Type_List_String: {
@ -142,11 +136,11 @@ void Options::Flush() {
for (std::vector<std::string>::const_iterator i_str = array_string.begin(); i_str != array_string.end(); ++i_str) { for (std::vector<std::string>::const_iterator i_str = array_string.begin(); i_str != array_string.end(); ++i_str) {
json::Object obj; json::Object obj;
obj["string"] = json::String(*i_str); obj["string"] = *i_str;
array.push_back(obj); array.push_back(obj);
} }
PutOption(obj_out, i->first, (json::Array)array); PutOption(obj_out, i->first, array);
} }
break; break;
@ -158,10 +152,10 @@ void Options::Flush() {
for (std::vector<int64_t>::const_iterator i_int = array_int.begin(); i_int != array_int.end(); ++i_int) { for (std::vector<int64_t>::const_iterator i_int = array_int.begin(); i_int != array_int.end(); ++i_int) {
json::Object obj; json::Object obj;
obj["int"] = json::Number((const double)*i_int); obj["int"] = (double)*i_int;
array.push_back(obj); array.push_back(obj);
} }
PutOption(obj_out, i->first, (json::Array)array); PutOption(obj_out, i->first, array);
} }
break; break;
@ -173,10 +167,10 @@ void Options::Flush() {
for (std::vector<double>::const_iterator i_double = array_double.begin(); i_double != array_double.end(); ++i_double) { for (std::vector<double>::const_iterator i_double = array_double.begin(); i_double != array_double.end(); ++i_double) {
json::Object obj; json::Object obj;
obj["double"] = json::Number(*i_double); obj["double"] = *i_double;
array.push_back(obj); array.push_back(obj);
} }
PutOption(obj_out, i->first, (json::Array)array); PutOption(obj_out, i->first, array);
} }
break; break;
@ -187,15 +181,10 @@ void Options::Flush() {
json::Array array; json::Array array;
for (std::vector<Colour>::const_iterator i_colour = array_colour.begin(); i_colour != array_colour.end(); ++i_colour) { for (std::vector<Colour>::const_iterator i_colour = array_colour.begin(); i_colour != array_colour.end(); ++i_colour) {
json::Object obj; json::Object obj;
obj["colour"] = *i_colour;
Colour col = *i_colour;
std::string str = std::string(col);
obj["colour"] = json::String(str);
array.push_back(obj); array.push_back(obj);
} }
PutOption(obj_out, i->first, (json::Array)array); PutOption(obj_out, i->first, array);
} }
break; break;
@ -207,10 +196,10 @@ void Options::Flush() {
i->second->GetListBool(array_bool); i->second->GetListBool(array_bool);
for (std::vector<bool>::const_iterator i_bool = array_bool.begin(); i_bool != array_bool.end(); ++i_bool) { for (std::vector<bool>::const_iterator i_bool = array_bool.begin(); i_bool != array_bool.end(); ++i_bool) {
json::Object obj; json::Object obj;
obj["bool"] = json::Boolean(*i_bool); obj["bool"] = *i_bool;
array.push_back(obj); array.push_back(obj);
} }
PutOption(obj_out, i->first, (json::Array)array); PutOption(obj_out, i->first, array);
} }
break; break;
} }

View File

@ -43,12 +43,8 @@ void ConfigVisitor::Visit(const json::Object& object) {
name += "/"; name += "/";
for (; index != index_end; ++index) { for (; index != index_end; ++index) {
const std::string &member_name = index->first; ConfigVisitor config_visitor(values, name + index->first);
const json::UnknownElement& element = index->second; index->second.Accept(config_visitor);
ConfigVisitor config_visitor(values, name + member_name);
element.Accept(config_visitor);
} }
} }
@ -68,32 +64,31 @@ void ConfigVisitor::Visit(const json::Array& array) {
// 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 (!array_list) { 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")
array_list = new OptionValueListInt(name); array_list = new OptionValueListInt(name);
} else if (member_name == "double") { else if (member_name == "double")
array_list = new OptionValueListDouble(name); array_list = new OptionValueListDouble(name);
} else if (member_name == "bool") { else if (member_name == "bool")
array_list = new OptionValueListBool(name); array_list = new OptionValueListBool(name);
} else if (member_name == "colour") { else if (member_name == "colour")
array_list = new OptionValueListColour(name); array_list = new OptionValueListColour(name);
} else { else
throw OptionJsonValueArray("Array type not handled"); throw OptionJsonValueArray("Array type not handled");
}
} }
try { try {
if (member_name == "string") if (member_name == "string")
array_list->InsertString((json::String)it->second); array_list->InsertString(it->second);
else if (member_name == "int") else if (member_name == "int")
array_list->InsertInt((int64_t)(json::Number)it->second); array_list->InsertInt((int64_t)(double)it->second);
else if (member_name == "double") else if (member_name == "double")
array_list->InsertDouble((json::Number)it->second); array_list->InsertDouble(it->second);
else if (member_name == "bool") else if (member_name == "bool")
array_list->InsertBool((json::Boolean)it->second); array_list->InsertBool(it->second);
else if (member_name == "colour") else if (member_name == "colour")
array_list->InsertColour((std::string)(json::String)it->second); array_list->InsertColour(it->second);
} catch (agi::Exception&) { } catch (agi::Exception&) {
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");
@ -107,27 +102,22 @@ void ConfigVisitor::Visit(const json::Array& array) {
void ConfigVisitor::Visit(const json::Number& number) { void ConfigVisitor::Visit(const json::Number& number) {
if (int64_t(number) == ceil(number)) { if (int64_t(number) == ceil(number)) {
OptionValue *opt = new OptionValueInt(name, int64_t(number)); AddOptionValue(new OptionValueInt(name, int64_t(number)));
AddOptionValue(opt);
} else { } else {
OptionValue *opt = new OptionValueDouble(name, number); AddOptionValue(new OptionValueDouble(name, number));
AddOptionValue(opt);
} }
} }
void ConfigVisitor::Visit(const json::String& string) { void ConfigVisitor::Visit(const json::String& string) {
OptionValue *opt;
if (string.find("rgb(") == 0) { if (string.find("rgb(") == 0) {
opt = new OptionValueColour(name, string); AddOptionValue(new OptionValueColour(name, string));
} else { } else {
opt = new OptionValueString(name, string); AddOptionValue(new OptionValueString(name, string));
} }
AddOptionValue(opt);
} }
void ConfigVisitor::Visit(const json::Boolean& boolean) { void ConfigVisitor::Visit(const json::Boolean& boolean) {
OptionValue *opt = new OptionValueBool(name, boolean); AddOptionValue(new OptionValueBool(name, boolean));
AddOptionValue(opt);
} }
void ConfigVisitor::Visit(const json::Null& null) { void ConfigVisitor::Visit(const json::Null& null) {