Compare commits

...

3 Commits

Author SHA1 Message Date
falsycat 4af1bf878a add nf7core_null 2024-01-01 14:35:22 +09:00
falsycat f8da6c54fc fix entity&idea iface 2024-01-01 14:34:43 +09:00
falsycat dce4613870 update nf7 iface 2024-01-01 14:07:28 +09:00
19 changed files with 163 additions and 25 deletions

View File

@ -1,10 +1,14 @@
# ---- basic modules
set(MODS
# ---- interface layer
exec
# ---- implementation layer
lua
null
sdl2
# ---- usecase layer
test
)

View File

@ -20,6 +20,7 @@ for name in $@; do
echo " nf7util_log_debug(\"loading module: %s\", nf7core_${name}.name);"
echo " mods[i] = nf7core_${name}_new(nf7);"
echo " if (nullptr != mods[i]) {"
echo " assert(nullptr != mods[i]->nf7);"
echo " assert(nullptr != mods[i]->meta);"
echo " ++i;"
echo " nf7util_log_info(\"loaded module: %s\", nf7core_${name}.name);"

View File

@ -3,13 +3,15 @@
#include <assert.h>
#include "util/log.h"
#include "core/exec/idea.h"
#include "core/exec/mod.h"
struct nf7core_exec_entity {
struct nf7core_exec* mod;
const struct nf7core_exec_idea* idea;
struct nf7core_exec* mod;
void* data;
void (*on_recv)(
@ -32,6 +34,8 @@ static inline struct nf7core_exec_entity* nf7core_exec_entity_new(
nf7util_log_error("failed to create entity of '%.*s'", (int) namelen, name);
return nullptr;
}
assert(idea == entity->idea);
assert(mod == entity->mod);
return entity;
}

View File

@ -10,9 +10,9 @@
struct nf7core_exec_idea {
const char* name;
const char* details;
const struct nf7_mod* mod;
const uint8_t* name;
const uint8_t* details;
const struct nf7_mod_meta* mod;
struct nf7core_exec_entity* (*new)(struct nf7core_exec*);
void (*del)(struct nf7core_exec_entity*);

View File

@ -20,14 +20,14 @@ struct nf7_mod* nf7core_exec_new(struct nf7* nf7) {
*this = (struct nf7core_exec) {
.super = {
.nf7 = nf7,
.meta = &nf7core_exec,
},
.nf7 = nf7,
.malloc = nf7->malloc,
};
nf7core_exec_ideas_init(&this->ideas, this->malloc);
return (struct nf7_mod*) this;
return &this->super;
ABORT:
nf7util_log_warn("aborting module init");
@ -46,5 +46,5 @@ const struct nf7_mod_meta nf7core_exec = {
.desc = (const uint8_t*) "provides a registry for executables",
.ver = NF7_VERSION,
.delete = del_,
.del = del_,
};

View File

@ -21,9 +21,9 @@ struct nf7_mod* nf7core_lua_new(struct nf7* nf7) {
}
*this = (struct nf7core_lua) {
.super = {
.nf7 = nf7,
.meta = &nf7core_lua,
},
.nf7 = nf7,
.malloc = nf7->malloc,
.uv = nf7->uv,
};
@ -33,7 +33,7 @@ struct nf7_mod* nf7core_lua_new(struct nf7* nf7) {
nf7util_log_error("failed to create main thread");
goto ABORT;
}
return (struct nf7_mod*) this;
return &this->super;
ABORT:
nf7util_log_warn("aborting lua module init");
@ -62,5 +62,5 @@ const struct nf7_mod_meta nf7core_lua = {
.desc = (const uint8_t*) "lua script execution",
.ver = NF7_VERSION,
.delete = del_mod_,
.del = del_mod_,
};

View File

@ -16,7 +16,6 @@ extern const struct nf7_mod_meta nf7core_lua;
struct nf7core_lua {
struct nf7_mod super;
const struct nf7* nf7;
struct nf7util_malloc* malloc;
uv_loop_t* uv;

14
core/null/CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
add_library(nf7core_null)
target_sources(nf7core_null
PRIVATE
idea.c
mod.c
PUBLIC
mod.h
)
target_link_libraries(nf7core_null
PRIVATE
nf7if
nf7util
)

64
core/null/idea.c Normal file
View File

@ -0,0 +1,64 @@
// No copyright
#include <assert.h>
#include "nf7.h"
#include "util/malloc.h"
#include "core/exec/mod.h"
#include "core/null/mod.h"
static struct nf7core_exec_entity* new_(struct nf7core_exec*);
struct nf7core_exec_entity* nf7core_null_entity_new(struct nf7* nf7) {
assert(nullptr != nf7);
struct nf7core_exec* exec = (void*) nf7_get_mod_by_meta(nf7, &nf7core_exec);
if (nullptr == exec) {
nf7util_log_error("nf7core_exec module is missing");
return nullptr;
}
return new_(exec);
}
static struct nf7core_exec_entity* new_(struct nf7core_exec* mod) {
assert(nullptr != mod);
const struct nf7* nf7 = mod->super.nf7;
struct nf7core_exec_entity* this =
nf7util_malloc_alloc(nf7->malloc, sizeof(*this));
if (nullptr == this) {
return nullptr;
}
*this = (struct nf7core_exec_entity) {
.idea = &nf7core_null_idea,
.mod = mod,
};
return this;
}
static void del_(struct nf7core_exec_entity* this) {
if (nullptr != this) {
assert(nullptr != this->mod);
const struct nf7* nf7 = this->mod->super.nf7;
assert(nullptr != nf7);
assert(nullptr != nf7->malloc);
nf7util_malloc_free(nf7->malloc, this);
}
}
static void send_(struct nf7core_exec_entity*, struct nf7util_buffer*) { }
const struct nf7core_exec_idea nf7core_null_idea = {
.name = (const uint8_t*) "nf7core_null_idea",
.details = (const uint8_t*) "null implementation of an idea",
.new = new_,
.del = del_,
.send = send_,
};

41
core/null/mod.c Normal file
View File

@ -0,0 +1,41 @@
// No copyright
#include "core/null/mod.h"
#include <assert.h>
#include "util/log.h"
#include "util/malloc.h"
struct nf7_mod* nf7core_null_new(struct nf7* nf7) {
assert(nullptr != nf7);
struct nf7_mod* this = nf7util_malloc_alloc(nf7->malloc, sizeof(*this));
if (nullptr == this) {
nf7util_log_error("failed to allocate module context");
return nullptr;
}
*this = (struct nf7_mod) {
.nf7 = nf7,
.meta = &nf7core_null,
};
return this;
}
static void del_(struct nf7_mod* this) {
if (nullptr != this) {
assert(nullptr != this->nf7);
assert(nullptr != this->nf7->malloc);
nf7util_malloc_free(this->nf7->malloc, this);
}
}
const struct nf7_mod_meta nf7core_null = {
.name = (const uint8_t*) "nf7core_null",
.desc = (const uint8_t*) "null implementations of each interfaces",
.ver = NF7_VERSION,
.del = del_,
};

13
core/null/mod.h Normal file
View File

@ -0,0 +1,13 @@
// No copyright
#pragma once
#include "nf7.h"
#include "core/exec/entity.h"
extern const struct nf7_mod_meta nf7core_null;
extern const struct nf7core_exec_idea nf7core_null_idea;
struct nf7core_exec_entity* nf7core_null_entity_new(struct nf7*);

View File

@ -43,9 +43,9 @@ struct nf7_mod* nf7core_sdl2_new(const struct nf7* nf7) {
}
*this = (struct nf7core_sdl2) {
.super = {
.nf7 = nf7,
.meta = &nf7core_sdl2,
},
.nf7 = nf7,
.malloc = nf7->malloc,
.uv = nf7->uv,
};
@ -60,7 +60,7 @@ struct nf7_mod* nf7core_sdl2_new(const struct nf7* nf7) {
nf7util_signal_init(&this->event_signal, this->malloc);
nf7core_sdl2_ref(this);
return (struct nf7_mod*) this;
return &this->super;
ABORT:
nf7util_log_warn("initialization is aborted");
@ -101,5 +101,5 @@ const struct nf7_mod_meta nf7core_sdl2 = {
.desc = (const uint8_t*) "provides SDL2 features",
.ver = NF7_VERSION,
.delete = unref_,
.del = unref_,
};

View File

@ -21,7 +21,6 @@ struct nf7core_sdl2 {
struct nf7_mod super;
// library pointers (immutable)
const struct nf7* nf7;
struct nf7util_malloc* malloc;
uv_loop_t* uv;
SDL_Window* win;

View File

@ -38,7 +38,7 @@ static struct nf7core_sdl2_poll* poll_new_(struct nf7core_sdl2* mod) {
return nullptr;
}
*this = (struct nf7core_sdl2_poll) {
.nf7 = mod->nf7,
.nf7 = mod->super.nf7,
.malloc = mod->malloc,
.uv = mod->uv,

View File

@ -24,9 +24,9 @@ struct nf7_mod* nf7core_test_new(const struct nf7* nf7) {
}
*this = (struct nf7core_test) {
.super = {
.nf7 = nf7,
.meta = &nf7core_test,
},
.nf7 = nf7,
.malloc = nf7->malloc,
.uv = nf7->uv,
};
@ -35,7 +35,7 @@ struct nf7_mod* nf7core_test_new(const struct nf7* nf7) {
nf7util_log_error("failed to setup runner");
return nullptr;
}
return (struct nf7_mod*) this;
return &this->super;
}
static void del_(struct nf7_mod* mod) {
@ -49,5 +49,5 @@ const struct nf7_mod_meta nf7core_test = {
.desc = (const uint8_t*) "executes tests after the initialization",
.ver = NF7_VERSION,
.delete = del_,
.del = del_,
};

View File

@ -14,7 +14,6 @@ struct nf7core_test_run;
struct nf7core_test {
struct nf7_mod super;
const struct nf7* nf7;
struct nf7util_malloc* malloc;
uv_loop_t* uv;

View File

@ -51,7 +51,7 @@ static bool run_trigger_setup_(struct nf7core_test* mod) {
.malloc = mod->malloc,
.uv = mod->uv,
.test = {
.nf7 = mod->nf7,
.nf7 = mod->super.nf7,
.malloc = mod->malloc,
.data = this,
.run = run_single_test_,

4
main.c
View File

@ -52,10 +52,10 @@ int main(int argc, char** argv) {
// destroy modules
for (uint32_t i = 0; i < nf7.mods.n; ++i) {
struct nf7_mod* mod = nf7.mods.ptr[i];
assert(mod->meta->delete);
assert(mod->meta->del);
nf7util_log_debug("unloading module: %s", mod->meta->name);
mod->meta->delete(mod);
mod->meta->del(mod);
}
nf7util_log_info("unloaded all modules");

4
nf7.h
View File

@ -31,6 +31,7 @@ struct nf7 {
};
struct nf7_mod {
const struct nf7* nf7;
const struct nf7_mod_meta* meta;
};
@ -39,8 +40,7 @@ struct nf7_mod_meta {
const uint8_t* desc;
uint32_t ver;
void (*delete)(struct nf7_mod*);
void (*push_lua)(struct nf7_mod*);
void (*del)(struct nf7_mod*);
};