OpenMiner/include/core/ApplicationStateStack.hpp

57 lines
1.3 KiB
C++
Raw Normal View History

2014-12-15 16:47:30 +01:00
/*
* =====================================================================================
*
* Filename: ApplicationStateStack.hpp
*
2018-06-05 01:24:54 +02:00
* Description:
2014-12-15 16:47:30 +01:00
*
* Created: 14/12/2014 13:48:48
*
2018-06-05 16:17:40 +02:00
* Author: Quentin Bazin, <quent42340@gmail.com>
2014-12-15 16:47:30 +01:00
*
* =====================================================================================
*/
#ifndef APPLICATIONSTATESTACK_HPP_
#define APPLICATIONSTATESTACK_HPP_
#include <memory>
#include <stack>
#include "ApplicationState.hpp"
class ApplicationStateStack {
public:
template<typename T, typename... Args>
T &push(Args &&...args) {
m_states.emplace(std::make_shared<T>(std::forward<Args>(args)...));
m_states.top()->setStateStack(this);
return static_cast<T&>(top());
}
void pop();
void clearDeletedStates();
ApplicationState &top() const { return *m_states.top().get(); }
bool empty() const { return m_states.empty(); }
std::size_t size() const { return m_states.size(); }
2018-06-05 01:24:54 +02:00
2018-06-05 16:51:58 +02:00
static ApplicationStateStack &getInstance() {
return *s_instance;
}
static void setInstance(ApplicationStateStack &instance) {
s_instance = &instance;
2018-06-05 16:51:58 +02:00
}
2018-06-05 01:24:54 +02:00
2014-12-15 16:47:30 +01:00
private:
static ApplicationStateStack *s_instance;
std::stack<std::shared_ptr<ApplicationState>> m_states;
std::stack<std::shared_ptr<ApplicationState>> m_trash;
2014-12-15 16:47:30 +01:00
};
#endif // APPLICATIONSTATESTACK_HPP_