-
Notifications
You must be signed in to change notification settings - Fork 24
/
filecache.h
103 lines (77 loc) · 3.06 KB
/
filecache.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef FILECACHE_H
#define FILECACHE_H
#include <functional>
#include <QObject>
#include <QString>
#include <QDateTime>
#include <QMap>
#include <QSet>
//------------------------------------------------------------------------------
struct FileCacheError {};
//------------------------------------------------------------------------------
class AbstractFileCacheItem : public QObject
{
Q_OBJECT
public:
explicit AbstractFileCacheItem(const QString& path, const QString& key, int cost, const QDateTime& date_time, QObject* parent = 0);
virtual ~AbstractFileCacheItem();
const QString& path() const { return m_path; }
const QString& key() const { return m_key; }
int cost() const { return m_cost; }
const QDateTime& dateTime() const { return m_dateTime; }
void removeFileFromDisk() const { removeFileFromDisk(m_path); }
protected:
virtual void removeFileFromDisk(const QString& path) const = 0;
private:
QString m_path;
QString m_key;
int m_cost;
QDateTime m_dateTime;
};
//------------------------------------------------------------------------------
class FileCacheItem : public AbstractFileCacheItem
{
Q_OBJECT
public:
explicit FileCacheItem(const QString& path, const QString& key, int cost, const QDateTime& date_time, QObject* parent = 0);
virtual ~FileCacheItem();
private:
virtual void removeFileFromDisk(const QString& some_path) const;
mutable bool m_removed;
};
//------------------------------------------------------------------------------
class FileCache : public QObject
{
Q_OBJECT
public:
typedef std::function<AbstractFileCacheItem* (const QString&, // path
const QString&, // key
int, // cost
const QDateTime&, // access time
QObject* // parent
)> ItemGenerator;
FileCache(int maxCost = 0, QObject* parent = 0);
virtual ~FileCache();
int maxCost() const { return m_maxCost; }
void setMaxCost(int max_cost);
bool hasItem(const QString& key) const;
void addItem(AbstractFileCacheItem* item);
void addItem(const QByteArray& data, const QString& key, ItemGenerator item_generator);
int totalCost() const { return m_totalCost; }
int size() const { return m_items.size(); }
QList<QString> keys() const { return m_items.keys(); }
const AbstractFileCacheItem* item(const QString& key) const { return m_items.value(key); }
void clear();
void clearFromDisk();
bool setPath(const QString& path, ItemGenerator item_generator);
const QString& path() const { return m_path; }
private:
bool updateFromDisk(const QString &path, ItemGenerator item_generator);
QString m_path;
int m_maxCost;
int m_totalCost;
QMap<QString, AbstractFileCacheItem*> m_items;
QList<QString> m_indexByDate;
};
//------------------------------------------------------------------------------
#endif // FILECACHE_H