Dawn/src/dawn/save/SaveFile.cpp

124 lines
2.6 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SaveFile.hpp"
#include "assert/assert.hpp"
using namespace Dawn;
SaveFile::SaveFile() {
}
bool_t SaveFile::doesSaveHaveChanges() {
return this->hasChanges;
}
bool_t SaveFile::has(const std::string &key) {
assertFalse(key.empty(), "Key cannot be empty.");
return this->data.find(key) != this->data.end();
}
// String (raw)
template<>
std::string SaveFile::get<std::string>(const std::string &key) {
assertFalse(key.empty(), "Key cannot be empty.");
if(!this->has(key)) return "";
return this->data[key];
}
template<>
void SaveFile::set<std::string>(
const std::string &key,
const std::string value
) {
assertFalse(key.empty(), "Key cannot be empty.");
this->data[key] = value;
this->hasChanges = true;
}
// int32_t
template<>
int32_t SaveFile::get<int32_t>(const std::string &key) {
assertFalse(key.empty(), "Key cannot be empty.");
if(!this->has(key)) return 0;
return std::stoi(this->data[key]);
}
template<>
void SaveFile::set<int32_t>(
const std::string &key,
const int32_t value
) {
this->set<std::string>(key, std::to_string(value));
}
// uint32_t
template<>
uint32_t SaveFile::get<uint32_t>(const std::string &key) {
assertFalse(key.empty(), "Key cannot be empty.");
if(!this->has(key)) return 0;
return std::stoul(this->data[key]);
}
template<>
void SaveFile::set<uint32_t>(
const std::string &key,
const uint32_t value
) {
this->set<std::string>(key, std::to_string(value));
}
// int64_t
template<>
int64_t SaveFile::get<int64_t>(const std::string &key) {
assertFalse(key.empty(), "Key cannot be empty.");
if(!this->has(key)) return 0;
return std::stoll(this->data[key]);
}
template<>
void SaveFile::set<int64_t>(
const std::string &key,
const int64_t value
) {
this->set<std::string>(key, std::to_string(value));
}
// uint64_t
template<>
uint64_t SaveFile::get<uint64_t>(const std::string &key) {
assertFalse(key.empty(), "Key cannot be empty.");
if(!this->has(key)) return 0;
return std::stoull(this->data[key]);
}
template<>
void SaveFile::set<uint64_t>(
const std::string &key,
const uint64_t value
) {
this->set<std::string>(key, std::to_string(value));
}
// float_t
template<>
float_t SaveFile::get<float_t>(const std::string &key) {
assertFalse(key.empty(), "Key cannot be empty.");
if(!this->has(key)) return 0.0f;
return std::stof(this->data[key]);
}
template<>
void SaveFile::set<float_t>(
const std::string &key,
const float_t value
) {
this->set<std::string>(key, std::to_string(value));
}
SaveFile::~SaveFile() {
}