Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (2,33 KB) Statistiken
| Zweig: | Revision:
/*
* 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/>.
*/
#ifndef CACHE_H
#define CACHE_H
#include <QHash>
#include <QWeakPointer>
#include <QSharedPointer>
#include "entityinstancefactory.h"
#include "entity.h"

namespace CuteEntityManager {
class Entity;
class Cache {
public:
Q_DECL_EXPORT Cache();
Q_DECL_EXPORT QHash<QString, QWeakPointer<Entity> > getCache() const;
Q_DECL_EXPORT bool contains(qint64 id, const QString &classname);
Q_DECL_EXPORT bool contains(const QString &key);
template<class T> Q_DECL_EXPORT bool contains(qint64 id) {
bool ok = false;
Entity *e = EntityInstanceFactory::createInstance<T>();
if (e) {
ok = this->contains(id, QString(e->getClassname()));
delete e;
}
return ok;
}
Q_DECL_EXPORT void insert(QSharedPointer<Entity> &entity);
Q_DECL_EXPORT void remove(const QSharedPointer<Entity> &entity);
Q_DECL_EXPORT void remove(const qint64 &id, const QString &classname);
template<class T> Q_DECL_EXPORT void remove(qint64 id) {
Entity *e = EntityInstanceFactory::createInstance<T>();
if (e) {
this->remove(id, QString(e->getClassname()));
delete e;
}
}

Q_DECL_EXPORT QSharedPointer<Entity> get(qint64 id, const QString &classname);
template<class T> Q_DECL_EXPORT QSharedPointer<Entity> get(qint64 id) {
Entity *e = EntityInstanceFactory::createInstance<T>();
if (e) {
return this->get(id, QString(e->getClassname()));
delete e;
}
return QSharedPointer<Entity>();
}

protected:
QString generateKey(qint64 id, const QString &classname);
private:
QHash<QString, QWeakPointer<Entity>> cache;
};
}

#endif // CACHE_H
(2-2/22)