// 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(const std::string &key) { assertFalse(key.empty(), "Key cannot be empty."); if(!this->has(key)) return ""; return this->data[key]; } template<> void SaveFile::set( 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(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( const std::string &key, const int32_t value ) { this->set(key, std::to_string(value)); } // uint32_t template<> uint32_t SaveFile::get(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( const std::string &key, const uint32_t value ) { this->set(key, std::to_string(value)); } // int64_t template<> int64_t SaveFile::get(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( const std::string &key, const int64_t value ) { this->set(key, std::to_string(value)); } // uint64_t template<> uint64_t SaveFile::get(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( const std::string &key, const uint64_t value ) { this->set(key, std::to_string(value)); } // float_t template<> float_t SaveFile::get(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( const std::string &key, const float_t value ) { this->set(key, std::to_string(value)); } SaveFile::~SaveFile() { }