Skip to content

Commit

Permalink
use singleton macro wherever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 10, 2024
1 parent 1f5f8ee commit 45f83c8
Show file tree
Hide file tree
Showing 44 changed files with 155 additions and 156 deletions.
1 change: 1 addition & 0 deletions source/Base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_library(Base
Physics.cpp
Physics.h
Resources.h
Singleton.h
StringHelper.cpp
StringHelper.h
Vector2D.cpp
Expand Down
16 changes: 0 additions & 16 deletions source/Base/Definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,3 @@ inline uint8_t toUInt8(T const& value)
_##name = std::move(name); \
return *this; \
}

#define MAKE_SINGLETON(ClassName) \
public: \
static ClassName& get() \
{ \
static ClassName instance; \
return instance; \
} \
\
private: \
ClassName() = default; \
~ClassName() = default; \
ClassName(ClassName const&) = delete; \
ClassName& operator=(ClassName const&) = delete; \
ClassName(ClassName&&) = delete; \
ClassName& operator=(ClassName&&) = delete \
4 changes: 2 additions & 2 deletions source/Base/FileLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

_FileLogger::_FileLogger()
{
LoggingService::getInstance().registerCallBack(this);
LoggingService::get().registerCallBack(this);

std::remove(Const::LogFilename.c_str());
_outfile.open(Const::LogFilename, std::ios_base::app);
}

_FileLogger::~_FileLogger()
{
LoggingService::getInstance().unregisterCallBack(this);
LoggingService::get().unregisterCallBack(this);
}

void _FileLogger::newLogMessage(Priority priority, std::string const& message)
Expand Down
2 changes: 1 addition & 1 deletion source/Base/GlobalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct GlobalSettingsImpl
};


