Significantly simplify agi::Options::PutOption

Originally committed to SVN as r6014.
This commit is contained in:
Thomas Goyne 2011-12-22 21:11:46 +00:00
parent 56165305a4
commit 4c3d191de8
3 changed files with 11 additions and 44 deletions

View File

@ -203,25 +203,18 @@ void Options::Flush() {
json::Writer::Write(obj_out, file.Get());
}
bool Options::PutOption(json::Object &obj, const std::string &path, const json::UnknownElement &value) {
// Having a '/' denotes it is a leaf.
if (path.find('/') == std::string::npos) {
void Options::PutOption(json::Object &obj, const std::string &path, const json::UnknownElement &value) {
std::string::size_type pos = path.find('/');
// Not having a '/' denotes it is a leaf.
if (pos == std::string::npos) {
assert(obj.find(path) == obj.end());
obj[path] = value;
return true;
} else {
std::string thispart = path.substr(0, path.find("/"));
std::string restpart = path.substr(path.find("/")+1, path.size());
json::Object::iterator pos = obj.find(thispart);
// New key, make object.
if (pos == obj.end())
pos = obj.insert(make_pair(thispart, json::Object())).first;
PutOptionVisitor visitor(restpart, value);
pos->second.Accept(visitor);
return visitor.result;
}
else {
PutOption(
obj[path.substr(0, pos)],
path.substr(pos + 1),
value);
}
}

View File

@ -48,28 +48,4 @@ public:
void Visit(const json::Null& null);
};
class PutOptionVisitor : public json::Visitor {
public:
bool result;
const std::string &path;
const json::UnknownElement &value;
PutOptionVisitor(const std::string &path, const json::UnknownElement &value)
: result(false), path(path), value(value)
{}
// all of these are a fail
virtual void Visit(json::Array& array) { }
virtual void Visit(json::Number& number) { }
virtual void Visit(json::String& string) { }
virtual void Visit(json::Boolean& boolean) { }
virtual void Visit(json::Null& null) { }
// this one is the win
virtual void Visit(json::Object& object) {
result = Options::PutOption(object, path, value);
}
};
} // namespace agi

View File

@ -52,8 +52,6 @@ public:
};
class Options {
friend class PutOptionVisitor;
public:
/// Options class settings.
enum OptionSetting {
@ -89,7 +87,7 @@ private:
/// @param[out] obj Parent object
/// @param[in] path Path option should be stored in.
/// @param[in] value Value to write.
static bool PutOption(::json::Object &obj, const std::string &path, const ::json::UnknownElement &value);
static void PutOption(::json::Object &obj, const std::string &path, const ::json::UnknownElement &value);
public:
/// @brief Constructor