60 lines
1.9 KiB
C++
60 lines
1.9 KiB
C++
#pragma once
|
|
|
|
#include "IResource.h"
|
|
#include <mutex>
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
class Resource final
|
|
{
|
|
static std::mutex mStringResource;
|
|
static const char* StringResources;
|
|
|
|
static std::mutex mIntResource;
|
|
static const char* IntResources;
|
|
|
|
static std::mutex mLongResource;
|
|
static const char* LongResources;
|
|
|
|
static std::mutex mDoubleResource;
|
|
static const char* DoubleResources;
|
|
|
|
static std::mutex mObjectResource;
|
|
static const char* ObjectResources;
|
|
|
|
// throw: std::runtime_error("Can't find resource") если ресурса не существует
|
|
static std::string GetResourceData(const char* name, const char* file_name);
|
|
|
|
static void WriteResource(const char* name, const char* file_name, const std::string& value);
|
|
|
|
public:
|
|
/*
|
|
* Устанавливает значение в ресурс
|
|
* Не бросают исключений
|
|
*/
|
|
static void SetString(const char* name, const char* value);
|
|
static void SetInt(const char* name, int value);
|
|
static void SetLong(const char* name, long long value);
|
|
static void SetDouble(const char* name, double value);
|
|
static void SetObject(const char* name, IResource* object);
|
|
|
|
/*
|
|
* Метуды получения значений по ключу
|
|
* throw: std::runtime_error("Can't find resource") если ресурса не существует
|
|
*/
|
|
static std::string GetString(const char* name);
|
|
static int GetInt(const char* name);
|
|
static long long GetLong(const char* name);
|
|
static double GetDouble(const char* name);
|
|
template<class T>
|
|
static T* GetObject(const char* name)
|
|
{
|
|
mObjectResource.lock();
|
|
std::string data = GetResourceData(name, ObjectResources);
|
|
mObjectResource.unlock();
|
|
T* object = new T();
|
|
object->deserialize(data);
|
|
return object;
|
|
}
|
|
};
|