MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Tapestry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Fabric.hpp"
4
5namespace MayaFlux::Vruta {
6class TaskScheduler;
7class EventManager;
8}
9
10namespace MayaFlux::Nexus {
11
12/**
13 * @class Tapestry
14 * @brief Owner of one or more Fabrics and the shared state they rely on.
15 *
16 * A Tapestry holds shared_ptrs to the TaskScheduler and EventManager each
17 * Fabric needs, and serves as the authoring-surface layer above raw Fabric
18 * construction. Fabrics woven through a Tapestry can be looked up by name
19 * for state encoding, cross-fabric queries, and coordinated commits.
20 *
21 * Tapestry holds owning references to the scheduler and event manager so
22 * that every Fabric it create_fabrics is guaranteed a valid backing for the
23 * Tapestry's lifetime.
24 */
25class MAYAFLUX_API Tapestry {
26public:
27 /**
28 * @brief Construct with shared scheduler and event manager.
29 */
31 std::shared_ptr<Vruta::TaskScheduler> scheduler,
32 std::shared_ptr<Vruta::EventManager> event_manager);
33
35
36 Tapestry(const Tapestry&) = delete;
37 Tapestry& operator=(const Tapestry&) = delete;
38 Tapestry(Tapestry&&) = delete;
40
41 // =========================================================================
42 // Fabric lifecycle
43 // =========================================================================
44
45 /**
46 * @brief create_fabric a new unnamed Fabric into the Tapestry.
47 * @param cell_size Grid cell edge length for the spatial index.
48 */
49 [[nodiscard]] std::shared_ptr<Fabric> create_fabric(float cell_size = 1.0F);
50
51 /**
52 * @brief create_fabric a new named Fabric into the Tapestry.
53 * @param name Identifier for lookup; must not collide with existing names.
54 * @param cell_size Grid cell edge length for the spatial index.
55 */
56 [[nodiscard]] std::shared_ptr<Fabric> create_fabric(std::string name, float cell_size = 1.0F);
57
58 /**
59 * @brief Remove a Fabric by pointer. No-op if not owned by this Tapestry.
60 */
61 void remove_fabric(const std::shared_ptr<Fabric>& fabric);
62
63 /**
64 * @brief Remove a Fabric by name. No-op if name is not registered.
65 */
66 void remove_fabric(std::string_view name);
67
68 /**
69 * @brief Look up a named Fabric.
70 * @return The Fabric, or nullptr if name is not registered.
71 */
72 [[nodiscard]] std::shared_ptr<Fabric> get_fabric(std::string_view name) const;
73
74 /**
75 * @brief All woven Fabrics, named and unnamed.
76 */
77 [[nodiscard]] const std::vector<std::shared_ptr<Fabric>>& all_fabrics() const;
78
79 /**
80 * @brief Read-only view of all Expanses owned by this Tapestry.
81 */
82 [[nodiscard]] const std::unordered_map<std::string, std::shared_ptr<Expanse>>&
84 {
85 return m_expanses;
86 }
87
88 // =========================================================================
89 // Expanse
90 // =========================================================================
91
92 /**
93 * @brief Create and register a named Expanse.
94 *
95 * The name also serves as the containment function's registry name for state
96 * encoding, unless set_fn_name is called. A name already in use warns and
97 * returns the existing Expanse, as create_fabric does.
98 *
99 * @return Shared pointer to the Expanse, for passing to Fabric::add_expanse.
100 */
101 [[nodiscard]] std::shared_ptr<Expanse> create_expanse(
102 std::string name,
106
107 /**
108 * @brief Create and register a named Expanse from a containment predicate alone.
109 * @return Shared pointer to the new Expanse, for passing to Fabric::add_expanse.
110 */
111 [[nodiscard]] std::shared_ptr<Expanse> create_expanse(
112 std::string name,
114
115 /**
116 * @brief Create and register a named Expanse that is a box.
117 * @return Shared pointer to the new Expanse, for passing to Fabric::add_expanse.
118 */
119 [[nodiscard]] std::shared_ptr<Expanse> create_expanse(
120 std::string name,
121 const Kinesis::AABB3D& bounds);
122
123 /**
124 * @brief Create and register a named Expanse of a derived type.
125 * @tparam T Expanse subclass, for example Hull.
126 * @param args Forwarded to T's constructor.
127 * @return Shared pointer to the Expanse, for passing to Fabric::add_expanse. A name
128 * already in use warns and returns the existing Expanse, or null if that
129 * one is not a T.
130 */
131 template <typename T, typename... Args>
132 [[nodiscard]] std::shared_ptr<T> create_expanse(std::string name, Args&&... args)
133 {
134 static_assert(std::is_base_of_v<Expanse, T>, "T must derive from Expanse");
135 return std::dynamic_pointer_cast<T>(
136 claim(std::move(name), std::make_shared<T>(std::forward<Args>(args)...)));
137 }
138
139 /**
140 * @brief Look up a named Expanse.
141 * @return The Expanse, or nullptr if name is not registered.
142 */
143 [[nodiscard]] std::shared_ptr<Expanse> get_expanse(std::string_view name) const;
144
145 /**
146 * @brief Remove Tapestry's owning reference to a named Expanse.
147 *
148 * Fabrics holding the shared_ptr keep it alive and functional, so nothing
149 * fires here; Fabric::remove_expanse fires the exits for its own occupants.
150 */
151 void remove_expanse(std::string_view name);
152
153 // =========================================================================
154 // Commit
155 // =========================================================================
156
157 /**
158 * @brief Commit all woven Fabrics in insertion order.
159 */
160 void commit_all();
161
162private:
163 std::shared_ptr<Expanse> claim(std::string name, std::shared_ptr<Expanse> expanse);
164
165 std::shared_ptr<Vruta::TaskScheduler> m_scheduler;
166 std::shared_ptr<Vruta::EventManager> m_event_manager;
167
168 std::vector<std::shared_ptr<Fabric>> m_fabrics;
169 std::unordered_map<std::string, std::weak_ptr<Fabric>> m_named_fabrics;
170 std::unordered_map<std::string, std::shared_ptr<Expanse>> m_expanses;
171
172 uint32_t m_next_id { 1 };
173};
174
175} // namespace MayaFlux::Nexus
Expanse::ContainsFn contains
Definition Decoder.cpp:517
Expanse::CrossingFn on_enter
Definition Decoder.cpp:518
Expanse::CrossingFn on_exit
Definition Decoder.cpp:519
std::string name
Definition VKDevice.cpp:123
std::function< void(uint32_t)> CrossingFn
Definition Expanse.hpp:58
std::function< bool(const glm::vec3 &)> ContainsFn
Definition Expanse.hpp:57
std::vector< std::shared_ptr< Fabric > > m_fabrics
Definition Tapestry.hpp:168
std::shared_ptr< T > create_expanse(std::string name, Args &&... args)
Create and register a named Expanse of a derived type.
Definition Tapestry.hpp:132
Tapestry & operator=(Tapestry &&)=delete
Tapestry & operator=(const Tapestry &)=delete
std::unordered_map< std::string, std::shared_ptr< Expanse > > m_expanses
Definition Tapestry.hpp:170
std::shared_ptr< Vruta::EventManager > m_event_manager
Definition Tapestry.hpp:166
const std::unordered_map< std::string, std::shared_ptr< Expanse > > & all_expanses() const
Read-only view of all Expanses owned by this Tapestry.
Definition Tapestry.hpp:83
std::shared_ptr< Vruta::TaskScheduler > m_scheduler
Definition Tapestry.hpp:165
Tapestry(Tapestry &&)=delete
std::unordered_map< std::string, std::weak_ptr< Fabric > > m_named_fabrics
Definition Tapestry.hpp:169
Tapestry(const Tapestry &)=delete
Owner of one or more Fabrics and the shared state they rely on.
Definition Tapestry.hpp:25
Axis-aligned bounding box in 3D world space.
Definition Bounds.hpp:188