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()); json::Writer::Write(obj_out, file.Get());
} }
void Options::PutOption(json::Object &obj, const std::string &path, const json::UnknownElement &value) {
bool Options::PutOption(json::Object &obj, const std::string &path, const json::UnknownElement &value) { std::string::size_type pos = path.find('/');
// Having a '/' denotes it is a leaf. // Not having a '/' denotes it is a leaf.
if (path.find('/') == std::string::npos) { if (pos == std::string::npos) {
assert(obj.find(path) == obj.end()); assert(obj.find(path) == obj.end());
obj[path] = value; obj[path] = value;
return true; }
} else { else {
std::string thispart = path.substr(0, path.find("/")); PutOption(
std::string restpart = path.substr(path.find("/")+1, path.size()); obj[path.substr(0, pos)],
json::Object::iterator pos = obj.find(thispart); path.substr(pos + 1),
value);
// 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;
} }
} }

View File

@ -48,28 +48,4 @@ public:
void Visit(const json::Null& null); 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 } // namespace agi

View File

@ -52,8 +52,6 @@ public:
}; };
class Options { class Options {
friend class PutOptionVisitor;
public: public:
/// Options class settings. /// Options class settings.
enum OptionSetting { enum OptionSetting {
@ -89,7 +87,7 @@ private:
/// @param[out] obj Parent object /// @param[out] obj Parent object
/// @param[in] path Path option should be stored in. /// @param[in] path Path option should be stored in.
/// @param[in] value Value to write. /// @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: public:
/// @brief Constructor /// @brief Constructor