Update the module create calls to return unique_ptrs

develop
Stoyan Gaydarov 2018-07-08 20:48:39 -07:00
parent 6f90273bb6
commit 38cccdb0f4
6 changed files with 17 additions and 20 deletions

@ -2300,10 +2300,6 @@ int Core::Shutdown ( void )
plug_mgr = 0; plug_mgr = 0;
} }
// invalidate all modules // invalidate all modules
for(size_t i = 0 ; i < allModules.size(); i++)
{
delete allModules[i];
}
allModules.clear(); allModules.clear();
memset(&(s_mods), 0, sizeof(s_mods)); memset(&(s_mods), 0, sizeof(s_mods));
d.reset(); d.reset();
@ -2826,9 +2822,9 @@ TYPE * Core::get##TYPE() \
if(errorstate) return NULL;\ if(errorstate) return NULL;\
if(!s_mods.p##TYPE)\ if(!s_mods.p##TYPE)\
{\ {\
Module * mod = create##TYPE();\ std::unique_ptr<Module> mod = create##TYPE();\
s_mods.p##TYPE = (TYPE *) mod;\ s_mods.p##TYPE = (TYPE *) mod.get();\
allModules.push_back(mod);\ allModules.push_back(std::move(mod));\
}\ }\
return s_mods.p##TYPE;\ return s_mods.p##TYPE;\
} }

@ -243,7 +243,7 @@ namespace DFHack
Notes * pNotes; Notes * pNotes;
Graphic * pGraphic; Graphic * pGraphic;
} s_mods; } s_mods;
std::vector <Module *> allModules; std::vector<std::unique_ptr<Module>> allModules;
DFHack::PluginManager * plug_mgr; DFHack::PluginManager * plug_mgr;
std::vector<std::string> script_paths[2]; std::vector<std::string> script_paths[2];

@ -27,13 +27,13 @@ distribution.
#ifndef MODULE_FACTORY_H_INCLUDED #ifndef MODULE_FACTORY_H_INCLUDED
#define MODULE_FACTORY_H_INCLUDED #define MODULE_FACTORY_H_INCLUDED
#include <memory>
namespace DFHack namespace DFHack
{ {
class Module; class Module;
Module* createGui(); std::unique_ptr<Module> createMaterials();
Module* createWorld(); std::unique_ptr<Module> createNotes();
Module* createMaterials(); std::unique_ptr<Module> createGraphic();
Module* createNotes();
Module* createGraphic();
} }
#endif #endif

@ -36,14 +36,15 @@ using namespace std;
#include "Error.h" #include "Error.h"
#include "VersionInfo.h" #include "VersionInfo.h"
#include "MemAccess.h" #include "MemAccess.h"
#include "MiscUtils.h"
#include "ModuleFactory.h" #include "ModuleFactory.h"
#include "Core.h" #include "Core.h"
using namespace DFHack; using namespace DFHack;
Module* DFHack::createGraphic() std::unique_ptr<Module> DFHack::createGraphic()
{ {
return new Graphic(); return dts::make_unique<Graphic>();
} }
struct Graphic::Private struct Graphic::Private

@ -592,12 +592,11 @@ bool DFHack::isStoneInorganic(int material)
return true; return true;
} }
Module* DFHack::createMaterials() std::unique_ptr<Module> DFHack::createMaterials()
{ {
return new Materials(); return dts::make_unique<Materials>();
} }
Materials::Materials() Materials::Materials()
{ {
} }

@ -33,6 +33,7 @@ using namespace std;
#include "Types.h" #include "Types.h"
#include "Error.h" #include "Error.h"
#include "MemAccess.h" #include "MemAccess.h"
#include "MiscUtils.h"
#include "ModuleFactory.h" #include "ModuleFactory.h"
#include "Core.h" #include "Core.h"
#include "modules/Notes.h" #include "modules/Notes.h"
@ -40,9 +41,9 @@ using namespace std;
#include "df/ui.h" #include "df/ui.h"
using namespace DFHack; using namespace DFHack;
Module* DFHack::createNotes() std::unique_ptr<Module> DFHack::createNotes()
{ {
return new Notes(); return dts::make_unique<Notes>();
} }
// FIXME: not even a wrapper now // FIXME: not even a wrapper now