Add copy and move constructors for SaveMap
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
#include "SaveMap.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
#include "Game/ObjectFactory.h"
|
||||
@@ -312,11 +311,53 @@ SaveMap::SaveMap(const std::string& json_data)
|
||||
i++;
|
||||
|
||||
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
|
||||
{
|
||||
save_long[name] = value;
|
||||
@@ -569,19 +610,19 @@ std::string SaveMap::serialize() noexcept
|
||||
buffer.replace(buffer.length() - 1, buffer.length() - 1, "]");
|
||||
}
|
||||
|
||||
if (parent != nullptr) {
|
||||
buffer += ",\"Parent parameters\":" + parent->serialize() + '}';
|
||||
if (parent_ != nullptr) {
|
||||
buffer += ",\"Parent parameters\":" + parent_->serialize() + '}';
|
||||
} else
|
||||
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");
|
||||
|
||||
this->parent = parent;
|
||||
this->parent_ = parent;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
class SaveMap final
|
||||
{
|
||||
std::string class_name;
|
||||
std::shared_ptr<SaveMap> parent;
|
||||
std::shared_ptr<SaveMap> parent_;
|
||||
|
||||
// Индивидуальные значения
|
||||
std::map<std::string, long long> save_long;
|
||||
@@ -38,6 +38,10 @@ public:
|
||||
SaveMap(const char* class_name);
|
||||
// Токенезирут JSON строку
|
||||
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;
|
||||
@@ -67,11 +71,11 @@ public:
|
||||
std::list<std::string>& GetListString(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; }
|
||||
|
||||
// Собирает все токены в JSON объект
|
||||
std::string serialize() noexcept;
|
||||
|
||||
SaveMap* connect_to(std::shared_ptr<SaveMap> parent);
|
||||
SaveMap* connect_to(const std::shared_ptr<SaveMap>& parent);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user