simplified kt initialiation function names

This commit is contained in:
Timerix22 2023-02-15 21:45:10 +06:00
parent 5bbab0a414
commit 5391862d02
8 changed files with 24 additions and 17 deletions

View File

@ -26,9 +26,9 @@ Every registered type should have it's own descriptor (`ktDescriptor`). It's a s
## type registration
To finally register a type, you should call macro `kt_register()` between `ktDescriptors_beginInit()` and `ktDescriptors_endInit()`. Better do it at the start of your program. To register all types from kerep, call `ktDescriptors_initKerepTypes()`.
To finally register a type, you should call macro `kt_register()` between `kt_beginInit()` and `kt_endInit()`. Better do it at the start of your program. To register all types from kerep, call `kt_initKerepTypes()`.
You can free internal ktDescriptors storage by calling `ktDescriptors_free()` at exit, if your debugger (valgrind in my case) sees a memory leak.
You can free internal ktDescriptors storage by calling `kt_free()` at exit, if your debugger (valgrind in my case) sees a memory leak.
Examples:
+ [ktDescriptors_initKerepTypes()](src/base/type_system/init.c)
+ [kerep types registration](tests/main.cpp)
+ [kerep types registration](src/base/type_system/init.c)
+ [kt_initKerepTypes()](tests/main.cpp)

View File

@ -7,7 +7,7 @@
#include "../../Filesystem/filesystem.h"
#include "base_toString.h"
void ktDescriptors_initKerepTypes(){
void kt_initKerepTypes(){
// base types
kt_register(Pointer);
if(ktid_Pointer!=0) // this can break UnitypeNull

View File

@ -4,8 +4,8 @@
extern "C" {
#endif
// call this between ktDescriptors_beginInit() and ktDescriptors_endInit()
void ktDescriptors_initKerepTypes();
// call this between kt_beginInit() and kt_endInit()
void kt_initKerepTypes();
#if __cplusplus
}

View File

@ -41,6 +41,11 @@ STRUCT(ktDescriptor,
toString_t toString; // NULL or function which generates string representaion of object
)
/// gets descriptor for TYPE
#define ktDescriptor_name(TYPE) ktDescriptor_##TYPE
/// gets descriptor for pointer to TYPE
#define ktDescriptor_namePtr(TYPE) ktDescriptor_##TYPE##_Ptr
#if __cplusplus
}
#endif

View File

@ -31,14 +31,14 @@ ENUM(ktDescriptorsState,
)
ktDescriptorsState initState=NotInitialized;
void ktDescriptors_beginInit(){
void kt_beginInit(){
kprintf("\e[94mtype descriptors initializing...\n");
__descriptorPointers=Autoarr_create(Pointer, 256, 256);
if(__descriptorPointers==NULL)
throw(ERR_NULLPTR);
}
void ktDescriptors_endInit(){
void kt_endInit(){
typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers);
Autoarr_free(__descriptorPointers,true);
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
@ -58,6 +58,6 @@ ktDescriptor* ktDescriptor_get(ktid id){
return typeDescriptors[id];
}
void ktDescriptors_free(){
void kt_free(){
free(typeDescriptors);
}

View File

@ -17,14 +17,14 @@ void __kt_register(ktDescriptor* descriptor);
__kt_register(&ktDescriptor_##TYPE##_Ptr); \
ktid_##TYPE##_Ptr=ktid_last;
void ktDescriptors_beginInit();
void ktDescriptors_endInit();
void kt_beginInit();
void kt_endInit();
/// @param id id of registered type
ktDescriptor* ktDescriptor_get(ktid id);
// call it to free heap-allocated ktDescriptors array
void ktDescriptors_free();
void kt_free();
kt_declare(Pointer);
kt_declare(char);

View File

@ -10,7 +10,9 @@ extern "C" {
typedef u16 ktid;
static const ktid ktid_undefined=-1;
/// gets descriptor id for TYPE
#define ktid_name(TYPE) ktid_##TYPE
/// gets descriptor id for pointer to TYPE
#define ktid_ptrName(TYPE) ktid_##TYPE##_Ptr
#define ktid_declare(TYPE) \

View File

@ -3,11 +3,11 @@
i32 main(){
if(!setlocale(LC_ALL, "C.UTF8"))
kprintf("\e[93msetlocale failed\n");
ktDescriptors_beginInit();
ktDescriptors_initKerepTypes();
ktDescriptors_endInit();
kt_beginInit();
kt_initKerepTypes();
kt_endInit();
test_all();
ktDescriptors_free();
kt_free();
kprintf("\e[0m\n");
return 0;
}