Add copy and move constructors for SaveMap

This commit is contained in:
Jiga228
2025-09-15 12:45:25 +07:00
parent 4c2f1e9000
commit 9bf9d678f7
2 changed files with 55 additions and 10 deletions
+48 -7
View File
@@ -1,6 +1,5 @@
#include "SaveMap.h" #include "SaveMap.h"
#include <cstring>
#include <filesystem> #include <filesystem>
#include <stdexcept> #include <stdexcept>
#include "Game/ObjectFactory.h" #include "Game/ObjectFactory.h"
@@ -312,11 +311,53 @@ SaveMap::SaveMap(const std::string& json_data)
i++; i++;
std::string str = json_data.substr(parent_begin, parent_end - parent_begin + 1); std::string str = json_data.substr(parent_begin, parent_end - parent_begin + 1);
parent = std::make_shared<SaveMap>(str); parent_ = std::make_shared<SaveMap>(str);
} }
} }
} }
SaveMap::SaveMap(SaveMap& save_map):
class_name(save_map.class_name),
parent_(save_map.parent_),
save_long(save_map.save_long),
save_double(save_map.save_double),
save_string(save_map.save_string),
save_objects(save_map.save_objects),
save_vector_integer(save_map.save_vector_integer),
save_vector_double(save_map.save_vector_double),
save_vector_strings(save_map.save_vector_strings),
save_vector_objects(save_map.save_vector_objects),
save_list_integer(save_map.save_list_integer),
save_list_double(save_map.save_list_double),
save_list_strings(save_map.save_list_strings),
save_list_objects(save_map.save_list_objects)
{
}
SaveMap::SaveMap(SaveMap&& save_map) noexcept :
class_name(std::move(save_map.class_name)),
parent_(std::move(save_map.parent_)),
save_long(std::move(save_map.save_long)),
save_double(std::move(save_map.save_double)),
save_string(std::move(save_map.save_string)),
save_objects(std::move(save_map.save_objects)),
save_vector_integer(std::move(save_map.save_vector_integer)),
save_vector_double(std::move(save_map.save_vector_double)),
save_vector_strings(std::move(save_map.save_vector_strings)),
save_vector_objects(std::move(save_map.save_vector_objects)),
save_list_integer(std::move(save_map.save_list_integer)),
save_list_double(std::move(save_map.save_list_double)),
save_list_strings(std::move(save_map.save_list_strings)),
save_list_objects(std::move(save_map.save_list_objects))
{
}
std::shared_ptr<SaveMap> SaveMap::SaveInteger(const char* name, int value) noexcept std::shared_ptr<SaveMap> SaveMap::SaveInteger(const char* name, int value) noexcept
{ {
save_long[name] = value; save_long[name] = value;
@@ -569,19 +610,19 @@ std::string SaveMap::serialize() noexcept
buffer.replace(buffer.length() - 1, buffer.length() - 1, "]"); buffer.replace(buffer.length() - 1, buffer.length() - 1, "]");
} }
if (parent != nullptr) { if (parent_ != nullptr) {
buffer += ",\"Parent parameters\":" + parent->serialize() + '}'; buffer += ",\"Parent parameters\":" + parent_->serialize() + '}';
} else } else
buffer += '}'; buffer += '}';
return buffer; return buffer;
} }
SaveMap* SaveMap::connect_to(std::shared_ptr<SaveMap> parent) SaveMap* SaveMap::connect_to(const std::shared_ptr<SaveMap>& parent)
{ {
if (this->parent != nullptr) if (this->parent_ != nullptr)
throw std::runtime_error("Can't connect to second parent"); throw std::runtime_error("Can't connect to second parent");
this->parent = parent; this->parent_ = parent;
return this; return this;
} }
+7 -3
View File
@@ -11,7 +11,7 @@
class SaveMap final class SaveMap final
{ {
std::string class_name; std::string class_name;
std::shared_ptr<SaveMap> parent; std::shared_ptr<SaveMap> parent_;
// Индивидуальные значения // Индивидуальные значения
std::map<std::string, long long> save_long; std::map<std::string, long long> save_long;
@@ -38,6 +38,10 @@ public:
SaveMap(const char* class_name); SaveMap(const char* class_name);
// Токенезирут JSON строку // Токенезирут JSON строку
SaveMap(const std::string& json_data); SaveMap(const std::string& json_data);
// Коприрование
SaveMap(SaveMap& save_map);
// Перемещение
SaveMap(SaveMap&& save_map) noexcept;
// Методы созранения значений // Методы созранения значений
std::shared_ptr<SaveMap> SaveInteger(const char* name, int value) noexcept; std::shared_ptr<SaveMap> SaveInteger(const char* name, int value) noexcept;
@@ -67,11 +71,11 @@ public:
std::list<std::string>& GetListString(const char* name); std::list<std::string>& GetListString(const char* name);
std::list<ISave*>& GetListObject(const char* name); std::list<ISave*>& GetListObject(const char* name);
std::shared_ptr<SaveMap> getParent() const { return parent; } std::shared_ptr<SaveMap> getParent() const { return parent_; }
const std::string& getClassName() const { return class_name; } const std::string& getClassName() const { return class_name; }
// Собирает все токены в JSON объект // Собирает все токены в JSON объект
std::string serialize() noexcept; std::string serialize() noexcept;
SaveMap* connect_to(std::shared_ptr<SaveMap> parent); SaveMap* connect_to(const std::shared_ptr<SaveMap>& parent);
}; };