Skip to content

Commit 0991d14

Browse files
committed
feat(util): use inline functions instead of global variables
BREAKING CHANGE: `DictType_StringKey` and `DictType_StringCopyKey` have been replaced by inline functions
1 parent 103544c commit 0991d14

12 files changed

Lines changed: 39 additions & 24 deletions

File tree

include/LCUI/util/dict.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,25 @@ LCUI_API int StringKeyDict_KeyCompare(void *privdata, const void *key1,
244244
LCUI_API void *StringKeyDict_KeyDup(void *privdata, const void *key);
245245
LCUI_API void StringKeyDict_KeyDestructor(void *privdata, void *key);
246246

247-
/* Hash table types */
248-
LCUI_API extern DictType DictType_StringKey;
249-
LCUI_API extern DictType DictType_StringCopyKey;
247+
INLINE void Dict_InitStringKeyType(DictType *t)
248+
{
249+
t->hashFunction = StringKeyDict_KeyHash;
250+
t->keyDup = NULL;
251+
t->valDup = NULL;
252+
t->keyCompare = StringKeyDict_KeyCompare;
253+
t->keyDestructor = NULL;
254+
t->valDestructor = NULL;
255+
}
256+
257+
INLINE void Dict_InitStringCopyKeyType(DictType *t)
258+
{
259+
t->hashFunction = StringKeyDict_KeyHash;
260+
t->keyDup = StringKeyDict_KeyDup;
261+
t->valDup = NULL;
262+
t->keyCompare = StringKeyDict_KeyCompare;
263+
t->keyDestructor = StringKeyDict_KeyDestructor;
264+
t->valDestructor = NULL;
265+
}
250266

251267
LCUI_END_HEADER
252268

src/font/fontlibrary.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ static void LCUIFont_InitBase(void)
836836
fontlib.font_cache = NEW(LCUI_FontCache, 1);
837837
fontlib.font_cache[0] = FontCache();
838838
RBTree_Init(&fontlib.bitmap_cache);
839-
fontlib.font_families_type = DictType_StringKey;
839+
Dict_InitStringKeyType(&fontlib.font_families_type);
840840
fontlib.font_families_type.valDestructor = DestroyFontFamilyNode;
841841
fontlib.font_families = Dict_Create(&fontlib.font_families_type, NULL);
842842
RBTree_OnDestroy(&fontlib.bitmap_cache, DestroyTreeNode);

src/gui/css_library.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,9 +1164,12 @@ static void DeleteStyleNode(StyleNode node)
11641164
static StyleLink CreateStyleLink(void)
11651165
{
11661166
StyleLink link = NEW(StyleLinkRec, 1);
1167+
static DictType t;
1168+
1169+
Dict_InitStringCopyKeyType(&t);
11671170
link->group = NULL;
11681171
LinkedList_Init(&link->styles);
1169-
link->parents = Dict_Create(&DictType_StringCopyKey, NULL);
1172+
link->parents = Dict_Create(&t, NULL);
11701173
return link;
11711174
}
11721175

@@ -1210,7 +1213,7 @@ static void InitStyleGroupDict(void)
12101213
{
12111214
DictType *dt = &library.style_group_dict;
12121215

1213-
*dt = DictType_StringCopyKey;
1216+
Dict_InitStringCopyKeyType(dt);
12141217
dt->valDestructor = StyleLinkGroupDestructor;
12151218
}
12161219

@@ -1700,7 +1703,7 @@ static void StyleLinkDestructor(void *privdata, void *data)
17001703

17011704
static void InitStyleLinkDict(void)
17021705
{
1703-
library.style_link_dict = DictType_StringCopyKey;
1706+
Dict_InitStringCopyKeyType(&library.style_link_dict);
17041707
library.style_link_dict.valDestructor = StyleLinkDestructor;
17051708
}
17061709

@@ -1731,7 +1734,7 @@ static void InitStyleValueLibrary(void)
17311734
memset(names_dt, 0, sizeof(DictType));
17321735
names_dt->keyCompare = IntKeyDict_KeyCompare;
17331736
names_dt->hashFunction = IntKeyDict_HashFunction;
1734-
*keys_dt = DictType_StringKey;
1737+
Dict_InitStringKeyType(keys_dt);
17351738
keys_dt->valDestructor = KeyNameGroupDestructor;
17361739
/* value_keys 表用于存放 key 和 name 数据 */
17371740
library.value_keys = Dict_Create(keys_dt, NULL);

src/gui/css_parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ void LCUI_InitCSSParser(void)
12891289
LCUI_CSSPropertyParser new_sp, sp, sp_end;
12901290

12911291
self.count = 0;
1292-
self.dicttype = DictType_StringKey;
1292+
Dict_InitStringKeyType(&self.dicttype);
12931293
self.dicttype.valDestructor = DestroyStyleParser;
12941294
self.parsers = Dict_Create(&self.dicttype, NULL);
12951295
sp_end = style_parser_map + LEN(style_parser_map);

src/gui/widget_attribute.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int Widget_SetAttributeEx(LCUI_Widget w, const char *name, void *value,
6161
LCUI_WidgetAttribute attr;
6262

6363
if (!self.available) {
64-
self.dt_attributes = DictType_StringCopyKey;
64+
Dict_InitStringKeyType(&self.dt_attributes);
6565
self.dt_attributes.valDestructor = OnClearWidgetAttribute;
6666
self.available = TRUE;
6767
}

src/gui/widget_background.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static void AsyncLoadImage(LCUI_Widget widget, const char *path)
193193
void LCUIWidget_InitImageLoader(void)
194194
{
195195
RBTree_Init(&self.refs);
196-
self.dtype = DictType_StringKey;
196+
Dict_InitStringKeyType(&self.dtype);
197197
self.dtype.valDestructor = ImageCacheDestructor;
198198
self.images = Dict_Create(&self.dtype, NULL);
199199
RBTree_OnCompare(&self.refs, OnCompareWidget);

src/gui/widget_event.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ static struct LCUIWidgetEvnetModule {
9797
LinkedList event_mappings; /**< 事件标识号和名称映射记录列表 */
9898
RBTree event_records; /**< 当前正执行的事件的记录 */
9999
RBTree event_names; /**< 事件名称表,以标识号作为索引 */
100+
DictType event_ids_type;
100101
Dict *event_ids; /**< 事件标识号表,以事件名称作为索引 */
101102
int base_event_id; /**< 事件标识号计数器 */
102103
ClickRecord click; /**< 上次鼠标点击记录 */
@@ -1305,7 +1306,8 @@ void LCUIWidget_InitEvent(void)
13051306
self.click.widget = NULL;
13061307
self.click.interval = DBLCLICK_INTERVAL;
13071308
self.base_event_id = LCUI_WEVENT_USER + 1000;
1308-
self.event_ids = Dict_Create(&DictType_StringKey, NULL);
1309+
Dict_InitStringKeyType(&self.event_ids_type);
1310+
self.event_ids = Dict_Create(&self.event_ids_type, NULL);
13091311
n = sizeof(mappings) / sizeof(mappings[0]);
13101312
for (i = 0; i < n; ++i) {
13111313
LCUIWidget_SetEventName(mappings[i].id, mappings[i].name);

src/gui/widget_helper.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,10 @@ static void Widget_CollectReference(LCUI_Widget w, void *arg)
251251
Dict *Widget_CollectReferences(LCUI_Widget w)
252252
{
253253
Dict *dict;
254+
static DictType t;
254255

255-
dict = Dict_Create(&DictType_StringKey, NULL);
256+
Dict_InitStringKeyType(&t);
257+
dict = Dict_Create(&t, NULL);
256258
Widget_Each(w, Widget_CollectReference, dict);
257259
return dict;
258260
}

src/gui/widget_id.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static void OnClearWidgetList(void *privdata, void *data)
141141
void LCUIWidget_InitIdLibrary(void)
142142
{
143143
LCUIMutex_Init(&self.mutex);
144-
self.dt_ids = DictType_StringCopyKey;
144+
Dict_InitStringCopyKeyType(&self.dt_ids);
145145
self.dt_ids.valDestructor = OnClearWidgetList;
146146
self.ids = Dict_Create(&self.dt_ids, NULL);
147147
}

src/gui/widget_prototype.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static void DeletePrototype(void *privdata, void *data)
8686

8787
void LCUIWidget_InitPrototype(void)
8888
{
89-
self.dicttype = DictType_StringKey;
89+
Dict_InitStringKeyType(&self.dicttype);
9090
self.dicttype.valDestructor = DeletePrototype;
9191
self.prototypes = Dict_Create(&self.dicttype, NULL);
9292
self.default_prototype.name = NULL;

0 commit comments

Comments
 (0)