registred all types

This commit is contained in:
timerix 2022-08-25 01:37:14 +06:00
parent 2e378d1458
commit d62405ccff
24 changed files with 201 additions and 213 deletions

View File

@ -1,19 +1,23 @@
#include "Autoarr.h"
Autoarr_define(uint8);
Autoarr_define(int8);
Autoarr_define(uint16);
Autoarr_define(int16);
Autoarr_define(uint32);
Autoarr_define(int32);
Autoarr_define(uint64);
Autoarr_define(int64);
Autoarr_define(float);
Autoarr_define(double);
Autoarr_define(Unitype);
Autoarr_define(char)
Autoarr_define(bool)
Autoarr_define(float32)
Autoarr_define(float64)
Autoarr_define(uint8)
Autoarr_define(int8)
Autoarr_define(uint16)
Autoarr_define(int16)
Autoarr_define(uint32)
Autoarr_define(int32)
Autoarr_define(uint64)
Autoarr_define(int64)
Autoarr_define(Unitype)
// right func to clear array of unitype values
void Autoarr_free_Unitype(Autoarr(Unitype)* ar){
void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){
Autoarr_foreach(ar, u,Unitype_free(u));
Autoarr_free(ar);
if(freePtr) Autoarr_free(ar, freePtr);
}
void ____Autoarr_free_Unitype_(void* ar) { __Autoarr_free_Unitype_((Autoarr(Unitype)*)ar, false); }

View File

