Files
MapLibreParticleLab/include/mbgl/storage/default/sqlite_cache.hpp
Konstantin Käfer bba3d9838a add Thread<T> and RunLoop
Thread<T> is a generic thread management object that starts a thread, creates an object T in that thread and calls its .start() function. When the Thread<T> object is destructed (which must happen in the thread that created it), it'll call the object's .stop() function, and wait for thread termination. The .stop() function must somehow get the thread to terminate. Note that object T's constructor/destructor/start()/stop() must be protected/private, and Thread<T> must be declared as a friend class. All public functions may be called from any thread and are exposed through operator->().

RunLoop encapsulates a libuv runloop and has a facility of dispatching function objects to the thread. It can be used either as a worker thread class by itself, or it can be derived from. This commit converts SQLiteCache to derive from RunLoop and removes the custom code used previously for managing the cache thread.
2015-04-10 15:36:32 +02:00

45 lines
1.2 KiB
C++

#ifndef MBGL_STORAGE_DEFAULT_SQLITE_CACHE
#define MBGL_STORAGE_DEFAULT_SQLITE_CACHE
#include <mbgl/storage/file_cache.hpp>
#include <mbgl/util/run_loop.hpp>
#include <string>
namespace mapbox { namespace sqlite { class Database; class Statement; } }
namespace mbgl {
class SQLiteCache : public FileCache, protected util::RunLoop {
friend class util::Thread<SQLiteCache>;
private:
SQLiteCache(const std::string &path = ":memory:");
~SQLiteCache();
public:
// FileCache API
void get(const Resource &resource, Callback callback) override;
void put(const Resource &resource, std::shared_ptr<const Response> response, Hint hint) override;
private:
void createDatabase();
void createSchema();
void processGet(const Resource& resource, Callback callback);
void processPut(const Resource& resource, std::shared_ptr<const Response> response);
void processRefresh(const Resource& resource, int64_t expires);
private:
const std::string path;
std::unique_ptr<::mapbox::sqlite::Database> db;
std::unique_ptr<::mapbox::sqlite::Statement> getStmt;
std::unique_ptr<::mapbox::sqlite::Statement> putStmt;
std::unique_ptr<::mapbox::sqlite::Statement> refreshStmt;
bool schema = false;
};
}
#endif