new TypeId system

This commit is contained in:
timerix 2022-08-24 19:18:24 +06:00
parent 11c5b57856
commit 2e378d1458
14 changed files with 390 additions and 202 deletions

View File

@ -7,18 +7,44 @@ extern "C" {
#include "Autoarr_declare.h" #include "Autoarr_declare.h"
#include "Autoarr_define.h" #include "Autoarr_define.h"
Autoarr_declare(uint8) Autoarr_declare(float32)
Autoarr_declare(float64)
Autoarr_declare(int8) Autoarr_declare(int8)
Autoarr_declare(uint16) Autoarr_declare(uint8)
Autoarr_declare(int16) Autoarr_declare(int16)
Autoarr_declare(uint32) Autoarr_declare(uint16)
Autoarr_declare(int32) Autoarr_declare(int32)
Autoarr_declare(uint64) Autoarr_declare(uint32)
Autoarr_declare(int64) Autoarr_declare(int64)
Autoarr_declare(float) Autoarr_declare(uint64)
Autoarr_declare(double)
Autoarr_declare(Unitype) Autoarr_declare(Unitype)
kerepType_declare(AutoarrChar);
kerepType_declare(AutoarrBool);
kerepType_declare(AutoarrFloat32);
kerepType_declare(AutoarrFloat64);
kerepType_declare(AutoarrInt8);
kerepType_declare(AutoarrUInt8);
kerepType_declare(AutoarrInt16);
kerepType_declare(AutoarrUInt16);
kerepType_declare(AutoarrInt32);
kerepType_declare(AutoarrUInt32);
kerepType_declare(AutoarrInt64);
kerepType_declare(AutoarrUInt64);
kerepType_declare(AutoarrCharPtr);
kerepType_declare(AutoarrBoolPtr);
kerepType_declare(AutoarrFloat32Ptr);
kerepType_declare(AutoarrFloat64Ptr);
kerepType_declare(AutoarrInt8Ptr);
kerepType_declare(AutoarrUInt8Ptr);
kerepType_declare(AutoarrInt16Ptr);
kerepType_declare(AutoarrUInt16Ptr);
kerepType_declare(AutoarrInt32Ptr);
kerepType_declare(AutoarrUInt32Ptr);
kerepType_declare(AutoarrInt64Ptr);
kerepType_declare(AutoarrUInt64Ptr);
// right func to clear array of unitype values // right func to clear array of unitype values
void Autoarr_free_Unitype(Autoarr(Unitype)* ar); void Autoarr_free_Unitype(Autoarr(Unitype)* ar);

View File

@ -16,6 +16,7 @@ typedef struct {\
type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\ type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\
void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\ void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\
void (*_free)(struct Autoarr_##type* ar);\ void (*_free)(struct Autoarr_##type* ar);\
type* (*toArray)(struct Autoarr_##type* ar);\
} __functions_list_t_##type;\ } __functions_list_t_##type;\
\ \
typedef struct Autoarr_##type{\ typedef struct Autoarr_##type{\
@ -27,11 +28,6 @@ typedef struct Autoarr_##type{\
__functions_list_t_##type* functions;\ __functions_list_t_##type* functions;\
} Autoarr_##type;\ } Autoarr_##type;\
\ \
void __Autoarr_add_##type(Autoarr_##type* ar, type element);\
type __Autoarr_get_##type(Autoarr_##type* ar, uint32 index);\
type* __Autoarr_getptr_##type(Autoarr_##type* ar, uint32 index);\
void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element);\
void __Autoarr_free_##type(Autoarr_##type* ar);\
Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length); Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);
#define Autoarr(type) Autoarr_##type #define Autoarr(type) Autoarr_##type
@ -48,6 +44,8 @@ Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_bloc
autoarr->functions->_free(autoarr) autoarr->functions->_free(autoarr)
#define Autoarr_create(type, max_blocks_count, max_block_length)\ #define Autoarr_create(type, max_blocks_count, max_block_length)\
__Autoarr_create_##type(max_blocks_count, max_block_length) __Autoarr_create_##type(max_blocks_count, max_block_length)
#define Autoarr_toArray(autoarr)\
autoarr->functions->toArray(autoarr)
#define Autoarr_length(autoarr) \ #define Autoarr_length(autoarr) \
(uint32)(!autoarr->blocks_count ? 0 : \ (uint32)(!autoarr->blocks_count ? 0 : \

View File

@ -46,12 +46,21 @@ void __Autoarr_free_##type(Autoarr_##type* ar){\
free(ar);\ free(ar);\
}\ }\
\ \
type* __Autoarr_toArray_##type(Autoarr_##type* ar){\
uint32 length=Autoarr_length(ar);\
type* array=malloc(length * sizeof(type));\
for(uint32 i=0; i<length; i++)\
array[i]=__Autoarr_get_##type(ar, i);\
return array;\
}\
\
__functions_list_t_##type __functions_list_##type={\ __functions_list_t_##type __functions_list_##type={\
&__Autoarr_add_##type,\ &__Autoarr_add_##type,\
&__Autoarr_get_##type,\ &__Autoarr_get_##type,\
&__Autoarr_getptr_##type,\ &__Autoarr_getptr_##type,\
&__Autoarr_set_##type,\ &__Autoarr_set_##type,\
&__Autoarr_free_##type\ &__Autoarr_free_##type,\
&__Autoarr_toArray_##type\
};\ };\
\ \
Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length){\ Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length){\

View File

@ -11,6 +11,8 @@ typedef struct Hashtable{
uint8 hein; // height=HT_HEIGHTS[hein] uint8 hein; // height=HT_HEIGHTS[hein]
Autoarr(KVPair)** rows; // Autoarr[height] Autoarr(KVPair)** rows; // Autoarr[height]
} Hashtable; } Hashtable;
kerepType_declare(Hashtable);
kerepType_declare(HashtablePtr);
Hashtable* Hashtable_create(); Hashtable* Hashtable_create();
void Hashtable_free(Hashtable* ht); void Hashtable_free(Hashtable* ht);

