Hack to work arround dynctor not being registered when not defined in `crand.cpp" (no idea why, the symbol exists. looking into it.)

Fortune for rngxx's current commit: Small curse − 小凶
master
Avril 3 years ago
parent dfb3d74816
commit 3165ef13fd
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -0,0 +1,14 @@
#ifndef _RNGXX_DCTOR_HACK_H
#define _RNGXX_DCTOR_HACK_H
#include <rngxx.hpp>
#include <rngxx/crand.h>
#include <rngxx/sm64.h>
#include <init.hpp>
// For some reason, dctors defined in any other file than crand.cpp do not work??!
RNGXX_APPLY_CTOR(rng, sm64, u64);
RNGXX_APPLY_CTOR(rng, crand, u64);
#endif /* _RNGXX_DCTOR_HACK_H */

@ -10,9 +10,10 @@
namespace rng::init
{
using ctor_func = std::function<Random* (const void*)>;
struct DCLookupFailed final : std::exception{ inline DCLookupFailed(const std::string_view& n) : std::exception(), name(std::move(n)){} std::string name; };
const std::function<Random* (const void*)>& apply_raw(std::string_view name, std::function<Random* (const void*)> fn) noexcept;
const ctor_func& apply_raw(std::string_view name, ctor_func fn) noexcept;
template<typename T, typename _Ptr = void>
inline auto apply(std::string_view name, auto&& lam) noexcept
@ -22,10 +23,11 @@ namespace rng::init
});
}
std::function<Random* (const void*)>& get(std::string_view name);
ctor_func& get(std::string_view name);
}
// To add to dynamic CTOR map
#define RNGXX_APPLY_CTOR(ns, T, TSeed) \
const auto RNGXX_DCTOR_NAME(T) = rng::init::apply<ns::T, TSeed>(#T, [](const TSeed* ptr) { return new ns::T(*ptr); })
#define RNGXX_APPLY_CTOR(ns, T, TSeed) __attribute__((used))\
const static auto& RNGXX_DCTOR_NAME(T) = rng::init::apply<ns::T, TSeed>(#T, [](const TSeed* ptr) { return new ns::T(*ptr); })
//const extern init::ctor_func& RNGXX_DCTOR_NAME(sm64);

@ -3,13 +3,13 @@
#include <init.hpp>
using ctor_map = std::unordered_map<std::string_view, std::function<Random* (const void*)>>;
using ctor_map = std::unordered_map<std::string_view, rng::init::ctor_func>;
static ctor_map* INIT_MAP=nullptr;
namespace rng::init
{
const std::function<Random* (const void*)>& apply_raw(std::string_view name, std::function<Random* (const void*)> fn) noexcept
const ctor_func& apply_raw(std::string_view name, ctor_func fn) noexcept
{
if(!INIT_MAP) INIT_MAP = new ctor_map();
@ -18,7 +18,7 @@ namespace rng::init
return map[name];
}
std::function<Random* (const void*)>& get(std::string_view name)
ctor_func& get(std::string_view name)
{
if(UNLIKELY(!INIT_MAP)) throw DCLookupFailed ( name );
auto& map = *INIT_MAP;

@ -7,7 +7,6 @@
#include <rngxx.hpp>
#include <mem.h>
#include <init.hpp>
#include <rngxx/crand.h>
#include "crand.h"
@ -71,4 +70,5 @@ namespace rng
}
RNGXX_APPLY_CTOR(rng, crand, u64);
//TODO: Why does this have to be in THIS TU?
#include "../internal/dctor.h"

@ -1,7 +1,12 @@
#include <rngxx.hpp>
#include <rngxx/sm64.h>
#include <bit>
#include <climits>
#include <cmath>
#include <rngxx.hpp>
#include <mem.h>
#include <init.hpp>
__attribute__((used))
RNGXX_APPLY_CTOR(rng, sm64, u64);
#include <rngxx/crand.h>
#include <rngxx/sm64.h>
#include "crand.h"

@ -18,10 +18,17 @@ static int next(rng_t* rng, const int* min, const int* max)
#define TREF(x) ( (const __typeof(x)[]){ (x) } )
int main()
const struct { const char* const name; const enum rng_kind static_kind; } DEFAULT_ALG = { "crand", RNG_KIND_CRAND };
int main(int argc, char** argv)
{
rng_dyn_ctor_ref rcref = rng_ctor_ref("sm64");
rng_ctor_fn rctor = rng_ctor("crand");
(void)argc;
const char* alg_name = argv[1] ?: DEFAULT_ALG.name;
const enum rng_kind* alg_dev = ((int)DEFAULT_ALG.static_kind) == -1 ? NULL : &DEFAULT_ALG.static_kind;
printf("constructing for %s...\n", alg_name);
rng_dyn_ctor_ref rcref = rng_ctor_ref(alg_name);
rng_ctor_fn rctor = rng_ctor(alg_name);
rng_t* engine = NULL;
if(rctor) {
@ -33,11 +40,11 @@ int main()
}
if(!engine) {
printf("fallback to `rng_new_named()`\n");
engine = rng_new_named("crand", TREF((u64)time(NULL))); //rng_new(rng_kind_crand, (const u64[]){ time(null) });
engine = rng_new_named(alg_name, TREF((u64)time(NULL))); //rng_new(rng_kind_crand, (const u64[]){ time(null) });
}
if(!engine) {
printf("fallback to static init\n");
engine = rng_new(RNG_KIND_CRAND, (const u64[]){ time(NULL) });
if(!engine && alg_dev) {
printf("fallback to static init of default arg (%d)\n", *alg_dev);
engine = rng_new(*alg_dev, (const u64[]){ time(NULL) });
}
if(!engine) {
fprintf(stderr, "failed to create engine\n");

Loading…
Cancel
Save