Skip to content

Commit 0d82320

Browse files
committed
make capi headers C++ compatible
1 parent 3ba82a7 commit 0d82320

4 files changed

Lines changed: 92 additions & 27 deletions

File tree

src/carray_capi.h

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515
#define CARRAY_CAPI_VERSION_MINOR 0
1616
#define CARRAY_CAPI_VERSION_PATCH 0
1717

18-
typedef struct carray_capi carray_capi;
19-
typedef struct carray_info carray_info;
20-
typedef struct carray carray;
21-
22-
typedef enum carray_type carray_type;
23-
typedef enum carray_attr carray_attr;
24-
2518
#ifndef CARRAY_CAPI_IMPLEMENT_SET_CAPI
2619
# define CARRAY_CAPI_IMPLEMENT_SET_CAPI 0
2720
#endif
@@ -34,6 +27,25 @@ typedef enum carray_attr carray_attr;
3427
# define CARRAY_CAPI_IMPLEMENT_GET_CAPI 0
3528
#endif
3629

30+
#ifdef __cplusplus
31+
32+
extern "C" {
33+
34+
struct carray_capi;
35+
struct carray_info;
36+
struct carray;
37+
38+
#else /* __cplusplus */
39+
40+
typedef struct carray_capi carray_capi;
41+
typedef struct carray_info carray_info;
42+
typedef struct carray carray;
43+
44+
typedef enum carray_type carray_type;
45+
typedef enum carray_attr carray_attr;
46+
47+
#endif /* ! __cplusplus */
48+
3749
enum carray_type
3850
{
3951
CARRAY_UCHAR = 1,
@@ -213,7 +225,7 @@ struct carray_capi
213225
static int carray_set_capi(lua_State* L, int index, const carray_capi* capi)
214226
{
215227
lua_pushlstring(L, CARRAY_CAPI_ID_STRING, strlen(CARRAY_CAPI_ID_STRING)); /* -> key */
216-
void** udata = lua_newuserdata(L, sizeof(void*) + strlen(CARRAY_CAPI_ID_STRING) + 1); /* -> key, value */
228+
void** udata = (void**) lua_newuserdata(L, sizeof(void*) + strlen(CARRAY_CAPI_ID_STRING) + 1); /* -> key, value */
217229
*udata = (void*)capi;
218230
strcpy((char*)(udata + 1), CARRAY_CAPI_ID_STRING); /* -> key, value */
219231
lua_rawset(L, (index < 0) ? (index - 2) : index); /* -> */
@@ -233,22 +245,22 @@ static const carray_capi* carray_get_capi(lua_State* L, int index, int* errorRea
233245
{
234246
if (luaL_getmetafield(L, index, CARRAY_CAPI_ID_STRING) != LUA_TNIL) /* -> _capi */
235247
{
236-
void** udata = lua_touserdata(L, -1); /* -> _capi */
248+
const void** udata = (const void**) lua_touserdata(L, -1); /* -> _capi */
237249

238250
if ( udata
239251
&& (lua_rawlen(L, -1) >= sizeof(void*) + strlen(CARRAY_CAPI_ID_STRING) + 1)
240252
&& (memcmp((char*)(udata + 1), CARRAY_CAPI_ID_STRING,
241253
strlen(CARRAY_CAPI_ID_STRING) + 1) == 0))
242254
{
243-
const carray_capi* capi = *udata; /* -> _capi */
255+
const carray_capi* capi = (const carray_capi*) *udata; /* -> _capi */
244256
while (capi) {
245257
if ( capi->version_major == CARRAY_CAPI_VERSION_MAJOR
246258
&& capi->version_minor >= CARRAY_CAPI_VERSION_MINOR)
247259
{ /* -> _capi */
248260
lua_pop(L, 1); /* -> */
249261
return capi;
250262
}
251-
capi = capi->next_capi;
263+
capi = (const carray_capi*) capi->next_capi;
252264
}
253265
if (errorReason) {
254266
*errorReason = 1;
@@ -291,4 +303,8 @@ static const carray_capi* carray_require_capi(lua_State* L)
291303

292304
#endif /* CARRAY_CAPI_IMPLEMENT_REQUIRE_CAPI */
293305

306+
#ifdef __cplusplus
307+
} /* extern "C" */
308+
#endif
309+
294310
#endif /* CARRAY_CAPI_H */

src/notify_capi.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#define NOTIFY_CAPI_VERSION_MINOR 0
77
#define NOTIFY_CAPI_VERSION_PATCH 1
88

9-
typedef struct notify_notifier notify_notifier;
10-
typedef struct notify_capi notify_capi;
11-
129
#ifndef NOTIFY_CAPI_IMPLEMENT_SET_CAPI
1310
# define NOTIFY_CAPI_IMPLEMENT_SET_CAPI 0
1411
#endif
@@ -17,6 +14,20 @@ typedef struct notify_capi notify_capi;
1714
# define NOTIFY_CAPI_IMPLEMENT_GET_CAPI 0
1815
#endif
1916

17+
#ifdef __cplusplus
18+
19+
extern "C" {
20+
21+
struct notify_notifier;
22+
struct notify_capi;
23+
24+
#else /* __cplusplus */
25+
26+
typedef struct notify_notifier notify_notifier;
27+
typedef struct notify_capi notify_capi;
28+
29+
#endif /* ! __cplusplus */
30+
2031
/**
2132
* Type for pointer to function that may be called if an error occurs.
2233
* ehdata: void pointer that is given in notify method (see below)
@@ -100,7 +111,7 @@ struct notify_capi
100111
static int notify_set_capi(lua_State* L, int index, const notify_capi* capi)
101112
{
102113
lua_pushlstring(L, NOTIFY_CAPI_ID_STRING, strlen(NOTIFY_CAPI_ID_STRING)); /* -> key */
103-
void** udata = lua_newuserdata(L, sizeof(void*) + strlen(NOTIFY_CAPI_ID_STRING) + 1); /* -> key, value */
114+
void** udata = (void**) lua_newuserdata(L, sizeof(void*) + strlen(NOTIFY_CAPI_ID_STRING) + 1); /* -> key, value */
104115
*udata = (void*)capi;
105116
strcpy((char*)(udata + 1), NOTIFY_CAPI_ID_STRING); /* -> key, value */
106117
lua_rawset(L, (index < 0) ? (index - 2) : index); /* -> */
@@ -120,22 +131,22 @@ static const notify_capi* notify_get_capi(lua_State* L, int index, int* errorRea
120131
{
121132
if (luaL_getmetafield(L, index, NOTIFY_CAPI_ID_STRING) != LUA_TNIL) /* -> _capi */
122133
{
123-
void** udata = lua_touserdata(L, -1); /* -> _capi */
134+
const void** udata = (const void**) lua_touserdata(L, -1); /* -> _capi */
124135

125136
if ( udata
126137
&& (lua_rawlen(L, -1) >= sizeof(void*) + strlen(NOTIFY_CAPI_ID_STRING) + 1)
127138
&& (memcmp((char*)(udata + 1), NOTIFY_CAPI_ID_STRING,
128139
strlen(NOTIFY_CAPI_ID_STRING) + 1) == 0))
129140
{
130-
const notify_capi* capi = *udata; /* -> _capi */
141+
const notify_capi* capi = (const notify_capi*) *udata; /* -> _capi */
131142
while (capi) {
132143
if ( capi->version_major == NOTIFY_CAPI_VERSION_MAJOR
133144
&& capi->version_minor >= NOTIFY_CAPI_VERSION_MINOR)
134145
{ /* -> _capi */
135146
lua_pop(L, 1); /* -> */
136147
return capi;
137148
}
138-
capi = capi->next_capi;
149+
capi = (const notify_capi*) capi->next_capi;
139150
}
140151
if (errorReason) {
141152
*errorReason = 1;
@@ -155,4 +166,8 @@ static const notify_capi* notify_get_capi(lua_State* L, int index, int* errorRea
155166
}
156167
#endif /* NOTIFY_CAPI_IMPLEMENT_GET_CAPI */
157168

169+
#ifdef __cplusplus
170+
} /* extern "C" */
171+
#endif
172+
158173
#endif /* NOTIFY_CAPI_H */

src/receiver_capi.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,23 @@
2323
# define RECEIVER_CAPI_IMPLEMENT_GET_CAPI 0
2424
#endif
2525

26+
#ifdef __cplusplus
27+
28+
extern "C" {
29+
30+
struct receiver_object;
31+
struct receiver_writer;
32+
struct receiver_capi;
33+
34+
#else /* __cplusplus */
35+
2636
typedef struct receiver_object receiver_object;
2737
typedef struct receiver_writer receiver_writer;
2838
typedef struct receiver_capi receiver_capi;
2939
typedef enum receiver_array_type receiver_array_type;
3040

41+
#endif /* ! __cplusplus */
42+
3143
enum receiver_array_type
3244
{
3345
RECEIVER_UCHAR = 1,
@@ -198,7 +210,7 @@ struct receiver_capi
198210
static int receiver_set_capi(lua_State* L, int index, const receiver_capi* capi)
199211
{
200212
lua_pushlstring(L, RECEIVER_CAPI_ID_STRING, strlen(RECEIVER_CAPI_ID_STRING)); /* -> key */
201-
void** udata = lua_newuserdata(L, sizeof(void*) + strlen(RECEIVER_CAPI_ID_STRING) + 1); /* -> key, value */
213+
void** udata = (void**) lua_newuserdata(L, sizeof(void*) + strlen(RECEIVER_CAPI_ID_STRING) + 1); /* -> key, value */
202214
*udata = (void*)capi;
203215
strcpy((char*)(udata + 1), RECEIVER_CAPI_ID_STRING); /* -> key, value */
204216
lua_rawset(L, (index < 0) ? (index - 2) : index); /* -> */
@@ -218,22 +230,22 @@ static const receiver_capi* receiver_get_capi(lua_State* L, int index, int* erro
218230
{
219231
if (luaL_getmetafield(L, index, RECEIVER_CAPI_ID_STRING) != LUA_TNIL) /* -> _capi */
220232
{
221-
void** udata = lua_touserdata(L, -1); /* -> _capi */
233+
const void** udata = (const void**) lua_touserdata(L, -1); /* -> _capi */
222234

223235
if ( udata
224236
&& (lua_rawlen(L, -1) >= sizeof(void*) + strlen(RECEIVER_CAPI_ID_STRING) + 1)
225237
&& (memcmp((char*)(udata + 1), RECEIVER_CAPI_ID_STRING,
226238
strlen(RECEIVER_CAPI_ID_STRING) + 1) == 0))
227239
{
228-
const receiver_capi* capi = *udata; /* -> _capi */
240+
const receiver_capi* capi = (const receiver_capi*) *udata; /* -> _capi */
229241
while (capi) {
230242
if ( capi->version_major == RECEIVER_CAPI_VERSION_MAJOR
231243
&& capi->version_minor >= RECEIVER_CAPI_VERSION_MINOR)
232244
{ /* -> _capi */
233245
lua_pop(L, 1); /* -> */
234246
return capi;
235247
}
236-
capi = capi->next_capi;
248+
capi = (const receiver_capi*) capi->next_capi;
237249
}
238250
if (errorReason) {
239251
*errorReason = 1;
@@ -253,4 +265,8 @@ static const receiver_capi* receiver_get_capi(lua_State* L, int index, int* erro
253265
}
254266
#endif /* RECEIVER_CAPI_IMPLEMENT_GET_CAPI */
255267

268+
#ifdef __cplusplus
269+
} /* extern "C" */
270+
#endif
271+
256272
#endif /* RECEIVER_CAPI_H */

src/sender_capi.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,27 @@
2323
# define SENDER_CAPI_IMPLEMENT_GET_CAPI 0
2424
#endif
2525

26+
#ifdef __cplusplus
27+
28+
extern "C" {
29+
30+
struct sender_object;
31+
struct sender_reader;
32+
struct sender_capi;
33+
struct sender_capi_value;
34+
35+
#else /* __cplusplus */
36+
2637
typedef struct sender_object sender_object;
2738
typedef struct sender_reader sender_reader;
2839
typedef struct sender_capi sender_capi;
29-
typedef enum sender_capi_value_type sender_capi_value_type;
3040
typedef struct sender_capi_value sender_capi_value;
41+
42+
typedef enum sender_capi_value_type sender_capi_value_type;
3143
typedef enum sender_array_type sender_array_type;
3244

45+
#endif /* ! __cplusplus */
46+
3347
enum sender_capi_value_type
3448
{
3549
SENDER_CAPI_TYPE_NONE = 0,
@@ -209,7 +223,7 @@ struct sender_capi
209223
static int sender_set_capi(lua_State* L, int index, const sender_capi* capi)
210224
{
211225
lua_pushlstring(L, SENDER_CAPI_ID_STRING, strlen(SENDER_CAPI_ID_STRING)); /* -> key */
212-
void** udata = lua_newuserdata(L, sizeof(void*) + strlen(SENDER_CAPI_ID_STRING) + 1); /* -> key, value */
226+
void** udata = (void**) lua_newuserdata(L, sizeof(void*) + strlen(SENDER_CAPI_ID_STRING) + 1); /* -> key, value */
213227
*udata = (void*)capi;
214228
strcpy((char*)(udata + 1), SENDER_CAPI_ID_STRING); /* -> key, value */
215229
lua_rawset(L, (index < 0) ? (index - 2) : index); /* -> */
@@ -229,22 +243,22 @@ static const sender_capi* sender_get_capi(lua_State* L, int index, int* errorRea
229243
{
230244
if (luaL_getmetafield(L, index, SENDER_CAPI_ID_STRING) != LUA_TNIL) /* -> _capi */
231245
{
232-
void** udata = lua_touserdata(L, -1); /* -> _capi */
246+
const void** udata = (const void**) lua_touserdata(L, -1); /* -> _capi */
233247

234248
if ( udata
235249
&& (lua_rawlen(L, -1) >= sizeof(void*) + strlen(SENDER_CAPI_ID_STRING) + 1)
236250
&& (memcmp((char*)(udata + 1), SENDER_CAPI_ID_STRING,
237251
strlen(SENDER_CAPI_ID_STRING) + 1) == 0))
238252
{
239-
const sender_capi* capi = *udata; /* -> _capi */
253+
const sender_capi* capi = (const sender_capi*) *udata; /* -> _capi */
240254
while (capi) {
241255
if ( capi->version_major == SENDER_CAPI_VERSION_MAJOR
242256
&& capi->version_minor >= SENDER_CAPI_VERSION_MINOR)
243257
{ /* -> _capi */
244258
lua_pop(L, 1); /* -> */
245259
return capi;
246260
}
247-
capi = capi->next_capi;
261+
capi = (const sender_capi*) capi->next_capi;
248262
}
249263
if (errorReason) {
250264
*errorReason = 1;
@@ -264,4 +278,8 @@ static const sender_capi* sender_get_capi(lua_State* L, int index, int* errorRea
264278
}
265279
#endif /* SENDER_CAPI_IMPLEMENT_GET_CAPI */
266280

281+
#ifdef __cplusplus
282+
} /* extern "C" */
283+
#endif
284+
267285
#endif /* SENDER_CAPI_H */

0 commit comments

Comments
 (0)