Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (2,32 KB) Statistiken
| Zweig: | Revision:
c22391b2 Christian Ehringfeld
/*
* Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
d84c91e7 Christian Ehringfeld
#ifndef CACHE_H
#define CACHE_H
11bbe9a6 Christian Ehringfeld
#include <QHash>
#include <QWeakPointer>
5ca225ed Christian Ehringfeld
#include <QMutex>
11bbe9a6 Christian Ehringfeld
#include <QSharedPointer>
#include "entityinstancefactory.h"
#include "entity.h"
e8d1537c Christian Ehringfeld
#include "entityhelper.h"
d84c91e7 Christian Ehringfeld
namespace CuteEntityManager {
11bbe9a6 Christian Ehringfeld
class Entity;
d84c91e7 Christian Ehringfeld
class Cache {
public:
d933d48e Christian Ehringfeld
Cache();
QHash<QString, QWeakPointer<Entity> > getCache() const;
bool contains(qint64 id, const QString &classname);
bool contains(const QString &key);
template<class T> bool contains(qint64 id) {
11bbe9a6 Christian Ehringfeld
bool ok = false;
Entity *e = EntityInstanceFactory::createInstance<T>();
if (e) {
e8d1537c Christian Ehringfeld
ok = this->contains(id, QString(EntityHelper::getClassname(e)));
11bbe9a6 Christian Ehringfeld
delete e;
}
return ok;
}
a873a3ba Christian Ehringfeld
void clear();
d933d48e Christian Ehringfeld
void insert(QSharedPointer<Entity> &entity);
void remove(const QSharedPointer<Entity> &entity);
void remove(const qint64 &id, const QString &classname);
template<class T> void remove(qint64 id) {
11bbe9a6 Christian Ehringfeld
Entity *e = EntityInstanceFactory::createInstance<T>();
if (e) {
e8d1537c Christian Ehringfeld
this->remove(id, QString(EntityHelper::getClassname(e)));
11bbe9a6 Christian Ehringfeld
delete e;
}
}

d933d48e Christian Ehringfeld
QSharedPointer<Entity> get(qint64 id, const QString &classname);
template<class T> QSharedPointer<Entity> get(qint64 id) {
11bbe9a6 Christian Ehringfeld
Entity *e = EntityInstanceFactory::createInstance<T>();
if (e) {
5ca225ed Christian Ehringfeld
auto r = this->get(id, QString(EntityHelper::getClassname(e)));
11bbe9a6 Christian Ehringfeld
delete e;
5ca225ed Christian Ehringfeld
return r;
11bbe9a6 Christian Ehringfeld
}
return QSharedPointer<Entity>();
}

protected:
0d155b40 Christian Ehringfeld
QString generateKey(qint64 id, const QString &classname) const;
11bbe9a6 Christian Ehringfeld
private:
QHash<QString, QWeakPointer<Entity>> cache;
5ca225ed Christian Ehringfeld
QMutex mutex;
d84c91e7 Christian Ehringfeld
};
}

#endif // CACHE_H