QxOrm 1.1.8
C++ Object Relational Mapping library
QxCache.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _QX_CACHE_H_
00027 #define _QX_CACHE_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <boost/any.hpp>
00041 
00042 #include <QxCommon/QxBool.h>
00043 
00044 #include <QxCollection/QxCollection.h>
00045 
00046 #include <QxSingleton/QxSingleton.h>
00047 
00048 namespace qx {
00049 namespace cache {
00050 namespace detail {
00051 
00052 class QX_DLL_EXPORT QxCache : public qx::QxSingleton<QxCache>
00053 {
00054 
00055    friend class qx::QxSingleton<QxCache>;
00056 
00057 protected:
00058 
00059    typedef std::pair<long, boost::any> type_qx_cache;
00060    typedef qx::QxCollection<QString, type_qx_cache> type_qx_lst_cache;
00061 
00062    type_qx_lst_cache m_cache;    
00063    QMutex m_oMutexCache;         
00064    long m_lMaxCost;              
00065    long m_lCurrCost;             
00066 
00067 public:
00068 
00069    QxCache() : qx::QxSingleton<QxCache>("qx::cache::detail::QxCache"), m_lMaxCost(999999999), m_lCurrCost(0) { ; }
00070    virtual ~QxCache() { ; }
00071 
00072    inline long getCurrCost() const  { return m_lCurrCost; }
00073    inline long getMaxCost() const   { return m_lMaxCost; }
00074    inline void setMaxCost(long l)   { QMutexLocker locker(& m_oMutexCache); m_lMaxCost = ((l < 0) ? 0 : l); updateCost(); }
00075 
00076    inline long count() const                          { return m_cache.count(); }
00077    inline long size() const                           { return this->count(); }
00078    inline bool isEmpty() const                        { return (this->count() == 0); }
00079    inline bool exist(const QString & sKey) const      { return m_cache.exist(sKey); }
00080    inline bool contains(const QString & sKey) const   { return this->exist(sKey); }
00081    inline boost::any at(const QString & sKey)         { QMutexLocker locker(& m_oMutexCache); return (this->exist(sKey) ? m_cache.getByKey(sKey).second : boost::any()); }
00082    inline void clear()                                { QMutexLocker locker(& m_oMutexCache); m_cache.clear(); m_lCurrCost = 0; }
00083 
00084    bool insert(const QString & sKey, const boost::any & pObj, long lCost = 1);
00085    bool remove(const QString & sKey);
00086 
00087 private:
00088 
00089    void updateCost();
00090 
00091 };
00092 
00093 } // namespace detail
00094 
00099 inline void max_cost(long l) { qx::cache::detail::QxCache::getSingleton()->setMaxCost(l); }
00100 
00105 inline long max_cost() { return qx::cache::detail::QxCache::getSingleton()->getMaxCost(); }
00106 
00111 inline long current_cost() { return qx::cache::detail::QxCache::getSingleton()->getCurrCost(); }
00112 
00117 inline long count() { return qx::cache::detail::QxCache::getSingleton()->count(); }
00118 
00123 inline bool is_empty() { return qx::cache::detail::QxCache::getSingleton()->isEmpty(); }
00124 
00129 inline void clear() { qx::cache::detail::QxCache::getSingleton()->clear(); }
00130 
00135 inline bool exist(const QString & sKey) { return qx::cache::detail::QxCache::getSingleton()->exist(sKey); }
00136 
00141 inline bool remove(const QString & sKey) { return qx::cache::detail::QxCache::getSingleton()->remove(sKey); }
00142 
00147 template <typename T>
00148 inline bool set(const QString & sKey, const T & t, long lCost = 1)
00149 { boost::any obj(t); return qx::cache::detail::QxCache::getSingleton()->insert(sKey, obj, lCost); }
00150 
00155 template <typename T>
00156 inline T get(const QString & sKey)
00157 {
00158    boost::any obj = qx::cache::detail::QxCache::getSingleton()->at(sKey);
00159    if (obj.empty()) { return T(); }
00160    try { return boost::any_cast<T>(obj); }
00161    catch (const boost::bad_any_cast & err) { Q_UNUSED(err); return T(); }
00162 }
00163 
00168 template <typename T>
00169 inline qx_bool get(const QString & sKey, T & t)
00170 {
00171    if (! qx::cache::exist(sKey)) { return qx_bool(false, 0, "[QxOrm] Key doesn't exist in cache"); }
00172    boost::any obj = qx::cache::detail::QxCache::getSingleton()->at(sKey);
00173    try { t = boost::any_cast<T>(obj); return qx_bool(true); }
00174    catch (const boost::bad_any_cast & err) { Q_UNUSED(err); return qx_bool(false, 0, "[QxOrm] Bad any cast exception"); }
00175 }
00176 
00177 } // namespace cache
00178 } // namespace qx
00179 
00180 #endif // _QX_CACHE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines