summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile8
-rw-r--r--config.mk1
-rw-r--r--pk.h.in6
-rw-r--r--pkev.h93
4 files changed, 107 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 1fe6142..a264610 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,7 @@ SRC = \
pkmem-types.h \
pkmem.h \
pkstr.h \
+ pkev.h \
test/pkmacros.c \
test/pkmacros.cpp \
test/pkmem-types.c \
@@ -29,6 +30,7 @@ all: options .WAIT clean .WAIT \
pkmem-types \
pkmem \
pkstr \
+ pkev \
test-pkmem-types test-pkmem-types-cpp \
test-pkmem test-pkmem-cpp \
test-pkmacros test-pkmacros-cpp \
@@ -64,15 +66,19 @@ pkmem: pkmem-types pkmem.gch pkmem.gchpp
pkstr: pkmacros pkstr.gch pkstr.gchpp
+pkev: pkev.gch pkev.gchpp
+
build: pkmacros
build: pkmem-types
build: pkmem
build: pkstr
+build: pkev
cat pk.h.in \
pkmacros.h \
pkmem-types.h \
pkmem.h \
pkstr.h \
+ pkev.h \
> pk.h
sed -i -r \
-e "s/@@PK_VERSION@@/$(VERSION)/g" \
@@ -106,7 +112,7 @@ test-pkstr: test/pkstr.o
test-pkstr-cpp: test/pkstr.so
$(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS)
-test: pkmacros pkmem-types pkmem pkstr
+test: pkmacros pkmem-types pkmem pkstr pkev
test: test-pkmacros test-pkmacros-cpp
test: test-pkmem-types test-pkmem-types-cpp
test: test-pkmem test-pkmem-cpp
diff --git a/config.mk b/config.mk
index 5cc5bb7..8fe1369 100644
--- a/config.mk
+++ b/config.mk
@@ -19,6 +19,7 @@ SHARED_FLAGS = -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L \
-DPK_IMPL_MEM_TYPES \
-DPK_IMPL_MEM \
-DPK_IMPL_STR \
+ -DPK_IMPL_EV \
CPPFLAGS += -Wall $(INCS) $(SHARED_FLAGS)
CFLAGS += -Wall $(INCS) $(SHARED_FLAGS)
diff --git a/pk.h.in b/pk.h.in
index eb33eee..af36589 100644
--- a/pk.h.in
+++ b/pk.h.in
@@ -18,6 +18,9 @@
*******************************************************************************
* pkstr.h:
*
+*******************************************************************************
+* pkev.h:
+*
******************************************************************************/
#define PK_VERSION "@@PK_VERSION@@"
@@ -32,4 +35,7 @@
# ifndef PK_IMPL_STR
# define PK_IMPL_STR
# endif
+# ifndef PK_IMPL_EV
+# define PK_IMPL_EV
+# endif
#endif
diff --git a/pkev.h b/pkev.h
new file mode 100644
index 0000000..e3dc772
--- /dev/null
+++ b/pkev.h
@@ -0,0 +1,93 @@
+#ifndef PK_EV_H
+#define PK_EV_H
+
+#include <stdint.h>
+
+struct pk_ev_mgr;
+typedef uint64_t pk_ev_id_T;
+
+struct pk_ev_mgr *pk_ev_create();
+void pk_ev_destroy(struct pk_ev_mgr *evmgr);
+
+typedef void (*pk_ev_cb)();
+
+void pk_ev_register_ev(struct pk_ev_mgr *evmgr, pk_ev_id_T evid);
+void pk_ev_register_cb(struct pk_ev_mgr *evmgr, pk_ev_id_T evid, pk_ev_cb *cb);
+void pk_ev_emit(struct pk_ev_mgr *evmgr, pk_ev_id_T evid, ...);
+
+#endif /* PK_EV_H */
+
+#ifdef PK_IMPL_EV
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef PK_EV_MAX_COUNT
+# define PK_EV_MAX_COUNT 16
+#endif
+
+struct pk_ev {
+ pk_ev_id_T evid;
+ pk_ev_cb **cb;
+ uint8_t n_cb;
+};
+
+struct pk_ev_mgr {
+ struct pk_ev *ev;
+ uint8_t n_ev;
+};
+
+inline struct pk_ev_mgr*
+pk_ev_create()
+{
+ struct pk_ev_mgr *mgr = (struct pk_ev_mgr*)malloc(sizeof(struct pk_ev_mgr));
+ if (mgr == NULL) return NULL;
+ memset(mgr, 0, sizeof(struct pk_ev_mgr));
+ mgr->ev = (struct pk_ev*)malloc(sizeof(struct pk_ev) * PK_EV_MAX_COUNT);
+ if (mgr->ev == NULL) return NULL;
+ memset(mgr->ev, 0, sizeof(struct pk_ev) * PK_EV_MAX_COUNT);
+ mgr->n_ev = 0;
+ return mgr;
+}
+
+inline void
+pk_ev_destroy(struct pk_ev_mgr *evmgr)
+{
+ memset(evmgr->ev, 0, sizeof(struct pk_ev) * PK_EV_MAX_COUNT);
+ free(evmgr->ev);
+ memset(evmgr, 0, sizeof(struct pk_ev_mgr));
+ free(evmgr);
+}
+
+inline void
+pk_ev_register_ev(struct pk_ev_mgr *evmgr, pk_ev_id_T evid)
+{
+ // TODO
+}
+
+inline void
+pk_ev_register_cb(struct pk_ev_mgr *evmgr, pk_ev_id_T evid, pk_ev_cb *cb)
+{
+ // TODO
+}
+
+inline void
+pk_ev_emit(struct pk_ev_mgr *evmgr, pk_ev_id_T evid, ...)
+{
+ assert(evmgr != NULL);
+ uint8_t i;
+ struct pk_ev *ev = {0};
+ for (i = 0; i < evmgr->n_ev; ++i) {
+ if (evmgr->ev[i].evid == evid) {
+ ev = &evmgr->ev[i];
+ break;
+ }
+ }
+ if (ev == NULL) return;
+ for (i = 0; i < ev->n_cb; ++i) {
+ (*ev->cb[i])();
+ }
+}
+
+#endif