From f3033b22461ef7695f98e39c0e428b97cdde4ca1 Mon Sep 17 00:00:00 2001 From: Jeroen Vlek Date: Sat, 16 Mar 2013 16:18:44 +0100 Subject: [PATCH] Changed factory singleton to reference --- Config.cpp | 8 ++++---- Factory.cpp | 12 +++--------- Factory.h | 10 +--------- Image.cpp | 19 ++++++++++--------- Organism.cpp | 6 +++--- QtFactory.cpp | 1 + 6 files changed, 22 insertions(+), 34 deletions(-) diff --git a/Config.cpp b/Config.cpp index 2e6b9cf..f0934cb 100644 --- a/Config.cpp +++ b/Config.cpp @@ -88,25 +88,25 @@ void Config::SetPopulationSize(const unsigned int populationSize) void Config::SetHeight(const unsigned int Height) { m_Height = Height; - Factory::Instance()->update(); + Factory::Instance().update(); } void Config::SetMaxGeneSize(const unsigned int MaxGeneSize) { m_MaxGeneSize = MaxGeneSize; - Factory::Instance()->update(); + Factory::Instance().update(); } void Config::SetMinGeneSize(const unsigned int MinGeneSize) { m_MinGeneSize = MinGeneSize; - Factory::Instance()->update(); + Factory::Instance().update(); } void Config::SetWidth(const unsigned int Width) { m_Width = Width; - Factory::Instance()->update(); + Factory::Instance().update(); } std::string Config::GetTestImageName() diff --git a/Factory.cpp b/Factory.cpp index 421a321..0cef5ed 100644 --- a/Factory.cpp +++ b/Factory.cpp @@ -17,8 +17,6 @@ #include #include -FactoryPtr Factory::m_Instance; - Factory::Factory() : m_gen(boost::rand48(std::time(0))), m_randPrimary( @@ -65,15 +63,11 @@ Factory::~Factory() { delete m_randGenomeIndex; } -FactoryPtr Factory::Instance() { - if(Factory::m_Instance.get() == NULL) { +Factory& Factory::Instance() { #ifdef QT_YES - std::cout << "[ Factory::Instance() ] Using QtFactory" << std::endl; - Factory::m_Instance.reset(new QtFactory()); + static QtFactory factory; + return factory; #endif - } - - return Factory::m_Instance; } void Factory::update() { diff --git a/Factory.h b/Factory.h index 5956083..dde2930 100644 --- a/Factory.h +++ b/Factory.h @@ -24,9 +24,6 @@ #include -class Factory; -typedef boost::shared_ptr FactoryPtr; - /** * Abstract factory for producing Genes and ImageImps. This is a singleton class. */ @@ -35,7 +32,7 @@ class Factory public: virtual ~Factory(); - static FactoryPtr Instance(); + static Factory& Instance(); Gene makeRandomGene(); @@ -76,11 +73,6 @@ class Factory unsigned int randGeneSize(); private: - /** - * Pointer to only instance. - */ - static FactoryPtr m_Instance; - /** * Fastest random number generator in the boost library. */ diff --git a/Image.cpp b/Image.cpp index fae23bc..9e052e1 100644 --- a/Image.cpp +++ b/Image.cpp @@ -16,15 +16,20 @@ #include -void Image::clear() + +Image::Image() { - m_imp->clear(); + m_imp = Factory::Instance().makeImageImp(); } -Image::Image() +Image::~Image() +{ + delete m_imp; +} + +void Image::clear() { - FactoryPtr factory = Factory::Instance(); - m_imp = factory->makeImageImp(); + m_imp->clear(); } unsigned int Image::getWidth() @@ -37,10 +42,6 @@ unsigned int Image::getHeight() return m_imp->getHeight(); } -Image::~Image() -{ - delete m_imp; -} void Image::loadFromFile(const std::string& filename) { diff --git a/Organism.cpp b/Organism.cpp index b9a47bb..289cf8d 100644 --- a/Organism.cpp +++ b/Organism.cpp @@ -23,7 +23,7 @@ Organism::Organism(const unsigned int genomeLength) { m_genome.reserve(genomeLength); for(unsigned int i = 0; i < genomeLength; ++i) { - Gene randomGene = Factory::Instance()->makeRandomGene(); + Gene randomGene = Factory::Instance().makeRandomGene(); m_genome.push_back(randomGene); } createPhenotype(); @@ -44,8 +44,8 @@ Organism::Organism(const Organism& parentA, const Organism& parentB, const bool } if(doMutation) { - unsigned int index = Factory::Instance()->randGenomeIndex(); - Gene randomGene = Factory::Instance()->makeRandomGene(); + unsigned int index = Factory::Instance().randGenomeIndex(); + Gene randomGene = Factory::Instance().makeRandomGene(); m_genome[index] = randomGene; } diff --git a/QtFactory.cpp b/QtFactory.cpp index 733c3cb..25e7ebf 100644 --- a/QtFactory.cpp +++ b/QtFactory.cpp @@ -20,6 +20,7 @@ QtFactory::QtFactory() { + std::cout << "[ QtFactory::QtFactory() ] Using QtFactory" << std::endl; } QtFactory::~QtFactory()