Skip to content

Commit 610a976

Browse files
committed
make capi header C++ compatible
1 parent 79e7cea commit 610a976

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

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 */

0 commit comments

Comments
 (0)