diff options
author | randomdan <randomdan@localhost> | 2012-02-14 14:39:47 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2012-02-14 14:39:47 +0000 |
commit | 198a291007cd218066124c2ca3a88c872f880a58 (patch) | |
tree | a9ed108d112b4ee33e6346d415dc6be36f16c17f | |
parent | Optimize and tweak (diff) | |
download | project2-198a291007cd218066124c2ca3a88c872f880a58.tar.bz2 project2-198a291007cd218066124c2ca3a88c872f880a58.tar.xz project2-198a291007cd218066124c2ca3a88c872f880a58.zip |
Pull the script location back into the core and add support for caching of parsed script documents
-rw-r--r-- | project2/common/environment.cpp | 20 | ||||
-rw-r--r-- | project2/common/environment.h | 4 | ||||
-rw-r--r-- | project2/common/scripts.h | 1 | ||||
-rw-r--r-- | project2/xml/xmlScriptParser.cpp | 20 | ||||
-rw-r--r-- | project2/xml/xmlScriptParser.h | 1 |
5 files changed, 31 insertions, 15 deletions
diff --git a/project2/common/environment.cpp b/project2/common/environment.cpp index c0f84a2..9d79ca0 100644 --- a/project2/common/environment.cpp +++ b/project2/common/environment.cpp @@ -6,6 +6,7 @@ #include <stdio.h> #include <fstream> #include <boost/filesystem/convenience.hpp> +#include <boost/tuple/tuple_comparison.hpp> #include <boost/foreach.hpp> #include <boost/bind.hpp> @@ -109,11 +110,22 @@ ScriptReaderPtr Environment::resolveScript(const std::string & group, const std::string & name, bool ii) const { typedef std::map<std::string, boost::shared_ptr<ScriptReaderLoader> > ReaderLoaders; - BOOST_FOREACH(const ReaderLoaders::value_type & rl, *LoaderBase::objLoaders<ScriptReaderLoader>()) { - ScriptReaderPtr rs = rl.second->resolveScript(group, name); - if (rs) { - return rs; + boost::filesystem::path e(name); + while (!e.empty()) { + ScriptKey sk(group, e.string()); + ScriptCache::const_iterator i = scriptCache.find(sk); + if (i != scriptCache.end() && i->second->isCurrent()) { + return i->second; } + else { + BOOST_FOREACH(const ReaderLoaders::value_type & rl, *LoaderBase::objLoaders<ScriptReaderLoader>()) { + ScriptReaderPtr rs = rl.second->resolveScript(group, e.string()); + if (rs) { + return (scriptCache[sk] = rs); + } + } + } + e = e.parent_path(); } if (ii) { throw DependencyNotFound(group, name); diff --git a/project2/common/environment.h b/project2/common/environment.h index 944010a..accbb80 100644 --- a/project2/common/environment.h +++ b/project2/common/environment.h @@ -4,6 +4,7 @@ #include <string> #include <glibmm/ustring.h> #include <boost/function.hpp> +#include <boost/tuple/tuple.hpp> #include <boost/filesystem/path.hpp> #include "optionsSource.h" #include "exceptions.h" @@ -40,6 +41,9 @@ class Environment { int slLevel; typedef std::vector<OptionsSourcePtr> ConfigsMap; ConfigsMap configs; + typedef boost::tuple<const std::string, const std::string> ScriptKey; + typedef std::map<ScriptKey, ScriptReaderPtr> ScriptCache; + mutable ScriptCache scriptCache; public: std::string datasourceRoot; diff --git a/project2/common/scripts.h b/project2/common/scripts.h index 497ea6d..020cf50 100644 --- a/project2/common/scripts.h +++ b/project2/common/scripts.h @@ -53,6 +53,7 @@ class ScriptReader : public virtual IntrusivePtrBase { public: virtual ScriptNodePtr root() const = 0; virtual void load(const CommonObjects *, bool childrenOnly) const; + virtual bool isCurrent() const = 0; mutable LoaderBase loader; }; diff --git a/project2/xml/xmlScriptParser.cpp b/project2/xml/xmlScriptParser.cpp index 47db20f..abb4cbe 100644 --- a/project2/xml/xmlScriptParser.cpp +++ b/project2/xml/xmlScriptParser.cpp @@ -37,20 +37,18 @@ XmlScriptParser::load(const CommonObjects * co, bool childrenOnly) const _root.reset(); } +bool +XmlScriptParser::isCurrent() const +{ + return false; +} + class XmlScriptReaderLoader : public ScriptReaderLoader { public: ScriptReaderPtr resolveScript(const std::string & group, const std::string & name) const { - boost::filesystem::path script(boost::filesystem::current_path() / group); - BOOST_FOREACH(const boost::filesystem::path & e, boost::filesystem::path(name)) { - if (boost::filesystem::is_directory(script / e)) { - script /= e; - } - else { - boost::filesystem::path path = (script / e).string() + ".xml"; - if (boost::filesystem::is_regular_file(path)) { - return new XmlScriptParser(path); - } - } + boost::filesystem::path script(boost::filesystem::current_path() / group / (name + ".xml")); + if (boost::filesystem::is_regular_file(script)) { + return new XmlScriptParser(script); } return NULL; } diff --git a/project2/xml/xmlScriptParser.h b/project2/xml/xmlScriptParser.h index 39e27bc..c85795a 100644 --- a/project2/xml/xmlScriptParser.h +++ b/project2/xml/xmlScriptParser.h @@ -40,6 +40,7 @@ class XmlScriptParser : public xmlpp::DomParser, public ScriptReader { XmlScriptParser(const boost::filesystem::path & file); ScriptNodePtr root() const; void load(const CommonObjects *, bool childrenOnly) const; + bool isCurrent() const; private: mutable ScriptNodePtr _root; }; |