Blender  V2.93
idtype.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  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
17  * All rights reserved.
18  * Modifier stack implementation.
19  *
20  * BKE_modifier.h contains the function prototypes for this file.
21  */
22 
27 #include <string.h>
28 
29 #include "MEM_guardedalloc.h"
30 
31 #include "BLI_ghash.h"
32 #include "BLI_utildefines.h"
33 
34 #include "CLG_log.h"
35 
36 #include "BLT_translation.h"
37 
38 #include "DNA_ID.h"
39 #include "DNA_collection_types.h"
40 #include "DNA_node_types.h"
41 #include "DNA_scene_types.h"
42 
43 #include "BKE_main.h"
44 #include "BKE_node.h"
45 
46 #include "BKE_idtype.h"
47 
48 // static CLG_LogRef LOG = {"bke.idtype"};
49 
50 uint BKE_idtype_cache_key_hash(const void *key_v)
51 {
52  const IDCacheKey *key = key_v;
56 }
57 
58 bool BKE_idtype_cache_key_cmp(const void *key_a_v, const void *key_b_v)
59 {
60  const IDCacheKey *key_a = key_a_v;
61  const IDCacheKey *key_b = key_b_v;
62  return (key_a->id_session_uuid != key_b->id_session_uuid) ||
63  (key_a->offset_in_ID != key_b->offset_in_ID) || (key_a->cache_v != key_b->cache_v);
64 }
65 
67 
68 static void id_type_init(void)
69 {
70 #define INIT_TYPE(_id_code) \
71  { \
72  BLI_assert(IDType_##_id_code.main_listbase_index == INDEX_##_id_code); \
73  id_types[INDEX_##_id_code] = &IDType_##_id_code; \
74  } \
75  (void)0
76 
100  INIT_TYPE(ID_NT);
101  INIT_TYPE(ID_BR);
102  INIT_TYPE(ID_PA);
103  INIT_TYPE(ID_GD);
104  INIT_TYPE(ID_WM);
105  INIT_TYPE(ID_MC);
106  INIT_TYPE(ID_MSK);
107  INIT_TYPE(ID_LS);
108  INIT_TYPE(ID_PAL);
109  INIT_TYPE(ID_PC);
110  INIT_TYPE(ID_CF);
111  INIT_TYPE(ID_WS);
112  INIT_TYPE(ID_LP);
113  INIT_TYPE(ID_HA);
114  INIT_TYPE(ID_PT);
115  INIT_TYPE(ID_VO);
116  INIT_TYPE(ID_SIM);
117 
118  /* Special naughty boy... */
121 
122 #undef INIT_TYPE
123 }
124 
125 void BKE_idtype_init(void)
126 {
127  /* Initialize data-block types. */
128  id_type_init();
129 }
130 
131 const IDTypeInfo *BKE_idtype_get_info_from_idcode(const short id_code)
132 {
133  int id_index = BKE_idtype_idcode_to_index(id_code);
134 
135  if (id_index >= 0 && id_index < ARRAY_SIZE(id_types) && id_types[id_index] != NULL &&
136  id_types[id_index]->name[0] != '\0') {
137  return id_types[id_index];
138  }
139 
140  return NULL;
141 }
142 
144 {
146 }
147 
148 static const IDTypeInfo *idtype_get_info_from_name(const char *idtype_name)
149 {
150  for (int i = ARRAY_SIZE(id_types); i--;) {
151  if (id_types[i] != NULL && STREQ(idtype_name, id_types[i]->name)) {
152  return id_types[i];
153  }
154  }
155 
156  return NULL;
157 }
158 
159 /* Various helpers/wrappers around #IDTypeInfo structure. */
160 
168 const char *BKE_idtype_idcode_to_name(const short idcode)
169 {
170  const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
171  BLI_assert(id_type != NULL);
172  return id_type != NULL ? id_type->name : NULL;
173 }
174 
182 const char *BKE_idtype_idcode_to_name_plural(const short idcode)
183 {
184  const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
185  BLI_assert(id_type != NULL);
186  return id_type != NULL ? id_type->name_plural : NULL;
187 }
188 
195 const char *BKE_idtype_idcode_to_translation_context(const short idcode)
196 {
197  const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
198  BLI_assert(id_type != NULL);
199  return id_type != NULL ? id_type->translation_context : BLT_I18NCONTEXT_DEFAULT;
200 }
201 
208 short BKE_idtype_idcode_from_name(const char *idtype_name)
209 {
210  const IDTypeInfo *id_type = idtype_get_info_from_name(idtype_name);
211  BLI_assert(id_type);
212  return id_type != NULL ? id_type->id_code : 0;
213 }
214 
221 bool BKE_idtype_idcode_is_valid(const short idcode)
222 {
223  return BKE_idtype_get_info_from_idcode(idcode) != NULL ? true : false;
224 }
225 
232 bool BKE_idtype_idcode_is_linkable(const short idcode)
233 {
234  const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
235  BLI_assert(id_type != NULL);
236  return id_type != NULL ? (id_type->flags & IDTYPE_FLAGS_NO_LIBLINKING) == 0 : false;
237 }
238 
243 {
244 #define CASE_IDFILTER(_id) \
245  case ID_##_id: \
246  return FILTER_ID_##_id
247 
248  switch (idcode) {
249  CASE_IDFILTER(AC);
250  CASE_IDFILTER(AR);
251  CASE_IDFILTER(BR);
252  CASE_IDFILTER(CA);
253  CASE_IDFILTER(CF);
254  CASE_IDFILTER(CU);
255  CASE_IDFILTER(GD);
256  CASE_IDFILTER(GR);
257  CASE_IDFILTER(HA);
258  CASE_IDFILTER(IM);
259  CASE_IDFILTER(LA);
260  CASE_IDFILTER(LS);
261  CASE_IDFILTER(LT);
262  CASE_IDFILTER(MA);
263  CASE_IDFILTER(MB);
264  CASE_IDFILTER(MC);
265  CASE_IDFILTER(ME);
266  CASE_IDFILTER(MSK);
267  CASE_IDFILTER(NT);
268  CASE_IDFILTER(OB);
269  CASE_IDFILTER(PA);
270  CASE_IDFILTER(PAL);
271  CASE_IDFILTER(PC);
272  CASE_IDFILTER(PT);
273  CASE_IDFILTER(LP);
274  CASE_IDFILTER(SCE);
275  CASE_IDFILTER(SIM);
276  CASE_IDFILTER(SPK);
277  CASE_IDFILTER(SO);
278  CASE_IDFILTER(TE);
279  CASE_IDFILTER(TXT);
280  CASE_IDFILTER(VF);
281  CASE_IDFILTER(VO);
282  CASE_IDFILTER(WO);
283  CASE_IDFILTER(WS);
284  default:
285  return 0;
286  }
287 
288 #undef CASE_IDFILTER
289 }
290 
295 {
296 #define CASE_IDFILTER(_id) \
297  case FILTER_ID_##_id: \
298  return ID_##_id
299 
300  switch (idfilter) {
301  CASE_IDFILTER(AC);
302  CASE_IDFILTER(AR);
303  CASE_IDFILTER(BR);
304  CASE_IDFILTER(CA);
305  CASE_IDFILTER(CF);
306  CASE_IDFILTER(CU);
307  CASE_IDFILTER(GD);
308  CASE_IDFILTER(GR);
309  CASE_IDFILTER(HA);
310  CASE_IDFILTER(IM);
311  CASE_IDFILTER(LA);
312  CASE_IDFILTER(LS);
313  CASE_IDFILTER(LT);
314  CASE_IDFILTER(MA);
315  CASE_IDFILTER(MB);
316  CASE_IDFILTER(MC);
317  CASE_IDFILTER(ME);
318  CASE_IDFILTER(MSK);
319  CASE_IDFILTER(NT);
320  CASE_IDFILTER(OB);
321  CASE_IDFILTER(PA);
322  CASE_IDFILTER(PAL);
323  CASE_IDFILTER(PC);
324  CASE_IDFILTER(PT);
325  CASE_IDFILTER(LP);
326  CASE_IDFILTER(SCE);
327  CASE_IDFILTER(SIM);
328  CASE_IDFILTER(SPK);
329  CASE_IDFILTER(SO);
330  CASE_IDFILTER(TE);
331  CASE_IDFILTER(TXT);
332  CASE_IDFILTER(VF);
333  CASE_IDFILTER(VO);
334  CASE_IDFILTER(WO);
335  default:
336  return 0;
337  }
338 
339 #undef CASE_IDFILTER
340 }
341 
345 int BKE_idtype_idcode_to_index(const short idcode)
346 {
347 #define CASE_IDINDEX(_id) \
348  case ID_##_id: \
349  return INDEX_ID_##_id
350 
351  switch ((ID_Type)idcode) {
352  CASE_IDINDEX(AC);
353  CASE_IDINDEX(AR);
354  CASE_IDINDEX(BR);
355  CASE_IDINDEX(CA);
356  CASE_IDINDEX(CF);
357  CASE_IDINDEX(CU);
358  CASE_IDINDEX(GD);
359  CASE_IDINDEX(GR);
360  CASE_IDINDEX(HA);
361  CASE_IDINDEX(IM);
362  CASE_IDINDEX(IP);
363  CASE_IDINDEX(KE);
364  CASE_IDINDEX(LA);
365  CASE_IDINDEX(LI);
366  CASE_IDINDEX(LS);
367  CASE_IDINDEX(LT);
368  CASE_IDINDEX(MA);
369  CASE_IDINDEX(MB);
370  CASE_IDINDEX(MC);
371  CASE_IDINDEX(ME);
372  CASE_IDINDEX(MSK);
373  CASE_IDINDEX(NT);
374  CASE_IDINDEX(OB);
375  CASE_IDINDEX(PA);
376  CASE_IDINDEX(PAL);
377  CASE_IDINDEX(PC);
378  CASE_IDINDEX(PT);
379  CASE_IDINDEX(LP);
380  CASE_IDINDEX(SCE);
381  CASE_IDINDEX(SCR);
382  CASE_IDINDEX(SIM);
383  CASE_IDINDEX(SPK);
384  CASE_IDINDEX(SO);
385  CASE_IDINDEX(TE);
386  CASE_IDINDEX(TXT);
387  CASE_IDINDEX(VF);
388  CASE_IDINDEX(VO);
389  CASE_IDINDEX(WM);
390  CASE_IDINDEX(WO);
391  CASE_IDINDEX(WS);
392  }
393 
394  /* Special naughty boy... */
395  if (idcode == ID_LINK_PLACEHOLDER) {
396  return INDEX_ID_NULL;
397  }
398 
399  return -1;
400 
401 #undef CASE_IDINDEX
402 }
403 
407 short BKE_idtype_idcode_from_index(const int index)
408 {
409 #define CASE_IDCODE(_id) \
410  case INDEX_ID_##_id: \
411  return ID_##_id
412 
413  switch (index) {
414  CASE_IDCODE(AC);
415  CASE_IDCODE(AR);
416  CASE_IDCODE(BR);
417  CASE_IDCODE(CA);
418  CASE_IDCODE(CF);
419  CASE_IDCODE(CU);
420  CASE_IDCODE(GD);
421  CASE_IDCODE(GR);
422  CASE_IDCODE(HA);
423  CASE_IDCODE(IM);
424  CASE_IDCODE(IP);
425  CASE_IDCODE(KE);
426  CASE_IDCODE(LA);
427  CASE_IDCODE(LI);
428  CASE_IDCODE(LS);
429  CASE_IDCODE(LT);
430  CASE_IDCODE(MA);
431  CASE_IDCODE(MB);
432  CASE_IDCODE(MC);
433  CASE_IDCODE(ME);
434  CASE_IDCODE(MSK);
435  CASE_IDCODE(NT);
436  CASE_IDCODE(OB);
437  CASE_IDCODE(PA);
438  CASE_IDCODE(PAL);
439  CASE_IDCODE(PC);
440  CASE_IDCODE(PT);
441  CASE_IDCODE(LP);
442  CASE_IDCODE(SCE);
443  CASE_IDCODE(SCR);
444  CASE_IDCODE(SIM);
445  CASE_IDCODE(SPK);
446  CASE_IDCODE(SO);
447  CASE_IDCODE(TE);
448  CASE_IDCODE(TXT);
449  CASE_IDCODE(VF);
450  CASE_IDCODE(VO);
451  CASE_IDCODE(WM);
452  CASE_IDCODE(WO);
453  CASE_IDCODE(WS);
454  }
455 
456  /* Special naughty boy... */
457  if (index == INDEX_ID_NULL) {
458  return ID_LINK_PLACEHOLDER;
459  }
460 
461  return -1;
462 
463 #undef CASE_IDCODE
464 }
465 
473 {
474  return (*index < ARRAY_SIZE(id_types)) ? BKE_idtype_idcode_from_index((*index)++) : 0;
475 }
476 
481  IDTypeForeachCacheFunctionCallback function_callback,
482  void *user_data)
483 {
484  const IDTypeInfo *type_info = BKE_idtype_get_info_from_id(id);
485  if (type_info->foreach_cache != NULL) {
486  type_info->foreach_cache(id, function_callback, user_data);
487  }
488 
489  /* Handle 'private IDs'. */
490  bNodeTree *nodetree = ntreeFromID(id);
491  if (nodetree != NULL) {
492  type_info = BKE_idtype_get_info_from_id(&nodetree->id);
493  if (type_info == NULL) {
494  /* Very old .blend file seem to have empty names for their embedded node trees, see
495  * `blo_do_versions_250()`. Assume those are nodetrees then. */
497  }
498  if (type_info->foreach_cache != NULL) {
499  type_info->foreach_cache(&nodetree->id, function_callback, user_data);
500  }
501  }
502 
503  if (GS(id->name) == ID_SCE) {
504  Scene *scene = (Scene *)id;
505  if (scene->master_collection != NULL) {
507  if (type_info->foreach_cache != NULL) {
508  type_info->foreach_cache(&scene->master_collection->id, function_callback, user_data);
509  }
510  }
511  }
512 }
@ IDTYPE_FLAGS_NO_LIBLINKING
Definition: BKE_idtype.h:47
IDTypeInfo IDType_ID_LINK_PLACEHOLDER
Definition: lib_id.c:93
void(* IDTypeForeachCacheFunctionCallback)(struct ID *id, const struct IDCacheKey *cache_key, void **cache_p, uint flags, void *user_data)
Definition: BKE_idtype.h:89
struct bNodeTree * ntreeFromID(struct ID *id)
Definition: node.cc:3147
#define BLI_assert(a)
Definition: BLI_assert.h:58
size_t BLI_ghashutil_combine_hash(size_t hash_a, size_t hash_b)
unsigned int BLI_ghashutil_uinthash(unsigned int key)
unsigned int BLI_ghashutil_ptrhash(const void *key)
unsigned int uint
Definition: BLI_sys_types.h:83
#define ARRAY_SIZE(arr)
#define STREQ(a, b)
#define BLT_I18NCONTEXT_DEFAULT
ID and Library types, which are fundamental for sdna.
@ INDEX_ID_NULL
Definition: DNA_ID.h:858
@ INDEX_ID_MAX
Definition: DNA_ID.h:859
ID_Type
Definition: DNA_ID_enums.h:56
@ ID_WM
Definition: DNA_ID_enums.h:84
@ ID_CA
Definition: DNA_ID_enums.h:68
@ ID_AR
Definition: DNA_ID_enums.h:78
@ ID_MC
Definition: DNA_ID_enums.h:85
@ ID_CF
Definition: DNA_ID_enums.h:90
@ ID_LI
Definition: DNA_ID_enums.h:58
@ ID_TE
Definition: DNA_ID_enums.h:64
@ ID_IM
Definition: DNA_ID_enums.h:65
@ ID_VO
Definition: DNA_ID_enums.h:95
@ ID_WS
Definition: DNA_ID_enums.h:91
@ ID_NT
Definition: DNA_ID_enums.h:80
@ ID_LA
Definition: DNA_ID_enums.h:67
@ ID_KE
Definition: DNA_ID_enums.h:70
@ ID_TXT
Definition: DNA_ID_enums.h:74
@ ID_SO
Definition: DNA_ID_enums.h:76
@ ID_SCE
Definition: DNA_ID_enums.h:57
@ ID_LS
Definition: DNA_ID_enums.h:87
@ ID_MSK
Definition: DNA_ID_enums.h:86
@ ID_GD
Definition: DNA_ID_enums.h:83
@ ID_PAL
Definition: DNA_ID_enums.h:88
@ ID_BR
Definition: DNA_ID_enums.h:81
@ ID_LP
Definition: DNA_ID_enums.h:92
@ ID_HA
Definition: DNA_ID_enums.h:93
@ ID_WO
Definition: DNA_ID_enums.h:71
@ ID_SIM
Definition: DNA_ID_enums.h:96
@ ID_MA
Definition: DNA_ID_enums.h:63
@ ID_AC
Definition: DNA_ID_enums.h:79
@ ID_SCR
Definition: DNA_ID_enums.h:72
@ ID_VF
Definition: DNA_ID_enums.h:73
@ ID_ME
Definition: DNA_ID_enums.h:60
@ ID_IP
Definition: DNA_ID_enums.h:69
@ ID_GR
Definition: DNA_ID_enums.h:77
@ ID_SPK
Definition: DNA_ID_enums.h:75
@ ID_MB
Definition: DNA_ID_enums.h:62
@ ID_LT
Definition: DNA_ID_enums.h:66
@ ID_OB
Definition: DNA_ID_enums.h:59
@ ID_PA
Definition: DNA_ID_enums.h:82
@ ID_PT
Definition: DNA_ID_enums.h:94
@ ID_CU
Definition: DNA_ID_enums.h:61
@ ID_PC
Definition: DNA_ID_enums.h:89
#define ID_LINK_PLACEHOLDER
Definition: DNA_ID_enums.h:100
Object groups, one object can be in many groups at once.
Read Guarded memory(de)allocation.
#define BR
Definition: boxpack_2d.c:98
Scene scene
void * user_data
const char * BKE_idtype_idcode_to_name_plural(const short idcode)
Definition: idtype.c:182
#define INIT_TYPE(_id_code)
#define CASE_IDINDEX(_id)
#define CASE_IDCODE(_id)
short BKE_idtype_idcode_from_idfilter(const uint64_t idfilter)
Definition: idtype.c:294
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition: idtype.c:143
const char * BKE_idtype_idcode_to_translation_context(const short idcode)
Definition: idtype.c:195
bool BKE_idtype_cache_key_cmp(const void *key_a_v, const void *key_b_v)
Definition: idtype.c:58
short BKE_idtype_idcode_from_index(const int index)
Definition: idtype.c:407
bool BKE_idtype_idcode_is_linkable(const short idcode)
Definition: idtype.c:232
bool BKE_idtype_idcode_is_valid(const short idcode)
Definition: idtype.c:221
static const IDTypeInfo * idtype_get_info_from_name(const char *idtype_name)
Definition: idtype.c:148
#define CASE_IDFILTER(_id)
void BKE_idtype_id_foreach_cache(struct ID *id, IDTypeForeachCacheFunctionCallback function_callback, void *user_data)
Definition: idtype.c:480
short BKE_idtype_idcode_iter_step(int *index)
Definition: idtype.c:472
const char * BKE_idtype_idcode_to_name(const short idcode)
Definition: idtype.c:168
const IDTypeInfo * BKE_idtype_get_info_from_idcode(const short id_code)
Definition: idtype.c:131
short BKE_idtype_idcode_from_name(const char *idtype_name)
Definition: idtype.c:208
static void id_type_init(void)
Definition: idtype.c:68
static IDTypeInfo * id_types[INDEX_ID_MAX]
Definition: idtype.c:66
int BKE_idtype_idcode_to_index(const short idcode)
Definition: idtype.c:345
uint BKE_idtype_cache_key_hash(const void *key_v)
Definition: idtype.c:50
uint64_t BKE_idtype_idcode_to_idfilter(const short idcode)
Definition: idtype.c:242
void BKE_idtype_init(void)
Definition: idtype.c:125
#define GS(x)
Definition: iris.c:241
#define LT
#define hash
Definition: noise.c:169
unsigned __int64 uint64_t
Definition: stdint.h:93
unsigned int id_session_uuid
Definition: BKE_idtype.h:56
size_t offset_in_ID
Definition: BKE_idtype.h:59
void * cache_v
Definition: BKE_idtype.h:61
int main_listbase_index
Definition: BKE_idtype.h:132
short id_code
Definition: BKE_idtype.h:120
const char * name
Definition: BKE_idtype.h:138
const char * name_plural
Definition: BKE_idtype.h:140
IDTypeForeachCacheFunction foreach_cache
Definition: BKE_idtype.h:184
uint32_t flags
Definition: BKE_idtype.h:145
const char * translation_context
Definition: BKE_idtype.h:142
Definition: DNA_ID.h:273
char name[66]
Definition: DNA_ID.h:283
struct Collection * master_collection