Get rid of some more manual memory management

master
Piotr Dziwinski 2015-06-26 00:24:05 +02:00
parent bed9c19fc8
commit 7e21d3bd79
6 changed files with 83 additions and 111 deletions

View File

@ -100,18 +100,12 @@ struct ApplicationPrivate
CApplication::CApplication() CApplication::CApplication()
: m_private(new ApplicationPrivate())
, m_eventQueue(new CEventQueue())
, m_profile(new CProfile())
, m_input(new CInput())
, m_pathManager(new CPathManager())
{ {
m_private = new ApplicationPrivate();
m_pathManager = new CPathManager();
m_eventQueue = new CEventQueue();
m_profile = new CProfile();
m_input = new CInput();
m_engine = nullptr;
m_device = nullptr;
m_controller = nullptr;
m_sound = nullptr;
m_exitCode = 0; m_exitCode = 0;
m_active = false; m_active = false;
m_debugModes = 0; m_debugModes = 0;
@ -164,21 +158,6 @@ CApplication::CApplication()
CApplication::~CApplication() CApplication::~CApplication()
{ {
delete m_private;
m_private = nullptr;
delete m_input;
m_input = nullptr;
delete m_eventQueue;
m_eventQueue = nullptr;
delete m_profile;
m_profile = nullptr;
delete m_pathManager;
m_pathManager = nullptr;
GetSystemUtils()->DestroyTimeStamp(m_baseTimeStamp); GetSystemUtils()->DestroyTimeStamp(m_baseTimeStamp);
GetSystemUtils()->DestroyTimeStamp(m_curTimeStamp); GetSystemUtils()->DestroyTimeStamp(m_curTimeStamp);
GetSystemUtils()->DestroyTimeStamp(m_lastTimeStamp); GetSystemUtils()->DestroyTimeStamp(m_lastTimeStamp);
@ -192,12 +171,12 @@ CApplication::~CApplication()
CEventQueue* CApplication::GetEventQueue() CEventQueue* CApplication::GetEventQueue()
{ {
return m_eventQueue; return m_eventQueue.get();
} }
CSoundInterface* CApplication::GetSound() CSoundInterface* CApplication::GetSound()
{ {
return m_sound; return m_sound.get();
} }
ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[]) ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
@ -399,7 +378,8 @@ bool CApplication::Create()
GetLogger()->Info("Creating CApplication\n"); GetLogger()->Info("Creating CApplication\n");
m_errorMessage = m_pathManager->VerifyPaths(); m_errorMessage = m_pathManager->VerifyPaths();
if(!m_errorMessage.empty()) { if (!m_errorMessage.empty())
{
m_exitCode = 1; m_exitCode = 1;
return false; return false;
} }
@ -425,10 +405,13 @@ bool CApplication::Create()
//Create the sound instance. //Create the sound instance.
#ifdef OPENAL_SOUND #ifdef OPENAL_SOUND
if(!m_headless) { if (!m_headless)
m_sound = static_cast<CSoundInterface *>(new ALSound()); {
} else { m_sound.reset(new ALSound());
m_sound = new CSoundInterface(); }
else
{
m_sound.reset(new CSoundInterface());
} }
#else #else
GetLogger()->Info("No sound support.\n"); GetLogger()->Info("No sound support.\n");
@ -474,7 +457,8 @@ bool CApplication::Create()
return false; return false;
} }
if(!m_headless) { if (!m_headless)
{
// load settings from profile // load settings from profile
int iValue; int iValue;
std::string sValue; std::string sValue;
@ -534,13 +518,13 @@ bool CApplication::Create()
if (m_device == nullptr) if (m_device == nullptr)
{ {
m_device = new Gfx::CNullDevice(); m_device.reset(new Gfx::CNullDevice());
GetLogger()->Error("Unknown graphics device: %s\n", m_graphics.c_str()); GetLogger()->Error("Unknown graphics device: %s\n", m_graphics.c_str());
} }
} }
else else
{ {
m_device = new Gfx::CNullDevice(); m_device.reset(new Gfx::CNullDevice());
} }
if (! m_device->Create() ) if (! m_device->Create() )
@ -551,9 +535,9 @@ bool CApplication::Create()
} }
// Create the 3D engine // Create the 3D engine
m_engine = new Gfx::CEngine(this); m_engine.reset(new Gfx::CEngine(this));
m_engine->SetDevice(m_device); m_engine->SetDevice(m_device.get());
if (! m_engine->Create() ) if (! m_engine->Create() )
{ {
@ -563,11 +547,12 @@ bool CApplication::Create()
} }
// Create the robot application. // Create the robot application.
m_controller = new CController(this, !defaultValues); m_controller.reset(new CController(this, !defaultValues));
if (m_runSceneName.empty()) if (m_runSceneName.empty())
m_controller->StartApp(); m_controller->StartApp();
else { else
{
m_controller->GetRobotMain()->ChangePhase(PHASE_USER); // To load userlevel list - TODO: this is ugly m_controller->GetRobotMain()->ChangePhase(PHASE_USER); // To load userlevel list - TODO: this is ugly
m_controller->GetRobotMain()->SetExitAfterMission(true); m_controller->GetRobotMain()->SetExitAfterMission(true);
m_controller->StartGame(m_runSceneName, m_runSceneRank/100, m_runSceneRank%100); m_controller->StartGame(m_runSceneName, m_runSceneRank/100, m_runSceneRank%100);
@ -631,26 +616,21 @@ void CApplication::Destroy()
{ {
m_joystickEnabled = false; m_joystickEnabled = false;
delete m_controller; m_controller.reset();
m_controller = nullptr; m_sound.reset();
delete m_sound;
m_sound = nullptr;
if (m_engine != nullptr) if (m_engine != nullptr)
{ {
m_engine->Destroy(); m_engine->Destroy();
delete m_engine; m_engine.reset();
m_engine = nullptr;
} }
if (m_device != nullptr) if (m_device != nullptr)
{ {
m_device->Destroy(); m_device->Destroy();
delete m_device; m_device.reset();
m_device = nullptr;
} }
if (m_private->joystick != nullptr) if (m_private->joystick != nullptr)

View File

@ -354,23 +354,23 @@ protected:
protected: protected:
//! Private (SDL-dependent data) //! Private (SDL-dependent data)
ApplicationPrivate* m_private; std::unique_ptr<ApplicationPrivate> m_private;
//! Global event queue //! Global event queue
CEventQueue* m_eventQueue; std::unique_ptr<CEventQueue> m_eventQueue;
//! Graphics engine //! Graphics engine
Gfx::CEngine* m_engine; std::unique_ptr<Gfx::CEngine> m_engine;
//! Graphics device //! Graphics device
Gfx::CDevice* m_device; std::unique_ptr<Gfx::CDevice> m_device;
//! Sound subsystem //! Sound subsystem
CSoundInterface* m_sound; std::unique_ptr<CSoundInterface> m_sound;
//! Game controller - game engine and UI //! Game controller - game engine and UI
CController* m_controller; std::unique_ptr<CController> m_controller;
//! Profile (INI) reader/writer //! Profile (INI) reader/writer
CProfile* m_profile; std::unique_ptr<CProfile> m_profile;
//! Input manager //! Input manager
CInput* m_input; std::unique_ptr<CInput> m_input;
//! Path manager //! Path manager
CPathManager* m_pathManager; std::unique_ptr<CPathManager> m_pathManager;
//! Code to return at exit //! Code to return at exit
int m_exitCode; int m_exitCode;

View File

@ -25,15 +25,12 @@
#include "ui/maindialog.h" #include "ui/maindialog.h"
template<> CController* CSingleton<CController>::m_instance = nullptr;
CController::CController(CApplication* app, bool loadProfile) CController::CController(CApplication* app, bool loadProfile)
: m_app(app)
, m_main(new CRobotMain(this))
, m_dialog(new Ui::CMainDialog())
{ {
m_app = app;
m_main = new CRobotMain(this);
m_dialog = new Ui::CMainDialog();
m_main->Create(loadProfile); m_main->Create(loadProfile);
m_dialog->Create(); m_dialog->Create();
if (!loadProfile) if (!loadProfile)
@ -44,13 +41,6 @@ CController::CController(CApplication* app, bool loadProfile)
CController::~CController() CController::~CController()
{ {
delete m_dialog;
m_dialog = nullptr;
delete m_main;
m_main = nullptr;
m_app = nullptr;
} }
CApplication* CController::GetApplication() CApplication* CController::GetApplication()
@ -60,12 +50,12 @@ CApplication* CController::GetApplication()
CRobotMain* CController::GetRobotMain() CRobotMain* CController::GetRobotMain()
{ {
return m_main; return m_main.get();
} }
Ui::CMainDialog* CController::GetMainDialog() Ui::CMainDialog* CController::GetMainDialog()
{ {
return m_dialog; return m_dialog.get();
} }
void CController::StartApp() void CController::StartApp()

View File

@ -24,11 +24,12 @@
#pragma once #pragma once
#include "common/event.h" #include <memory>
#include "common/singleton.h" #include <string>
class CApplication; class CApplication;
class CRobotMain; class CRobotMain;
struct Event;
namespace Ui { namespace Ui {
class CMainDialog; class CMainDialog;
} }
@ -37,7 +38,7 @@ class CMainDialog;
* \class CController * \class CController
* \brief Entry point into CRobotMain and CMainDialog * \brief Entry point into CRobotMain and CMainDialog
*/ */
class CController : public CSingleton<CController> class CController
{ {
public: public:
CController(CApplication* app, bool loadProfile = true); CController(CApplication* app, bool loadProfile = true);
@ -60,6 +61,6 @@ public:
private: private:
CApplication* m_app; CApplication* m_app;
CRobotMain* m_main; std::unique_ptr<CRobotMain> m_main;
Ui::CMainDialog* m_dialog; std::unique_ptr<Ui::CMainDialog> m_dialog;
}; };

View File

@ -39,21 +39,20 @@ FramebufferSupport DetectFramebufferSupport()
return FBS_NONE; return FBS_NONE;
} }
CDevice* CreateDevice(const DeviceConfig &config, const char *name) std::unique_ptr<CDevice> CreateDevice(const DeviceConfig &config, const std::string& name)
{ {
if (name == nullptr) return nullptr; if (name == "default") return std::unique_ptr<CDevice>{new CGLDevice(config)};
else if (std::strcmp(name, "default") == 0) return new CGLDevice(config); else if (name == "opengl") return std::unique_ptr<CDevice>{new CGLDevice(config)};
else if (std::strcmp(name, "opengl") == 0) return new CGLDevice(config); else if (name == "gl14") return std::unique_ptr<CDevice>{new CGLDevice(config)};
else if (std::strcmp(name, "gl14") == 0) return new CGLDevice(config); else if (name == "gl21") return std::unique_ptr<CDevice>{new CGL21Device(config)};
else if (std::strcmp(name, "gl21") == 0) return new CGL21Device(config); else if (name == "gl33") return std::unique_ptr<CDevice>{new CGL33Device(config)};
else if (std::strcmp(name, "gl33") == 0) return new CGL33Device(config); else if (name == "auto")
else if (std::strcmp(name, "auto") == 0)
{ {
int version = GetOpenGLVersion(); int version = GetOpenGLVersion();
if (version >= 33) return new CGL33Device(config); if (version >= 33) return std::unique_ptr<CDevice>{new CGL33Device(config)};
else if (version >= 21) return new CGL21Device(config); else if (version >= 21) return std::unique_ptr<CDevice>{new CGL21Device(config)};
else return new CGLDevice(config); else return std::unique_ptr<CDevice>{new CGLDevice(config)};
} }
return nullptr; return nullptr;

View File

@ -26,6 +26,8 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <memory>
// Graphics module namespace // Graphics module namespace
namespace Gfx namespace Gfx
@ -41,7 +43,7 @@ enum FramebufferSupport
FramebufferSupport DetectFramebufferSupport(); FramebufferSupport DetectFramebufferSupport();
//! Creates OpenGL device //! Creates OpenGL device
CDevice* CreateDevice(const DeviceConfig &config, const char *name); std::unique_ptr<CDevice> CreateDevice(const DeviceConfig &config, const std::string& name);
//! Returns OpenGL version as one number. //! Returns OpenGL version as one number.
// First digit is major part, second digit is minor part. // First digit is major part, second digit is minor part.