aboutsummaryrefslogtreecommitdiff
path: root/src/application.hpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-11-09 20:57:29 +0800
committercrupest <crupest@outlook.com>2018-11-09 20:57:29 +0800
commit9ef75fe91837394620edb91f332065a4f34a0281 (patch)
tree39a3e67f9a34ce44cc65b74d0ec82691cffd5dfa /src/application.hpp
parentefdce672123284847bd7fb6f12ac1ec96f28f3ef (diff)
downloadcru-9ef75fe91837394620edb91f332065a4f34a0281.tar.gz
cru-9ef75fe91837394620edb91f332065a4f34a0281.tar.bz2
cru-9ef75fe91837394620edb91f332065a4f34a0281.zip
Add singleton system.
Diffstat (limited to 'src/application.hpp')
-rw-r--r--src/application.hpp46
1 files changed, 22 insertions, 24 deletions
diff --git a/src/application.hpp b/src/application.hpp
index fe38ba0f..7357bbaf 100644
--- a/src/application.hpp
+++ b/src/application.hpp
@@ -4,6 +4,8 @@
#include <memory>
#include <optional>
#include <functional>
+#include <typeindex>
+#include <type_traits>
#include "base.hpp"
@@ -86,25 +88,6 @@ namespace cru
int Run();
void Quit(int quit_code);
- ui::WindowManager* GetWindowManager() const
- {
- return window_manager_.get();
- }
-
- graph::GraphManager* GetGraphManager() const
- {
- return graph_manager_.get();
- }
-
- TimerManager* GetTimerManager() const
- {
- return timer_manager_.get();
- }
-
- ui::animations::details::AnimationManager* GetAnimationManager() const
- {
- return animation_manager_.get();
- }
HINSTANCE GetInstanceHandle() const
{
@@ -116,6 +99,23 @@ namespace cru
return god_window_.get();
}
+ // Resolve a singleton.
+ // All singletons will be delete in reverse order of resolve.
+ template<typename T, typename = std::enable_if_t<std::is_base_of_v<Object, T>>>
+ T* ResolveSingleton(const std::function<T*(Application*)>& creator)
+ {
+ const auto& index = std::type_index{typeid(T)};
+ const auto find_result = singleton_map_.find(index);
+ if (find_result != singleton_map_.cend())
+ return static_cast<T*>(find_result->second);
+
+ auto singleton = creator(this);
+ singleton_map_.emplace(index, static_cast<Object*>(singleton));
+ singleton_list_.push_back(singleton);
+ return singleton;
+ }
+
+
CaretInfo GetCaretInfo() const
{
return caret_info_;
@@ -130,14 +130,12 @@ namespace cru
private:
HINSTANCE h_instance_;
-
- std::unique_ptr<ui::WindowManager> window_manager_;
- std::unique_ptr<graph::GraphManager> graph_manager_;
- std::unique_ptr<TimerManager> timer_manager_;
- std::unique_ptr<ui::animations::details::AnimationManager> animation_manager_;
std::unique_ptr<GodWindow> god_window_;
+ std::unordered_map<std::type_index, Object*> singleton_map_;
+ std::list<Object*> singleton_list_; // used for reverse destroy.
+
#ifdef CRU_DEBUG_LAYOUT
DebugLayoutResource debug_layout_resource_;
#endif