Blender  V2.93
wm_message_bus_static.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <stdio.h>
22 
23 #include "CLG_log.h"
24 
25 #include "MEM_guardedalloc.h"
26 
27 #include "BLI_ghash.h"
28 #include "BLI_listbase.h"
29 #include "BLI_utildefines.h"
30 
31 #include "WM_message.h"
32 #include "WM_types.h"
34 
35 /* -------------------------------------------------------------------------- */
36 
37 static uint wm_msg_static_gset_hash(const void *key_p)
38 {
39  const wmMsgSubscribeKey_Static *key = key_p;
40  const wmMsgParams_Static *params = &key->msg.params;
41  uint k = params->event;
42  return k;
43 }
44 static bool wm_msg_static_gset_cmp(const void *key_a_p, const void *key_b_p)
45 {
46  const wmMsgParams_Static *params_a = &((const wmMsgSubscribeKey_Static *)key_a_p)->msg.params;
47  const wmMsgParams_Static *params_b = &((const wmMsgSubscribeKey_Static *)key_b_p)->msg.params;
48  return !((params_a->event == params_b->event));
49 }
50 static void wm_msg_static_gset_key_free(void *key_p)
51 {
52  wmMsgSubscribeKey *key = key_p;
53  wmMsgSubscribeValueLink *msg_lnk_next;
54  for (wmMsgSubscribeValueLink *msg_lnk = key->values.first; msg_lnk; msg_lnk = msg_lnk_next) {
55  msg_lnk_next = msg_lnk->next;
56  BLI_remlink(&key->values, msg_lnk);
57  MEM_freeN(msg_lnk);
58  }
59  MEM_freeN(key);
60 }
61 
62 static void wm_msg_static_repr(FILE *stream, const wmMsgSubscribeKey *msg_key)
63 {
65  fprintf(stream,
66  "<wmMsg_Static %p, "
67  "id='%s', "
68  "values_len=%d\n",
69  m,
70  m->msg.head.id,
72 }
73 
75 {
76  msgtype_info->gset.hash_fn = wm_msg_static_gset_hash;
77  msgtype_info->gset.cmp_fn = wm_msg_static_gset_cmp;
79  msgtype_info->repr = wm_msg_static_repr;
80 
81  msgtype_info->msg_key_size = sizeof(wmMsgSubscribeKey_Static);
82 }
83 
84 /* -------------------------------------------------------------------------- */
85 
87  const wmMsgParams_Static *msg_key_params)
88 {
89  wmMsgSubscribeKey_Static key_test;
90  key_test.msg.params = *msg_key_params;
91  return BLI_gset_lookup(mbus->messages_gset[WM_MSG_TYPE_STATIC], &key_test);
92 }
93 
94 void WM_msg_publish_static_params(struct wmMsgBus *mbus, const wmMsgParams_Static *msg_key_params)
95 {
96  CLOG_INFO(WM_LOG_MSGBUS_PUB, 2, "static(event=%d)", msg_key_params->event);
97 
98  wmMsgSubscribeKey_Static *key = WM_msg_lookup_static(mbus, msg_key_params);
99  if (key) {
100  WM_msg_publish_with_key(mbus, &key->head);
101  }
102 }
103 
104 void WM_msg_publish_static(struct wmMsgBus *mbus, int event)
105 {
108  .event = event,
109  });
110 }
111 
113  const wmMsgParams_Static *msg_key_params,
114  const wmMsgSubscribeValue *msg_val_params,
115  const char *id_repr)
116 {
117  wmMsgSubscribeKey_Static msg_key_test = {{NULL}};
118 
119  /* use when added */
120  msg_key_test.msg.head.id = id_repr;
121  msg_key_test.msg.head.type = WM_MSG_TYPE_STATIC;
122  /* for lookup */
123  msg_key_test.msg.params = *msg_key_params;
124 
125  WM_msg_subscribe_with_key(mbus, &msg_key_test.head, msg_val_params);
126 }
127 
129  int event,
130  const wmMsgSubscribeValue *msg_val_params,
131  const char *id_repr)
132 {
134  &(const wmMsgParams_Static){
135  .event = event,
136  },
137  msg_val_params,
138  id_repr);
139 }
void * BLI_gset_lookup(GSet *gs, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:1280
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
unsigned int uint
Definition: BLI_sys_types.h:83
#define CLOG_INFO(clg_ref, level,...)
Definition: CLG_log.h:201
Read Guarded memory(de)allocation.
struct CLG_LogRef * WM_LOG_MSGBUS_PUB
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void * first
Definition: DNA_listBase.h:47
struct GSet * messages_gset[WM_MSG_TYPE_NUM]
wmMsgSubscribeKey head
void(* repr)(FILE *stream, const struct wmMsgSubscribeKey *msg_key)
unsigned int(* hash_fn)(const void *msg)
struct wmMsgTypeInfo::@1166 gset
void(* key_free_fn)(void *key)
bool(* cmp_fn)(const void *a, const void *b)
wmMsgParams_Static params
unsigned int type
const char * id
void WM_msg_publish_with_key(struct wmMsgBus *mbus, wmMsgSubscribeKey *msg_key)
wmMsgSubscribeKey * WM_msg_subscribe_with_key(struct wmMsgBus *mbus, const wmMsgSubscribeKey *msg_key_test, const wmMsgSubscribeValue *msg_val_params)
@ WM_MSG_TYPE_STATIC
struct wmMsgSubscribeKey_Static wmMsgSubscribeKey_Static
static bool wm_msg_static_gset_cmp(const void *key_a_p, const void *key_b_p)
void WM_msg_publish_static(struct wmMsgBus *mbus, int event)
void WM_msg_subscribe_static(struct wmMsgBus *mbus, int event, const wmMsgSubscribeValue *msg_val_params, const char *id_repr)
void WM_msgtypeinfo_init_static(wmMsgTypeInfo *msgtype_info)
void WM_msg_publish_static_params(struct wmMsgBus *mbus, const wmMsgParams_Static *msg_key_params)
wmMsgSubscribeKey_Static * WM_msg_lookup_static(struct wmMsgBus *mbus, const wmMsgParams_Static *msg_key_params)
static void wm_msg_static_repr(FILE *stream, const wmMsgSubscribeKey *msg_key)
static uint wm_msg_static_gset_hash(const void *key_p)
void WM_msg_subscribe_static_params(struct wmMsgBus *mbus, const wmMsgParams_Static *msg_key_params, const wmMsgSubscribeValue *msg_val_params, const char *id_repr)
static void wm_msg_static_gset_key_free(void *key_p)