diff --git a/include/rngxx.h b/include/rngxx.h index 09abe8e..9037f0a 100644 --- a/include/rngxx.h +++ b/include/rngxx.h @@ -10,6 +10,9 @@ #endif #ifdef __cplusplus +#ifndef _RNGXX_IMPL_ONLY_TYPES +#warning "C++ compilers might not like this header file. Please use the C++ interface for C++ TUs" +#endif extern "C" { #endif @@ -19,8 +22,10 @@ enum rng_kind { RNG_KIND_CRAND, }; -extern rng_t* rng_new(enum rng_kind kind, u64 seed[static restrict 1]); +#ifndef _RNGXX_IMPL_ONLY_TYPES +rng_t* rng_new(enum rng_kind kind, u64 seed[static restrict 1]); +#endif #ifdef __cplusplus } #endif diff --git a/src/capi-bridge.cpp b/src/capi-bridge.cpp new file mode 100644 index 0000000..e94cfdc --- /dev/null +++ b/src/capi-bridge.cpp @@ -0,0 +1,19 @@ +// Contains the work-doing code for the C API interface, which should be exported to the C API shim in the header "capi-bridge.h" + +#include +#include + +#include "capi-bridge.h" + +extern "C" { + _export(internal) + rng_t* RNG_IMPL(mkdriver)(rng_kind kind, u64* restrict seed) + { + switch(kind) + { + case RNG_KIND_CRAND: + return new rng::crand(seed[0]); + default: return NULL; + } + } +} diff --git a/src/capi-bridge.h b/src/capi-bridge.h new file mode 100644 index 0000000..b2977b2 --- /dev/null +++ b/src/capi-bridge.h @@ -0,0 +1,25 @@ +// Bridges the working TU `capi-bridge.cpp`, which handles interfacing with rngxx's C++ API, and the C API shim TU defined in `capi.c` +// This file must be useable by both C and C++ compilers, and everything here must have C linkage +// +// Other headers shouldn't be included here. The only ones included are included for the types they define +#ifndef _RNG_CAPI_BRIDGE_H +#define _RNG_CAPI_BRIDGE_H + +#define _RNGXX_IMPL_ONLY_TYPES +#include +#undef _RNGXX_IMPL_ONLY_TYPES +#include + +#define RNG_IMPL(name) _rng__internal_ ## name + +#ifdef __cplusplus +extern "C" { +#endif + +rng_t* RNG_IMPL(mkdriver)(enum rng_kind kind, u64* restrict seed) _export(internal); + +#ifdef __cplusplus +} +#endif + +#endif /* _RNG_CAPI_BRIDGE_H */ diff --git a/src/capi.c b/src/capi.c new file mode 100644 index 0000000..e52848b --- /dev/null +++ b/src/capi.c @@ -0,0 +1,11 @@ +// Contains the library exported C API functions. +// Actual work may be done in `capi-bridge.cpp`, `capi-bridge.h` bridges this shim TU with that working one + +#include + +#include "capi-bridge.h" + +rng_t* rng_new(enum rng_kind kind, u64 seed[static restrict 1]) +{ + return RNG_IMPL(mkdriver) (kind, &seed[0]); +} diff --git a/src/capi.cpp b/src/capi.cpp deleted file mode 100644 index c1da629..0000000 --- a/src/capi.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include - -#include - -extern "C" { - //TODO: Make these C++ compiled ones intermediate, so the C interface used here can be used. use internal linkage for the C++ intermediates - rng_t* rng_new(rng_kind kind, u64 seed[static restrict 1]) - { - switch(kind) - { - case RNG_KIND_CRAND: - return new rng::crand(seed[0]); - default: return NULL; - } - } -}