Я пересоздал репозиторий из-за большого количества мусора в прошлом
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "RTTI_Meta.h"
|
||||
|
||||
GENERATE_META(IResource);
|
||||
class IResource
|
||||
{
|
||||
public:
|
||||
virtual std::string serialize() = 0;
|
||||
virtual void deserialize(const std::string& data) = 0;
|
||||
};
|
||||
@@ -0,0 +1,142 @@
|
||||
#include "Resource.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
std::mutex Resource::mStringResource;
|
||||
const char* Resource::StringResources = "./Resources/string.res";
|
||||
|
||||
std::mutex Resource::mIntResource;
|
||||
const char* Resource::IntResources = "./Resources/int.res";
|
||||
|
||||
std::mutex Resource::mLongResource;
|
||||
const char* Resource::LongResources = "./Resources/long.res";
|
||||
|
||||
std::mutex Resource::mDoubleResource;
|
||||
const char* Resource::DoubleResources = "./Resources/double.res";
|
||||
|
||||
std::mutex Resource::mObjectResource;
|
||||
const char* Resource::ObjectResources = "./Resources/object.res";
|
||||
|
||||
std::string Resource::GetResourceData(const char* name, const char* file_name)
|
||||
{
|
||||
size_t size_name = strlen(name);
|
||||
std::ifstream file(file_name);
|
||||
if (!file.is_open())
|
||||
throw std::runtime_error("Can't open resource file");
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (line.find(name) == 0 && line[size_name] == ':')
|
||||
{
|
||||
size_t begin = size_name + 2;
|
||||
file.close();
|
||||
return line.substr(begin, line.length() - begin);
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
throw std::runtime_error("Can't find resource");
|
||||
}
|
||||
|
||||
void Resource::WriteResource(const char* name, const char* file_name, const std::string& value)
|
||||
{
|
||||
size_t size_name = strlen(name);
|
||||
bool isFind = false;
|
||||
std::string buffer;
|
||||
std::ifstream file(file_name);
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (line.find(name) == 0 && line[size_name] == ':')
|
||||
{
|
||||
isFind = true;
|
||||
line.resize(size_name + 2 + value.length());
|
||||
line.replace(size_name + 2, value.length(), value);
|
||||
}
|
||||
buffer += line + '\n';
|
||||
}
|
||||
file.close();
|
||||
|
||||
if (!isFind)
|
||||
{
|
||||
buffer += name;
|
||||
buffer += ": ";
|
||||
buffer += value;
|
||||
buffer += '\n';
|
||||
}
|
||||
|
||||
std::ofstream drop_file(file_name, std::ios::trunc);
|
||||
drop_file << buffer;
|
||||
drop_file.close();
|
||||
}
|
||||
|
||||
void Resource::SetString(const char* name, const char* value)
|
||||
{
|
||||
mStringResource.lock();
|
||||
WriteResource(name, StringResources, std::string(value));
|
||||
mStringResource.unlock();
|
||||
}
|
||||
|
||||
void Resource::SetInt(const char* name, int value)
|
||||
{
|
||||
mIntResource.lock();
|
||||
WriteResource(name, IntResources, std::to_string(value));
|
||||
mIntResource.unlock();
|
||||
}
|
||||
|
||||
void Resource::SetLong(const char* name, long long value)
|
||||
{
|
||||
mLongResource.lock();
|
||||
WriteResource(name, LongResources, std::to_string(value));
|
||||
mLongResource.unlock();
|
||||
}
|
||||
|
||||
void Resource::SetDouble(const char* name, double value)
|
||||
{
|
||||
mDoubleResource.lock();
|
||||
WriteResource(name, DoubleResources, std::to_string(value));
|
||||
mDoubleResource.unlock();
|
||||
}
|
||||
|
||||
void Resource::SetObject(const char* name, IResource* object)
|
||||
{
|
||||
mObjectResource.lock();
|
||||
WriteResource(name, ObjectResources, object->serialize());
|
||||
mObjectResource.unlock();
|
||||
}
|
||||
|
||||
std::string Resource::GetString(const char* name)
|
||||
{
|
||||
mStringResource.lock();
|
||||
std::string data = GetResourceData(name, StringResources);
|
||||
mStringResource.unlock();
|
||||
return data;
|
||||
}
|
||||
|
||||
int Resource::GetInt(const char* name)
|
||||
{
|
||||
mIntResource.lock();
|
||||
int data = std::stoi(GetResourceData(name, IntResources));
|
||||
mIntResource.unlock();
|
||||
return data;
|
||||
}
|
||||
|
||||
long long Resource::GetLong(const char* name)
|
||||
{
|
||||
mLongResource.lock();
|
||||
long long data = std::stoll(GetResourceData(name, LongResources));
|
||||
mLongResource.unlock();
|
||||
return data;
|
||||
}
|
||||
|
||||
double Resource::GetDouble(const char* name)
|
||||
{
|
||||
mDoubleResource.lock();
|
||||
double data = std::stod(GetResourceData(name, DoubleResources));
|
||||
mDoubleResource.unlock();
|
||||
return data;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user