| QxOrm 1.2.3 C++ Object Relational Mapping library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

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 #include <boost/tuple/tuple.hpp>
    00042 #include <boost/tuple/tuple_comparison.hpp>
    00043 #include <boost/tuple/tuple_io.hpp>
    00044
    00045 #include <QxCommon/QxBool.h>
    00046
    00047 #include <QxCollection/QxCollection.h>
    00048
    00049 #include <QxSingleton/QxSingleton.h>
    00050
    00051 namespace qx {
    00052 namespace cache {
    00053 namespace detail {
    00054
    00055 class QX_DLL_EXPORT QxCache : public qx::QxSingleton<QxCache>
    00056 {
    00057
    00058    friend class qx::QxSingleton<QxCache>;
    00059
    00060 protected:
    00061
    00062    typedef boost::tuple<long, QDateTime, boost::any> type_qx_cache;
    00063    typedef qx::QxCollection<QString, type_qx_cache> type_qx_lst_cache;
    00064
    00065    type_qx_lst_cache m_cache;
    00066    QMutex m_oMutexCache;
    00067    long m_lMaxCost;
    00068    long m_lCurrCost;
    00069
    00070 public:
    00071
    00072    QxCache();
    00073    virtual ~QxCache();
    00074
    00075    long getCurrCost() const;
    00076    long getMaxCost() const;
    00077    void setMaxCost(long l);
    00078
    00079    long count() const;
    00080    long size() const;
    00081    bool isEmpty() const;
    00082    bool exist(const QString & sKey) const;
    00083    bool contains(const QString & sKey) const;
    00084    boost::any at(const QString & sKey);
    00085    long insertionCost(const QString & sKey);
    00086    QDateTime insertionDateTime(const QString & sKey);
    00087    void clear();
    00088
    00089    bool insert(const QString & sKey, const boost::any & anyObj, long lCost = 1, const QDateTime & dt = QDateTime());
    00090    bool remove(const QString & sKey);
    00091
    00092 private:
    00093
    00094    void updateCost();
    00095
    00096 };
    00097
    00098 } // namespace detail
    00099 } // namespace cache
    00100 } // namespace qx
    00101
    00102 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::cache::detail::QxCache)
    00103
    00104 namespace qx {
    00105 namespace cache {
    00106
    00111 inline void max_cost(long l)
    00112 { qx::cache::detail::QxCache::getSingleton()->setMaxCost(l); }
    00113
    00118 inline long max_cost()
    00119 { return qx::cache::detail::QxCache::getSingleton()->getMaxCost(); }
    00120
    00125 inline long current_cost()
    00126 { return qx::cache::detail::QxCache::getSingleton()->getCurrCost(); }
    00127
    00132 inline long count()
    00133 { return qx::cache::detail::QxCache::getSingleton()->count(); }
    00134
    00139 inline bool is_empty()
    00140 { return qx::cache::detail::QxCache::getSingleton()->isEmpty(); }
    00141
    00146 inline void clear()
    00147 { qx::cache::detail::QxCache::getSingleton()->clear(); }
    00148
    00153 inline bool exist(const QString & sKey)
    00154 { return qx::cache::detail::QxCache::getSingleton()->exist(sKey); }
    00155
    00160 inline bool remove(const QString & sKey)
    00161 { return qx::cache::detail::QxCache::getSingleton()->remove(sKey); }
    00162
    00167 template <typename T>
    00168 inline bool set(const QString & sKey, T & t, long lCost = 1, const QDateTime & dt = QDateTime())
    00169 {
    00170    boost::any obj(t);
    00171    return qx::cache::detail::QxCache::getSingleton()->insert(sKey, obj, lCost, dt);
    00172 }
    00173
    00178 template <typename T>
    00179 inline T get(const QString & sKey)
    00180 {
    00181    boost::any obj = qx::cache::detail::QxCache::getSingleton()->at(sKey);
    00182    if (obj.empty()) { return T(); }
    00183    try { return boost::any_cast<T>(obj); }
    00184    catch (const boost::bad_any_cast & err) { Q_UNUSED(err); return T(); }
    00185 }
    00186
    00191 template <typename T>
    00192 inline qx_bool get(const QString & sKey, T & t, QDateTime & dt)
    00193 {
    00194    dt = QDateTime();
    00195    if (! qx::cache::exist(sKey)) { return qx_bool(false, 0, "[QxOrm] qx::cache : key doesn't exist in cache"); }
    00196    boost::any obj = qx::cache::detail::QxCache::getSingleton()->at(sKey);
    00197    dt = qx::cache::detail::QxCache::getSingleton()->insertionDateTime(sKey);
    00198    try { t = boost::any_cast<T>(obj); return qx_bool(true); }
    00199    catch (const boost::bad_any_cast & err) { Q_UNUSED(err); return qx_bool(false, 0, "[QxOrm] qx::cache : bad any cast exception"); }
    00200 }
    00201
    00206 template <typename T>
    00207 inline qx_bool get(const QString & sKey, T & t)
    00208 {
    00209    QDateTime dt;
    00210    return qx::cache::get<T>(sKey, t, dt);
    00211 }
    00212
    00213 } // namespace cache
    00214 } // namespace qx
    00215
    00216 #endif // _QX_CACHE_H_