Rework C API shim. Now bridges: librngxx (main library C++ interface) @ rngxx.hpp -> >|capi-bridge.cpp <-> capi-bridge.h <-> capi.c|> -> rngxx.h

Fortune for rngxx's current commit: Small blessing − 小吉
capi
Avril 3 years ago
parent 8cdc9648a0
commit c38d58b7fe
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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

@ -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 <rngxx.hpp>
#include <rngxx/crand.h>
#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;
}
}
}

@ -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 <rngxx.h>
#undef _RNGXX_IMPL_ONLY_TYPES
#include <common.h>
#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 */

@ -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 <rngxx.h>
#include "capi-bridge.h"
rng_t* rng_new(enum rng_kind kind, u64 seed[static restrict 1])
{
return RNG_IMPL(mkdriver) (kind, &seed[0]);
}

@ -1,17 +0,0 @@
#include <rngxx.hpp>
#include <rngxx/crand.h>
#include <rngxx.h>
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;
}
}
}
Loading…
Cancel
Save