Convert BrickDatabase to namespace (#1142)

* Convert BrickDatabase to namespace

This did not need to be a class.

* Fix linker errors

* convert to anonymous namespace

so the cache is unmodifiable outside the brickcache

* Move to lower scope level and remove log
This commit is contained in:
David Markowitz
2023-07-17 15:55:25 -07:00
committed by GitHub
parent 455f9470a5
commit 080a833144
5 changed files with 19 additions and 34 deletions

View File

@@ -5,14 +5,12 @@
#include "Game.h"
#include "AssetManager.h"
#include "tinyxml2.h"
#include "Brick.h"
std::vector<Brick> BrickDatabase::emptyCache{};
BrickDatabase* BrickDatabase::m_Address = nullptr;
const BrickList& BrickDatabase::GetBricks(const LxfmlPath& lxfmlPath) {
static std::unordered_map<LxfmlPath, BrickList> m_Cache;
static const BrickList emptyCache;
BrickDatabase::BrickDatabase() = default;
BrickDatabase::~BrickDatabase() = default;
std::vector<Brick>& BrickDatabase::GetBricks(const std::string& lxfmlPath) {
const auto cached = m_Cache.find(lxfmlPath);
if (cached != m_Cache.end()) {
@@ -45,7 +43,7 @@ std::vector<Brick>& BrickDatabase::GetBricks(const std::string& lxfmlPath) {
return emptyCache;
}
std::vector<Brick> parts;
BrickList parts;
auto* lxfml = doc->FirstChildElement("LXFML");
auto* bricks = lxfml->FirstChildElement("Bricks");

View File

@@ -1,29 +1,16 @@
#ifndef __BRICKDATABASE__H__
#define __BRICKDATABASE__H__
#pragma once
#include "Entity.h"
class BrickDatabase
{
public:
static BrickDatabase* Instance() {
if (m_Address == nullptr) {
m_Address = new BrickDatabase();
}
class Brick;
using BrickList = std::vector<Brick>;
using LxfmlPath = std::string;
return m_Address;
}
std::vector<Brick>& GetBricks(const std::string& lxfmlPath);
explicit BrickDatabase();
~BrickDatabase();
private:
std::unordered_map<std::string, std::vector<Brick>> m_Cache;
static std::vector<Brick> emptyCache;
static BrickDatabase* m_Address; //For singleton method
/* data */
namespace BrickDatabase {
const BrickList& GetBricks(const LxfmlPath& lxfmlPath);
};
#endif //!__BRICKDATABASE__H__