GlobalSettings& GlobalSettings::getInstance()
GlobalSettings& GlobalSettings::get()
{
static GlobalSettings instance;
return instance;
Expand Down
7 changes: 3 additions & 4 deletions source/Base/GlobalSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ struct GlobalSettingsImpl;
class GlobalSettings
{
public:
static GlobalSettings& getInstance();
static GlobalSettings& get();
GlobalSettings(GlobalSettings const&) = delete;
void operator=(GlobalSettings const&) = delete;

bool isDebugMode() const;
void setDebugMode(bool value) const;

GlobalSettings(GlobalSettings const&) = delete;
void operator=(GlobalSettings const&) = delete;

bool getBool(std::string const& key, bool defaultValue);
void setBool(std::string const& key, bool value);

Expand Down
6 changes: 0 additions & 6 deletions source/Base/LoggingService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
#include <sstream>
#include <algorithm>

LoggingService& LoggingService::getInstance()
{
static LoggingService instance;
return instance;
}

void LoggingService::log(Priority priority, std::string const& message)
{
std::lock_guard<std::mutex> lock(_mutex);
Expand Down
10 changes: 5 additions & 5 deletions source/Base/LoggingService.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <vector>
#include <mutex>

#include "Singleton.h"

enum class Priority
{
Unimportant,
Expand All @@ -18,22 +20,20 @@ class LoggingCallBack

class LoggingService
{
public:
static LoggingService& getInstance();
MAKE_SINGLETON(LoggingService);

public:
void log(Priority priority, std::string const& message);

void registerCallBack(LoggingCallBack* callback);
void unregisterCallBack(LoggingCallBack* callback);

private:
LoggingService() = default;

std::vector<LoggingCallBack*> _callbacks;
std::mutex _mutex;
};

inline void log(Priority priority, std::string const& message)
{
LoggingService::getInstance().log(priority, message);
LoggingService::get().log(priority, message);
}
2 changes: 1 addition & 1 deletion source/Base/NumberGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ NumberGenerator::~NumberGenerator()
{
}

NumberGenerator& NumberGenerator::getInstance()
NumberGenerator& NumberGenerator::get()
{
static NumberGenerator instance;
return instance;
Expand Down
8 changes: 3 additions & 5 deletions source/Base/NumberGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
class NumberGenerator
{
public:
static NumberGenerator& getInstance();
static NumberGenerator& get();
NumberGenerator(NumberGenerator const&) = delete;
void operator=(NumberGenerator const&) = delete;

uint32_t getRandomInt();
uint32_t getRandomInt(uint32_t range);
Expand All @@ -16,10 +18,6 @@ class NumberGenerator

uint64_t getId();

public:
NumberGenerator(NumberGenerator const&) = delete;
void operator=(NumberGenerator const&) = delete;

uint32_t getLargeRandomInt(uint32_t range);
uint32_t getNumberFromArray();

Expand Down
17 changes: 17 additions & 0 deletions source/Base/Singleton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#define MAKE_SINGLETON(ClassName) \
public: \
static ClassName& get() \
{ \
static ClassName instance; \
return instance; \
} \
\
private: \
ClassName() = default; \
~ClassName() = default; \
ClassName(ClassName const&) = delete; \
ClassName& operator=(ClassName const&) = delete; \
ClassName(ClassName&&) = delete; \
ClassName& operator=(ClassName&&) = delete\
4 changes: 2 additions & 2 deletions source/EngineImpl/DescriptionConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ void DescriptionConverter::addParticle(DataTO const& dataTO, ParticleDescription
auto particleIndex = (*dataTO.numParticles)++;

ParticleTO& particleTO = dataTO.particles[particleIndex];
particleTO.id = particleDesc.id == 0 ? NumberGenerator::getInstance().getId() : particleDesc.id;
particleTO.id = particleDesc.id == 0 ? NumberGenerator::get().getId() : particleDesc.id;
particleTO.pos = {particleDesc.pos.x, particleDesc.pos.y};
particleTO.vel = {particleDesc.vel.x, particleDesc.vel.y};
particleTO.energy = particleDesc.energy;
Expand All @@ -550,7 +550,7 @@ void DescriptionConverter::addCell(
{
int cellIndex = (*dataTO.numCells)++;
CellTO& cellTO = dataTO.cells[cellIndex];
cellTO.id = cellDesc.id == 0 ? NumberGenerator::getInstance().getId() : cellDesc.id;
cellTO.id = cellDesc.id == 0 ? NumberGenerator::get().getId() : cellDesc.id;
cellTO.pos= { cellDesc.pos.x, cellDesc.pos.y };
cellTO.vel = {cellDesc.vel.x, cellDesc.vel.y};
cellTO.energy = cellDesc.energy;
Expand Down
38 changes: 19 additions & 19 deletions source/EngineInterface/DescriptionEditService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
DataDescription DescriptionEditService::createRect(CreateRectParameters const& parameters)
{
DataDescription result;
auto creatureId = parameters._randomCreatureId ? toInt(NumberGenerator::getInstance().getRandomInt(std::numeric_limits<int>::max())) : 0;
auto creatureId = parameters._randomCreatureId ? toInt(NumberGenerator::get().getRandomInt(std::numeric_limits<int>::max())) : 0;
for (int i = 0; i < parameters._width; ++i) {
for (int j = 0; j < parameters._height; ++j) {
result.addCell(CellDescription()
.setId(NumberGenerator::getInstance().getId())
.setId(NumberGenerator::get().getId())
.setPos({toFloat(i) * parameters._cellDistance, toFloat(j) * parameters._cellDistance})
.setEnergy(parameters._energy)
.setStiffness(parameters._stiffness)
Expand All @@ -40,14 +40,14 @@ DataDescription DescriptionEditService::createRect(CreateRectParameters const& p
DataDescription DescriptionEditService::createHex(CreateHexParameters const& parameters)
{
DataDescription result;
auto creatureId = parameters._randomCreatureId ? toInt(NumberGenerator::getInstance().getRandomInt(std::numeric_limits<int>::max())) : 0;
auto creatureId = parameters._randomCreatureId ? toInt(NumberGenerator::get().getRandomInt(std::numeric_limits<int>::max())) : 0;
auto incY = sqrt(3.0) * parameters._cellDistance / 2.0;
for (int j = 0; j < parameters._layers; ++j) {
for (int i = -(parameters._layers - 1); i < parameters._layers - j; ++i) {

//create cell: upper layer
result.addCell(CellDescription()
.setId(NumberGenerator::getInstance().getId())
.setId(NumberGenerator::get().getId())
.setEnergy(parameters._energy)
.setStiffness(parameters._stiffness)
.setPos({toFloat(i * parameters._cellDistance + j * parameters._cellDistance / 2.0), toFloat(-j * incY)})
Expand All @@ -59,7 +59,7 @@ DataDescription DescriptionEditService::createHex(CreateHexParameters const& par
//create cell: under layer (except for 0-layer)
if (j > 0) {
result.addCell(CellDescription()
.setId(NumberGenerator::getInstance().getId())
.setId(NumberGenerator::get().getId())
.setEnergy(parameters._energy)
.setStiffness(parameters._stiffness)
.setPos({toFloat(i * parameters._cellDistance + j * parameters._cellDistance / 2.0), toFloat(j * incY)})
Expand All @@ -83,11 +83,11 @@ DataDescription DescriptionEditService::createHex(CreateHexParameters const& par
DataDescription DescriptionEditService::createUnconnectedCircle(CreateUnconnectedCircleParameters const& parameters)
{
DataDescription result;
auto creatureId = parameters._randomCreatureId ? toInt(NumberGenerator::getInstance().getRandomInt(std::numeric_limits<int>::max())) : 0;
auto creatureId = parameters._randomCreatureId ? toInt(NumberGenerator::get().getRandomInt(std::numeric_limits<int>::max())) : 0;

if (parameters._radius <= 1 + NEAR_ZERO) {
result.addCell(CellDescription()
.setId(NumberGenerator::getInstance().getId())
.setId(NumberGenerator::get().getId())
.setPos(parameters._center)
.setEnergy(parameters._energy)
.setStiffness(parameters._stiffness)
Expand All @@ -112,7 +112,7 @@ DataDescription DescriptionEditService::createUnconnectedCircle(CreateUnconnecte
continue;
}
result.addCell(CellDescription()
.setId(NumberGenerator::getInstance().getId())
.setId(NumberGenerator::get().getId())
.setEnergy(parameters._energy)
.setStiffness(parameters._stiffness)
.setPos({parameters._center.x + dxMod, parameters._center.y + dy})
Expand All @@ -130,7 +130,7 @@ namespace
{
void generateNewIds(DataDescription& data)
{
auto& numberGen = NumberGenerator::getInstance();
auto& numberGen = NumberGenerator::get();
std::unordered_map<uint64_t, uint64_t> newByOldIds;
for (auto& cell : data.cells) {
uint64_t newId = numberGen.getId();
Expand All @@ -147,7 +147,7 @@ namespace

void generateNewIds(ClusterDescription& cluster)
{
auto& numberGen = NumberGenerator::getInstance();
auto& numberGen = NumberGenerator::get();
//cluster.id = numberGen.getId();
std::unordered_map<uint64_t, uint64_t> newByOldIds;
for (auto& cell : cluster.cells) {
Expand Down Expand Up @@ -191,7 +191,7 @@ void DescriptionEditService::duplicate(ClusteredDataDescription& data, IntVector
auto origPos = particle.pos;
particle.pos = RealVector2D{origPos.x + incX, origPos.y + incY};
if (particle.pos.x < size.x && particle.pos.y < size.y) {
particle.setId(NumberGenerator::getInstance().getId());
particle.setId(NumberGenerator::get().getId());
result.addParticle(particle);
}
}
Expand Down Expand Up @@ -287,7 +287,7 @@ DataDescription DescriptionEditService::randomMultiply(
//do multiplication
DataDescription result = input;
generateNewIds(result);
auto& numberGen = NumberGenerator::getInstance();
auto& numberGen = NumberGenerator::get();
for (int i = 0; i < parameters._number; ++i) {
bool overlapping = false;
DataDescription copy;
Expand Down Expand Up @@ -421,7 +421,7 @@ void DescriptionEditService::correctConnections(ClusteredDataDescription& data,
void DescriptionEditService::randomizeCellColors(ClusteredDataDescription& data, std::vector<int> const& colorCodes)
{
for (auto& cluster : data.clusters) {
auto newColor = colorCodes[NumberGenerator::getInstance().getRandomInt(toInt(colorCodes.size()))];
auto newColor = colorCodes[NumberGenerator::get().getRandomInt(toInt(colorCodes.size()))];
for (auto& cell : cluster.cells) {
cell.color = newColor;
}
Expand All @@ -446,7 +446,7 @@ namespace
void DescriptionEditService::randomizeGenomeColors(ClusteredDataDescription& data, std::vector<int> const& colorCodes)
{
for (auto& cluster : data.clusters) {
auto newColor = colorCodes[NumberGenerator::getInstance().getRandomInt(toInt(colorCodes.size()))];
auto newColor = colorCodes[NumberGenerator::get().getRandomInt(toInt(colorCodes.size()))];
for (auto& cell : cluster.cells) {
if (cell.hasGenome()) {
colorizeGenomeNodes(cell.getGenomeRef(), newColor);
Expand All @@ -458,7 +458,7 @@ void DescriptionEditService::randomizeGenomeColors(ClusteredDataDescription& dat
void DescriptionEditService::randomizeEnergies(ClusteredDataDescription& data, float minEnergy, float maxEnergy)
{
for (auto& cluster : data.clusters) {
auto energy = NumberGenerator::getInstance().getRandomReal(toDouble(minEnergy), toDouble(maxEnergy));
auto energy = NumberGenerator::get().getRandomReal(toDouble(minEnergy), toDouble(maxEnergy));
for (auto& cell : cluster.cells) {
cell.energy = energy;
}
Expand All @@ -468,7 +468,7 @@ void DescriptionEditService::randomizeEnergies(ClusteredDataDescription& data, f
void DescriptionEditService::randomizeAges(ClusteredDataDescription& data, int minAge, int maxAge)
{
for (auto& cluster : data.clusters) {
auto age = NumberGenerator::getInstance().getRandomReal(toDouble(minAge), toDouble(maxAge));
auto age = NumberGenerator::get().getRandomReal(toDouble(minAge), toDouble(maxAge));
for (auto& cell : cluster.cells) {
cell.age = age;
}
Expand All @@ -478,7 +478,7 @@ void DescriptionEditService::randomizeAges(ClusteredDataDescription& data, int m
void DescriptionEditService::randomizeCountdowns(ClusteredDataDescription& data, int minValue, int maxValue)
{
for (auto& cluster : data.clusters) {
auto countdown = NumberGenerator::getInstance().getRandomReal(toDouble(minValue), toDouble(maxValue));
auto countdown = NumberGenerator::get().getRandomReal(toDouble(minValue), toDouble(maxValue));
for (auto& cell : cluster.cells) {
if (cell.getCellFunctionType() == CellFunction_Detonator) {
std::get<DetonatorDescription>(*cell.cellFunction).countdown = countdown;
Expand All @@ -490,7 +490,7 @@ void DescriptionEditService::randomizeCountdowns(ClusteredDataDescription& data,
void DescriptionEditService::randomizeMutationIds(ClusteredDataDescription& data)
{
for (auto& cluster : data.clusters) {
auto mutationId = NumberGenerator::getInstance().getRandomInt() % 65536;
auto mutationId = NumberGenerator::get().getRandomInt() % 65536;
for (auto& cell : cluster.cells) {
cell.mutationId = toInt(mutationId);
if (cell.getCellFunctionType() == CellFunction_Constructor) {
Expand Down Expand Up @@ -568,7 +568,7 @@ namespace
} else {
int newCreatureId = 0;
while (newCreatureId == 0) {
newCreatureId = NumberGenerator::getInstance().getRandomInt();
newCreatureId = NumberGenerator::get().getRandomInt();
}
origToNewCreatureIdMap.emplace(origCreatureId, newCreatureId);
return newCreatureId;
Expand Down
2 changes: 1 addition & 1 deletion source/EngineTests/ConstructorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ConstructorTests : public IntegrationTestFramework
std::vector<uint8_t> result;
result.reserve(size);
for (int i = 0; i < size; ++i) {
result.emplace_back(static_cast<uint8_t>(NumberGenerator::getInstance().getRandomInt(256)));
result.emplace_back(static_cast<uint8_t>(NumberGenerator::get().getRandomInt(256)));
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion source/EngineTests/DataTransferTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ TEST_F(DataTransferTests, cellCluster)

TEST_F(DataTransferTests, largeData)
{
auto& numberGen = NumberGenerator::getInstance();
auto& numberGen = NumberGenerator::get();
auto addCellAndParticles = [&](DataDescription& data) {
data.addCell(CellDescription()
.setId(numberGen.getId())
Expand Down
6 changes: 3 additions & 3 deletions source/Gui/AlienImGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1594,10 +1594,10 @@ void AlienImGui::NeuronSelection(
if (AlienImGui::Button("Randomize")) {
for (int i = 0; i < MAX_CHANNELS; ++i) {
for (int j = 0; j < MAX_CHANNELS; ++j) {
weights[i][j] = NumberGenerator::getInstance().getRandomFloat(-4.0f, 4.0f);
weights[i][j] = NumberGenerator::get().getRandomFloat(-4.0f, 4.0f);
}
biases[i] = NumberGenerator::getInstance().getRandomFloat(-4.0f, 4.0f);
activationFunctions[i] = NumberGenerator::getInstance().getRandomInt(NeuronActivationFunction_Count);
biases[i] = NumberGenerator::get().getRandomFloat(-4.0f, 4.0f);
activationFunctions[i] = NumberGenerator::get().getRandomInt(NeuronActivationFunction_Count);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/Gui/AlienWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ _AlienWindow::_AlienWindow(std::string const& title, std::string const& settings
: _title(title)
, _settingsNode(settingsNode)
{
_on = GlobalSettings::getInstance().getBool(settingsNode + ".active", defaultOn);
_on = GlobalSettings::get().getBool(settingsNode + ".active", defaultOn);
}

_AlienWindow::~_AlienWindow()
{
GlobalSettings::getInstance().setBool(_settingsNode + ".active", _on);
GlobalSettings::get().setBool(_settingsNode + ".active", _on);
}

void _AlienWindow::process()
Expand Down
Loading

0 comments on commit 45f83c8

Please sign in to comment.