GameKit  0.0.1a
C++ gamedev tools
ApplicationStateStack.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: ApplicationStateStack.hpp
5  *
6  * Description:
7  *
8  * Created: 14/12/2014 13:48:48
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_APPLICATIONSTATESTACK_HPP_
15 #define GK_APPLICATIONSTATESTACK_HPP_
16 
17 #include <memory>
18 #include <stack>
19 
21 
22 namespace gk {
23 
29  public:
38  template<typename T, typename... Args>
39  auto push(Args &&...args) -> typename std::enable_if<std::is_base_of<ApplicationState, T>::value, T&>::type {
40  m_states.emplace(std::make_shared<T>(std::forward<Args>(args)...));
41  m_states.top()->setStateStack(this);
42  return static_cast<T&>(top());
43  }
44 
51  void pop();
52 
57  void clear() { while(!empty()) pop(); }
58 
65  void clearDeletedStates();
66 
73  ApplicationState &top() const { return *m_states.top().get(); }
74 
81  bool empty() const { return m_states.empty(); }
82 
89  std::size_t size() const { return m_states.size(); }
90 
100  return *s_instance;
101  }
102 
111  static void setInstance(ApplicationStateStack &instance) {
112  s_instance = &instance;
113  }
114 
115  private:
117  // Member data
120 
121  std::stack<std::shared_ptr<ApplicationState>> m_states;
122  std::stack<std::shared_ptr<ApplicationState>> m_trash;
123 };
124 
125 } // namespace gk
126 
127 #endif // GK_APPLICATIONSTATESTACK_HPP_
128 
void clear()
Clear the stack.
ApplicationState & top() const
Get the top ApplicationState in the stack.
std::stack< std::shared_ptr< ApplicationState > > m_trash
Removed states waiting to be deleted.
static ApplicationStateStack & getInstance()
Get the current singleton instance.
void pop()
Remove the top ApplicationState in the stack.
void clearDeletedStates()
Clear the removed states of the stack.
std::size_t size() const
Get the amount of ApplicationState in the stack.
bool empty() const
Check if the container is empty.
static void setInstance(ApplicationStateStack &instance)
Set the current singleton instance.
static ApplicationStateStack * s_instance
Current singleton instance.
Stack containing ApplicationState instances.
auto push(Args &&...args) -> typename std::enable_if< std::is_base_of< ApplicationState, T >::value, T &>::type
Push a new gk::ApplicationState to the stack.
Abstract base class for game states.
std::stack< std::shared_ptr< ApplicationState > > m_states
Stack containing the states.