Use std::vector<std::unique_ptr> to store objects in gd::Project

This commit is contained in:
Victor Levasseur
2016-09-18 13:45:45 +02:00
parent 2ff461f3b1
commit 1bf86461f0
4 changed files with 39 additions and 13 deletions

View File

@@ -457,8 +457,8 @@ private:
* \brief Functor testing layout name.
* \see gd::Layout
*/
struct LayoutHasName : public std::binary_function<std::shared_ptr<Layout>, gd::String, bool> {
bool operator()(const std::shared_ptr<Layout> & layout, gd::String name) const { return layout->GetName() == name; }
struct LayoutHasName : public std::binary_function<std::unique_ptr<Layout>, gd::String, bool> {
bool operator()(const std::unique_ptr<Layout> & layout, gd::String name) const { return layout->GetName() == name; }
};
/**

View File

@@ -37,6 +37,7 @@
#include "GDCore/Tools/VersionWrapper.h"
#include "GDCore/Tools/Log.h"
#include "GDCore/Tools/Localization.h"
#include "GDCore/Tools/PolymorphicClone.h"
#include "GDCore/Serialization/Serializer.h"
#include "GDCore/Serialization/Splitter.h"
#include "Project.h"
@@ -263,11 +264,11 @@ void Project::SwapLayouts(std::size_t first, std::size_t second)
gd::Layout & Project::InsertNewLayout(const gd::String & name, std::size_t position)
{
std::shared_ptr<gd::Layout> newScene = std::shared_ptr<gd::Layout>(new Layout);
std::unique_ptr<gd::Layout> newScene = std::unique_ptr<gd::Layout>(new Layout);
if (position<scenes.size())
scenes.insert(scenes.begin()+position, newScene);
scenes.insert(scenes.begin()+position, std::move(newScene));
else
scenes.push_back(newScene);
scenes.push_back(std::move(newScene));
newScene->SetName(name);
#if defined(GD_IDE_ONLY)
@@ -279,11 +280,11 @@ gd::Layout & Project::InsertNewLayout(const gd::String & name, std::size_t posit
gd::Layout & Project::InsertLayout(const gd::Layout & layout, std::size_t position)
{
std::shared_ptr<gd::Layout> newScene = std::shared_ptr<gd::Layout>(new Layout(layout));
std::unique_ptr<gd::Layout> newScene = std::unique_ptr<gd::Layout>(new Layout(layout));
if (position<scenes.size())
scenes.insert(scenes.begin()+position, newScene);
scenes.insert(scenes.begin()+position, std::move(newScene));
else
scenes.push_back(newScene);
scenes.push_back(std::move(newScene));
#if defined(GD_IDE_ONLY)
newScene->UpdateBehaviorsSharedData(*this);
@@ -294,7 +295,7 @@ gd::Layout & Project::InsertLayout(const gd::Layout & layout, std::size_t positi
void Project::RemoveLayout(const gd::String & name)
{
std::vector< std::shared_ptr<gd::Layout> >::iterator scene = find_if(scenes.begin(), scenes.end(), bind2nd(gd::LayoutHasName(), name));
std::vector< std::unique_ptr<gd::Layout> >::iterator scene = find_if(scenes.begin(), scenes.end(), bind2nd(gd::LayoutHasName(), name));
if ( scene == scenes.end() ) return;
scenes.erase(scene);
@@ -1037,9 +1038,7 @@ void Project::Init(const gd::Project & game)
for (std::size_t i =0;i<game.GetObjects().size();++i)
GetObjects().push_back( std::shared_ptr<gd::Object>(game.GetObjects()[i]->Clone()) );
scenes.clear();
for (std::size_t i =0;i<game.scenes.size();++i)
scenes.push_back( std::shared_ptr<gd::Layout>(new gd::Layout(*game.scenes[i])) );
scenes = gd::Clone(game.scenes);
#if defined(GD_IDE_ONLY)
externalEvents.clear();

View File

@@ -723,7 +723,7 @@ private:
int maxFPS; ///< Maximum Frame Per Seconds, -1 for unlimited
unsigned int minFPS; ///< Minimum Frame Per Seconds ( slow down game if FPS are below this number )
bool verticalSync; ///< If true, must activate vertical synchronization.
std::vector < std::shared_ptr<gd::Layout> > scenes; ///< List of all scenes
std::vector < std::unique_ptr<gd::Layout> > scenes; ///< List of all scenes
gd::VariablesContainer variables; ///< Initial global variables
std::vector < std::shared_ptr<gd::ExternalLayout> > externalLayouts; ///< List of all externals layouts
gd::ResourcesManager resourcesManager; ///< Contains all resources used by the project

View File

@@ -0,0 +1,27 @@
#ifndef GD_CORE_POLYMORPHICCLONE_H
#define GD_CORE_POLYMORPHICCLONE_H
#include <memory>
namespace gd
{
template<class T>
std::unique_ptr<T> Clone(const std::unique_ptr<T> & object)
{
return std::unique_ptr<T>(object->Clone());
}
template<class T>
std::vector<std::unique_ptr<T>> Clone(const std::vector<std::unique_ptr<T>> & vector)
{
std::vector<std::unique_ptr<T>> copy;
for(const auto & element : vector)
copy.push_back(gd::Clone(element));
return copy;
}
}
#endif