mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
DOM Caching
- Implemented DOM caching on name. May need a 'clear' method later.
This commit is contained in:
parent
b49523c06d
commit
5808e2073f
2 changed files with 35 additions and 18 deletions
50
src/DOM.cpp
50
src/DOM.cpp
|
@ -71,18 +71,26 @@ DOM::~DOM ()
|
|||
// system.os
|
||||
const std::string DOM::get (const std::string& name)
|
||||
{
|
||||
// Cache test.
|
||||
std::map <std::string, std::string>::iterator hit = _cache.find (name);
|
||||
if (hit != _cache.end ())
|
||||
return hit->second;
|
||||
|
||||
int len = name.length ();
|
||||
Nibbler n (name);
|
||||
|
||||
// Primitives
|
||||
if (is_primitive (name))
|
||||
{
|
||||
_cache[name] = name;
|
||||
return name;
|
||||
}
|
||||
|
||||
// rc. --> context.config
|
||||
else if (len > 3 &&
|
||||
name.substr (0, 3) == "rc.")
|
||||
{
|
||||
return context.config.get (name.substr (3));
|
||||
return _cache[name] = context.config.get (name.substr (3));
|
||||
}
|
||||
|
||||
// context.*
|
||||
|
@ -90,25 +98,25 @@ const std::string DOM::get (const std::string& name)
|
|||
name.substr (0, 8) == "context.")
|
||||
{
|
||||
if (name == "context.program")
|
||||
return context.args[0].first;
|
||||
return _cache[name] = context.args[0].first;
|
||||
|
||||
else if (name == "context.args")
|
||||
{
|
||||
std::string combined;
|
||||
join (combined, " ", context.args.list ());
|
||||
return combined;
|
||||
return _cache[name] = combined;
|
||||
}
|
||||
else if (name == "context.width")
|
||||
{
|
||||
std::stringstream s;
|
||||
s << context.terminal_width;
|
||||
return s.str ();
|
||||
return _cache[name] = s.str ();
|
||||
}
|
||||
else if (name == "context.height")
|
||||
{
|
||||
std::stringstream s;
|
||||
s << context.terminal_height;
|
||||
return s.str ();
|
||||
return _cache[name] = s.str ();
|
||||
}
|
||||
|
||||
else
|
||||
|
@ -124,39 +132,39 @@ const std::string DOM::get (const std::string& name)
|
|||
{
|
||||
// Taskwarrior version number.
|
||||
if (name == "system.version")
|
||||
return VERSION;
|
||||
return _cache[name] = VERSION;
|
||||
|
||||
#ifdef HAVE_LIBLUA
|
||||
// Lua version number.
|
||||
else if (name == "system.lua.version")
|
||||
return LUA_RELEASE;
|
||||
return _cache[name] = LUA_RELEASE;
|
||||
#endif
|
||||
|
||||
// OS type.
|
||||
else if (name == "system.os")
|
||||
#if defined (DARWIN)
|
||||
return "Darwin";
|
||||
return _cache[name] = "Darwin";
|
||||
#elif defined (SOLARIS)
|
||||
return "Solaris";
|
||||
return _cache[name] = "Solaris";
|
||||
#elif defined (CYGWIN)
|
||||
return "Cygwin";
|
||||
return _cache[name] = "Cygwin";
|
||||
#elif defined (OPENBSD)
|
||||
return "OpenBSD";
|
||||
return _cache[name] = "OpenBSD";
|
||||
#elif defined (HAIKU)
|
||||
return "Haiku";
|
||||
return _cache[name] = "Haiku";
|
||||
#elif defined (FREEBSD)
|
||||
return "FreeBSD";
|
||||
return _cache[name] = "FreeBSD";
|
||||
#elif defined (LINUX)
|
||||
return "Linux";
|
||||
return _cache[name] = "Linux";
|
||||
#else
|
||||
return STRING_DOM_UNKNOWN
|
||||
return _cache[name] = STRING_DOM_UNKNOWN
|
||||
#endif
|
||||
|
||||
else
|
||||
throw format (STRING_DOM_UNREC, name);
|
||||
}
|
||||
|
||||
return name;
|
||||
return _cache[name] = name;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -197,13 +205,18 @@ const std::string DOM::get (const std::string& name)
|
|||
//
|
||||
const std::string DOM::get (const std::string& name, Task& task)
|
||||
{
|
||||
// Cache test.
|
||||
std::map <std::string, std::string>::iterator hit = _cache.find (name);
|
||||
if (hit != _cache.end ())
|
||||
return hit->second;
|
||||
|
||||
Nibbler n (name);
|
||||
int id;
|
||||
std::string uuid;
|
||||
|
||||
// Primitives
|
||||
if (is_primitive (name))
|
||||
return name;
|
||||
return _cache[name] = name;
|
||||
|
||||
// <id>.<name>
|
||||
else if (n.getInt (id))
|
||||
|
@ -291,7 +304,8 @@ void DOM::set (const std::string& name, const std::string& value)
|
|||
if (len > 3 &&
|
||||
name.substr (0, 3) == "rc.")
|
||||
{
|
||||
return context.config.set (name.substr (3), value);
|
||||
_cache[name] = value;
|
||||
context.config.set (name.substr (3), value);
|
||||
}
|
||||
|
||||
// Unrecognized --> error.
|
||||
|
|
|
@ -44,6 +44,9 @@ public:
|
|||
|
||||
private:
|
||||
bool is_primitive (const std::string&);
|
||||
|
||||
private:
|
||||
std::map <std::string, std::string> _cache;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue