This font manager is part of a game engine. It's supposed to mostly load fonts at the start, so I'm more worried about reading performance than loading performance. If there's any way of improving loading performance as well I'm happy to hear it.
- How would I optimize this font manager or managers in general, and what is the best practices for doing so?
- Is there any way of improving the use of the
unordered_map
container? - I'm also trying to write more modern C++ code. Is there anything that could be improved on that front as well?
Header file:
class FontManager {
public:
static FontManager &Instance() {
static FontManager singleton;
return singleton;
}
const Font *LoadFont(const std::string fileName);
void BatchLoadFonts(const std::vector<std::string>& infos);
const Font *GetFont(const std::string& fileName);
private:
FontManager() { }
~FontManager();
private:
std::unordered_map<std::string, const Font*> m_Fonts;
};
.cpp file:
const Font *FontManager::LoadFont(const std::string fileName) {
if (m_Fonts.count(fileName))
return m_Fonts.at(fileName);
m_Fonts.emplace(fileName, new Font(FONT_FOLDER_PATH + fileName));
return m_Fonts.at(fileName);
}
void FontManager::BatchLoadFonts(const std::vector<std::string>& infos) {
for (auto info : infos) {
LoadFont(info);
}
}
const Font *FontManager::GetFont(const std::string &fileName) {
if (m_Fonts.count(fileName)) {
return m_Fonts.at(fileName);
}
LoadFont(fileName);
return m_Fonts.at(fileName);
}
FontManager::~FontManager() {
for (auto const& f : m_Fonts) {
delete f.second;
}
}