Second attempt at deriving application from a singleton
This commit is contained in:
+4
-3
@@ -4,11 +4,12 @@
|
|||||||
class Game : public Inferno::Application
|
class Game : public Inferno::Application
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Game() {}
|
Game() : Application({}) {}
|
||||||
~Game() {}
|
~Game() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
Inferno::Application* Inferno::createApplication()
|
Inferno::Application& Inferno::createApplication()
|
||||||
{
|
{
|
||||||
return new Game();
|
Game::initialize();
|
||||||
|
return Game::the();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,11 +21,9 @@
|
|||||||
|
|
||||||
namespace Inferno {
|
namespace Inferno {
|
||||||
|
|
||||||
Application* Application::s_instance = nullptr;
|
Application::Application(s)
|
||||||
|
|
||||||
Application::Application()
|
|
||||||
{
|
{
|
||||||
ASSERT(!s_instance, "Application already exists!");
|
// Set singleton instance early
|
||||||
s_instance = this;
|
s_instance = this;
|
||||||
|
|
||||||
// Initialize
|
// Initialize
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <memory> // std::unique_ptr, std::shared_ptr
|
#include <memory> // std::unique_ptr, std::shared_ptr
|
||||||
|
|
||||||
|
#include "inferno/singleton.h"
|
||||||
|
|
||||||
namespace Inferno {
|
namespace Inferno {
|
||||||
|
|
||||||
class Event;
|
class Event;
|
||||||
@@ -14,9 +16,9 @@ namespace Inferno {
|
|||||||
class WindowCloseEvent;
|
class WindowCloseEvent;
|
||||||
class WindowResizeEvent;
|
class WindowResizeEvent;
|
||||||
|
|
||||||
class Application {
|
class Application : public Singleton<Application> {
|
||||||
public:
|
public:
|
||||||
Application();
|
Application(s);
|
||||||
virtual ~Application();
|
virtual ~Application();
|
||||||
|
|
||||||
int run();
|
int run();
|
||||||
@@ -31,8 +33,6 @@ namespace Inferno {
|
|||||||
|
|
||||||
inline Window& getWindow() const { return *m_window; }
|
inline Window& getWindow() const { return *m_window; }
|
||||||
|
|
||||||
static inline Application& the() { return *s_instance; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_status { 0 };
|
int m_status { 0 };
|
||||||
float m_lastFrameTime { 0.0f };
|
float m_lastFrameTime { 0.0f };
|
||||||
@@ -42,12 +42,11 @@ namespace Inferno {
|
|||||||
|
|
||||||
//
|
//
|
||||||
std::shared_ptr<Font> m_font;
|
std::shared_ptr<Font> m_font;
|
||||||
|
//
|
||||||
static Application* s_instance;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// To be defined in the game
|
// To be defined in the game
|
||||||
extern Application* createApplication();
|
extern Application& createApplication();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ int main(int argc, char* argv[])
|
|||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
||||||
auto app = Inferno::createApplication();
|
auto& app = Inferno::createApplication();
|
||||||
|
|
||||||
int status = app->run();
|
int status = app.run();
|
||||||
|
|
||||||
delete app;
|
app.destroy();
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
#ifndef SINGLETON_H
|
||||||
|
#define SINGLETON_H
|
||||||
|
|
||||||
|
#include "inferno/assert.h"
|
||||||
|
|
||||||
|
namespace Inferno {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Singleton {
|
||||||
|
// Application is allowed to access its Singleton instance for early setting
|
||||||
|
friend class Application;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static inline void initialize()
|
||||||
|
{
|
||||||
|
ASSERT(!s_instance, "Singleton already exists!");
|
||||||
|
s_instance = new T { s {} };
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void destroy()
|
||||||
|
{
|
||||||
|
ASSERT(s_instance, "Singleton does not exist!");
|
||||||
|
delete s_instance;
|
||||||
|
s_instance = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline T& the()
|
||||||
|
{
|
||||||
|
ASSERT(s_instance, "Singleton does not exist!");
|
||||||
|
return *s_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove copy constructor and copy assignment operator
|
||||||
|
Singleton(const Singleton&) = delete;
|
||||||
|
Singleton& operator=(const Singleton&) = delete;
|
||||||
|
Singleton(Singleton&&) = delete;
|
||||||
|
Singleton& operator=(Singleton&&) = delete;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Singleton() {}
|
||||||
|
|
||||||
|
// Constructor token
|
||||||
|
struct s {};
|
||||||
|
|
||||||
|
private:
|
||||||
|
static T* s_instance;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T* Singleton<T>::s_instance = nullptr;
|
||||||
|
|
||||||
|
} // namespace Inferno
|
||||||
|
|
||||||
|
|
||||||
|
#endif // SINGLETON_H
|
||||||
Reference in New Issue
Block a user