summaryrefslogtreecommitdiff
path: root/libadhocutil/cache.impl.h
diff options
context:
space:
mode:
Diffstat (limited to 'libadhocutil/cache.impl.h')
-rw-r--r--libadhocutil/cache.impl.h60
1 files changed, 48 insertions, 12 deletions
diff --git a/libadhocutil/cache.impl.h b/libadhocutil/cache.impl.h
index 0a2b0b3..dffed7f 100644
--- a/libadhocutil/cache.impl.h
+++ b/libadhocutil/cache.impl.h
@@ -18,43 +18,63 @@ Cacheable<T, K>::Cacheable(const K & k, time_t vu) :
template<typename T, typename K>
ObjectCacheable<T, K>::ObjectCacheable(const T & t, const K & k, time_t vu) :
Cacheable<T, K>(k, vu),
+ value(new T(t))
+{
+}
+
+template<typename T, typename K>
+ObjectCacheable<T, K>::ObjectCacheable(typename Cacheable<T, K>::Value & t, const K & k, time_t vu) :
+ Cacheable<T, K>(k, vu),
value(t)
{
}
template<typename T, typename K>
-const T &
+typename Cacheable<T, K>::Value
ObjectCacheable<T, K>::item() const
{
return value;
}
template<typename T, typename K>
-CallCacheable<T, K>::CallCacheable(const T & t, const K & k, time_t vu) :
+CallCacheable<T, K>::CallCacheable(const Factory & t, const K & k, time_t vu) :
Cacheable<T, K>(k, vu),
value(t)
{
}
template<typename T, typename K>
-CallCacheable<T, K>::CallCacheable(const boost::function<T()> & t, const K & k, time_t vu) :
+typename Cacheable<T, K>::Value
+CallCacheable<T, K>::item() const
+{
+ Lock(lock);
+ if (auto t = boost::get<typename Cacheable<T, K>::Value>(&value)) {
+ return *t;
+ }
+ const Factory & f = boost::get<Factory>(value);
+ value = typename Cacheable<T, K>::Value(new T(f()));
+ return boost::get<typename Cacheable<T, K>::Value>(value);
+}
+
+
+template<typename T, typename K>
+PointerCallCacheable<T, K>::PointerCallCacheable(const Factory & t, const K & k, time_t vu) :
Cacheable<T, K>(k, vu),
value(t)
{
}
template<typename T, typename K>
-const T &
-CallCacheable<T, K>::item() const
+typename Cacheable<T, K>::Value
+PointerCallCacheable<T, K>::item() const
{
Lock(lock);
- const T * t = boost::get<T>(&value);
- if (t) {
+ if (auto t = boost::get<typename Cacheable<T, K>::Value>(&value)) {
return *t;
}
- const boost::function<T()> & f = boost::get<boost::function<T()>>(value);
+ const Factory & f = boost::get<Factory>(value);
value = f();
- return boost::get<T>(value);
+ return boost::get<typename Cacheable<T, K>::Value>(value);
}
@@ -74,13 +94,29 @@ Cache<T, K>::add(const K & k, const T & t, time_t validUntil)
template<typename T, typename K>
void
-Cache<T, K>::add(const K & k, const boost::function<T()> & tf, time_t validUntil)
+Cache<T, K>::add(const K & k, Value & t, time_t validUntil)
+{
+ Lock(lock);
+ cached.insert(Element(new ObjectCacheable<T, K>(t, k, validUntil)));
+}
+
+template<typename T, typename K>
+void
+Cache<T, K>::add(const K & k, const Factory & tf, time_t validUntil)
{
Lock(lock);
cached.insert(Element(new CallCacheable<T, K>(tf, k, validUntil)));
}
template<typename T, typename K>
+void
+Cache<T, K>::add(const K & k, const PointerFactory & tf, time_t validUntil)
+{
+ Lock(lock);
+ cached.insert(Element(new PointerCallCacheable<T, K>(tf, k, validUntil)));
+}
+
+template<typename T, typename K>
typename Cache<T, K>::Element
Cache<T, K>::getItem(const K & k) const
{
@@ -100,12 +136,12 @@ Cache<T, K>::getItem(const K & k) const
}
template<typename T, typename K>
-const T *
+typename Cache<T, K>::Value
Cache<T, K>::get(const K & k) const
{
auto i = getItem(k);
if (i) {
- return &i->item();
+ return i->item();
}
return nullptr;
}