Skip to content

Commit

Permalink
refactor: replace RandomGenerator singleton with global instance
Browse files Browse the repository at this point in the history
- Remove singleton pattern (static get(), deleted constructors)
- Add global pointer instance gRandomGenerator
- Rename members to follow camelCase convention
  • Loading branch information
y0usaf committed Feb 6, 2025
1 parent f04fb59 commit 2d8c817
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
24 changes: 14 additions & 10 deletions src/helpers/ImagePicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@
std::string getRandomImageFromDirectory(const std::string &dirPath, const std::string &ignore) {
std::vector<std::filesystem::path> images;
for (const auto &entry : std::filesystem::directory_iterator(dirPath)) {
if (entry.is_regular_file()) {
auto ext = entry.path().extension().string();
if ((ext == ".png" || ext == ".jpg" || ext == ".jpeg" ||
ext == ".webp" || ext == ".jxl")) {
if (ignore.empty() || entry.path().string() != ignore) {
images.push_back(entry.path());
}
}
}
if (!entry.is_regular_file())
continue;

auto ext = entry.path().extension().string();
if (ext != ".png" && ext != ".jpg" && ext != ".jpeg" &&
ext != ".webp" && ext != ".jxl")
continue;

if (!ignore.empty() && entry.path().string() == ignore)
continue;

images.push_back(entry.path());
}

if (images.empty()) {
// If no alternative was found but the ignored file exists, return it.
if (!ignore.empty() && std::filesystem::exists(ignore))
return ignore;
return "";
}

// Note: Could replace shuffle with direct random selection as suggested:
// return images.at(randomInt(0, images.size() - 1));
std::shuffle(images.begin(), images.end(), CRandomGenerator::get().getGenerator());
return images.front().string();
}
29 changes: 11 additions & 18 deletions src/helpers/RandomGenerator.hpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
#pragma once
#include <random>
#include <memory>

class CRandomGenerator {
public:
static CRandomGenerator& get() {
static CRandomGenerator instance;
return instance;
}

CRandomGenerator() : mGenerator(mRandomDevice()) {}

// Generate random index for vector size
size_t getRandomIndex(size_t size) {
if (size == 0) return 0;
std::uniform_int_distribution<size_t> dis(0, size - 1);
return dis(m_Generator);
std::uniform_int_distribution<size_t> distribution(0, size - 1);
return distribution(mGenerator);
}

// Get raw generator if needed
std::mt19937& getGenerator() {
return m_Generator;
return mGenerator;
}

private:
CRandomGenerator() : m_Generator(m_RandomDevice()) {}

std::random_device m_RandomDevice;
std::mt19937 m_Generator;
std::random_device mRandomDevice;
std::mt19937 mGenerator;
};

// Delete copy/move to ensure singleton
CRandomGenerator(const CRandomGenerator&) = delete;
CRandomGenerator& operator=(const CRandomGenerator&) = delete;
CRandomGenerator(CRandomGenerator&&) = delete;
CRandomGenerator& operator=(CRandomGenerator&&) = delete;
};
// Global pointer to random generator
inline std::unique_ptr<CRandomGenerator> gRandomGenerator;

0 comments on commit 2d8c817

Please sign in to comment.