From e32964bb5083e0050c2bdfde389f0bea92816f8c Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 31 Jul 2014 23:20:31 +0200 Subject: [PATCH] Made Font ownership explicit using unique_ptr --- src/font.cpp | 4 ++-- src/font.h | 12 +++++++++--- src/gmenu2x.cpp | 14 +------------- src/gmenu2x.h | 2 +- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/font.cpp b/src/font.cpp index 9f63ab2..02d7c23 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -14,9 +14,9 @@ using namespace std; -Font *Font::defaultFont() +unique_ptr Font::defaultFont() { - return new Font(TTF_FONT, TTF_FONT_SIZE); + return unique_ptr(new Font(TTF_FONT, TTF_FONT_SIZE)); } Font::Font(const std::string &path, unsigned int size) diff --git a/src/font.h b/src/font.h index dce6904..be09539 100644 --- a/src/font.h +++ b/src/font.h @@ -5,20 +5,26 @@ #define FONT_H #include +#include #include class Surface; +/** + * Wrapper around a TrueType or other FreeType-supported font. + * The wrapper is valid even if the font couldn't be loaded, but in that case + * nothing will be drawn. + */ class Font { public: enum HAlign { HAlignLeft, HAlignRight, HAlignCenter }; enum VAlign { VAlignTop, VAlignBottom, VAlignMiddle }; /** - * Returns a newly created Font object for the default font, - * or nullptr if there was a problem creating it. + * Returns a newly created Font object for the default font. */ - static Font *defaultFont(); + static std::unique_ptr defaultFont(); + Font(const std::string &path, unsigned int size); ~Font(); diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 5412978..df186bb 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -278,7 +278,6 @@ GMenu2X::~GMenu2X() { delete PowerSaver::getInstance(); quit(); - delete font; #ifdef ENABLE_INOTIFY delete monitor; #endif @@ -355,11 +354,6 @@ void GMenu2X::initBG() { } void GMenu2X::initFont() { - if (font) { - delete font; - font = NULL; - } - string path = skinConfStr["font"]; if (!path.empty()) { unsigned int size = skinConfInt["fontsize"]; @@ -367,16 +361,10 @@ void GMenu2X::initFont() { size = 12; if (path.substr(0,5)=="skin:") path = sc.getSkinFilePath(path.substr(5, path.length())); - font = new Font(path, size); + font.reset(new Font(path, size)); } else { font = Font::defaultFont(); } - - if (!font) { - ERROR("Cannot function without font; aborting...\n"); - quit(); - exit(-1); - } } void GMenu2X::initMenu() { diff --git a/src/gmenu2x.h b/src/gmenu2x.h index b883e6a..ca00e80 100644 --- a/src/gmenu2x.h +++ b/src/gmenu2x.h @@ -159,7 +159,7 @@ public: SurfaceCollection sc; Translator tr; Surface *s, *bg; - Font *font; + std::unique_ptr font; //Status functions void main();