Blender V5.0
addon.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 <cstddef>
10#include <cstdlib>
11
12#include "RNA_types.hh"
13
14#include "BLI_ghash.h"
15#include "BLI_listbase.h"
16#include "BLI_string.h"
17
18#include "BKE_addon.h" /* own include */
19#include "BKE_idprop.hh"
20
21#include "DNA_listBase.h"
22#include "DNA_userdef_types.h"
23
24#include "MEM_guardedalloc.h"
25
26#include "CLG_log.h"
27
28static CLG_LogRef LOG = {"addon"};
29
30/* -------------------------------------------------------------------- */
33
35{
36 bAddon *addon = MEM_callocN<bAddon>("bAddon");
37 return addon;
38}
39
40bAddon *BKE_addon_find(const ListBase *addon_list, const char *module)
41{
42 return static_cast<bAddon *>(BLI_findstring(addon_list, module, offsetof(bAddon, module)));
43}
44
45bAddon *BKE_addon_ensure(ListBase *addon_list, const char *module)
46{
47 bAddon *addon = BKE_addon_find(addon_list, module);
48 if (addon == nullptr) {
49 addon = BKE_addon_new();
50 STRNCPY(addon->module, module);
51 BLI_addtail(addon_list, addon);
52 }
53 return addon;
54}
55
56bool BKE_addon_remove_safe(ListBase *addon_list, const char *module)
57{
58 bAddon *addon = static_cast<bAddon *>(
60 if (addon) {
61 BLI_remlink(addon_list, addon);
62 BKE_addon_free(addon);
63 return true;
64 }
65 return false;
66}
67
69{
70 if (addon->prop) {
71 IDP_FreeProperty(addon->prop);
72 }
73 MEM_freeN(addon);
74}
75
77
78/* -------------------------------------------------------------------- */
81
83
84bAddonPrefType *BKE_addon_pref_type_find(const char *idname, bool quiet)
85{
86 if (idname[0]) {
87 bAddonPrefType *apt;
88
89 apt = static_cast<bAddonPrefType *>(BLI_ghash_lookup(global_addonpreftype_hash, idname));
90 if (apt) {
91 return apt;
92 }
93
94 if (!quiet) {
95 CLOG_WARN(&LOG, "search for unknown addon-pref '%s'", idname);
96 }
97 }
98 else {
99 if (!quiet) {
100 CLOG_WARN(&LOG, "search for empty addon-pref");
101 }
102 }
103
104 return nullptr;
105}
106
111
116
122
128
struct bAddonPrefType bAddonPrefType
Definition BKE_addon.h:23
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1251
#define BLI_assert(a)
Definition BLI_assert.h:46
GHash * BLI_ghash_str_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
bool BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:787
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:731
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:707
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:860
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:608
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:131
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:693
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
These structs are the foundation for all linked lists in the library system.
Read Guarded memory(de)allocation.
void BKE_addon_free(bAddon *addon)
Definition addon.cc:68
void BKE_addon_pref_type_add(bAddonPrefType *apt)
Definition addon.cc:107
bAddon * BKE_addon_ensure(ListBase *addon_list, const char *module)
Definition addon.cc:45
bAddonPrefType * BKE_addon_pref_type_find(const char *idname, bool quiet)
Definition addon.cc:84
void BKE_addon_pref_type_init()
Definition addon.cc:117
bAddon * BKE_addon_find(const ListBase *addon_list, const char *module)
Definition addon.cc:40
void BKE_addon_pref_type_remove(const bAddonPrefType *apt)
Definition addon.cc:112
bool BKE_addon_remove_safe(ListBase *addon_list, const char *module)
Definition addon.cc:56
static GHash * global_addonpreftype_hash
Definition addon.cc:82
void BKE_addon_pref_type_free()
Definition addon.cc:123
bAddon * BKE_addon_new()
Definition addon.cc:34
#define offsetof(t, d)
#define LOG(level)
Definition log.h:97
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
static struct PyModuleDef module
Definition python.cpp:796
struct IDProperty * prop
char module[128]