Skip to content

Commit

Permalink
Changed factory singleton to reference
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenvlek committed Mar 16, 2013
1 parent 74339a9 commit f3033b2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 34 deletions.
8 changes: 4 additions & 4 deletions Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 3 additions & 9 deletions Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include <ctime>
#include <iostream>

FactoryPtr Factory::m_Instance;

Factory::Factory()
: m_gen(boost::rand48(std::time(0))),
m_randPrimary(
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 1 addition & 9 deletions Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
#include <boost/random/variate_generator.hpp>


class Factory;
typedef boost::shared_ptr<Factory> FactoryPtr;

/**
* Abstract factory for producing Genes and ImageImps. This is a singleton class.
*/
Expand All @@ -35,7 +32,7 @@ class Factory
public:
virtual ~Factory();

static FactoryPtr Instance();
static Factory& Instance();

Gene makeRandomGene();

Expand Down Expand Up @@ -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.
*/
Expand Down
19 changes: 10 additions & 9 deletions Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

#include <iostream>

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()
Expand All @@ -37,10 +42,6 @@ unsigned int Image::getHeight()
return m_imp->getHeight();
}

Image::~Image()
{
delete m_imp;
}

void Image::loadFromFile(const std::string& filename)
{
Expand Down
6 changes: 3 additions & 3 deletions Organism.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions QtFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

QtFactory::QtFactory()
{
std::cout << "[ QtFactory::QtFactory() ] Using QtFactory" << std::endl;
}

QtFactory::~QtFactory()
Expand Down

0 comments on commit f3033b2

Please sign in to comment.