Добавил shared_from_this как основной способ создать умный указатель на "себя". Добавил пример работы с ним

This commit is contained in:
Jiga228
2026-06-28 15:44:58 +07:00
parent 311772cca4
commit ad881a97a9
13 changed files with 77 additions and 47 deletions
+4 -4
View File
@@ -11,7 +11,7 @@ class World;
struct WorldFactory
{
const char* world_name;
World*(*factory)(GameInstance&);
std::shared_ptr<World>(*factory)(GameInstance&);
};
// Создаёт ассоцеативный список
@@ -20,8 +20,8 @@ struct WorldFactory
// Генерирует элемент списка
#define GENERATE_WORLD_FACTORY(Class, WorldName) WorldFactory{#WorldName,\
[](GameInstance& game_insance)->World* {\
Class* world = new Class(game_insance);\
[](GameInstance& game_insance)->std::shared_ptr<World> {\
std::shared_ptr<Class> world = std::make_shared<Class>(game_insance);\
std::ifstream config_file("./Worlds/" #WorldName ".world");\
if (config_file.fail())\
throw std::runtime_error("Can't open world file");\
@@ -32,7 +32,7 @@ struct WorldFactory
data.resize(file_size);\
config_file.read(data.data(), file_size);\
world->load(std::make_shared<SaveMap>(data));\
return world;\
return std::static_pointer_cast<World>(world);\
}},
/*