diff --git a/dust3d.pro b/dust3d.pro index 0d1017e9..e3856ec5 100644 --- a/dust3d.pro +++ b/dust3d.pro @@ -508,6 +508,9 @@ HEADERS += src/simulateclothmeshes.h SOURCES += src/ddsfile.cpp HEADERS += src/ddsfile.h +SOURCES += src/fileforever.cpp +HEADERS += src/fileforever.h + SOURCES += src/main.cpp HEADERS += src/version.h diff --git a/src/fileforever.cpp b/src/fileforever.cpp new file mode 100644 index 00000000..1a9c6283 --- /dev/null +++ b/src/fileforever.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include "fileforever.h" + +struct FileForeverItem +{ + QString name; + QUuid id; + QByteArray *byteArray; +}; +static std::map g_foreverMap; +static QMutex g_mapMutex; + +const QString *FileForever::getName(const QUuid &id) +{ + QMutexLocker locker(&g_mapMutex); + auto findResult = g_foreverMap.find(id); + if (findResult == g_foreverMap.end()) + return nullptr; + return &findResult->second.name; +} + +const QByteArray *FileForever::getContent(const QUuid &id) +{ + QMutexLocker locker(&g_mapMutex); + auto findResult = g_foreverMap.find(id); + if (findResult == g_foreverMap.end()) + return nullptr; + return findResult->second.byteArray; +} + +QUuid FileForever::add(const QString &name, const QByteArray &content, QUuid toId) +{ + QMutexLocker locker(&g_mapMutex); + QUuid newId = toId.isNull() ? QUuid::createUuid() : toId; + if (g_foreverMap.find(newId) != g_foreverMap.end()) + return newId; + QByteArray *byteArray = new QByteArray(content); + g_foreverMap[newId] = {name, newId, byteArray}; + return newId; +} + +void FileForever::remove(const QUuid &id) +{ + QMutexLocker locker(&g_mapMutex); + auto findFile = g_foreverMap.find(id); + if (findFile == g_foreverMap.end()) + return; + delete findFile->second.byteArray; + g_foreverMap.erase(id); +} diff --git a/src/fileforever.h b/src/fileforever.h new file mode 100644 index 00000000..e9c118e6 --- /dev/null +++ b/src/fileforever.h @@ -0,0 +1,16 @@ +#ifndef DUST3D_FILE_FOREVER_H +#define DUST3D_FILE_FOREVER_H +#include +#include +#include + +class FileForever +{ +public: + static const QString *getName(const QUuid &id); + static const QByteArray *getContent(const QUuid &id); + static QUuid add(const QString &name, const QByteArray &content, QUuid toId=QUuid()); + static void remove(const QUuid &id); +}; + +#endif