From 4222d8a297ce2fe897261366c0dcb3d14d1eecbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 7 Jul 2013 01:07:52 +0200 Subject: [PATCH] Platform: pass application configuration via reference, not pointer. Removes the need for explicit new/delete calls. --- src/Platform/AbstractXApplication.cpp | 21 ++++---- src/Platform/AbstractXApplication.h | 28 ++++++----- src/Platform/GlutApplication.cpp | 26 +++++----- src/Platform/GlutApplication.h | 56 +++++++++++----------- src/Platform/GlxApplication.h | 8 ++-- src/Platform/NaClApplication.cpp | 30 +++++++----- src/Platform/NaClApplication.h | 42 +++++++--------- src/Platform/Sdl2Application.cpp | 36 +++++++------- src/Platform/Sdl2Application.h | 40 +++++++++------- src/Platform/WindowlessGlxApplication.cpp | 15 +++--- src/Platform/WindowlessGlxApplication.h | 14 ++++-- src/Platform/WindowlessNaClApplication.cpp | 22 +++++---- src/Platform/WindowlessNaClApplication.h | 30 +++++------- src/Platform/XEglApplication.h | 8 ++-- 14 files changed, 201 insertions(+), 175 deletions(-) diff --git a/src/Platform/AbstractXApplication.cpp b/src/Platform/AbstractXApplication.cpp index 6a38ad9ec..8a7a1217a 100644 --- a/src/Platform/AbstractXApplication.cpp +++ b/src/Platform/AbstractXApplication.cpp @@ -36,18 +36,22 @@ namespace Magnum { namespace Platform { -AbstractXApplication::AbstractXApplication(AbstractContextHandler* contextHandler, const Arguments&): contextHandler(contextHandler), c(nullptr), flags(Flag::Redraw) { - createContext(new Configuration); +/** @todo Delegating constructor when support for GCC 4.6 is dropped */ + +AbstractXApplication::AbstractXApplication(AbstractContextHandler* contextHandler, const Arguments&, const Configuration& configuration): contextHandler(contextHandler), c(nullptr), flags(Flag::Redraw) { + createContext(configuration); } -AbstractXApplication::AbstractXApplication(AbstractContextHandler* contextHandler, const Arguments&, Configuration* configuration): contextHandler(contextHandler), c(nullptr), flags(Flag::Redraw) { - if(configuration) createContext(configuration); +AbstractXApplication::AbstractXApplication(AbstractContextHandler* contextHandler, const Arguments&): contextHandler(contextHandler), c(nullptr), flags(Flag::Redraw) { + createContext({}); } -void AbstractXApplication::createContext(AbstractXApplication::Configuration* configuration) { +AbstractXApplication::AbstractXApplication(AbstractContextHandler* contextHandler, const Arguments&, std::nullptr_t): contextHandler(contextHandler), c(nullptr), flags(Flag::Redraw) {} + +void AbstractXApplication::createContext(const Configuration& configuration) { CORRADE_ASSERT(!c, "AbstractXApplication::createContext(): context already created", ); - viewportSize = configuration->size(); + viewportSize = configuration.size(); /* Get default X display */ display = XOpenDisplay(nullptr); @@ -73,8 +77,8 @@ void AbstractXApplication::createContext(AbstractXApplication::Configuration* co attr.colormap = XCreateColormap(display, root, visInfo->visual, AllocNone); attr.event_mask = 0; unsigned long mask = CWBackPixel|CWBorderPixel|CWColormap|CWEventMask; - window = XCreateWindow(display, root, 20, 20, configuration->size().x(), configuration->size().y(), 0, visInfo->depth, InputOutput, visInfo->visual, mask, &attr); - XSetStandardProperties(display, window, configuration->title().c_str(), nullptr, None, nullptr, 0, nullptr); + window = XCreateWindow(display, root, 20, 20, configuration.size().x(), configuration.size().y(), 0, visInfo->depth, InputOutput, visInfo->visual, mask, &attr); + XSetStandardProperties(display, window, configuration.title().data(), nullptr, None, nullptr, 0, nullptr); XFree(visInfo); /* Be notified about closing the window */ @@ -94,7 +98,6 @@ void AbstractXApplication::createContext(AbstractXApplication::Configuration* co ExtensionWrangler::initialize(contextHandler->experimentalExtensionWranglerFeatures()); c = new Context; - delete configuration; } AbstractXApplication::~AbstractXApplication() { diff --git a/src/Platform/AbstractXApplication.h b/src/Platform/AbstractXApplication.h index ba5bb5680..bf9b46a29 100644 --- a/src/Platform/AbstractXApplication.h +++ b/src/Platform/AbstractXApplication.h @@ -73,10 +73,17 @@ class AbstractXApplication { * @param contextHandler OpenGL context handler * @param arguments Application arguments * - * Creates application with default configuration. See Configuration - * for more information. + * Creates application with default or user-specified configuration. + * See Configuration for more information. The program exits if the + * context cannot be created, see below for an alternative. */ + #ifdef DOXYGEN_GENERATING_OUTPUT + explicit AbstractXApplication(AbstractContextHandler* contextHandler, const Arguments& arguments, const Configuration& configuration = Configuration()); + #else + /* To avoid "invalid use of incomplete type" */ + explicit AbstractXApplication(AbstractContextHandler* contextHandler, const Arguments& arguments, const Configuration& configuration); explicit AbstractXApplication(AbstractContextHandler* contextHandler, const Arguments& arguments); + #endif /** * @brief Constructor @@ -84,11 +91,10 @@ class AbstractXApplication { * @param arguments Application arguments * @param configuration Configuration * - * The @p configuration is deleted afterwards. If `nullptr` is passed - * as @p configuration, the context is not created and must be created - * with createContext(). + * Unlike above, the context is not created and must be created later + * with createContext() or tryCreateContext(). */ - explicit AbstractXApplication(AbstractContextHandler* contextHandler, const Arguments& arguments, Configuration* configuration); + explicit AbstractXApplication(AbstractContextHandler* contextHandler, const Arguments& arguments, std::nullptr_t); /** * @brief Execute main loop @@ -110,7 +116,7 @@ class AbstractXApplication { ~AbstractXApplication(); /** @copydoc GlutApplication::createContext() */ - void createContext(Configuration* configuration); + void createContext(const Configuration& configuration); /** @{ @name Drawing functions */ @@ -202,9 +208,9 @@ class AbstractXApplication::Configuration { * * Default is `"Magnum X Application"`. */ - Configuration* setTitle(std::string title) { + Configuration& setTitle(std::string title) { _title = std::move(title); - return this; + return *this; } /** @brief Window size */ @@ -216,9 +222,9 @@ class AbstractXApplication::Configuration { * * Default is `{800, 600}`. */ - Configuration* setSize(const Vector2i& size) { + Configuration& setSize(const Vector2i& size) { _size = size; - return this; + return *this; } private: diff --git a/src/Platform/GlutApplication.cpp b/src/Platform/GlutApplication.cpp index 65ea0566c..fb92c6c3e 100644 --- a/src/Platform/GlutApplication.cpp +++ b/src/Platform/GlutApplication.cpp @@ -31,14 +31,20 @@ namespace Magnum { namespace Platform { GlutApplication* GlutApplication::instance = nullptr; +/** @todo Delegating constructor when support for GCC 4.6 is dropped */ + +GlutApplication::GlutApplication(const Arguments& arguments, const Configuration& configuration): c(nullptr) { + initialize(arguments.argc, arguments.argv); + createContext(configuration); +} + GlutApplication::GlutApplication(const Arguments& arguments): c(nullptr) { initialize(arguments.argc, arguments.argv); - createContext(new Configuration); + createContext({}); } -GlutApplication::GlutApplication(const Arguments& arguments, Configuration* configuration): c(nullptr) { +GlutApplication::GlutApplication(const Arguments& arguments, std::nullptr_t): c(nullptr) { initialize(arguments.argc, arguments.argv); - if(configuration) createContext(configuration); } void GlutApplication::initialize(int& argc, char** argv) { @@ -50,26 +56,24 @@ void GlutApplication::initialize(int& argc, char** argv) { glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION); } -void GlutApplication::createContext(Configuration* configuration) { +void GlutApplication::createContext(const Configuration& configuration) { if(!tryCreateContext(configuration)) { Error() << "Platform::GlutApplication::createContext(): cannot create context"; - delete configuration; std::exit(1); - - } else delete configuration; + } } -bool GlutApplication::tryCreateContext(Configuration* configuration) { +bool GlutApplication::tryCreateContext(const Configuration& configuration) { CORRADE_ASSERT(!c, "Platform::GlutApplication::tryCreateContext(): context already created", false); unsigned int flags = GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL; /* Multisampling */ - if(configuration->sampleCount()) flags |= GLUT_MULTISAMPLE; + if(configuration.sampleCount()) flags |= GLUT_MULTISAMPLE; glutInitDisplayMode(flags); - glutInitWindowSize(configuration->size().x(), configuration->size().y()); - if(!glutCreateWindow(configuration->title().c_str())) + glutInitWindowSize(configuration.size().x(), configuration.size().y()); + if(!glutCreateWindow(configuration.title().data())) return false; glutReshapeFunc(staticViewportEvent); glutSpecialFunc(staticKeyEvent); diff --git a/src/Platform/GlutApplication.h b/src/Platform/GlutApplication.h index 9f01f6053..e037a849b 100644 --- a/src/Platform/GlutApplication.h +++ b/src/Platform/GlutApplication.h @@ -82,24 +82,28 @@ class GlutApplication { /** * @brief Default constructor * @param arguments Application arguments + * @param configuration %Configuration * - * Creates application with default configuration. See Configuration - * for more information. The program exits if the context cannot be - * created, see tryCreateContext() for an alternative. + * Creates application with default or user-specified configuration. + * See Configuration for more information. The program exits if the + * context cannot be created, see below for an alternative. */ + #ifdef DOXYGEN_GENERATING_OUTPUT + explicit GlutApplication(const Arguments& arguments, const Configuration& configuration = Configuration()); + #else + /* To avoid "invalid use of incomplete type" */ + explicit GlutApplication(const Arguments& arguments, const Configuration& configuration); explicit GlutApplication(const Arguments& arguments); + #endif /** * @brief Constructor * @param arguments Application arguments - * @param configuration Configuration * - * The @p configuration is deleted afterwards. If `nullptr` is passed - * as @p configuration, the context is not created and must be created - * with createContext(). The program exits if the context cannot be - * created, see tryCreateContext() for an alternative. + * Unlike above, the context is not created and must be created later + * with createContext() or tryCreateContext(). */ - explicit GlutApplication(const Arguments& arguments, Configuration* configuration); + explicit GlutApplication(const Arguments& arguments, std::nullptr_t); /** * @brief Execute main loop @@ -118,21 +122,19 @@ class GlutApplication { /** * @brief Create context with given configuration * - * The @p configuration is deleted afterwards. Must be called if and - * only if the context wasn't created by the constructor itself. The - * program exits if the context cannot be created, see tryCreateContext() - * for an alternative. + * Must be called if and only if the context wasn't created by the + * constructor itself. The program exits if the context cannot be + * created, see tryCreateContext() for an alternative. */ - void createContext(Configuration* configuration); + void createContext(const Configuration& configuration); /** * @brief Try to create context with given configuration * - * Unlike createContext() the @p configuration is *not* deleted - * afterwards. Returns `false` if the context cannot be created, `true` - * otherwise. + * Unlike createContext() returns `false` if the context cannot be + * created, `true` otherwise. */ - bool tryCreateContext(Configuration* configuration); + bool tryCreateContext(const Configuration& configuration); /** @{ @name Drawing functions */ @@ -288,13 +290,13 @@ class GlutApplication::Configuration { /** * @brief Set window title - * @return Pointer to self (for method chaining) + * @return Reference to self (for method chaining) * * Default is `"Magnum GLUT Application"`. */ - Configuration* setTitle(std::string title) { + Configuration& setTitle(std::string title) { _title = std::move(title); - return this; + return *this; } /** @brief Window size */ @@ -302,13 +304,13 @@ class GlutApplication::Configuration { /** * @brief Set window size - * @return Pointer to self (for method chaining) + * @return Reference to self (for method chaining) * * Default is `{800, 600}`. */ - Configuration* setSize(const Vector2i& size) { + Configuration& setSize(const Vector2i& size) { _size = size; - return this; + return *this; } /** @brief Sample count */ @@ -316,15 +318,15 @@ class GlutApplication::Configuration { /** * @brief Set sample count - * @return Pointer to self (for method chaining) + * @return Reference to self (for method chaining) * * Default is `0`, thus no multisampling. The actual sample count is * ignored, GLUT either enables it or disables. See also * @ref Renderer::Feature "Renderer::Feature::Multisampling". */ - Configuration* setSampleCount(Int count) { + Configuration& setSampleCount(Int count) { _sampleCount = count; - return this; + return *this; } private: diff --git a/src/Platform/GlxApplication.h b/src/Platform/GlxApplication.h index d48975e01..b3aeb562a 100644 --- a/src/Platform/GlxApplication.h +++ b/src/Platform/GlxApplication.h @@ -56,11 +56,11 @@ to simplify porting. */ class GlxApplication: public AbstractXApplication { public: - /** @copydoc GlutApplication::GlutApplication(const Arguments&) */ - explicit GlxApplication(const Arguments& arguments): AbstractXApplication(new GlxContextHandler, arguments) {} + /** @copydoc GlutApplication::GlutApplication(const Arguments&, const Configuration&) */ + explicit GlxApplication(const Arguments& arguments, const Configuration& configuration = Configuration()): AbstractXApplication(new GlxContextHandler, arguments, configuration) {} - /** @copydoc GlutApplication::GlutApplication(const Arguments&, Configuration*) */ - explicit GlxApplication(const Arguments& arguments, Configuration* configuration): AbstractXApplication(new GlxContextHandler, arguments, configuration) {} + /** @copydoc GlutApplication::GlutApplication(const Arguments&, std::nullptr_t) */ + explicit GlxApplication(const Arguments& arguments, std::nullptr_t): AbstractXApplication(new GlxContextHandler, arguments, nullptr) {} protected: /* Nobody will need to have (and delete) GlxApplication*, thus this is diff --git a/src/Platform/NaClApplication.cpp b/src/Platform/NaClApplication.cpp index c00ee9df5..b4a5ba4c2 100644 --- a/src/Platform/NaClApplication.cpp +++ b/src/Platform/NaClApplication.cpp @@ -49,38 +49,42 @@ NaClApplication::ConsoleDebugOutput::ConsoleDebugOutput(pp::Instance* instance): Error::setOutput(&errorOutput); } +/** @todo Delegating constructor when support for GCC 4.6 is dropped */ + +NaClApplication::NaClApplication(const Arguments& arguments, const Configuration& configuration): Instance(arguments), Graphics3DClient(this), MouseLock(this), graphics(nullptr), fullscreen(nullptr), c(nullptr) { + debugOutput = new ConsoleDebugOutput(this); + createContext(configuration); +} + NaClApplication::NaClApplication(const Arguments& arguments): Instance(arguments), Graphics3DClient(this), MouseLock(this), c(nullptr) { debugOutput = new ConsoleDebugOutput(this); - createContext(new Configuration); + createContext({}); } -NaClApplication::NaClApplication(const Arguments& arguments, Configuration* configuration): Instance(arguments), Graphics3DClient(this), MouseLock(this), graphics(nullptr), fullscreen(nullptr), c(nullptr) { +NaClApplication::NaClApplication(const Arguments& arguments, std::nullptr_t): Instance(arguments), Graphics3DClient(this), MouseLock(this), c(nullptr) { debugOutput = new ConsoleDebugOutput(this); - if(configuration) createContext(configuration); } -void NaClApplication::createContext(Configuration* configuration) { +void NaClApplication::createContext(const Configuration& configuration) { if(!tryCreateContext(configuration)) { Error() << "Platform::NaClApplication::createContext(): cannot create context"; - delete configuration; std::exit(1); - - } else delete configuration; + } } -bool NaClApplication::tryCreateContext(Configuration* configuration) { +bool NaClApplication::tryCreateContext(const Configuration& configuration) { CORRADE_ASSERT(!c, "Platform::NaClApplication::tryCreateContext(): context already created", false); - viewportSize = configuration->size(); + viewportSize = configuration.size(); const std::int32_t attributes[] = { PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24, PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8, - PP_GRAPHICS3DATTRIB_SAMPLES, configuration->sampleCount(), - PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, configuration->sampleCount() > 1? 1 : 0, - PP_GRAPHICS3DATTRIB_WIDTH, configuration->size().x(), - PP_GRAPHICS3DATTRIB_HEIGHT, configuration->size().y(), + PP_GRAPHICS3DATTRIB_SAMPLES, configuration.sampleCount(), + PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, configuration.sampleCount() > 1 ? 1 : 0, + PP_GRAPHICS3DATTRIB_WIDTH, configuration.size().x(), + PP_GRAPHICS3DATTRIB_HEIGHT, configuration.size().y(), PP_GRAPHICS3DATTRIB_NONE }; diff --git a/src/Platform/NaClApplication.h b/src/Platform/NaClApplication.h index 75b65d68c..367baad35 100644 --- a/src/Platform/NaClApplication.h +++ b/src/Platform/NaClApplication.h @@ -137,25 +137,17 @@ class NaClApplication: public pp::Instance, public pp::Graphics3DClient, public class MouseEvent; class MouseMoveEvent; - /** - * @brief Default constructor - * @param arguments Application arguments - * - * Creates application with default configuration. See Configuration - * for more information. - */ + /** @copydoc GlutApplication::GlutApplication(const Arguments&, const Configuration&) */ + #ifdef DOXYGEN_GENERATING_OUTPUT + explicit NaClApplication(const Arguments& arguments, const Configuration& configuration = Configuration()); + #else + /* To avoid "invalid use of incomplete type" */ + explicit NaClApplication(const Arguments& arguments, const Configuration& configuration); explicit NaClApplication(const Arguments& arguments); + #endif - /** - * @brief Constructor - * @param arguments Application arguments - * @param configuration Configuration - * - * The @p configuration is deleted afterwards. If `nullptr` is passed - * as @p configuration, the context is not created and must be created - * with createContext(). - */ - explicit NaClApplication(const Arguments& arguments, Configuration* configuration); + /** @copydoc GlutApplication::GlutApplication(const Arguments&, std::nullptr_t) */ + explicit NaClApplication(const Arguments& arguments, std::nullptr_t); /** @brief Whether the application runs fullscreen */ bool isFullscreen(); @@ -176,10 +168,10 @@ class NaClApplication: public pp::Instance, public pp::Graphics3DClient, public ~NaClApplication(); /** @copydoc GlutApplication::createContext() */ - void createContext(Configuration* configuration); + void createContext(const Configuration& configuration); /** @copydoc GlutApplication::tryCreateContext() */ - bool tryCreateContext(Configuration* configuration); + bool tryCreateContext(const Configuration& configuration); /** @{ @name Drawing functions */ @@ -323,13 +315,13 @@ class NaClApplication::Configuration { /** * @brief Set window size - * @return Pointer to self (for method chaining) + * @return Reference to self (for method chaining) * * Default is `{640, 480}`. */ - Configuration* setSize(const Vector2i& size) { + Configuration& setSize(const Vector2i& size) { _size = size; - return this; + return *this; } /** @brief Sample count */ @@ -337,14 +329,14 @@ class NaClApplication::Configuration { /** * @brief Set sample count - * @return Pointer to self (for method chaining) + * @return Reference to self (for method chaining) * * Default is `0`, thus no multisampling. See also * @ref Renderer::Feature "Renderer::Feature::Multisampling". */ - Configuration* setSampleCount(Int count) { + Configuration& setSampleCount(Int count) { _sampleCount = count; - return this; + return *this; } private: diff --git a/src/Platform/Sdl2Application.cpp b/src/Platform/Sdl2Application.cpp index 803928ece..734f6312c 100644 --- a/src/Platform/Sdl2Application.cpp +++ b/src/Platform/Sdl2Application.cpp @@ -47,14 +47,20 @@ Sdl2Application::InputEvent::Modifiers fixedModifiers(Uint16 mod) { } +/** @todo Delegating constructor when support for GCC 4.6 is dropped */ + +Sdl2Application::Sdl2Application(const Arguments&, const Configuration& configuration): context(nullptr), flags(Flag::Redraw) { + initialize(); + createContext(configuration); +} + Sdl2Application::Sdl2Application(const Arguments&): context(nullptr), flags(Flag::Redraw) { initialize(); - createContext(new Configuration); + createContext({}); } -Sdl2Application::Sdl2Application(const Arguments&, Configuration* configuration): context(nullptr), flags(Flag::Redraw) { +Sdl2Application::Sdl2Application(const Arguments&, std::nullptr_t): context(nullptr), flags(Flag::Redraw) { initialize(); - if(configuration) createContext(configuration); } void Sdl2Application::initialize() { @@ -64,16 +70,14 @@ void Sdl2Application::initialize() { } } -void Sdl2Application::createContext(Configuration* configuration) { +void Sdl2Application::createContext(const Configuration& configuration) { if(!tryCreateContext(configuration)) { Error() << "Platform::Sdl2Application::createContext(): cannot create context:" << SDL_GetError(); - delete configuration; std::exit(1); - - } else delete configuration; + } } -bool Sdl2Application::tryCreateContext(Configuration* configuration) { +bool Sdl2Application::tryCreateContext(const Configuration& configuration) { CORRADE_ASSERT(!context, "Platform::Sdl2Application::tryCreateContext(): context already created", false); /* Enable double buffering and 24bt depth buffer */ @@ -81,16 +85,16 @@ bool Sdl2Application::tryCreateContext(Configuration* configuration) { SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); /* Multisampling */ - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, configuration->sampleCount() > 1 ? 1 : 0); - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, configuration->sampleCount()); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, configuration.sampleCount() > 1 ? 1 : 0); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, configuration.sampleCount()); /* Flags: if not hidden, set as shown */ - Uint32 flags(configuration->flags()); - if(!(configuration->flags() & Configuration::Flag::Hidden)) flags |= SDL_WINDOW_SHOWN; + Uint32 flags(configuration.flags()); + if(!(configuration.flags() & Configuration::Flag::Hidden)) flags |= SDL_WINDOW_SHOWN; - if(!(window = SDL_CreateWindow(configuration->title().c_str(), + if(!(window = SDL_CreateWindow(configuration.title().data(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - configuration->size().x(), configuration->size().y(), + configuration.size().x(), configuration.size().y(), SDL_WINDOW_OPENGL|flags))) return false; @@ -108,8 +112,8 @@ bool Sdl2Application::tryCreateContext(Configuration* configuration) { SDL_Event* sizeEvent = new SDL_Event; sizeEvent->type = SDL_WINDOWEVENT; sizeEvent->window.event = SDL_WINDOWEVENT_RESIZED; - sizeEvent->window.data1 = configuration->size().x(); - sizeEvent->window.data2 = configuration->size().y(); + sizeEvent->window.data1 = configuration.size().x(); + sizeEvent->window.data2 = configuration.size().y(); SDL_PushEvent(sizeEvent); c = new Context; diff --git a/src/Platform/Sdl2Application.h b/src/Platform/Sdl2Application.h index 06c5319db..1fd62c431 100644 --- a/src/Platform/Sdl2Application.h +++ b/src/Platform/Sdl2Application.h @@ -80,11 +80,17 @@ class Sdl2Application { class MouseEvent; class MouseMoveEvent; - /** @copydoc GlutApplication::GlutApplication(const Arguments&) */ + /** @copydoc GlutApplication::GlutApplication(const Arguments&, const Configuration&) */ + #ifdef DOXYGEN_GENERATING_OUTPUT + explicit Sdl2Application(const Arguments& arguments, const Configuration& configuration = Configuration()); + #else + /* To avoid "invalid use of incomplete type" */ + explicit Sdl2Application(const Arguments& arguments, const Configuration& configuration); explicit Sdl2Application(const Arguments& arguments); + #endif - /** @copydoc GlutApplication::GlutApplication(const Arguments&, Configuration*) */ - explicit Sdl2Application(const Arguments& arguments, Configuration* configuration); + /** @copydoc GlutApplication::GlutApplication(const Arguments&, std::nullptr_t) */ + explicit Sdl2Application(const Arguments& arguments, std::nullptr_t); /** @copydoc GlutApplication::exec() */ int exec(); @@ -98,10 +104,10 @@ class Sdl2Application { virtual ~Sdl2Application(); /** @copydoc GlutApplication::createContext() */ - void createContext(Configuration* configuration); + void createContext(const Configuration& configuration); /** @copydoc GlutApplication::tryCreateContext() */ - bool tryCreateContext(Configuration* configuration); + bool tryCreateContext(const Configuration& configuration); /** @{ @name Drawing functions */ @@ -231,13 +237,13 @@ class Sdl2Application::Configuration { /** * @brief Set window title - * @return Pointer to self (for method chaining) + * @return Reference to self (for method chaining) * * Default is `"Magnum SDL2 Application"`. */ - Configuration* setTitle(std::string title) { + Configuration& setTitle(std::string title) { _title = std::move(title); - return this; + return *this; } /** @brief Window size */ @@ -245,13 +251,13 @@ class Sdl2Application::Configuration { /** * @brief Set window size - * @return Pointer to self (for method chaining) + * @return Reference to self (for method chaining) * * Default is `{800, 600}`. */ - Configuration* setSize(const Vector2i& size) { + Configuration& setSize(const Vector2i& size) { _size = size; - return this; + return *this; } /** @brief Window flags */ @@ -259,13 +265,13 @@ class Sdl2Application::Configuration { /** * @brief Set window flags - * @return Pointer to self (for method chaining) + * @return Reference to self (for method chaining) * * Default is @ref Flag "Flag::Resizable". */ - Configuration* setFlags(const Flags flags) { + Configuration& setFlags(const Flags flags) { _flags = flags; - return this; + return *this; } /** @brief Sample count */ @@ -273,14 +279,14 @@ class Sdl2Application::Configuration { /** * @brief Set sample count - * @return Pointer to self (for method chaining) + * @return Reference to self (for method chaining) * * Default is `0`, thus no multisampling. See also * @ref Renderer::Feature "Renderer::Feature::Multisampling". */ - Configuration* setSampleCount(Int count) { + Configuration& setSampleCount(Int count) { _sampleCount = count; - return this; + return *this; } private: diff --git a/src/Platform/WindowlessGlxApplication.cpp b/src/Platform/WindowlessGlxApplication.cpp index 0108bc2ca..00fe92feb 100644 --- a/src/Platform/WindowlessGlxApplication.cpp +++ b/src/Platform/WindowlessGlxApplication.cpp @@ -33,15 +33,19 @@ namespace Magnum { namespace Platform { -WindowlessGlxApplication::WindowlessGlxApplication(const Arguments&): c(nullptr) { - createContext(new Configuration); +/** @todo Delegating constructor when support for GCC 4.6 is dropped */ + +WindowlessGlxApplication::WindowlessGlxApplication(const Arguments&, const Configuration& configuration): c(nullptr) { + createContext(configuration); } -WindowlessGlxApplication::WindowlessGlxApplication(const Arguments&, Configuration* configuration): c(nullptr) { - if(configuration) createContext(configuration); +WindowlessGlxApplication::WindowlessGlxApplication(const Arguments&): c(nullptr) { + createContext({}); } -void WindowlessGlxApplication::createContext(Configuration* configuration) { +WindowlessGlxApplication::WindowlessGlxApplication(const Arguments&, std::nullptr_t): c(nullptr) {} + +void WindowlessGlxApplication::createContext(const Configuration&) { CORRADE_ASSERT(!c, "WindowlessGlxApplication::createContext(): context already created", ); display = XOpenDisplay(nullptr); @@ -100,7 +104,6 @@ void WindowlessGlxApplication::createContext(Configuration* configuration) { ExtensionWrangler::initialize(ExtensionWrangler::ExperimentalFeatures::Enable); c = new Context; - delete configuration; } WindowlessGlxApplication::~WindowlessGlxApplication() { diff --git a/src/Platform/WindowlessGlxApplication.h b/src/Platform/WindowlessGlxApplication.h index df11f2dda..ed9831b83 100644 --- a/src/Platform/WindowlessGlxApplication.h +++ b/src/Platform/WindowlessGlxApplication.h @@ -73,11 +73,17 @@ class WindowlessGlxApplication { class Configuration; - /** @copydoc GlutApplication::GlutApplication(const Arguments&) */ + /** @copydoc GlutApplication::GlutApplication(const Arguments&, const Configuration&) */ + #ifdef DOXYGEN_GENERATING_OUTPUT + explicit WindowlessGlxApplication(const Arguments& arguments, const Configuration& configuration = Configuration()); + #else + /* To avoid "invalid use of incomplete type" */ + explicit WindowlessGlxApplication(const Arguments& arguments, const Configuration& configuration); explicit WindowlessGlxApplication(const Arguments& arguments); + #endif - /** @copydoc GlutApplication::GlutApplication(const Arguments&, Configuration*) */ - explicit WindowlessGlxApplication(const Arguments& arguments, Configuration* configuration); + /** @copydoc GlutApplication::GlutApplication(const Arguments&, std::nullptr_t) */ + explicit WindowlessGlxApplication(const Arguments& arguments, std::nullptr_t); /** * @brief Execute application @@ -91,7 +97,7 @@ class WindowlessGlxApplication { ~WindowlessGlxApplication(); /** @copydoc GlutApplication::createContext() */ - void createContext(Configuration* configuration); + void createContext(const Configuration& configuration); private: Display* display; diff --git a/src/Platform/WindowlessNaClApplication.cpp b/src/Platform/WindowlessNaClApplication.cpp index a890b2c91..5336ba23c 100644 --- a/src/Platform/WindowlessNaClApplication.cpp +++ b/src/Platform/WindowlessNaClApplication.cpp @@ -41,33 +41,37 @@ struct WindowlessNaClApplication::ConsoleDebugOutput { WindowlessNaClApplication::ConsoleDebugOutput::ConsoleDebugOutput(pp::Instance* instance): debugBuffer(instance, Utility::NaClConsoleStreamBuffer::LogLevel::Log), warningBuffer(instance, Utility::NaClConsoleStreamBuffer::LogLevel::Warning), errorBuffer(instance, Utility::NaClConsoleStreamBuffer::LogLevel::Error), debugOutput(&debugBuffer), warningOutput(&warningBuffer), errorOutput(&errorBuffer) { /* Inform about this change on standard output */ - Debug() << "Platform::NaClApplication: redirecting Debug, Warning and Error output to JavaScript console"; + Debug() << "Platform::WindowlessNaClApplication: redirecting Debug, Warning and Error output to JavaScript console"; Debug::setOutput(&debugOutput); Warning::setOutput(&warningOutput); Error::setOutput(&errorOutput); } +/** @todo Delegating constructor when support for GCC 4.6 is dropped */ + +WindowlessNaClApplication::WindowlessNaClApplication(const Arguments& arguments, const Configuration& configuration): Instance(arguments), Graphics3DClient(this), graphics(nullptr), c(nullptr) { + debugOutput = new ConsoleDebugOutput(this); + createContext(configuration); +} + WindowlessNaClApplication::WindowlessNaClApplication(const Arguments& arguments): Instance(arguments), Graphics3DClient(this), c(nullptr) { debugOutput = new ConsoleDebugOutput(this); - createContext(new Configuration); + createContext({}); } -WindowlessNaClApplication::WindowlessNaClApplication(const Arguments& arguments, Configuration* configuration): Instance(arguments), Graphics3DClient(this), graphics(nullptr), c(nullptr) { +WindowlessNaClApplication::WindowlessNaClApplication(const Arguments& arguments, std::nullptr_t): Instance(arguments), Graphics3DClient(this), graphics(nullptr), c(nullptr) { debugOutput = new ConsoleDebugOutput(this); - if(configuration) createContext(configuration); } -void WindowlessNaClApplication::createContext(Configuration* configuration) { +void WindowlessNaClApplication::createContext(const Configuration& configuration) { if(!tryCreateContext(configuration)) { Error() << "Platform::WindowlessNaClApplication::createContext(): cannot create context"; - delete configuration; std::exit(1); - - } else delete configuration; + } } -bool WindowlessNaClApplication::tryCreateContext(Configuration*) { +bool WindowlessNaClApplication::tryCreateContext(const Configuration&) { CORRADE_ASSERT(!c, "Platform::WindowlessNaClApplication::tryCreateContext(): context already created", false); const std::int32_t attributes[] = { diff --git a/src/Platform/WindowlessNaClApplication.h b/src/Platform/WindowlessNaClApplication.h index 54de3ea05..1fe6e6b81 100644 --- a/src/Platform/WindowlessNaClApplication.h +++ b/src/Platform/WindowlessNaClApplication.h @@ -90,25 +90,17 @@ class WindowlessNaClApplication: public pp::Instance, public pp::Graphics3DClien class Configuration; - /** - * @brief Default constructor - * @param arguments Application arguments - * - * Creates application with default configuration. See Configuration - * for more information. - */ + /** @copydoc GlutApplication::GlutApplication(const Arguments&, const Configuration&) */ + #ifdef DOXYGEN_GENERATING_OUTPUT + explicit WindowlessNaClApplication(const Arguments& arguments, const Configuration& configuration = Configuration()); + #else + /* To avoid "invalid use of incomplete type" */ + explicit WindowlessNaClApplication(const Arguments& arguments, const Configuration& configuration); explicit WindowlessNaClApplication(const Arguments& arguments); + #endif - /** - * @brief Constructor - * @param arguments Application arguments - * @param configuration Configuration - * - * The @p configuration is deleted afterwards. If `nullptr` is passed - * as @p configuration, the context is not created and must be created - * with createContext(). - */ - explicit WindowlessNaClApplication(const Arguments& arguments, Configuration* configuration); + /** @copydoc GlutApplication::GlutApplication(const Arguments&, std::nullptr_t) */ + explicit WindowlessNaClApplication(const Arguments& arguments, std::nullptr_t); /** * @brief Execute application @@ -122,10 +114,10 @@ class WindowlessNaClApplication: public pp::Instance, public pp::Graphics3DClien ~WindowlessNaClApplication(); /** @copydoc GlutApplication::createContext() */ - void createContext(Configuration* configuration); + void createContext(const Configuration& configuration); /** @copydoc GlutApplication::tryCreateContext() */ - bool tryCreateContext(Configuration* configuration); + bool tryCreateContext(const Configuration& configuration); private: struct ConsoleDebugOutput; diff --git a/src/Platform/XEglApplication.h b/src/Platform/XEglApplication.h index c2c708b4e..1b99b87f1 100644 --- a/src/Platform/XEglApplication.h +++ b/src/Platform/XEglApplication.h @@ -56,11 +56,11 @@ to simplify porting. */ class XEglApplication: public AbstractXApplication { public: - /** @copydoc GlutApplication::GlutApplication(const Arguments&) */ - explicit XEglApplication(const Arguments& arguments): AbstractXApplication(new EglContextHandler, arguments) {} + /** @copydoc GlutApplication::GlutApplication(const Arguments&, const Configuration&) */ + explicit XEglApplication(const Arguments& arguments, const Configuration& configuration = Configuration()): AbstractXApplication(new EglContextHandler, arguments, configuration) {} - /** @copydoc GlutApplication::GlutApplication(const Arguments&, Configuration*) */ - explicit XEglApplication(const Arguments& arguments, Configuration* configuration): AbstractXApplication(new EglContextHandler, arguments, configuration) {} + /** @copydoc GlutApplication::GlutApplication(const Arguments&, std::nullptr_t) */ + explicit XEglApplication(const Arguments& arguments, std::nullptr_t): AbstractXApplication(new EglContextHandler, nullptr) {} protected: /* Nobody will need to have (and delete) XEglApplication*, thus this is