Blender V5.3
wm_message_bus.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "CLG_log.h"
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_listbase.hh"
16#include "BLI_utildefines.hh"
17
18#include "BLI_ghash.hh"
19
20#include "WM_types.hh"
21
24
25namespace blender {
26
27/* -------------------------------------------------------------------------- */
30
32
33using wmMsgTypeInitFn = void (*)(wmMsgTypeInfo *);
34
40
42{
43 for (uint i = 0; i < WM_MSG_TYPE_NUM; i++) {
45 }
46}
47
49{
50 wmMsgBus *mbus = MEM_new_zeroed<wmMsgBus>(__func__);
51 const uint gset_reserve = 512;
52 for (uint i = 0; i < WM_MSG_TYPE_NUM; i++) {
53 wmMsgTypeInfo *info = &wm_msg_types[i];
55 info->gset.hash_fn, info->gset.cmp_fn, __func__, gset_reserve);
56 }
57 return mbus;
58}
59
61{
62 for (uint i = 0; i < WM_MSG_TYPE_NUM; i++) {
63 wmMsgTypeInfo *info = &wm_msg_types[i];
64 BLI_gset_free(mbus->messages_gset[i], info->gset.key_free_fn);
65 }
66 MEM_delete(mbus);
67}
68
69void WM_msgbus_clear_by_owner(wmMsgBus *mbus, void *owner)
70{
71 wmMsgSubscribeKey *msg_key, *msg_key_next;
72 for (msg_key = mbus->messages.first(); msg_key; msg_key = msg_key_next) {
73 msg_key_next = msg_key->next;
74
75 wmMsgSubscribeValueLink *msg_lnk_next;
76 for (wmMsgSubscribeValueLink *msg_lnk = msg_key->values.first(); msg_lnk;
77 msg_lnk = msg_lnk_next)
78 {
79 msg_lnk_next = msg_lnk->next;
80 if (msg_lnk->params.owner == owner) {
81 if (msg_lnk->params.tag) {
82 mbus->messages_tag_count -= 1;
83 }
84 if (msg_lnk->params.free_data) {
85 msg_lnk->params.free_data(msg_key, &msg_lnk->params);
86 }
87 BLI_remlink(&msg_key->values, msg_lnk);
88 MEM_delete(msg_lnk);
89 }
90 }
91
92 if (msg_key->values.is_empty()) {
93 const wmMsg *msg = wm_msg_subscribe_value_msg_cast(msg_key);
94 wmMsgTypeInfo *info = &wm_msg_types[msg->type];
95 BLI_remlink(&mbus->messages, msg_key);
96 bool ok = BLI_gset_remove(mbus->messages_gset[msg->type], msg_key, info->gset.key_free_fn);
97 BLI_assert(ok);
99 }
100 }
101}
102
103void WM_msg_dump(wmMsgBus *mbus, const char *info_str)
104{
105 printf(">>>> %s\n", info_str);
106 for (wmMsgSubscribeKey &key : mbus->messages) {
107 const wmMsg *msg = wm_msg_subscribe_value_msg_cast(&key);
108 const wmMsgTypeInfo *info = &wm_msg_types[msg->type];
109 info->repr(stdout, &key);
110 }
111 printf("<<<< %s\n", info_str);
112}
113
115{
116 if (mbus->messages_tag_count == 0) {
117 // printf("msgbus: skipping\n");
118 return;
119 }
120
121 if (false) {
122 WM_msg_dump(mbus, __func__);
123 }
124
125 // uint a = 0, b = 0;
126 for (wmMsgSubscribeKey &key : mbus->messages) {
127 for (wmMsgSubscribeValueLink &msg_lnk : key.values) {
128 if (msg_lnk.params.tag) {
129 msg_lnk.params.notify(C, &key, &msg_lnk.params);
130 msg_lnk.params.tag = false;
131 mbus->messages_tag_count -= 1;
132 }
133 // b++;
134 }
135 // a++;
136 }
137 BLI_assert(mbus->messages_tag_count == 0);
138 mbus->messages_tag_count = 0;
139 // printf("msgbus: keys=%u values=%u\n", a, b);
140}
141
143 const wmMsgSubscribeKey *msg_key_test,
144 const wmMsgSubscribeValue *msg_val_params)
145{
146 const uint type = wm_msg_subscribe_value_msg_cast(msg_key_test)->type;
147 const wmMsgTypeInfo *info = &wm_msg_types[type];
149
150 BLI_assert(wm_msg_subscribe_value_msg_cast(msg_key_test)->id != nullptr);
151
152 void **r_key;
153 if (!BLI_gset_ensure_p_ex(mbus->messages_gset[type], msg_key_test, &r_key)) {
154 *r_key = info->gset.key_duplicate_fn(msg_key_test);
155 key = static_cast<wmMsgSubscribeKey *>(*r_key);
156 BLI_addtail(&mbus->messages, key);
157 }
158 else {
159 key = static_cast<wmMsgSubscribeKey *>(*r_key);
160 for (wmMsgSubscribeValueLink &msg_lnk : key->values) {
161 if ((msg_lnk.params.notify == msg_val_params->notify) &&
162 (msg_lnk.params.owner == msg_val_params->owner) &&
163 (msg_lnk.params.user_data == msg_val_params->user_data))
164 {
165 return key;
166 }
167 }
168 }
169
171 msg_lnk->params = *msg_val_params;
172 BLI_addtail(&key->values, msg_lnk);
173 return key;
174}
175
177{
179 "tagging subscribers: (ptr=%p, len=%d)",
180 msg_key,
181 msg_key->values.count());
182
183 for (wmMsgSubscribeValueLink &msg_lnk : msg_key->values) {
184 if (false) { /* Make an option? */
185 msg_lnk.params.notify(nullptr, msg_key, &msg_lnk.params);
186 }
187 else {
188 if (msg_lnk.params.tag == false) {
189 msg_lnk.params.tag = true;
190 mbus->messages_tag_count += 1;
191 }
192 }
193 }
194}
195
196void WM_msg_id_update(wmMsgBus *mbus, ID *id_src, ID *id_dst)
197{
198 for (uint i = 0; i < WM_MSG_TYPE_NUM; i++) {
199 wmMsgTypeInfo *info = &wm_msg_types[i];
200 if (info->update_by_id != nullptr) {
201 info->update_by_id(mbus, id_src, id_dst);
202 }
203 }
204}
205
206void WM_msg_id_remove(wmMsgBus *mbus, const ID *id)
207{
208 for (uint i = 0; i < WM_MSG_TYPE_NUM; i++) {
209 wmMsgTypeInfo *info = &wm_msg_types[i];
210 if (info->remove_by_id != nullptr) {
211 info->remove_by_id(mbus, id);
212 }
213 }
214}
215
217
218/* -------------------------------------------------------------------------- */
223
225{
226 if (msg_lnk->params.free_data) {
227 msg_lnk->params.free_data(msg_key, &msg_lnk->params);
228 }
229 BLI_remlink(&msg_key->values, msg_lnk);
230 MEM_delete(msg_lnk);
231}
232
234
235} // namespace blender
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UNUSED_VARS_NDEBUG(...)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
bool BLI_gset_ensure_p_ex(GSet *gs, const void *key, void ***r_key)
Definition BLI_ghash.cc:979
CLG_LogRef * WM_LOG_MSGBUS_SUB
BLI_INLINE const wmMsg * wm_msg_subscribe_value_msg_cast(const wmMsgSubscribeKey *key)
void WM_msg_id_update(wmMsgBus *mbus, ID *id_src, ID *id_dst)
void WM_msg_dump(wmMsgBus *mbus, const char *info_str)
void WM_msgbus_destroy(wmMsgBus *mbus)
unsigned int uint
Definition BKE_text.h:55
wmMsgBus * WM_msgbus_create()
void WM_msgbus_types_init()
void WM_msgbus_handle(wmMsgBus *mbus, bContext *C)
static wmMsgTypeInitFn wm_msg_init_fn[WM_MSG_TYPE_NUM]
wmMsgSubscribeKey * WM_msg_subscribe_with_key(wmMsgBus *mbus, const wmMsgSubscribeKey *msg_key_test, const wmMsgSubscribeValue *msg_val_params)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void wm_msg_subscribe_value_free(wmMsgSubscribeKey *msg_key, wmMsgSubscribeValueLink *msg_lnk)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void WM_msgtypeinfo_init_remote_io(wmMsgTypeInfo *msgtype_info)
static wmMsgTypeInfo wm_msg_types[WM_MSG_TYPE_NUM]
void WM_msg_publish_with_key(wmMsgBus *mbus, wmMsgSubscribeKey *msg_key)
void WM_msgbus_clear_by_owner(wmMsgBus *mbus, void *owner)
void(*)(wmMsgTypeInfo *) wmMsgTypeInitFn
void WM_msg_id_remove(wmMsgBus *mbus, const ID *id)
void WM_msgtypeinfo_init_static(wmMsgTypeInfo *msgtype_info)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
bool BLI_gset_remove(GSet *gs, const void *key, GSetKeyFreeFP keyfreefp)
GSet * BLI_gset_new_ex(GSetHashFP hashfp, GSetCmpFP cmpfp, const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:942
void WM_msgtypeinfo_init_rna(wmMsgTypeInfo *msgtype_info)
ListBaseT< wmMsgSubscribeKey > messages
GSet * messages_gset[WM_MSG_TYPE_NUM]
wmMsgSubscribeKey * next
ListBaseT< wmMsgSubscribeValueLink > values
wmMsgSubscribeValueFreeDataFn free_data
void(*) repr(FILE *stream, const wmMsgSubscribeKey *msg_key)
void(*) remove_by_id(wmMsgBus *mbus, const ID *id)
struct blender::wmMsgTypeInfo::@222252052342264033027352010177057107113002371022 gset
void(*) update_by_id(wmMsgBus *mbus, ID *id_src, ID *id_dst)
unsigned int type
#define WM_MSG_TYPE_NUM