View File

@ -11,6 +11,8 @@ typedef struct SearchTreeNode{
struct SearchTreeNode**** branches; // *STNode[8][8][4] struct SearchTreeNode**** branches; // *STNode[8][8][4]
Unitype value; Unitype value;
} STNode; } STNode;
kerepType_declare(STNode);
kerepType_declare(STNodePtr);
STNode* STNode_create(); STNode* STNode_create();
void STNode_free(STNode* node); void STNode_free(STNode* node);

View File

@ -5,10 +5,12 @@ extern "C" {
#endif #endif
#include "std.h" #include "std.h"
#include "types.h"
#include "errors.h" #include "errors.h"
#include "cptr.h" #include "cptr.h"
#include "optime.h" #include "optime.h"
#include "types.h"
#include "unitype.h"
#include "init.h"
#if __cplusplus #if __cplusplus
} }

66
src/base/init.c Normal file
View File

@ -0,0 +1,66 @@
#include "base.h"
void kerepInit(){
kerepType_register(NULL, Null, NULL);
kerepType_register(char, Char, NULL);
kerepType_register(bool, Bool, NULL);
kerepType_register(float32, Float32, NULL);
kerepType_register(float64, Float64, NULL);
kerepType_register(int8, Int8, NULL);
kerepType_register(uint8, UInt8, NULL);
kerepType_register(int16, Int16, NULL);
kerepType_register(uint16, UInt16, NULL);
kerepType_register(int32, Int32, NULL);
kerepType_register(uint32, UInt32, NULL);
kerepType_register(int64, Int64, NULL);
kerepType_register(uint64, UInt64, NULL);
kerepType_register(char*, CharPtr, NULL);
kerepType_register(bool*, BoolPtr, NULL);
kerepType_register(float32*, Float32Ptr, NULL);
kerepType_register(float64*, Float64Ptr, NULL);
kerepType_register(int8*, Int8Ptr, NULL);
kerepType_register(uint8*, UInt8Ptr, NULL);
kerepType_register(int16*, Int16Ptr, NULL);
kerepType_register(uint16*, UInt16Ptr, NULL);
kerepType_register(int32*, Int32Ptr, NULL);
kerepType_register(uint32*, UInt32Ptr, NULL);
kerepType_register(int64*, Int64Ptr, NULL);
kerepType_register(uint64*, UInt64Ptr, NULL);
kerepType_register(Unitype, Unitype, __UnitypePtr_free);
kerepType_register(Unitype*, UnitypePtr, __UnitypePtr_free);
kerepType_register(, AutoarrChar, Autoarr_free);
kerepType_register(, AutoarrBool, Autoarr_free);
kerepType_register(, AutoarrFloat32, Autoarr_free);
kerepType_register(, AutoarrFloat64, Autoarr_free);
kerepType_register(, AutoarrInt8, Autoarr_free);
kerepType_register(, AutoarrUInt8, Autoarr_free);
kerepType_register(, AutoarrInt16, Autoarr_free);
kerepType_register(, AutoarrUInt16, Autoarr_free);
kerepType_register(, AutoarrInt32, Autoarr_free);
kerepType_register(, AutoarrUInt32, Autoarr_free);
kerepType_register(, AutoarrInt64, Autoarr_free);
kerepType_register(, AutoarrUInt64, Autoarr_free);
kerepType_register(, AutoarrCharPtr, Autoarr_free);
kerepType_register(, AutoarrBoolPtr, Autoarr_free);
kerepType_register(, AutoarrFloat32Ptr, Autoarr_free);
kerepType_register(, AutoarrFloat64Ptr, Autoarr_free);
kerepType_register(, AutoarrInt8Ptr, Autoarr_free);
kerepType_register(, AutoarrUInt8Ptr, Autoarr_free);
kerepType_register(, AutoarrInt16Ptr, Autoarr_free);
kerepType_register(, AutoarrUInt16Ptr, Autoarr_free);
kerepType_register(, AutoarrInt32Ptr, Autoarr_free);
kerepType_register(, AutoarrUInt32Ptr, Autoarr_free);
kerepType_register(, AutoarrInt64Ptr, Autoarr_free);
kerepType_register(, AutoarrUInt64Ptr, Autoarr_free);
kerepType_register(, STNode, STNode_free);
kerepType_register(, STNodePtr, STNode_free);
kerepType_register(, Hashtable, Hashtable_free);
kerepType_register(, HashtablePtr, Hashtable_free);
}

