Add MakeUnique template to avoid use of naked new

master
Piotr Dziwinski 2015-07-17 19:22:31 +02:00
parent 2c0baeec35
commit a872ea128b
15 changed files with 148 additions and 98 deletions

View File

@ -29,6 +29,7 @@
#include "common/image.h" #include "common/image.h"
#include "common/key.h" #include "common/key.h"
#include "common/pathman.h" #include "common/pathman.h"
#include "common/make_unique.h"
#include "common/stringutils.h" #include "common/stringutils.h"
#include "common/resources/resourcemanager.h" #include "common/resources/resourcemanager.h"
@ -99,11 +100,11 @@ struct ApplicationPrivate
CApplication::CApplication() CApplication::CApplication()
: m_private(new ApplicationPrivate()) : m_private(MakeUnique<ApplicationPrivate>())
, m_eventQueue(new CEventQueue()) , m_eventQueue(MakeUnique<CEventQueue>())
, m_profile(new CProfile()) , m_profile(MakeUnique<CProfile>())
, m_input(new CInput()) , m_input(MakeUnique<CInput>())
, m_pathManager(new CPathManager()) , m_pathManager(MakeUnique<CPathManager>())
{ {
m_exitCode = 0; m_exitCode = 0;
m_active = false; m_active = false;
@ -407,11 +408,11 @@ bool CApplication::Create()
#ifdef OPENAL_SOUND #ifdef OPENAL_SOUND
if (!m_headless) if (!m_headless)
{ {
m_sound.reset(new ALSound()); m_sound = MakeUnique<ALSound>();
} }
else else
{ {
m_sound.reset(new CSoundInterface()); m_sound = MakeUnique<CSoundInterface>();
} }
#else #else
GetLogger()->Info("No sound support.\n"); GetLogger()->Info("No sound support.\n");
@ -539,13 +540,13 @@ bool CApplication::Create()
if (m_device == nullptr) if (m_device == nullptr)
{ {
m_device.reset(new Gfx::CNullDevice()); m_device = MakeUnique<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.reset(new Gfx::CNullDevice()); m_device = MakeUnique<Gfx::CNullDevice>();
} }
if (! m_device->Create() ) if (! m_device->Create() )
@ -556,7 +557,7 @@ bool CApplication::Create()
} }
// Create the 3D engine // Create the 3D engine
m_engine.reset(new Gfx::CEngine(this)); m_engine = MakeUnique<Gfx::CEngine>(this);
m_engine->SetDevice(m_device.get()); m_engine->SetDevice(m_device.get());
@ -568,7 +569,7 @@ bool CApplication::Create()
} }
// Create the robot application. // Create the robot application.
m_controller.reset(new CController(this, !defaultValues)); m_controller = MakeUnique<CController>(this, !defaultValues);
if (m_runSceneName.empty()) if (m_runSceneName.empty())
m_controller->StartApp(); m_controller->StartApp();

32
src/common/make_unique.h Normal file
View File

@ -0,0 +1,32 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
#pragma once
#include <memory>
/**
* A template function to make std::unique_ptr without naked new
* It can be replaced with std::make_unique once we use C++14
*/
template<typename T, typename... Args>
inline std::unique_ptr<T> MakeUnique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

View File

@ -22,6 +22,7 @@
#include "common/config.h" #include "common/config.h"
#include "common/logger.h" #include "common/logger.h"
#include "common/make_unique.h"
#include <physfs.h> #include <physfs.h>
@ -109,12 +110,12 @@ std::string CResourceManager::GetSaveLocation()
std::unique_ptr<CSDLFileWrapper> CResourceManager::GetSDLFileHandler(const std::string &filename) std::unique_ptr<CSDLFileWrapper> CResourceManager::GetSDLFileHandler(const std::string &filename)
{ {
return std::unique_ptr<CSDLFileWrapper>(new CSDLFileWrapper(CleanPath(filename))); return MakeUnique<CSDLFileWrapper>(CleanPath(filename));
} }
std::unique_ptr<CSNDFileWrapper> CResourceManager::GetSNDFileHandler(const std::string &filename) std::unique_ptr<CSNDFileWrapper> CResourceManager::GetSNDFileHandler(const std::string &filename)
{ {
return std::unique_ptr<CSNDFileWrapper>(new CSNDFileWrapper(CleanPath(filename))); return MakeUnique<CSNDFileWrapper>(CleanPath(filename));
} }

