// -*- C++ -*- /** * \file Cache.h * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * * \author Guillaume Munch * * Full author contact details are available in file CREDITS. */ #ifndef CACHE_H #define CACHE_H #include #include #include namespace lyx { /** * Cache implements a cache where objects are stored by copy. * * This is a wrapper for QCache. See the documentation of QCache: * . * * It is especially useful for storing shared pointers. This turns QCache into a * shared-ownership cache with no risks of dangling pointer. It is also useful * for small copyable objects. * * Use this rather than QCache directly, to avoid naked pointers. */ template class Cache : private QCache { #if !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 6)) static_assert(std::is_copy_constructible::value, "lyx::Cache only stores copyable objects!"); static_assert(std::is_default_constructible::value, "lyx::Cache only stores default-constructible objects!"); using Q = QCache; #else typedef QCache Q; #endif public: /// Cache(int max_cost = 100) : Q(max_cost) {} /// bool insert(Key const & key, Val object, int cost = 1) { return Q::insert(key, new Val(std::move(object)), cost); } // Returns the default value (e.g. null pointer) before using the result. If // this is not convenient for your type, check if it exists beforehand with // Cache::contains. Val object(Key const & key) const { if (Val * obj = Q::object(key)) return *obj; return Val(); } /// Everything from QCache except QCache::take. using Q::clear; using Q::contains; using Q::count; using Q::remove; using Q::size; bool empty() const { return Q::isEmpty(); } std::list keys() { return Q::keys().toStdList(); } int max_cost() const { return Q::maxCost(); } void set_max_cost(int cost) { Q::setMaxCost(cost); } int total_cost() const { return Q::totalCost(); } Val operator[](Key const & key) const { return object(key); } }; } // namespace lyx #endif