Skip to content

Commit 52b6200

Browse files
authored
feat: add luaA_class_add_properties (#4051)
* feat: add luaA_class_add_properties * feat: add array_growx and p_growx * chore: luaA_class_add_properties use growx * feat: add array_inserts * chore: luaA_class_add_properties use inserts
1 parent 471517a commit 52b6200

File tree

5 files changed

+248
-176
lines changed

5 files changed

+248
-176
lines changed

common/array.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
static inline void pfx##_array_grow(pfx##_array_t *arr, int newlen) { \
7777
p_grow(&arr->tab, newlen, &arr->size); \
7878
} \
79+
static inline void pfx##_array_growx(pfx##_array_t *arr, int newlen) { \
80+
p_growx(&arr->tab, newlen, &arr->size); \
81+
} \
7982
static inline void \
8083
pfx##_array_splice(pfx##_array_t *arr, int pos, int len, \
8184
type_t items[], int count) \
@@ -138,6 +141,14 @@
138141
} \
139142
pfx##_array_splice(arr, r, 0, &e, 1); \
140143
} \
144+
static inline void \
145+
pfx##_array_inserts(pfx##_array_t *arr, type_t items[], int count) \
146+
{ \
147+
pfx##_array_growx(arr, arr->len + count); \
148+
memcpy(arr->tab + arr->len, items, count * sizeof(*items)); \
149+
arr->len += count; \
150+
qsort(arr->tab, arr->len, sizeof(*items), cmp); \
151+
} \
141152
static inline type_t * \
142153
pfx##_array_lookup(pfx##_array_t *arr, type_t *e) \
143154
{ \

common/luaclass.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@
2424

2525
#define CONNECTED_SUFFIX "::connected"
2626

27-
struct lua_class_property
28-
{
29-
/** Name of the property */
30-
const char *name;
31-
/** Callback function called when the property is found in object creation. */
32-
lua_class_propfunc_t new;
33-
/** Callback function called when the property is found in object __index. */
34-
lua_class_propfunc_t index;
35-
/** Callback function called when the property is found in object __newindex. */
36-
lua_class_propfunc_t newindex;
37-
};
38-
3927
DO_ARRAY(lua_class_t *, lua_class, DO_NOTHING)
4028

4129
static lua_class_array_t luaA_classes;
@@ -165,6 +153,14 @@ luaA_class_add_property(lua_class_t *lua_class,
165153
});
166154
}
167155

156+
void
157+
luaA_class_add_properties(lua_class_t* lua_class,
158+
lua_class_property_t properties[],
159+
size_t count)
160+
{
161+
lua_class_property_array_inserts(&lua_class->properties, properties, count);
162+
}
163+
168164
/** Newindex meta function for objects after they were GC'd.
169165
* \param L The Lua VM state.
170166
* \return The number of elements pushed on stack.

common/luaclass.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
#include <lua.h>
2828
#include <lauxlib.h>
2929

30-
typedef struct lua_class_property lua_class_property_t;
31-
32-
ARRAY_TYPE(lua_class_property_t, lua_class_property)
33-
3430
#define LUA_OBJECT_HEADER \
3531
signal_array_t signals;
3632

@@ -49,6 +45,21 @@ typedef int (*lua_class_propfunc_t)(lua_State *, lua_object_t *);
4945

5046
typedef bool (*lua_class_checker_t)(lua_object_t *);
5147

48+
typedef struct
49+
{
50+
/** Name of the property */
51+
const char *name;
52+
/** Callback function called when the property is found in object creation. */
53+
lua_class_propfunc_t new;
54+
/** Callback function called when the property is found in object __index. */
55+
lua_class_propfunc_t index;
56+
/** Callback function called when the property is found in object __newindex. */
57+
lua_class_propfunc_t newindex;
58+
}
59+
lua_class_property_t;
60+
61+
ARRAY_TYPE(lua_class_property_t, lua_class_property)
62+
5263
typedef struct lua_class_t lua_class_t;
5364
struct lua_class_t
5465
{
@@ -97,6 +108,7 @@ void luaA_class_setup(lua_State *, lua_class_t *, const char *, lua_class_t *,
97108

98109
void luaA_class_add_property(lua_class_t *, const char *,
99110
lua_class_propfunc_t, lua_class_propfunc_t, lua_class_propfunc_t);
111+
void luaA_class_add_properties(lua_class_t *, lua_class_property_t[] , size_t);
100112

101113
int luaA_usemetatable(lua_State *, int, int);
102114
int luaA_class_index(lua_State *);

common/util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@
7979
p_realloc(pp, *(allocnb)); \
8080
} \
8181
} while (0)
82+
#define p_growx(pp, goalnb, allocnb) \
83+
do { \
84+
if ((goalnb) > *(allocnb)) { \
85+
*(allocnb) = (goalnb); \
86+
p_realloc(pp, *(allocnb)); \
87+
} \
88+
} while (0)
8289

8390
#define p_delete(mem_p) \
8491
do { \

0 commit comments

Comments
 (0)