From febeeeca3ac28e4d4e9d9be953016c84da039476 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Wed, 1 Mar 2017 20:58:00 -0500 Subject: [PATCH] namespace cache::FileCache and use unique_ptr --- .../src/model-networking/KTXCache.cpp | 20 ++++++++++++------- .../src/model-networking/KTXCache.h | 12 ++++++----- libraries/networking/src/FileCache.cpp | 6 ++++-- libraries/networking/src/FileCache.h | 12 +++++++---- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/libraries/model-networking/src/model-networking/KTXCache.cpp b/libraries/model-networking/src/model-networking/KTXCache.cpp index 036e520af3..d0380c7635 100644 --- a/libraries/model-networking/src/model-networking/KTXCache.cpp +++ b/libraries/model-networking/src/model-networking/KTXCache.cpp @@ -13,6 +13,9 @@ #include +using File = cache::File; +using FilePointer = cache::FilePointer; + KTXFilePointer KTXCache::writeFile(Data data) { return std::static_pointer_cast(FileCache::writeFile(data.key, data.data, data.length, (void*)&data)); } @@ -35,19 +38,22 @@ KTXFilePointer KTXCache::getFile(const QUrl& url) { return file; } -File* KTXCache::createFile(const Key& key, const std::string& filepath, size_t length, void* extra) { - const QUrl& url = reinterpret_cast(extra)->url; +std::unique_ptr KTXCache::createKTXFile(const Key& key, const std::string& filepath, size_t length, const QUrl& url) { Lock lock(_urlMutex); _urlMap[url] = key; - qCInfo(file_cache) << "Wrote KTX" << key.c_str() << url; - return new KTXFile(key, filepath, length, url); + return std::unique_ptr(new KTXFile(key, filepath, length, url)); } -File* KTXCache::loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) { +std::unique_ptr KTXCache::createFile(const Key& key, const std::string& filepath, size_t length, void* extra) { + const QUrl& url = reinterpret_cast(extra)->url; + qCInfo(file_cache) << "Wrote KTX" << key.c_str() << url; + return createKTXFile(key, filepath, length, url); +} + +std::unique_ptr KTXCache::loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) { const QUrl url = QString(metadata.c_str()); - _urlMap[url] = key; qCInfo(file_cache) << "Loaded KTX" << key.c_str() << url; - return new KTXFile(key, filepath, length, url); + return createKTXFile(key, filepath, length, url); } void KTXCache::evictedFile(const FilePointer& file) { diff --git a/libraries/model-networking/src/model-networking/KTXCache.h b/libraries/model-networking/src/model-networking/KTXCache.h index 7fe3ed872b..84dda48ee2 100644 --- a/libraries/model-networking/src/model-networking/KTXCache.h +++ b/libraries/model-networking/src/model-networking/KTXCache.h @@ -23,7 +23,7 @@ namespace ktx { class KTXFile; using KTXFilePointer = std::shared_ptr; -class KTXCache : public FileCache { +class KTXCache : public cache::FileCache { Q_OBJECT public: @@ -42,11 +42,13 @@ public: KTXFilePointer getFile(const QUrl& url); protected: - File* createFile(const Key& key, const std::string& filepath, size_t length, void* extra) override final; - File* loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) override final; - void evictedFile(const FilePointer& file) override final; + std::unique_ptr createFile(const Key& key, const std::string& filepath, size_t length, void* extra) override final; + std::unique_ptr loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) override final; + void evictedFile(const cache::FilePointer& file) override final; private: + std::unique_ptr createKTXFile(const Key& key, const std::string& filepath, size_t length, const QUrl& url); + using Mutex = std::mutex; using Lock = std::lock_guard; struct QUrlHasher { std::size_t operator()(QUrl const& url) const { return qHash(url); } }; @@ -55,7 +57,7 @@ private: Mutex _urlMutex; }; -class KTXFile : public File { +class KTXFile : public cache::File { Q_OBJECT public: diff --git a/libraries/networking/src/FileCache.cpp b/libraries/networking/src/FileCache.cpp index 40a2509b7c..034e24c8cd 100644 --- a/libraries/networking/src/FileCache.cpp +++ b/libraries/networking/src/FileCache.cpp @@ -22,6 +22,8 @@ Q_LOGGING_CATEGORY(file_cache, "hifi.file_cache") +using namespace cache; + static const std::string MANIFEST_NAME = "manifest"; void FileCache::setUnusedFileCacheSize(size_t unusedFilesMaxSize) { @@ -85,7 +87,7 @@ void FileCache::initialize() { const std::string filepath = dir.filePath(filename).toStdString(); const size_t length = std::ifstream(filepath, std::ios::binary | std::ios::ate).tellg(); - FilePointer file(loadFile(key, filepath, length, metadata), &fileDeleter); + FilePointer file(loadFile(key, filepath, length, metadata).release(), &fileDeleter); file->_cache = this; _files[key] = file; _numTotalFiles += 1; @@ -119,7 +121,7 @@ FilePointer FileCache::writeFile(const Key& key, const char* data, size_t length // write the new file FILE* saveFile = fopen(filepath.c_str(), "wb"); if (saveFile != nullptr && fwrite(data, length, 1, saveFile) && fclose(saveFile) == 0) { - file.reset(createFile(key, filepath, length, extra), &fileDeleter); + file.reset(createFile(key, filepath, length, extra).release(), &fileDeleter); file->_cache = this; _files[key] = file; _numTotalFiles += 1; diff --git a/libraries/networking/src/FileCache.h b/libraries/networking/src/FileCache.h index c5d8ce3fc2..7e751d56be 100644 --- a/libraries/networking/src/FileCache.h +++ b/libraries/networking/src/FileCache.h @@ -22,6 +22,8 @@ Q_DECLARE_LOGGING_CATEGORY(file_cache) +namespace cache { + class File; using FilePointer = std::shared_ptr; @@ -80,9 +82,9 @@ protected: FilePointer getFile(const Key& key); /// create a file (ex.: create a class derived from File and store it in a secondary map with extra->url) - virtual File* createFile(const Key& key, const std::string& filepath, size_t length, void* extra) = 0; + virtual std::unique_ptr createFile(const Key& key, const std::string& filepath, size_t length, void* extra) = 0; /// load a file - virtual File* loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) = 0; + virtual std::unique_ptr loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) = 0; /// take action when a file is evicted from the cache (ex.: evict it from a secondary map) virtual void evictedFile(const FilePointer& file) = 0; @@ -130,6 +132,8 @@ public: Key getKey() const { return _key; } size_t getLength() const { return _length; } + // the destructor should handle unlinking of the actual filepath + virtual ~File(); // overrides should call File::deleter to maintain caching behavior virtual void deleter(); @@ -137,8 +141,6 @@ protected: // when constructed, the file has already been created/written File(const Key& key, const std::string& filepath, size_t length) : _filepath(filepath), _key(key), _length(length) {} - // the destructor should handle unlinking of the actual filepath - virtual ~File(); /// get metadata to store with a file between instances (ex.: return the url of a hash) virtual std::string getMetadata() const = 0; @@ -157,4 +159,6 @@ private: bool _shouldPersist { false }; }; +} + #endif // hifi_FileCache_h