View File

@ -26,6 +26,7 @@
#include "common/image.h" #include "common/image.h"
#include "common/key.h" #include "common/key.h"
#include "common/logger.h" #include "common/logger.h"
#include "common/make_unique.h"
#include "graphics/core/device.h" #include "graphics/core/device.h"
#include "graphics/engine/camera.h" #include "graphics/engine/camera.h"
@ -302,8 +303,8 @@ bool CEngine::Create()
m_size = m_app->GetVideoConfig().size; m_size = m_app->GetVideoConfig().size;
m_mouseSize = Math::Point(0.04f, 0.04f * (m_size.x / m_size.y)); m_mouseSize = Math::Point(0.04f, 0.04f * (m_size.x / m_size.y));
m_modelManager.reset(new COldModelManager(this)); m_modelManager = MakeUnique<COldModelManager>(this);
m_pyroManager.reset(new CPyroManager()); m_pyroManager = MakeUnique<CPyroManager>();
m_lightMan = new CLightManager(this); m_lightMan = new CLightManager(this);
m_text = new CText(this); m_text = new CText(this);
m_particle = new CParticle(this); m_particle = new CParticle(this);

View File

@ -27,6 +27,7 @@
#include "common/event.h" #include "common/event.h"
#include "common/global.h" #include "common/global.h"
#include "common/make_unique.h"
#include "graphics/core/color.h" #include "graphics/core/color.h"
#include "graphics/engine/pyro_type.h" #include "graphics/engine/pyro_type.h"
@ -81,13 +82,13 @@ class CPyro
protected: protected:
friend class CPyroManager; friend class CPyroManager;
CPyro();
//! Creates pyrotechnic effect //! Creates pyrotechnic effect
bool Create(PyroType type, CObject* obj, float force); bool Create(PyroType type, CObject* obj, float force);
//! Destroys the object //! Destroys the object
void DeleteObject(); void DeleteObject();
public: public:
CPyro(); // should only be called by CPyroManager
~CPyro(); ~CPyro();
//! Indicates whether the pyrotechnic effect is complete //! Indicates whether the pyrotechnic effect is complete

View File