@ -7,6 +7,8 @@ extern "C" {
#include "Autoarr_declare.h"
#include "Autoarr_define.h"
Autoarr_declare(char)
Autoarr_declare(bool)
Autoarr_declare(float32)
Autoarr_declare(float64)
Autoarr_declare(int8)
@ -17,7 +19,6 @@ Autoarr_declare(int32)
Autoarr_declare(uint32)
Autoarr_declare(int64)
Autoarr_declare(uint64)
Autoarr_declare(Unitype)
kerepType_declare(AutoarrChar);
kerepType_declare(AutoarrBool);
@ -45,8 +46,13 @@ kerepType_declare(AutoarrUInt32Ptr);
kerepType_declare(AutoarrInt64Ptr);
kerepType_declare(AutoarrUInt64Ptr);
// right func to clear array of unitype values
void Autoarr_free_Unitype(Autoarr(Unitype)* ar);
Autoarr_declare(Unitype)
kerepType_declare(AutoarrUnitype);
kerepType_declare(AutoarrUnitypePtr);
// this function is injected in kerep_init()
void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr);
void ____Autoarr_free_Unitype_(void* ar);
#define Autoarr_foreach(ar,elem,codeblock)({\
if(ar->blocks_count>0) {\

View File

@ -10,7 +10,7 @@ EXPORT void CALL kerep_Autoarr_KVPair_create(uint16 max_blocks_count, uint16 max
}
EXPORT void CALL kerep_Autoarr_KVPair_free(Autoarr_KVPair* ar){
Autoarr_free_KVPair(ar);
Autoarr_free(ar, true);
}
EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, uint32 index, KVPair* output){

View File

@ -9,7 +9,7 @@ EXPORT void CALL kerep_Autoarr_Unitype_create(uint16 max_blocks_count, uint16 ma
}
EXPORT void CALL kerep_Autoarr_Unitype_free(Autoarr_Unitype* ar){
Autoarr_free_Unitype(ar);
Autoarr_free(ar, true);
}
EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, uint32 index, Unitype* output){

View File

@ -15,7 +15,7 @@ typedef struct {\
type (*get)(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 (*_free)(struct Autoarr_##type* ar);\
void (*freear)(struct Autoarr_##type* ar, bool freePtr);\
type* (*toArray)(struct Autoarr_##type* ar);\
} __functions_list_t_##type;\
\
@ -28,10 +28,13 @@ typedef struct Autoarr_##type{\
__functions_list_t_##type* functions;\
} Autoarr_##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);\
void ____Autoarr_free_##type(void* ar);
#define Autoarr(type) Autoarr_##type
#define Autoarr_create(type, max_blocks_count, max_block_length)\
__Autoarr_create_##type(max_blocks_count, max_block_length)
#define Autoarr_add(autoarr, element)\
autoarr->functions->add(autoarr, element)
#define Autoarr_get(autoarr, index)\
@ -40,10 +43,8 @@ Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_bloc
autoarr->functions->getptr(autoarr,index)
#define Autoarr_set(autoarr, index, element)\
autoarr->functions->set(autoarr, index, element)
#define Autoarr_free(autoarr)\
autoarr->functions->_free(autoarr)
#define Autoarr_create(type, max_blocks_count, max_block_length)\
__Autoarr_create_##type(max_blocks_count, max_block_length)
#define Autoarr_free(autoarr, freePtr)\
autoarr->functions->freear(autoarr, freePtr)
#define Autoarr_toArray(autoarr)\
autoarr->functions->toArray(autoarr)

View File

@ -39,11 +39,14 @@ void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element){\
ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\
}\
\
void __Autoarr_free_##type(Autoarr_##type* ar){\
void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){\
for(uint16 i=0; i<ar->blocks_count;i++)\
free(ar->values[i]); \
free(ar->values);\
free(ar);\
if(freePtr) free(ar);\
}\
void ____Autoarr_free_##type(void* ar){\
__Autoarr_free_##type((Autoarr_##type*)ar, false);\
}\
\
type* __Autoarr_toArray_##type(Autoarr_##type* ar){\

View File

@ -154,7 +154,7 @@ Maybe __ReadList(DeserializeSharedData* shared){
Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
bool readingList=true;
while (true){
try(ReadValue((&readingList)), val, Autoarr_free_Unitype(list))
try(ReadValue((&readingList)), val, Autoarr_free(list, true))
Autoarr_add(list,val.value);
if (!readingList){
if(val.value.type==Null)

View File

@ -17,10 +17,14 @@ Hashtable* Hashtable_create(){
return ht;
}
void Hashtable_free(Hashtable* ht){
void __Hashtable_free(void* _ht){
Hashtable* ht=_ht;
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
Autoarr_free_KVPair(ht->rows[i]);
Autoarr_free(ht->rows[i], true);
free(ht->rows);
}
void Hashtable_free(Hashtable* ht){
__Hashtable_free(ht);
free(ht);
}
@ -43,7 +47,7 @@ void Hashtable_expand(Hashtable* ht){
Autoarr(KVPair)* newar=newrows[newrown];
Autoarr_add(newar,p);
}
Autoarr_free(ar);
Autoarr_free(ar, true);
}
free(ht->rows);

View File

@ -16,6 +16,7 @@ kerepType_declare(HashtablePtr);
Hashtable* Hashtable_create();
void Hashtable_free(Hashtable* ht);
void __Hashtable_free(void* ht);
// amount of rows
uint16 Hashtable_height(Hashtable* ht);

View File

@ -8,11 +8,15 @@ void KVPair_free(KVPair p){
free(p.key);
Unitype_free(p.value);
}
void __KVPair_free(void* p){ KVPair_free(*(KVPair*)p); }
// func for KVP array clearing
void Autoarr_free_KVPair(Autoarr_KVPair* ar){
void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr){
Autoarr_foreach(ar,k,KVPair_free(k));
Autoarr_free(ar);
Autoarr_free(ar, freePtr);
}
void ____Autoarr_free_KVPair_(void* ar){
__Autoarr_free_KVPair_((Autoarr_KVPair*)ar, false);
}
void printkvp(KVPair p){

View File

@ -11,14 +11,20 @@ typedef struct KVPair{
char* key;
Unitype value;
} KVPair;
kerepType_declare(KVPair);
kerepType_declare(KVPairPtr);
Autoarr_declare(KVPair)
kerepType_declare(AutoarrKVPair);
kerepType_declare(AutoarrKVPairPtr);
// proper way to clear a KVP
void KVPair_free(KVPair p);
void __KVPair_free(void* p);
// func to clear KVP array
void Autoarr_free_KVPair(Autoarr_KVPair* ar);
void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr);
void ____Autoarr_free_KVPair_(void* ar);
void printkvp(KVPair p);

View File

@ -8,7 +8,8 @@ STNode* STNode_create(){
return node;
}
void STNode_free(STNode* node){
void __STNode_free(void* _node){
STNode* node=_node;
if (!node) throw(ERR_NULLPTR);
if(node->branches){
for(uint8 n32 = 0;n32<8;n32++){
@ -34,6 +35,10 @@ void STNode_free(STNode* node){
Unitype_free(node->value);
free(node);
}
void STNode_free(STNode* node){
__STNode_free(node);
free(node);
}
typedef struct {uint8 n32, n4, rem;} indexes3;

View File

@ -16,6 +16,7 @@ kerepType_declare(STNodePtr);
STNode* STNode_create();
void STNode_free(STNode* node);
void __STNode_free(void* node);
void ST_push(STNode* node, char* key, Unitype value);
void ST_pushString(STNode* node, string key, Unitype value);

View File

@ -17,7 +17,7 @@ void complete_buf(StringBuilder* b){
str.ptr[i++]=c;
}));
Autoarr_add(b->compl_bufs,str);
Autoarr_free(b->curr_buf);
Autoarr_free(b->curr_buf, true);
b->curr_buf=Autoarr_create(int8,BL_C,BL_L);
}
@ -34,9 +34,13 @@ StringBuilder* StringBuilder_create(){
return b;
}
void __StringBuilder_free(void* _b){
StringBuilder* b=_b;
if(b->compl_bufs) Autoarr_free(b->compl_bufs, true);
Autoarr_free(b->curr_buf, true);
}
void StringBuilder_free(StringBuilder* b){
if(b->compl_bufs) Autoarr_free(b->compl_bufs);
Autoarr_free(b->curr_buf);
__StringBuilder_free(b);
free(b);
}

View File

@ -13,9 +13,12 @@ typedef struct StringBuilder{
Autoarr(string)* compl_bufs;
Autoarr(int8)* curr_buf;
} StringBuilder;
kerepType_declare(StringBuilder);
kerepType_declare(StringBuilderPtr);
StringBuilder* StringBuilder_create(void);
void StringBuilder_free(StringBuilder* b);
void __StringBuilder_free(void* b);
// Joins all strings from compl_bufs.
// Returns zero-terminated string.
// No need to call string_extract()!

View File

@ -5,7 +5,7 @@ extern "C" {
#endif
#include "std.h"
#include "types.h"
#include "unitype.h"
typedef enum ErrorId {
SUCCESS, // not an error

View File

@ -1,8 +1,13 @@
#include "base.h"
#include "../Autoarr/Autoarr.h"
#include "../SearchTree/SearchTree.h"
#include "../Hashtable/Hashtable.h"
#include "../String/StringBuilder.h"
void kerepInit(){
void kerepTypeDescriptors_initKerepTypes(){
// null
kerepType_register(NULL, Null, NULL);
// base types
kerepType_register(char, Char, NULL);
kerepType_register(bool, Bool, NULL);
kerepType_register(float32, Float32, NULL);
@ -15,7 +20,7 @@ void kerepInit(){
kerepType_register(uint32, UInt32, NULL);
kerepType_register(int64, Int64, NULL);
kerepType_register(uint64, UInt64, NULL);
// base type pointers
kerepType_register(char*, CharPtr, NULL);
kerepType_register(bool*, BoolPtr, NULL);
kerepType_register(float32*, Float32Ptr, NULL);
@ -29,38 +34,62 @@ void kerepInit(){
kerepType_register(int64*, Int64Ptr, NULL);
kerepType_register(uint64*, UInt64Ptr, NULL);
// base type autoarrs
kerepType_register(Autoarr_char, AutoarrChar, ____Autoarr_free_char);
kerepType_register(Autoarr_bool, AutoarrBool, ____Autoarr_free_bool);
kerepType_register(Autoarr_float32, AutoarrFloat32, ____Autoarr_free_float32);
kerepType_register(Autoarr_float64, AutoarrFloat64, ____Autoarr_free_float64);
kerepType_register(Autoarr_int8, AutoarrInt8, ____Autoarr_free_int8);
kerepType_register(Autoarr_uint8, AutoarrUInt8, ____Autoarr_free_uint8);
kerepType_register(Autoarr_int16, AutoarrInt16, ____Autoarr_free_int16);
kerepType_register(Autoarr_uint16, AutoarrUInt16, ____Autoarr_free_uint16);
kerepType_register(Autoarr_int32, AutoarrInt32, ____Autoarr_free_int32);
kerepType_register(Autoarr_uint32, AutoarrUInt32, ____Autoarr_free_uint32);
kerepType_register(Autoarr_int64, AutoarrInt64, ____Autoarr_free_int64);
kerepType_register(Autoarr_uint64, AutoarrUInt64, ____Autoarr_free_uint64);
// base type autoarr pointers
kerepType_register(Autoarr_char*, AutoarrCharPtr, ____Autoarr_free_char);
kerepType_register(Autoarr_bool*, AutoarrBoolPtr, ____Autoarr_free_bool);
kerepType_register(Autoarr_float32*, AutoarrFloat32Ptr, ____Autoarr_free_float32);
kerepType_register(Autoarr_float64*, AutoarrFloat64Ptr, ____Autoarr_free_float64);
kerepType_register(Autoarr_int8*, AutoarrInt8Ptr, ____Autoarr_free_int8);
kerepType_register(Autoarr_uint8*, AutoarrUInt8Ptr, ____Autoarr_free_uint8);
kerepType_register(Autoarr_int16*, AutoarrInt16Ptr, ____Autoarr_free_int16);
kerepType_register(Autoarr_uint16*, AutoarrUInt16Ptr, ____Autoarr_free_uint16);
kerepType_register(Autoarr_int32*, AutoarrInt32Ptr, ____Autoarr_free_int32);
kerepType_register(Autoarr_uint32*, AutoarrUInt32Ptr, ____Autoarr_free_uint32);
kerepType_register(Autoarr_int64*, AutoarrInt64Ptr, ____Autoarr_free_int64);
kerepType_register(Autoarr_uint64*, AutoarrUInt64Ptr, ____Autoarr_free_uint64);
// Unitype
kerepType_register(Unitype, Unitype, __UnitypePtr_free);
kerepType_register(Unitype*, UnitypePtr, __UnitypePtr_free);
kerepType_register(Autoarr_Unitype, AutoarrUnitype, ____Autoarr_free_Unitype_);
kerepType_register(Autoarr_Unitype*, AutoarrUnitypePtr, ____Autoarr_free_Unitype_);
// replacing autogenerated freear() function to custom
Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1);
_uar->functions->freear=__Autoarr_free_Unitype_;
Autoarr_free(_uar, true);
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);
// SearchTreeNode
kerepType_register(STNode, STNode, __STNode_free);
kerepType_register(STNode*, STNodePtr, __STNode_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);
// KeyValuePair
kerepType_register(KVPair, KVPair, __KVPair_free);
kerepType_register(KVPair*, KVPairPtr, __KVPair_free);
kerepType_register(Autoarr_KVPair, AutoarrKVPair, ____Autoarr_free_KVPair_);
kerepType_register(Autoarr_KVPair*, AutoarrKVPairPtr, ____Autoarr_free_KVPair_);
// replacing autogenerated freear() function to custom
Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1);
_kvpar->functions->freear=__Autoarr_free_KVPair_;
Autoarr_free(_kvpar, true);
kerepType_register(, STNode, STNode_free);
kerepType_register(, STNodePtr, STNode_free);
// Hashtable
kerepType_register(Hashtable, Hashtable, __Hashtable_free);
kerepType_register(Hashtable*, HashtablePtr, __Hashtable_free);
kerepType_register(, Hashtable, Hashtable_free);
kerepType_register(, HashtablePtr, Hashtable_free);
// StringBuilder
kerepType_register(StringBuilder, StringBuilder, __StringBuilder_free);
kerepType_register(StringBuilder*, StringBuilderPtr, __StringBuilder_free);
}

View File

@ -4,8 +4,8 @@
extern "C" {
#endif
// call this between kerepTypesInitBegin() and kerepTypesInitEnd()
void kerepInit();
// call this between kerepTypeDescriptors_beginInit() and kerepTypeDescriptors_endInit()
void kerepTypeDescriptors_initKerepTypes();
#if __cplusplus
}

View File

@ -44,7 +44,6 @@ typedef double float64;
#pragma GCC error "unknown compiler"
#endif
#ifdef _MSC_VER
#define IFWIN(YES, NO) YES
#define IFMSC(YES, NO) YES
@ -58,6 +57,9 @@ typedef double float64;
#pragma GCC error "unknown compiler"
#endif
#ifndef sprintf_s
#define sprintf_s(BUF, BUFSIZE, FORMAT, ...) sprintf(BUF, FORMAT, ## __VA_ARGS__)
#endif
#if __cplusplus
}

View File

@ -22,7 +22,7 @@ void kerepTypeDescriptors_beginInit(){
void kerepTypeDescriptors_endInit(){
typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors);
Autoarr_free(__kerepTypeDescriptors);
Autoarr_free(__kerepTypeDescriptors,true);
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
}
void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){
@ -35,6 +35,6 @@ void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){
Autoarr_add(__kerepTypeDescriptors, typeDesc);
}
kerepTypeDescriptor typeDescriptor_get(kerepTypeId id){
kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id){
return typeDescriptors[id];
}

View File

@ -1,113 +0,0 @@
#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

@ -7,6 +7,7 @@ extern "C" {
#include "std.h"
typedef uint16 kerepTypeId;
typedef struct kerepTypeDescriptor{
void (*free_members)(void*); // NULL or function which frees all struct members
char* name;
@ -26,7 +27,7 @@ void __kerepType_register(char* name, int16 size, void (*free_members)(void*));
void kerepTypeDescriptors_beginInit();
void kerepTypeDescriptors_endInit();
kerepTypeDescriptor typeDescriptor_get(kerepTypeId id);
kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id);
kerepType_declare(Null);

View File

@ -1,10 +1,37 @@
#include "unitype.h"
#include "base.h"
void Unitype_free(Unitype u){
kerepTypeDescriptor type=typeDescriptor_get(u.typeId);
kerepTypeDescriptor type=kerepTypeDescriptor_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); }
void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
#define BUFSIZE 64
char* sprintuni(Unitype v){
char* buf=malloc(BUFSIZE);
kerepTypeDescriptor type=kerepTypeDescriptor_get(v.typeId);
if(v.typeId==kerepTypeId_Null)
sprintf_s(buf, BUFSIZE, "{Null}");
else if(v.typeId==kerepTypeId_Float64)
sprintf_s(buf, BUFSIZE, "{%s ) %lf}", type.name,v.Float64);
else if(v.typeId==kerepTypeId_Bool || v.typeId==kerepTypeId_UInt64)
sprintf_s(buf, BUFSIZE, "{%s ) " IFWIN("%llu", "%lu") "}", type.name,v.UInt64);
else if(v.typeId==kerepTypeId_Int64)
sprintf_s(buf, BUFSIZE, "{%s ) " IFWIN("%lld", "%ld") "}", type.name,v.Int64);
else if(v.typeId==kerepTypeId_CharPtr){
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
buf=realloc(buf, newBUFSIZE);
sprintf_s(buf, BUFSIZE, "{%s ) \"%s\"}", type.name,(char*)v.VoidPtr);
}
else sprintf_s(buf, BUFSIZE, "{%s ) %p}", type.name,v.VoidPtr);
return buf;
}
void printuni(Unitype v){
char* s=sprintuni(v);
fputs(s, stdout);
free(s);
}

View File

@ -23,7 +23,7 @@ kerepType_declare(UnitypePtr);
#define __Uni(TYPE,VAL) (Unitype){\
.TYPE_NAME=VAL, .type=kerepTypeId_##TYPE, .allocatedInHeap=false}
.TYPE_NAME=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false}
#define UniInt64(VAL) __Uni(Int64, VAL)
#define UniUInt64(VAL) __Uni(UInt64, VAL)
@ -41,7 +41,7 @@ kerepType_declare(UnitypePtr);
// frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u);
void __UnitypePtr_free(Unitype* u);
void __UnitypePtr_free(void* u);
void printuni(Unitype v);
char* sprintuni(Unitype v);