QxOrm  1.4.3
C++ Object Relational Mapping library
QxCache.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
00005 **
00006 ** This file is part of the QxOrm library
00007 **
00008 ** This software is provided 'as-is', without any express or implied
00009 ** warranty. In no event will the authors be held liable for any
00010 ** damages arising from the use of this software
00011 **
00012 ** Commercial Usage
00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
00014 ** accordance with the commercial license agreement provided with the
00015 ** Software or, alternatively, in accordance with the terms contained in
00016 ** a written agreement between you and Lionel Marty
00017 **
00018 ** GNU General Public License Usage
00019 ** Alternatively, this file may be used under the terms of the GNU
00020 ** General Public License version 3.0 as published by the Free Software
00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
00022 ** packaging of this file. Please review the following information to
00023 ** ensure the GNU General Public License version 3.0 requirements will be
00024 ** met : http://www.gnu.org/copyleft/gpl.html
00025 **
00026 ** If you are unsure which license is appropriate for your use, or
00027 ** if you have questions regarding the use of this file, please contact :
00028 ** contact@qxorm.com
00029 **
00030 ****************************************************************************/
00031 
00032 #ifndef _QX_CACHE_H_
00033 #define _QX_CACHE_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <boost/any.hpp>
00047 #include <boost/tuple/tuple.hpp>
00048 #include <boost/tuple/tuple_comparison.hpp>
00049 #include <boost/tuple/tuple_io.hpp>
00050 
00051 #include <QxCommon/QxBool.h>
00052 
00053 #include <QxCollection/QxCollection.h>
00054 
00055 #include <QxSingleton/QxSingleton.h>
00056 
00057 namespace qx {
00058 namespace cache {
00059 namespace detail {
00060 
00061 class QX_DLL_EXPORT QxCache : public qx::QxSingleton<QxCache>
00062 {
00063 
00064    friend class qx::QxSingleton<QxCache>;
00065 
00066 protected:
00067 
00068    typedef boost::tuple<long, QDateTime, boost::any> type_qx_cache;
00069    typedef qx::QxCollection<QString, type_qx_cache> type_qx_lst_cache;
00070 
00071    type_qx_lst_cache m_cache;    
00072    QMutex m_oMutexCache;         
00073    long m_lMaxCost;              
00074    long m_lCurrCost;             
00075 
00076 public:
00077 
00078    QxCache();
00079    virtual ~QxCache();
00080 
00081    long getCurrCost() const;
00082    long getMaxCost() const;
00083    void setMaxCost(long l);
00084 
00085    long count() const;
00086    long size() const;
00087    bool isEmpty() const;
00088    bool exist(const QString & sKey) const;
00089    bool contains(const QString & sKey) const;
00090    boost::any at(const QString & sKey);
00091    long insertionCost(const QString & sKey);
00092    QDateTime insertionDateTime(const QString & sKey);
00093    void clear();
00094 
00095    bool insert(const QString & sKey, const boost::any & anyObj, long lCost = 1, const QDateTime & dt = QDateTime());
00096    bool remove(const QString & sKey);
00097 
00098 private:
00099 
00100    void updateCost();
00101 
00102 };
00103 
00104 } // namespace detail
00105 } // namespace cache
00106 } // namespace qx
00107 
00108 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::cache::detail::QxCache)
00109 
00110 namespace qx {
00111 namespace cache {
00112 
00117 inline void max_cost(long l)
00118 { qx::cache::detail::QxCache::getSingleton()->setMaxCost(l); }
00119 
00124 inline long max_cost()
00125 { return qx::cache::detail::QxCache::getSingleton()->getMaxCost(); }
00126 
00131 inline long current_cost()
00132 { return qx::cache::detail::QxCache::getSingleton()->getCurrCost(); }
00133 
00138 inline long count()
00139 { return qx::cache::detail::QxCache::getSingleton()->count(); }
00140 
00145 inline bool is_empty()
00146 { return qx::cache::detail::QxCache::getSingleton()->isEmpty(); }
00147 
00152 inline void clear()
00153 { qx::cache::detail::QxCache::getSingleton()->clear(); }
00154 
00159 inline bool exist(const QString & sKey)
00160 { return qx::cache::detail::QxCache::getSingleton()->exist(sKey); }
00161 
00166 inline bool remove(const QString & sKey)
00167 { return qx::cache::detail::QxCache::getSingleton()->remove(sKey); }
00168 
00173 template <typename T>
00174 inline bool set(const QString & sKey, T & t, long lCost = 1, const QDateTime & dt = QDateTime())
00175 {
00176    boost::any obj(t);
00177    return qx::cache::detail::QxCache::getSingleton()->insert(sKey, obj, lCost, dt);
00178 }
00179 
00184 template <typename T>
00185 inline T get(const QString & sKey)
00186 {
00187    boost::any obj = qx::cache::detail::QxCache::getSingleton()->at(sKey);
00188    if (obj.empty()) { return T(); }
00189    try { return boost::any_cast<T>(obj); }
00190    catch (const boost::bad_any_cast & err) { Q_UNUSED(err); return T(); }
00191    catch (...) { return T(); }
00192 }
00193 
00198 template <typename T>
00199 inline qx_bool get(const QString & sKey, T & t, QDateTime & dt)
00200 {
00201    dt = QDateTime();
00202    if (! qx::cache::exist(sKey)) { return qx_bool(false, 0, "[QxOrm] qx::cache : key doesn't exist in cache"); }
00203    boost::any obj = qx::cache::detail::QxCache::getSingleton()->at(sKey);
00204    dt = qx::cache::detail::QxCache::getSingleton()->insertionDateTime(sKey);
00205    try { t = boost::any_cast<T>(obj); return qx_bool(true); }
00206    catch (const boost::bad_any_cast & err) { Q_UNUSED(err); return qx_bool(false, 0, "[QxOrm] qx::cache : bad any cast exception"); }
00207    catch (...) { return qx_bool(false, 0, "[QxOrm] qx::cache : unknown cast exception"); }
00208 }
00209 
00214 template <typename T>
00215 inline qx_bool get(const QString & sKey, T & t)
00216 {
00217    QDateTime dt;
00218    return qx::cache::get<T>(sKey, t, dt);
00219 }
00220 
00221 } // namespace cache
00222 } // namespace qx
00223 
00224 #endif // _QX_CACHE_H_