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;
for (json::Object::const_iterator index(object.begin()); index != object.end(); index++) {
const json::Object& obj = index->second;
BuildHotkey(index->first, obj);
BuildHotkey(index->first, index->second);
}
}
@ -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++) {
Combo combo(context, index->first);
const json::Object& obj = *arr_index;
const json::Array& arr_mod = obj["modifiers"];
const json::Array& arr_mod = (*arr_index)["modifiers"];
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(key_mod);
combo.KeyInsert(*arr_mod_index);
}
combo.KeyInsert(static_cast<const json::String&>(obj["key"]));
combo.KeyInsert((*arr_index)["key"]);
ComboInsert(combo);
} // for arr_index
} // for index
}
}
}
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;
hotkey["modifiers"] = modifiers;
hotkey["key"] = json::String(combo_map.back());
json::Object& context_obj = root[index->second.Context()];
json::Array& combo_array = context_obj[index->second.CmdName()];
hotkey["key"] = combo_map.back();
json::Array& combo_array = root[index->second.Context()][index->second.CmdName()];
combo_array.push_back(hotkey);
}

View File

@ -102,7 +102,6 @@ LogSink::~LogSink() {
timeval_close.push_back(time_close.tv_usec);
root["timeval"]["close"] = timeval_close;
root["log"] = array;
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());
for (; index_object != index_objectEnd; ++index_object) {
const std::string &member_name = index_object->first;
Load(member_name, (json::Array)index_object->second);
}
for (; index_object != index_objectEnd; ++index_object)
Load(index_object->first, index_object->second);
}
@ -92,11 +90,7 @@ void MRUManager::Flush() {
for (MRUMap::const_iterator i = mru.begin(); i != mru.end(); ++i) {
json::Array &array = out[i->first];
const MRUListMap &map_list = i->second;
for (MRUListMap::const_iterator i_lst = map_list.begin(); i_lst != map_list.end(); ++i_lst) {
array.push_back(json::String(*i_lst));
}
copy(i->second.begin(), i->second.end(), std::back_inserter(array));
}
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()));
}
static json::String cast_str(json::UnknownElement const& e) {
return static_cast<json::String>(e);
}
/// @brief Load MRU Lists.
/// @param key List name.
/// @param array json::Array of values.
void MRUManager::Load(const std::string &key, const json::Array& array) {
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&) {
// Out of date MRU file; just discard the data and skip it

View File

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

View File

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