14export template<
typename ContextType>
16 constexpr static const char*
type = refl::type_name<ContextUpdate<ContextType>>.data();
17 ContextType*
ptr =
nullptr;
22 explicit with_context(std::initializer_list<cydui::components::component_holder_t> &&components) {
23 for (
auto c: components) {
24 components_.append(c);
43 static constexpr refl::type_id_t type_id = refl::type_id<T>;
44 std::shared_ptr<void> ptr_ = std::static_pointer_cast<void>(ptr);
46 context_map_[type_id] = ptr_;
51 static constexpr refl::type_id_t type_id = refl::type_id<T>;
52 if (context_map_.contains(type_id)) {
53 return static_cast<T*
>(context_map_[type_id].get());
59 return context_map_.empty();
63 std::unordered_map<refl::type_id_t, std::shared_ptr<void>> context_map_ { };
70 export template <
typename ContextType>
74 static constexpr bool is_readonly = std::is_const_v<ContextType>;
87 other.stop_listening();
88 if (other.owns_context) {
89 this->owns_context =
true;
91 other.owns_context =
false;
97 other.stop_listening();
99 this->ctx = other.ctx;
100 this->state = other.state;
101 if (other.owns_context) {
102 this->owns_context =
true;
103 other.owns_context =
false;
128 void start_listening() {
133 state->force_redraw();
138 void stop_listening() {
139 if (listener.has_value()) {
140 listener.value().remove();
141 listener = std::nullopt;
147 bool owns_context =
false;
151 std::optional<fabric::async::listener<ContextUpdate<context_type>>> listener{std::nullopt};
166 export template <
typename ContextType>
188 return context_.get();
201 operator std::shared_ptr<context_type>() {
208 std::shared_ptr<context_type> context_;
213 template <
typename ContextType>
218 template <
typename ContextType>
222 template <
typename ContextType>
226 template <
typename ContextType>
228 it->owns_context = value;
230 template <
typename ContextType>
232 it->start_listening();
237 template <
typename ContextType>
242 template <
typename ContextType>
void add_context(const std::shared_ptr< T > &ptr)
context_store_t()=default
std::optional< T * > find_context()
cydui::components::component_holder_t build() const
with_context(std::initializer_list< cydui::components::component_holder_t > &&components)
std::shared_ptr< component_state_t > component_state_ref
static constexpr const char * type
static void set_context(provide_context< ContextType > *it, const std::shared_ptr< ContextType > &ctx)
static void set_state(provide_context< ContextType > *it, const components::component_state_ref &state)
provide_context(const provide_context &other)=default
context_type * operator->()
components::component_holder_t operator>(with_context &&components)
provide_context(provide_context &&other) noexcept=default
provide_context & operator=(provide_context &&other)=default
friend struct provide_context_delegate
context_type & operator*()
provide_context & operator=(const provide_context &other)=default
static void set_context(use_context< const ContextType > *it, ContextType *ctx)
static void set_context(use_context< ContextType > *it, ContextType *ctx)
static void set_state(use_context< ContextType > *it, const cydui::components::component_state_ref &state)
static void set_owns_context(use_context< ContextType > *it, bool value)
static void start_listening(use_context< ContextType > *it)
use_context & operator=(use_context &&other)
use_context(const use_context &other)=delete
use_context(use_context &&other) noexcept
ret_context_type * operator->()
ContextType ret_context_type
static constexpr bool is_readonly
friend struct use_context_delegate
use_context & operator=(const use_context &other)=delete
ret_context_type & operator*()
std::remove_const_t< ContextType > context_type