@ -19,6 +19,8 @@
#include "graphics/engine/pyro_manager.h" #include "graphics/engine/pyro_manager.h"
#include "common/make_unique.h"
#include "graphics/engine/pyro.h" #include "graphics/engine/pyro.h"
namespace Gfx { namespace Gfx {
@ -32,7 +34,7 @@ CPyroManager::~CPyroManager()
void Gfx::CPyroManager::Create(PyroType type, CObject* obj, float force) void Gfx::CPyroManager::Create(PyroType type, CObject* obj, float force)
{ {
CPyroUPtr pyroUPtr{new CPyro()}; auto pyroUPtr = MakeUnique<CPyro>();
pyroUPtr->Create(type, obj, force); pyroUPtr->Create(type, obj, force);
m_pyros.insert(std::move(pyroUPtr)); m_pyros.insert(std::move(pyroUPtr));
} }

View File

@ -18,10 +18,14 @@
*/ */
#include "graphics/opengl/glutil.h" #include "graphics/opengl/glutil.h"
#include "common/logger.h"
#include "common/make_unique.h"
#include "graphics/opengl/gldevice.h" #include "graphics/opengl/gldevice.h"
#include "graphics/opengl/gl21device.h" #include "graphics/opengl/gl21device.h"
#include "graphics/opengl/gl33device.h" #include "graphics/opengl/gl33device.h"
#include "common/logger.h"
#include <physfs.h> #include <physfs.h>
#include <cstring> #include <cstring>
@ -41,18 +45,18 @@ FramebufferSupport DetectFramebufferSupport()
std::unique_ptr<CDevice> CreateDevice(const DeviceConfig &config, const std::string& name) std::unique_ptr<CDevice> CreateDevice(const DeviceConfig &config, const std::string& name)
{ {
if (name == "default") return std::unique_ptr<CDevice>{new CGLDevice(config)}; if (name == "default") return MakeUnique<CGLDevice>(config);
else if (name == "opengl") return std::unique_ptr<CDevice>{new CGLDevice(config)}; else if (name == "opengl") return MakeUnique<CGLDevice>(config);
else if (name == "gl14") return std::unique_ptr<CDevice>{new CGLDevice(config)}; else if (name == "gl14") return MakeUnique<CGLDevice>(config);
else if (name == "gl21") return std::unique_ptr<CDevice>{new CGL21Device(config)}; else if (name == "gl21") return MakeUnique<CGL21Device>(config);
else if (name == "gl33") return std::unique_ptr<CDevice>{new CGL33Device(config)}; else if (name == "gl33") return MakeUnique<CGL33Device>(config);
else if (name == "auto") else if (name == "auto")
{ {
int version = GetOpenGLVersion(); int version = GetOpenGLVersion();
if (version >= 33) return std::unique_ptr<CDevice>{new CGL33Device(config)}; if (version >= 33) return MakeUnique<CGL33Device>(config);
else if (version >= 21) return std::unique_ptr<CDevice>{new CGL21Device(config)}; else if (version >= 21) return MakeUnique<CGL21Device>(config);
else return std::unique_ptr<CDevice>{new CGLDevice(config)}; else return MakeUnique<CGLDevice>(config);
} }
return nullptr; return nullptr;

View File

@ -19,6 +19,8 @@
#include "object/object_factory.h" #include "object/object_factory.h"
#include "common/make_unique.h"
#include "graphics/engine/engine.h" #include "graphics/engine/engine.h"
#include "graphics/engine/oldmodelmanager.h" #include "graphics/engine/oldmodelmanager.h"
#include "graphics/engine/terrain.h" #include "graphics/engine/terrain.h"
@ -300,7 +302,7 @@ CObjectUPtr CObjectFactory::CreateBuilding(const ObjectCreateParams& params)
ObjectType type = params.type; ObjectType type = params.type;
float power = params.power; float power = params.power;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -1088,7 +1090,7 @@ CObjectUPtr CObjectFactory::CreateResource(const ObjectCreateParams& params)
ObjectType type = params.type; ObjectType type = params.type;
float power = params.power; float power = params.power;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -1207,7 +1209,7 @@ CObjectUPtr CObjectFactory::CreateFlag(const ObjectCreateParams& params)
float angle = params.angle; float angle = params.angle;
ObjectType type = params.type; ObjectType type = params.type;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -1268,7 +1270,7 @@ CObjectUPtr CObjectFactory::CreateBarrier(const ObjectCreateParams& params)
float height = params.height; float height = params.height;
ObjectType type = params.type; ObjectType type = params.type;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -1365,7 +1367,7 @@ CObjectUPtr CObjectFactory::CreatePlant(const ObjectCreateParams& params)
float height = params.height; float height = params.height;
ObjectType type = params.type; ObjectType type = params.type;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -1589,7 +1591,7 @@ CObjectUPtr CObjectFactory::CreateMushroom(const ObjectCreateParams& params)
float height = params.height; float height = params.height;
ObjectType type = params.type; ObjectType type = params.type;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -1647,7 +1649,7 @@ CObjectUPtr CObjectFactory::CreateQuartz(const ObjectCreateParams& params)
float height = params.height; float height = params.height;
ObjectType type = params.type; ObjectType type = params.type;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -1753,7 +1755,7 @@ CObjectUPtr CObjectFactory::CreateRoot(const ObjectCreateParams& params)
float height = params.height; float height = params.height;
ObjectType type = params.type; ObjectType type = params.type;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -1920,7 +1922,7 @@ CObjectUPtr CObjectFactory::CreateHome(const ObjectCreateParams& params)
float height = params.height; float height = params.height;
ObjectType type = params.type; ObjectType type = params.type;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -1960,7 +1962,7 @@ CObjectUPtr CObjectFactory::CreateRuin(const ObjectCreateParams& params)
float height = params.height; float height = params.height;
ObjectType type = params.type; ObjectType type = params.type;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -2375,7 +2377,7 @@ CObjectUPtr CObjectFactory::CreateApollo(const ObjectCreateParams& params)
float angle = params.angle; float angle = params.angle;
ObjectType type = params.type; ObjectType type = params.type;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
@ -2558,14 +2560,14 @@ CObjectUPtr CObjectFactory::CreateVehicle(const ObjectCreateParams& params)
bool toy = params.toy; bool toy = params.toy;
int option = params.option; int option = params.option;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
obj->SetOption(option); obj->SetOption(option);
if ( type == OBJECT_TOTO ) if ( type == OBJECT_TOTO )
{ {
std::unique_ptr<CMotion> motion{new CMotionToto(obj.get())}; auto motion = MakeUnique<CMotionToto>(obj.get());
motion->Create(pos, angle, type, 1.0f, m_oldModelManager); motion->Create(pos, angle, type, 1.0f, m_oldModelManager);
obj->SetMotion(std::move(motion)); obj->SetMotion(std::move(motion));
return std::move(obj); return std::move(obj);
@ -2614,22 +2616,22 @@ CObjectUPtr CObjectFactory::CreateVehicle(const ObjectCreateParams& params)
} }
obj->SetShowLimitRadius(showLimitRadius); obj->SetShowLimitRadius(showLimitRadius);
std::unique_ptr<CPhysics> physics{new CPhysics(obj.get())}; auto physics = MakeUnique<CPhysics>(obj.get());
std::unique_ptr<CBrain> brain{new CBrain(obj.get())}; auto brain = MakeUnique<CBrain>(obj.get());
std::unique_ptr<CMotion> motion;
std::unique_ptr<CMotion> motion;
if ( type == OBJECT_HUMAN || if ( type == OBJECT_HUMAN ||
type == OBJECT_TECH ) type == OBJECT_TECH )
{ {
motion.reset(new CMotionHuman(obj.get())); motion = MakeUnique<CMotionHuman>(obj.get());
} }
else if ( type == OBJECT_CONTROLLER ) else if ( type == OBJECT_CONTROLLER )
{ {
motion.reset(new CMotionLevelController(obj.get())); //dummy object motion = MakeUnique<CMotionLevelController>(obj.get()); //dummy object
} }
else else
{ {
motion.reset(new CMotionVehicle(obj.get())); motion = MakeUnique<CMotionVehicle>(obj.get());
} }
brain->SetMotion(motion.get()); brain->SetMotion(motion.get());
@ -2656,33 +2658,33 @@ CObjectUPtr CObjectFactory::CreateInsect(const ObjectCreateParams& params)
float angle = params.angle; float angle = params.angle;
ObjectType type = params.type; ObjectType type = params.type;
COldObjectUPtr obj{new COldObject(params.id)}; auto obj = MakeUnique<COldObject>(params.id);
obj->SetType(type); obj->SetType(type);
std::unique_ptr<CPhysics> physics{new CPhysics(obj.get())}; auto physics = MakeUnique<CPhysics>(obj.get());
std::unique_ptr<CBrain> brain{new CBrain(obj.get())}; auto brain = MakeUnique<CBrain>(obj.get());
std::unique_ptr<CMotion> motion; std::unique_ptr<CMotion> motion;
if ( type == OBJECT_MOTHER ) if ( type == OBJECT_MOTHER )
{ {
motion.reset(new CMotionQueen(obj.get())); motion = MakeUnique<CMotionQueen>(obj.get());
} }
if ( type == OBJECT_ANT ) if ( type == OBJECT_ANT )
{ {
motion.reset(new CMotionAnt(obj.get())); motion = MakeUnique<CMotionAnt>(obj.get());
} }
if ( type == OBJECT_SPIDER ) if ( type == OBJECT_SPIDER )
{ {
motion.reset(new CMotionSpider(obj.get())); motion = MakeUnique<CMotionSpider>(obj.get());
} }
if ( type == OBJECT_BEE ) if ( type == OBJECT_BEE )
{ {
motion.reset(new CMotionBee(obj.get())); motion = MakeUnique<CMotionBee>(obj.get());
} }
if ( type == OBJECT_WORM ) if ( type == OBJECT_WORM )
{ {
motion.reset(new CMotionWorm(obj.get())); motion = MakeUnique<CMotionWorm>(obj.get());
} }
assert(motion != nullptr); assert(motion != nullptr);
@ -2712,87 +2714,87 @@ void CObjectFactory::AddObjectAuto(COldObject* obj)
if ( type == OBJECT_BASE ) if ( type == OBJECT_BASE )
{ {
objAuto.reset(new CAutoBase(obj)); objAuto = MakeUnique<CAutoBase>(obj);
} }
if ( type == OBJECT_PORTICO ) if ( type == OBJECT_PORTICO )
{ {
objAuto.reset(new CAutoPortico(obj)); objAuto = MakeUnique<CAutoPortico>(obj);
} }
if ( type == OBJECT_DERRICK ) if ( type == OBJECT_DERRICK )
{ {
objAuto.reset(new CAutoDerrick(obj)); objAuto = MakeUnique<CAutoDerrick>(obj);
} }
if ( type == OBJECT_FACTORY ) if ( type == OBJECT_FACTORY )
{ {
objAuto.reset(new CAutoFactory(obj)); objAuto = MakeUnique<CAutoFactory>(obj);
} }
if ( type == OBJECT_REPAIR ) if ( type == OBJECT_REPAIR )
{ {
objAuto.reset(new CAutoRepair(obj)); objAuto = MakeUnique<CAutoRepair>(obj);
} }
if ( type == OBJECT_DESTROYER ) if ( type == OBJECT_DESTROYER )
{ {
objAuto.reset(new CAutoDestroyer(obj)); objAuto = MakeUnique<CAutoDestroyer>(obj);
} }
if ( type == OBJECT_STATION ) if ( type == OBJECT_STATION )
{ {
objAuto.reset(new CAutoPowerStation(obj)); objAuto = MakeUnique<CAutoPowerStation>(obj);
} }
if ( type == OBJECT_CONVERT ) if ( type == OBJECT_CONVERT )
{ {
objAuto.reset(new CAutoConvert(obj)); objAuto = MakeUnique<CAutoConvert>(obj);
} }
if ( type == OBJECT_TOWER ) if ( type == OBJECT_TOWER )
{ {
objAuto.reset(new CAutoTower(obj)); objAuto = MakeUnique<CAutoTower>(obj);
} }
if ( type == OBJECT_RESEARCH ) if ( type == OBJECT_RESEARCH )
{ {
objAuto.reset(new CAutoResearch(obj)); objAuto = MakeUnique<CAutoResearch>(obj);
} }
if ( type == OBJECT_RADAR ) if ( type == OBJECT_RADAR )
{ {
objAuto.reset(new CAutoRadar(obj)); objAuto = MakeUnique<CAutoRadar>(obj);
} }
if ( type == OBJECT_ENERGY ) if ( type == OBJECT_ENERGY )
{ {
objAuto.reset(new CAutoPowerPlant(obj)); objAuto = MakeUnique<CAutoPowerPlant>(obj);
} }
if ( type == OBJECT_LABO ) if ( type == OBJECT_LABO )
{ {
objAuto.reset(new CAutoLabo(obj)); objAuto = MakeUnique<CAutoLabo>(obj);
} }
if ( type == OBJECT_NUCLEAR ) if ( type == OBJECT_NUCLEAR )
{ {
objAuto.reset(new CAutoNuclearPlant(obj)); objAuto = MakeUnique<CAutoNuclearPlant>(obj);
} }
if ( type == OBJECT_PARA ) if ( type == OBJECT_PARA )
{ {
objAuto.reset(new CAutoPowerCaptor(obj)); objAuto = MakeUnique<CAutoPowerCaptor>(obj);
} }
if ( type == OBJECT_SAFE ) if ( type == OBJECT_SAFE )
{ {
objAuto.reset(new CAutoVault(obj)); objAuto = MakeUnique<CAutoVault>(obj);
} }
if ( type == OBJECT_HUSTON ) if ( type == OBJECT_HUSTON )
{ {
objAuto.reset(new CAutoHouston(obj)); objAuto = MakeUnique<CAutoHouston>(obj);
} }
if ( type == OBJECT_EGG ) if ( type == OBJECT_EGG )
{ {
objAuto.reset(new CAutoEgg(obj)); objAuto = MakeUnique<CAutoEgg>(obj);
} }
if ( type == OBJECT_NEST ) if ( type == OBJECT_NEST )
{ {
objAuto.reset(new CAutoNest(obj)); objAuto = MakeUnique<CAutoNest>(obj);
} }
if ( type == OBJECT_ROOT5 ) if ( type == OBJECT_ROOT5 )
{ {
objAuto.reset(new CAutoRoot(obj)); objAuto = MakeUnique<CAutoRoot>(obj);
} }
if ( type == OBJECT_MUSHROOM2 ) if ( type == OBJECT_MUSHROOM2 )
{ {
objAuto.reset(new CAutoMush(obj)); objAuto = MakeUnique<CAutoMush>(obj);
} }
if ( type == OBJECT_FLAGb || if ( type == OBJECT_FLAGb ||
type == OBJECT_FLAGr || type == OBJECT_FLAGr ||
@ -2800,7 +2802,7 @@ void CObjectFactory::AddObjectAuto(COldObject* obj)
type == OBJECT_FLAGy || type == OBJECT_FLAGy ||
type == OBJECT_FLAGv ) type == OBJECT_FLAGv )
{ {
objAuto.reset(new CAutoFlag(obj)); objAuto = MakeUnique<CAutoFlag>(obj);
} }
if (objAuto != nullptr) if (objAuto != nullptr)

View File

@ -25,6 +25,7 @@
#include "app/app.h" #include "app/app.h"
#include "common/global.h" #include "common/global.h"
#include "common/make_unique.h"
#include "common/restext.h" #include "common/restext.h"
#include "graphics/engine/lightman.h" #include "graphics/engine/lightman.h"
@ -2461,7 +2462,7 @@ bool COldObject::JostleObject(float force)
{ {
if ( m_auto != nullptr ) return false; if ( m_auto != nullptr ) return false;
std::unique_ptr<CAutoJostle> autoJostle{new CAutoJostle(this)}; auto autoJostle = MakeUnique<CAutoJostle>(this);
autoJostle->Start(0, force); autoJostle->Start(0, force);
m_auto = std::move(autoJostle); m_auto = std::move(autoJostle);
} }

View File

@ -26,6 +26,8 @@
#include "object/object.h" #include "object/object.h"
#include "common/make_unique.h"
#include "object/interface/carrier_object.h" #include "object/interface/carrier_object.h"
#include "object/interface/interactive_object.h" #include "object/interface/interactive_object.h"
#include "object/interface/jostleable_object.h" #include "object/interface/jostleable_object.h"
@ -67,9 +69,6 @@ class COldObject : public CObject,
friend class CObjectManager; friend class CObjectManager;
protected: protected:
COldObject(int id);
void DeleteObject(bool bAll=false); void DeleteObject(bool bAll=false);
void SetPhysics(std::unique_ptr<CPhysics> physics); void SetPhysics(std::unique_ptr<CPhysics> physics);
void SetBrain(std::unique_ptr<CBrain> brain); void SetBrain(std::unique_ptr<CBrain> brain);
@ -83,6 +82,7 @@ protected:
public: public:
COldObject(int id); // should only be called by CObjectFactory
~COldObject(); ~COldObject();
void Simplify() override; void Simplify() override;

View File

@ -31,6 +31,7 @@
#include "common/event.h" #include "common/event.h"
#include "common/global.h" #include "common/global.h"
#include "common/logger.h" #include "common/logger.h"
#include "common/make_unique.h"
#include "common/misc.h" #include "common/misc.h"
#include "common/profile.h" #include "common/profile.h"
#include "common/restext.h" #include "common/restext.h"
@ -133,7 +134,7 @@ CRobotMain::CRobotMain(CController* controller)
m_engine = nullptr; m_engine = nullptr;
m_oldModelManager = nullptr; m_oldModelManager = nullptr;
m_modelManager = std::unique_ptr<Gfx::CModelManager>(new Gfx::CModelManager()); m_modelManager = MakeUnique<Gfx::CModelManager>();
m_lightMan = nullptr; m_lightMan = nullptr;
m_particle = nullptr; m_particle = nullptr;
m_water = nullptr; m_water = nullptr;
@ -912,13 +913,13 @@ bool CRobotMain::ProcessEvent(Event &event)
CObject* obj = GetSelect(); CObject* obj = GetSelect();
if (obj != nullptr) if (obj != nullptr)
{ {
CLevelParserLine* line = new CLevelParserLine("CreateObject"); CLevelParserLine line("CreateObject");
line->AddParam("type", CLevelParserParamUPtr{new CLevelParserParam(obj->GetType())}); line.AddParam("type", CLevelParserParamUPtr{new CLevelParserParam(obj->GetType())});
line->AddParam("pos", CLevelParserParamUPtr{new CLevelParserParam(obj->GetPosition())}); line.AddParam("pos", CLevelParserParamUPtr{new CLevelParserParam(obj->GetPosition())});
line->AddParam("dir", CLevelParserParamUPtr{new CLevelParserParam(obj->GetRotationZ()/(Math::PI/180.0f))}); line.AddParam("dir", CLevelParserParamUPtr{new CLevelParserParam(obj->GetRotationZ()/(Math::PI/180.0f))});
std::stringstream ss; std::stringstream ss;
ss << *line; ss << line;
widgetSetClipboardText(ss.str().c_str()); widgetSetClipboardText(ss.str().c_str());
} }
} }
@ -3070,7 +3071,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
if (line->GetCommand() == "AudioChange" && !resetObject && m_controller == nullptr) if (line->GetCommand() == "AudioChange" && !resetObject && m_controller == nullptr)
{ {
auto audioChange = std::unique_ptr<CAudioChangeCondition>{new CAudioChangeCondition()}; auto audioChange = MakeUnique<CAudioChangeCondition>();
audioChange->Read(line.get()); audioChange->Read(line.get());
m_sound->CacheMusic(audioChange->music); m_sound->CacheMusic(audioChange->music);
m_audioChange.push_back(std::move(audioChange)); m_audioChange.push_back(std::move(audioChange));
@ -3823,7 +3824,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
if (line->GetCommand() == "EndMissionTake" && !resetObject && m_controller == nullptr) if (line->GetCommand() == "EndMissionTake" && !resetObject && m_controller == nullptr)
{ {
auto endTake = std::unique_ptr<CSceneEndCondition>{new CSceneEndCondition()}; auto endTake = MakeUnique<CSceneEndCondition>();
endTake->Read(line.get()); endTake->Read(line.get());
m_endTake.push_back(std::move(endTake)); m_endTake.push_back(std::move(endTake));
continue; continue;

View File

@ -19,6 +19,7 @@
#include "object/subclass/exchange_post.h" #include "object/subclass/exchange_post.h"
#include "common/make_unique.h"
#include "common/regex_utils.h" #include "common/regex_utils.h"
#include "graphics/engine/oldmodelmanager.h" #include "graphics/engine/oldmodelmanager.h"
@ -48,7 +49,7 @@ std::unique_ptr<CExchangePost> CExchangePost::Create(
Gfx::COldModelManager* modelManager, Gfx::COldModelManager* modelManager,
Gfx::CEngine* engine) Gfx::CEngine* engine)
{ {
std::unique_ptr<CExchangePost> obj{new CExchangePost(params.id)}; auto obj = MakeUnique<CExchangePost>(params.id);
int rank = engine->CreateObject(); int rank = engine->CreateObject();
engine->SetObjectType(rank, Gfx::ENG_OBJTYPE_FIX); // it is a stationary object engine->SetObjectType(rank, Gfx::ENG_OBJTYPE_FIX); // it is a stationary object
@ -95,7 +96,7 @@ std::unique_ptr<CExchangePost> CExchangePost::Create(
pos.y += params.height; pos.y += params.height;
obj->SetPosition(pos); // to display the shadows immediately obj->SetPosition(pos); // to display the shadows immediately
std::unique_ptr<CAutoInfo> objAuto{new CAutoInfo(obj.get())}; auto objAuto = MakeUnique<CAutoInfo>(obj.get());
objAuto->Init(); objAuto->Init();
obj->SetAuto(std::move(objAuto)); obj->SetAuto(std::move(objAuto));

View File

@ -19,6 +19,8 @@
#include "object/subclass/static_object.h" #include "object/subclass/static_object.h"
#include "common/make_unique.h"
#include "graphics/engine/terrain.h" #include "graphics/engine/terrain.h"
#include "graphics/model/model.h" #include "graphics/model/model.h"
#include "graphics/model/model_io_exception.h" #include "graphics/model/model_io_exception.h"
@ -135,7 +137,7 @@ CStaticObjectUPtr CStaticObject::Create(int id,
if (model.GetMeshCount() != 1 || model.GetMesh("main") == nullptr) if (model.GetMeshCount() != 1 || model.GetMesh("main") == nullptr)
throw CObjectCreateException("Unexpected mesh configuration", type, modelFile); throw CObjectCreateException("Unexpected mesh configuration", type, modelFile);
return CStaticObjectUPtr{new CStaticObject(id, type, modelFile, adjustedPosition, angleY, model, engine)}; return MakeUnique<CStaticObject>(id, type, modelFile, adjustedPosition, angleY, model, engine);
} }
catch (const Gfx::CModelIOException& e) catch (const Gfx::CModelIOException& e)
{ {

View File

@ -34,7 +34,7 @@ using CStaticObjectUPtr = std::unique_ptr<CStaticObject>;
class CStaticObject : public CObject class CStaticObject : public CObject
{ {
protected: public:
CStaticObject(int id, CStaticObject(int id,
ObjectType type, ObjectType type,
const std::string& key, const std::string& key,
@ -43,7 +43,6 @@ protected:
const Gfx::CModel& model, const Gfx::CModel& model,
Gfx::CEngine* engine); Gfx::CEngine* engine);
public:
virtual ~CStaticObject(); virtual ~CStaticObject();
void Read(CLevelParserLine* line) override; void Read(CLevelParserLine* line) override;

View File

@ -20,6 +20,8 @@
#include "sound/oalsound/alsound.h" #include "sound/oalsound/alsound.h"
#include "common/make_unique.h"
#include <algorithm> #include <algorithm>
#include <iomanip> #include <iomanip>
@ -143,7 +145,7 @@ int ALSound::GetMusicVolume()
bool ALSound::Cache(SoundType sound, const std::string &filename) bool ALSound::Cache(SoundType sound, const std::string &filename)
{ {
std::unique_ptr<Buffer> buffer{new Buffer()}; auto buffer = MakeUnique<Buffer>();
if (buffer->LoadFromFile(filename, sound)) if (buffer->LoadFromFile(filename, sound))
{ {
m_sounds[sound] = std::move(buffer); m_sounds[sound] = std::move(buffer);
@ -156,7 +158,7 @@ bool ALSound::CacheMusic(const std::string &filename)
{ {
if (m_music.find("music/"+filename) == m_music.end()) if (m_music.find("music/"+filename) == m_music.end())
{ {
std::unique_ptr<Buffer> buffer{new Buffer()}; auto buffer = MakeUnique<Buffer>();
if (buffer->LoadFromFile("music/"+filename, static_cast<SoundType>(-1))) if (buffer->LoadFromFile("music/"+filename, static_cast<SoundType>(-1)))
{ {
m_music["music/"+filename] = std::move(buffer); m_music["music/"+filename] = std::move(buffer);
@ -251,7 +253,7 @@ bool ALSound::SearchFreeBuffer(SoundType sound, int &channel, bool &alreadyLoade
// just add a new channel if we dont have any // just add a new channel if we dont have any
if (m_channels.size() == 0) if (m_channels.size() == 0)
{ {
std::unique_ptr<Channel> chn{new Channel()}; auto chn = MakeUnique<Channel>();
// check if we channel ready to play music, if not report error // check if we channel ready to play music, if not report error
if (chn->IsReady()) if (chn->IsReady())
{ {
@ -276,7 +278,7 @@ bool ALSound::SearchFreeBuffer(SoundType sound, int &channel, bool &alreadyLoade
{ {
if (m_channels.find(i) == m_channels.end()) if (m_channels.find(i) == m_channels.end())
{ {
std::unique_ptr<Channel> chn{new Channel()}; auto chn = MakeUnique<Channel>();
// check if channel is ready to play music, if not destroy it and seek free one // check if channel is ready to play music, if not destroy it and seek free one
if (chn->IsReady()) if (chn->IsReady())
{ {
@ -618,7 +620,7 @@ bool ALSound::PlayMusic(const std::string &filename, bool repeat, float fadeTime
return false; return false;
} */ } */
std::unique_ptr<Buffer> newBuffer{new Buffer()}; auto newBuffer = MakeUnique<Buffer>();
buffer = newBuffer.get(); buffer = newBuffer.get();
if (!newBuffer->LoadFromFile("music/"+filename, static_cast<SoundType>(-1))) if (!newBuffer->LoadFromFile("music/"+filename, static_cast<SoundType>(-1)))
{ {
@ -641,7 +643,7 @@ bool ALSound::PlayMusic(const std::string &filename, bool repeat, float fadeTime
m_oldMusic.push_back(std::move(old)); m_oldMusic.push_back(std::move(old));
} }
m_currentMusic.reset(new Channel()); m_currentMusic = MakeUnique<Channel>();
m_currentMusic->SetBuffer(buffer); m_currentMusic->SetBuffer(buffer);
m_currentMusic->SetVolume(m_musicVolume); m_currentMusic->SetVolume(m_musicVolume);
m_currentMusic->SetLoop(repeat); m_currentMusic->SetLoop(repeat);