mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-16 16:36:55 +02:00
KTXCache
This commit is contained in:
parent
8969d7df1d
commit
cfe14518a1
2 changed files with 118 additions and 0 deletions
49
libraries/model-networking/src/model-networking/KTXCache.cpp
Normal file
49
libraries/model-networking/src/model-networking/KTXCache.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// KTXCache.cpp
|
||||
// libraries/model-networking/src
|
||||
//
|
||||
// Created by Zach Pomerantz on 2/22/2017.
|
||||
// Copyright 2017 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#include "KTXCache.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
KTXFilePointer KTXCache::writeFile(Data data) {
|
||||
return std::static_pointer_cast<KTXFile>(FileCache::writeFile(data.key, data.data, data.length, (void*)&data));
|
||||
}
|
||||
|
||||
KTXFilePointer KTXCache::getFile(const QUrl& url) {
|
||||
Key key;
|
||||
{
|
||||
Lock lock(_urlMutex);
|
||||
const auto it = _urlMap.find(url);
|
||||
if (it != _urlMap.cend()) {
|
||||
key = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
KTXFilePointer file;
|
||||
if (!key.empty()) {
|
||||
file = std::static_pointer_cast<KTXFile>(FileCache::getFile(key));
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
File* KTXCache::createFile(const Key& key, const std::string& filepath, size_t length, void* extra) {
|
||||
const QUrl& url = reinterpret_cast<Data*>(extra)->url;
|
||||
Lock lock(_urlMutex);
|
||||
_urlMap[url] = key;
|
||||
return new KTXFile(key, filepath, length, url);
|
||||
}
|
||||
|
||||
void KTXCache::evictedFile(const FilePointer& file) {
|
||||
const QUrl url = std::static_pointer_cast<KTXFile>(file)->getUrl();
|
||||
Lock lock(_urlMutex);
|
||||
_urlMap.erase(url);
|
||||
}
|
69
libraries/model-networking/src/model-networking/KTXCache.h
Normal file
69
libraries/model-networking/src/model-networking/KTXCache.h
Normal file
|
@ -0,0 +1,69 @@
|
|||
//
|
||||
// KTXCache.h
|
||||
// libraries/model-networking/src
|
||||
//
|
||||
// Created by Zach Pomerantz 2/22/2017.
|
||||
// Copyright 2017 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#ifndef hifi_KTXCache_h
|
||||
#define hifi_KTXCache_h
|
||||
|
||||
#include <QUrl>
|
||||
|
||||
#include <FileCache.h>
|
||||
|
||||
class KTXFile;
|
||||
using KTXFilePointer = std::shared_ptr<KTXFile>;
|
||||
|
||||
class KTXCache : public FileCache {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
KTXCache(const std::string& dir, const std::string& ext) : FileCache(dir, ext) {}
|
||||
|
||||
struct Data {
|
||||
Data(const QUrl& url, const Key& key, const char* data, size_t length) :
|
||||
url(url), key(key), data(data), length(length) {}
|
||||
const QUrl url;
|
||||
const Key key;
|
||||
const char* data;
|
||||
size_t length;
|
||||
};
|
||||
|
||||
KTXFilePointer writeFile(Data data);
|
||||
KTXFilePointer getFile(const QUrl& url);
|
||||
|
||||
protected:
|
||||
File* createFile(const Key& key, const std::string& filepath, size_t length, void* extra) override final;
|
||||
void evictedFile(const FilePointer& file) override final;
|
||||
|
||||
private:
|
||||
using Mutex = std::mutex;
|
||||
using Lock = std::lock_guard<Mutex>;
|
||||
struct QUrlHasher { std::size_t operator()(QUrl const& url) const { return qHash(url); } };
|
||||
|
||||
std::unordered_map<QUrl, Key, QUrlHasher> _urlMap;
|
||||
Mutex _urlMutex;
|
||||
};
|
||||
|
||||
class KTXFile : public File {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QUrl getUrl() const { return _url; }
|
||||
|
||||
protected:
|
||||
KTXFile(const Key& key, const std::string& filepath, size_t length, const QUrl& url) :
|
||||
File(key, filepath, length), _url(url) {}
|
||||
|
||||
private:
|
||||
friend class KTXCache;
|
||||
|
||||
const QUrl _url;
|
||||
};
|
||||
|
||||
#endif // hifi_KTXCache_h
|
Loading…
Reference in a new issue