SDEngine
Game Engine
Loading...
Searching...
No Matches
ViewManager.hpp
Go to the documentation of this file.
1// TODO(docs): Add file-level Doxygen header
2// - @file ViewManager.hpp
3// - @brief Manages View instances (create, destroy, query)
4// - Relationship to Application and View classes
5#pragma once
6
7#include <expected>
8#include <functional>
9#include <memory>
10#include <string>
11#include <unordered_map>
12#include <vector>
13
14#include "SD/core/Scene.hpp"
15#include "SD/core/View.hpp"
16#include "SD/core/base.hpp"
17#include "SD/export.hpp"
18
19namespace sd {
20
21// TODO(docs): Document ViewManager class
22// - Purpose: Factory and registry for View objects
23// - View naming and ID system
24// - Error handling patterns (std::expected)
25// - Example: Creating and managing multiple views
27public:
30
31 template<typename T, typename... Args>
32 requires std::is_base_of_v<View, T>
33 T& create(std::string name, Args&&... args) {
34 if (m_view_name_to_id.contains(name))
35 engine_abort("View name already exists: " + name);
36
37 ViewId id = m_next_view_id++;
38 auto view = std::make_unique<T>(std::move(name), std::forward<Args>(args)...);
39 view->m_view_id = id;
40
41 auto& ref = *view;
42 m_views_by_id.emplace(id, std::move(view));
43 m_view_name_to_id.emplace(ref.get_name(), id);
44 return ref;
45 }
46
47 using ViewResult = std::expected<std::reference_wrapper<View>, ViewError>;
48
49 ViewResult get(ViewId id);
50 ViewResult get(const std::string& name);
51 std::expected<ViewId, ViewError> get_id(const std::string& name) const;
52
53 ViewError remove(ViewId id);
54 ViewError remove(const std::string& name);
55
56 template<typename T, typename... Args>
57 requires std::is_base_of_v<Layer, T>
58 std::expected<std::reference_wrapper<T>, ViewError> push_layer(ViewId id, Args&&... args) {
59 auto it = m_views_by_id.find(id);
60 if (it == m_views_by_id.end())
61 return std::unexpected(VIEW_DOES_NOT_EXIST);
62 return it->second->push_layer<T>(std::forward<Args>(args)...);
63 }
64
65 const std::unordered_map<ViewId, std::unique_ptr<View>>& get_views() const {
66 return m_views_by_id;
67 }
68 auto& get_views() { return m_views_by_id; }
69
70 template<typename F>
71 void for_each(F&& fn) {
72 for (auto& [id, view] : m_views_by_id) {
73 fn(*view);
74 }
75 }
76
77 template<typename F>
78 void for_each(F&& fn) const {
79 for (const auto& [id, view] : m_views_by_id) {
80 fn(*view);
81 }
82 }
83
84 std::vector<Scene*> get_scenes();
85
86 void update_views(float dt);
87 void render_views(vk::CommandBuffer cmd);
88 void cleanup_closed_views();
89 void clear();
90
91private:
92 std::unordered_map<ViewId, std::unique_ptr<View>> m_views_by_id;
93 std::unordered_map<std::string, ViewId> m_view_name_to_id;
95};
96
97} // namespace sd
Definition ViewManager.hpp:26
ViewId m_next_view_id
Definition ViewManager.hpp:94
const std::unordered_map< ViewId, std::unique_ptr< View > > & get_views() const
Definition ViewManager.hpp:65
std::unordered_map< std::string, ViewId > m_view_name_to_id
Definition ViewManager.hpp:93
void for_each(F &&fn)
Definition ViewManager.hpp:71
std::expected< std::reference_wrapper< View >, ViewError > ViewResult
Definition ViewManager.hpp:47
std::unordered_map< ViewId, std::unique_ptr< View > > m_views_by_id
Definition ViewManager.hpp:92
void for_each(F &&fn) const
Definition ViewManager.hpp:78
T & create(std::string name, Args &&... args)
Definition ViewManager.hpp:33
auto & get_views()
Definition ViewManager.hpp:68
std::expected< std::reference_wrapper< T >, ViewError > push_layer(ViewId id, Args &&... args)
Definition ViewManager.hpp:58
Definition Application.hpp:28
void engine_abort(const std::string &message)
Definition base.hpp:41
ViewError
Definition View.hpp:26
@ VIEW_DOES_NOT_EXIST
Definition View.hpp:29
Definition id_types.hpp:7
constexpr T g_type_max
Definition types.hpp:21