-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageCache.hpp
93 lines (63 loc) · 2.78 KB
/
ImageCache.hpp
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
#ifndef __VIPER_IMAGECACHE_H
#define __VIPER_IMAGECACHE_H
#include "iOSutil/iOSutil.hpp"
#include "AndroidUtil/AndroidUtil.hpp"
#include <functional>
#include <fstream>
#include <iomanip>
#include "sha256.hpp"
#include "Dispatch.hpp"
#include "Mitsoko/Image.hpp"
namespace Mitsoko {
struct ImageCache{
static ImageCache shared;
struct Callback : Mitsoko::Disposable::Observer {
typedef std::function<void(Mitsoko::Image)> type;
type fun;
bool shouldFire = true;
Mitsoko::Disposable::Id disposableId;
Callback(type fun_,const Mitsoko::Disposable &disposable);
Callback(const Callback &other);
void operator()(Mitsoko::Image image) const;
virtual void disposableDidDispose(Mitsoko::Disposable::Id id) override;
};
/**
* Should be called from main thread only. Otherwise data races may occur.
*/
void get(const std::string &url, Callback cb);
/**
* Explicitly puts image with a given URL to RAM and FS
*/
void put(const std::string &url, Mitsoko::Image image);
/**
* Returns cached image from RAM or FS or null if one isn't cached.
* Doesn't perform request.
*/
Mitsoko::Image getCached(const std::string &url, std::string *keyPointer = nullptr, std::string *filepathPointer = nullptr);
std::string keyByUrl(const std::string &url) const;
/**
* Documents path is an absolute path of a diretory where client
* is able to save cache and other files. Must not have '/' at the end.
*/
void documentsPath(const std::string &newValue);
const std::string& documentsPath();
bool usesRamCache = true;
bool neverRestoresCachedImages = false;
protected:
std::map<std::string, std::vector<Callback>> callbacks;
void putIntoRAM(const std::string &key, Mitsoko::Image image);
Mitsoko::Image getImageFromRAM(const std::string &key);
Mitsoko::Image getImageFromFS(const std::string &filepath);
#ifdef __APPLE__
NS::MutableDictionary _ramCache;
NS::MutableDictionary& ramCache();
#else
java::util::HashMap<java::lang::String, android::graphics::Bitmap> _ramCache;
decltype(_ramCache)& ramCache();
#endif
std::string imageFileName(const std::string &key);
std::string getHexRepresentation(const unsigned char *bytes, size_t length) const;
std::string _documentsPath;
};
}
#endif /* __VIPER_IMAGECACHE_H */