12
src/base/init.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
// call this between kerepTypesInitBegin() and kerepTypesInitEnd()
void kerepInit();
#if __cplusplus
}
#endif

View File

@ -12,10 +12,19 @@ extern "C" {
#include <time.h> #include <time.h>
#include <setjmp.h> #include <setjmp.h>
typedef int8_t int8;
typedef uint8_t uint8;
typedef int16_t int16;
typedef uint16_t uint16;
typedef int32_t int32;
typedef uint32_t uint32;
typedef int64_t int64;
typedef uint64_t uint64;
typedef float float32;
typedef double float64;
#define dbg(N) printf("\e[95m%d\n",N) #define dbg(N) printf("\e[95m%d\n",N)
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK #pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK
#define _CRT_SECURE_NO_WARNINGS 1 #define _CRT_SECURE_NO_WARNINGS 1

View File

@ -1,154 +1,40 @@
#include "types.h"
#include "errors.h"
#include "../Autoarr/Autoarr.h" #include "../Autoarr/Autoarr.h"
#include "../Hashtable/Hashtable.h" #include "unitype.h"
#include "../SearchTree/SearchTree.h"
const char* my_type_name(my_type t){ Autoarr_declare(kerepTypeDescriptor)
switch (t) { Autoarr_define(kerepTypeDescriptor)
case Null: return "Null";
case Float64: return "Float64"; // type descriptors are stored here during initialization
case Float32: return "Float32"; Autoarr(kerepTypeDescriptor)* __kerepTypeDescriptors=NULL;
case Bool: return "Bool"; // here type descriptors are stored when initialization is complited
case Char: return "Char"; kerepTypeDescriptor* typeDescriptors=NULL;
case Int8: return "Int8"; kerepTypeId kerepTypeId_last=-1;
case UInt8: return "UInt8";
case Int16: return "Int16"; typedef enum{
case UInt16: return "UInt16"; NotInitialized, Initializing, Initialized
case Int32: return "Int32"; } kerepTypeDescriptorsState;
case UInt32: return "UInt32"; kerepTypeDescriptorsState initState=NotInitialized;
case Int64: return "Int64";
case UInt64: return "UInt64"; void kerepTypeDescriptors_beginInit(){
case Int8Ptr: return "Int8Ptr"; __kerepTypeDescriptors=Autoarr_create(kerepTypeDescriptor, 256, 256);
case UInt8Ptr: return "UInt8Ptr"; if(__kerepTypeDescriptors==NULL) throw(ERR_NULLPTR);
case Int16Ptr: return "Int16Ptr";
case UInt16Ptr: return "UInt16Ptr";
case Int32Ptr: return "Int32Ptr";
case UInt32Ptr: return "UInt32Ptr";
case Int64Ptr: return "Int64Ptr";
case UInt64Ptr: return "UInt64Ptr";
case CharPtr: return "CharPtr";
case STNodePtr: return "STNodePtr";
case HashtablePtr: return "HashtablePtr";
case UniversalType: return "Unitype";
case AutoarrInt8Ptr: return "AutoarrInt8Ptr";
case AutoarrUInt8Ptr: return "AutoarrUInt8Ptr";
case AutoarrInt16Ptr: return "AutoarrInt16Ptr";
case AutoarrUInt16Ptr: return "AutoarrUInt16Ptr";
case AutoarrInt32Ptr: return "AutoarrInt32Ptr";
case AutoarrUInt32Ptr: return "AutoarrUInt32Ptr";
case AutoarrInt64Ptr: return "AutoarrInt64Ptr";
case AutoarrUInt64Ptr: return "AutoarrUInt64Ptr";
case AutoarrUnitypePtr: return "AutoarrUnitypePtr";
case AutoarrKVPairPtr: return "AutoarrKVPairPtr";
default: throw(ERR_WRONGTYPE);
}
} }
// frees VoidPtr value or does nothing if type isn't pointer void kerepTypeDescriptors_endInit(){
void Unitype_free(Unitype u){ typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors);
switch (u.type) { Autoarr_free(__kerepTypeDescriptors);
case Null: if(typeDescriptors==NULL) throw(ERR_NULLPTR);
case Float32: }
case Float64: void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){
case Char: kerepTypeDescriptor typeDesc={
case Bool: .name=name,
case Int8: .size=size,
case UInt8: .free_members=free_members,
case Int16: .id=++kerepTypeId_last
case UInt16: };
case Int32: Autoarr_add(__kerepTypeDescriptors, typeDesc);
case UInt32:
case Int64:
case UInt64:
break;
case Int8Ptr:
case UInt8Ptr:
case Int16Ptr:
case UInt16Ptr:
case Int32Ptr:
case UInt32Ptr:
case Int64Ptr:
case UInt64Ptr:
case CharPtr:
free(u.VoidPtr);
break;
case HashtablePtr:
Hashtable_free(u.VoidPtr);
break;
case STNodePtr:
STNode_free(u.VoidPtr);
break;
case AutoarrInt8Ptr:
__Autoarr_free_int8(u.VoidPtr);
break;
case AutoarrUInt8Ptr:
__Autoarr_free_uint8(u.VoidPtr);
break;
case AutoarrInt16Ptr:
__Autoarr_free_int16(u.VoidPtr);
break;
case AutoarrUInt16Ptr:
__Autoarr_free_uint16(u.VoidPtr);
break;
case AutoarrInt32Ptr:
__Autoarr_free_int32(u.VoidPtr);
break;
case AutoarrUInt32Ptr:
__Autoarr_free_uint32(u.VoidPtr);
break;
case AutoarrInt64Ptr:
__Autoarr_free_int64(u.VoidPtr);
break;
case AutoarrUInt64Ptr:
__Autoarr_free_uint64(u.VoidPtr);
break;
case AutoarrUnitypePtr:
Autoarr_free_Unitype(u.VoidPtr);
break;
case AutoarrKVPairPtr:
Autoarr_free_KVPair(u.VoidPtr);
break;
default: throw(ERR_WRONGTYPE);
}
} }
#define BUFSIZE 64 kerepTypeDescriptor typeDescriptor_get(kerepTypeId id){
char* sprintuni(Unitype v){ return typeDescriptors[id];
char* buf=malloc(BUFSIZE);
IFMSC(
switch (v.type) {
case Null: sprintf_s(buf, BUFSIZE, "{Null}");break;
case Float64: sprintf_s(buf, BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break;
case Bool:
case UInt64: sprintf_s(buf, BUFSIZE, "{%s : %lu}", my_type_name(v.type),v.UInt64);break;
case Int64: sprintf_s(buf, BUFSIZE, "{%s : %ld}", my_type_name(v.type),v.Int64);break;
case CharPtr: ;
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
buf=realloc(buf, newBUFSIZE);
sprintf_s(buf, newBUFSIZE, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);
break;
default: sprintf_s(buf, BUFSIZE, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break;
},
switch (v.type) {
case Null: sprintf(buf, "{Null}"); break;
case Float64: sprintf(buf, "{%s : %lf}", my_type_name(v.type),v.Float64); break;
case Bool:
case UInt64: sprintf(buf, "{%s : " IFWIN("%llu", "%lu") "}", my_type_name(v.type),v.UInt64); break;
case Int64: sprintf(buf, "{%s : " IFWIN("%lld", "%ld") "}", my_type_name(v.type),v.Int64); break;
case CharPtr: ;
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
buf=realloc(buf, newBUFSIZE);
sprintf(buf, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);
break;
default: sprintf(buf, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break;
}
);
return buf;
}
void printuni(Unitype v){
char* s=sprintuni(v);
fputs(s, stdout);
free(s);
} }

113
src/base/types.c.old Normal file
View File

@ -0,0 +1,113 @@
#include "types.h"
#include "errors.h"
#include "../Autoarr/Autoarr.h"
#include "../Hashtable/Hashtable.h"
#include "../SearchTree/SearchTree.h"
// frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u){
switch (u.type) {
case Null:
case Float32:
case Float64:
case Char:
case Bool:
case Int8:
case UInt8:
case Int16:
case UInt16:
case Int32:
case UInt32:
case Int64:
case UInt64:
break;
case Int8Ptr:
case UInt8Ptr:
case Int16Ptr:
case UInt16Ptr:
case Int32Ptr:
case UInt32Ptr:
case Int64Ptr:
case UInt64Ptr:
case CharPtr:
free(u.VoidPtr);
break;
case HashtablePtr:
Hashtable_free(u.VoidPtr);
break;
case STNodePtr:
STNode_free(u.VoidPtr);
break;
case AutoarrInt8Ptr:
__Autoarr_free_int8(u.VoidPtr);
break;
case AutoarrUInt8Ptr:
__Autoarr_free_uint8(u.VoidPtr);
break;
case AutoarrInt16Ptr:
__Autoarr_free_int16(u.VoidPtr);
break;
case AutoarrUInt16Ptr:
__Autoarr_free_uint16(u.VoidPtr);
break;
case AutoarrInt32Ptr:
__Autoarr_free_int32(u.VoidPtr);
break;
case AutoarrUInt32Ptr:
__Autoarr_free_uint32(u.VoidPtr);
break;
case AutoarrInt64Ptr:
__Autoarr_free_int64(u.VoidPtr);
break;
case AutoarrUInt64Ptr:
__Autoarr_free_uint64(u.VoidPtr);
break;
case AutoarrUnitypePtr:
Autoarr_free_Unitype(u.VoidPtr);
break;
case AutoarrKVPairPtr:
Autoarr_free_KVPair(u.VoidPtr);
break;
default: throw(ERR_WRONGTYPE);
}
}
#define BUFSIZE 64
char* sprintuni(Unitype v){
char* buf=malloc(BUFSIZE);
IFMSC(
switch (v.type) {
case Null: sprintf_s(buf, BUFSIZE, "{Null}");break;
case Float64: sprintf_s(buf, BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break;
case Bool:
case UInt64: sprintf_s(buf, BUFSIZE, "{%s : %lu}", my_type_name(v.type),v.UInt64);break;
case Int64: sprintf_s(buf, BUFSIZE, "{%s : %ld}", my_type_name(v.type),v.Int64);break;
case CharPtr: ;
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
buf=realloc(buf, newBUFSIZE);
sprintf_s(buf, newBUFSIZE, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);
break;
default: sprintf_s(buf, BUFSIZE, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break;
},
switch (v.type) {
case Null: sprintf(buf, "{Null}"); break;
case Float64: sprintf(buf, "{%s : %lf}", my_type_name(v.type),v.Float64); break;
case Bool:
case UInt64: sprintf(buf, "{%s : " IFWIN("%llu", "%lu") "}", my_type_name(v.type),v.UInt64); break;
case Int64: sprintf(buf, "{%s : " IFWIN("%lld", "%ld") "}", my_type_name(v.type),v.Int64); break;
case CharPtr: ;
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
buf=realloc(buf, newBUFSIZE);
sprintf(buf, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);
break;
default: sprintf(buf, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break;
}
);
return buf;
}
void printuni(Unitype v){
char* s=sprintuni(v);
fputs(s, stdout);
free(s);
}

View File

@ -6,52 +6,55 @@ extern "C" {
#include "std.h" #include "std.h"
typedef int8_t int8; typedef uint16 kerepTypeId;
typedef uint8_t uint8; typedef struct kerepTypeDescriptor{
typedef int16_t int16; void (*free_members)(void*); // NULL or function which frees all struct members
typedef uint16_t uint16; char* name;
typedef int32_t int32; kerepTypeId id;
typedef uint32_t uint32; uint16 size;
typedef int64_t int64; } kerepTypeDescriptor;
typedef uint64_t uint64;
typedef float float32;
typedef double float64;
typedef enum __attribute__((__packed__)) my_type {
Null, Float32, Float64, Char, Bool,
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64,
UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr,
CharPtr, STNodePtr, HashtablePtr,
UniversalType,
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
AutoarrUnitypePtr, AutoarrKVPairPtr, knSocketPtr
} my_type;
#define my_type_last knSocketPtr
const char* my_type_name(my_type t); #define kerepType_declare(NAME)\
extern kerepTypeId kerepTypeId_##NAME
typedef struct Unitype{ extern kerepTypeId kerepTypeId_last;
union { void __kerepType_register(char* name, int16 size, void (*free_members)(void*));
int64 Int64;
uint64 UInt64;
double Float64;
bool Bool;
void* VoidPtr;
};
my_type type;
} Unitype;
static const Unitype UniNull={.VoidPtr=NULL,.type=Null}; #define kerepType_register(TYPE, NAME, FREE_MEMBERS_FUNC)\
static const Unitype UniTrue={.Bool=true,.type=Bool}; __kerepType_register(#NAME, sizeof(TYPE), FREE_MEMBERS_FUNC);\
static const Unitype UniFalse={.Bool=false,.type=Bool}; kerepTypeId_##NAME=kerepTypeId_last;
#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL} void kerepTypeDescriptors_beginInit();
#define UniPtr(TYPE,VAL) (Unitype){.type=TYPE,.VoidPtr=VAL} void kerepTypeDescriptors_endInit();
kerepTypeDescriptor typeDescriptor_get(kerepTypeId id);
// frees VoidPtr value or does nothing if type isn't pointer kerepType_declare(Null);
void Unitype_free(Unitype u);
void printuni(Unitype v); kerepType_declare(Char);
char* sprintuni(Unitype v); kerepType_declare(Bool);
kerepType_declare(Float32);
kerepType_declare(Float64);
kerepType_declare(Int8);
kerepType_declare(UInt8);
kerepType_declare(Int16);
kerepType_declare(UInt16);
kerepType_declare(Int32);
kerepType_declare(UInt32);
kerepType_declare(Int64);
kerepType_declare(UInt64);
kerepType_declare(CharPtr);
kerepType_declare(BoolPtr);
kerepType_declare(Float32Ptr);
kerepType_declare(Float64Ptr);
kerepType_declare(Int8Ptr);
kerepType_declare(UInt8Ptr);
kerepType_declare(Int16Ptr);
kerepType_declare(UInt16Ptr);
kerepType_declare(Int32Ptr);
kerepType_declare(UInt32Ptr);
kerepType_declare(Int64Ptr);
kerepType_declare(UInt64Ptr);
#if __cplusplus #if __cplusplus
} }

10
src/base/unitype.c Normal file
View File

@ -0,0 +1,10 @@
#include "unitype.h"
void Unitype_free(Unitype u){
kerepTypeDescriptor type=typeDescriptor_get(u.typeId);
if(type.free_members)
type.free_members(u.VoidPtr);
if(u.allocatedInHeap)
free(u.VoidPtr);
}
void __UnitypePtr_free(Unitype* u) { Unitype_free(&u); }

50
src/base/unitype.h Normal file
View File

@ -0,0 +1,50 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "types.h"
typedef struct Unitype{
union {
int64 Int64;
uint64 UInt64;
double Float64;
bool Bool;
void* VoidPtr;
char Bytes[8];
};
kerepTypeId typeId;
bool allocatedInHeap; // should Unitype_free call free() to VoidPtr*
} Unitype;
kerepType_declare(Unitype);
kerepType_declare(UnitypePtr);
#define __Uni(TYPE,VAL) (Unitype){\
.TYPE_NAME=VAL, .type=kerepTypeId_##TYPE, .allocatedInHeap=false}
#define UniInt64(VAL) __Uni(Int64, VAL)
#define UniUInt64(VAL) __Uni(UInt64, VAL)
#define UniFloat64(VAL) __Uni(Float64, VAL)
#define UniBool(VAL) __Uni(Bool, VAL)
#define UniPtrStack(TYPE_NAME,VAL) (Unitype){\
.VoidPtr=VAL, .typeId=kerepTypeId_##TYPE_NAME, .allocatedInHeap=false}
#define UniPtrHeap (TYPE_NAME,VAL) (Unitype){\
.VoidPtr=VAL, .typeId=kerepTypeId_##TYPE_NAME, .allocatedInHeap=true}
#define UniNull UniPtrStack(Null, NULL)
#define UniTrue UniBool(true)
#define UniFalse UniBool(false)
// frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u);
void __UnitypePtr_free(Unitype* u);
void printuni(Unitype v);
char* sprintuni(Unitype v);
#if __cplusplus
}
#endif