aboutsummaryrefslogtreecommitdiff
path: root/src/application.hpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-11-09 21:03:45 +0800
committercrupest <crupest@outlook.com>2018-11-09 21:03:45 +0800
commit4b219b569c16db6027dd36e4656152f261321c60 (patch)
tree2f40c6aeb74382c5a958f153e4598950dffae94c /src/application.hpp
parent6515948e3f5b97f90446536f72e99d71e96f9de7 (diff)
parent9ef75fe91837394620edb91f332065a4f34a0281 (diff)
downloadcru-4b219b569c16db6027dd36e4656152f261321c60.tar.gz
cru-4b219b569c16db6027dd36e4656152f261321c60.tar.bz2
cru-4b219b569c16db6027dd36e4656152f261321c60.zip
Merge branch 'master' into theme-resource
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 48c87a7a..c286cc08 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"
#include "any_map.h"
@@ -87,25 +89,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
{
@@ -117,6 +100,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_;
@@ -136,14 +136,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