diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index 89440a4..725b174 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -7,18 +7,44 @@ extern "C" { #include "Autoarr_declare.h" #include "Autoarr_define.h" -Autoarr_declare(uint8) +Autoarr_declare(float32) +Autoarr_declare(float64) Autoarr_declare(int8) -Autoarr_declare(uint16) +Autoarr_declare(uint8) Autoarr_declare(int16) -Autoarr_declare(uint32) +Autoarr_declare(uint16) Autoarr_declare(int32) -Autoarr_declare(uint64) +Autoarr_declare(uint32) Autoarr_declare(int64) -Autoarr_declare(float) -Autoarr_declare(double) +Autoarr_declare(uint64) 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 void Autoarr_free_Unitype(Autoarr(Unitype)* ar); diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index 80169bd..9b15bd7 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -16,6 +16,7 @@ typedef struct {\ type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\ void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\ void (*_free)(struct Autoarr_##type* ar);\ + type* (*toArray)(struct Autoarr_##type* ar);\ } __functions_list_t_##type;\ \ typedef struct Autoarr_##type{\ @@ -27,11 +28,6 @@ typedef struct Autoarr_##type{\ __functions_list_t_##type* functions;\ } 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); #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) #define 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) \ (uint32)(!autoarr->blocks_count ? 0 : \ diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index 441c9b6..ffc50e1 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -46,12 +46,21 @@ void __Autoarr_free_##type(Autoarr_##type* 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 #include +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) - #ifdef _MSC_VER #pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK #define _CRT_SECURE_NO_WARNINGS 1 diff --git a/src/base/types.c b/src/base/types.c index fed1f0b..2f0e1a7 100644 --- a/src/base/types.c +++ b/src/base/types.c @@ -1,154 +1,40 @@ -#include "types.h" -#include "errors.h" #include "../Autoarr/Autoarr.h" -#include "../Hashtable/Hashtable.h" -#include "../SearchTree/SearchTree.h" +#include "unitype.h" -const char* my_type_name(my_type t){ - switch (t) { - case Null: return "Null"; - case Float64: return "Float64"; - case Float32: return "Float32"; - case Bool: return "Bool"; - case Char: return "Char"; - case Int8: return "Int8"; - case UInt8: return "UInt8"; - case Int16: return "Int16"; - case UInt16: return "UInt16"; - case Int32: return "Int32"; - case UInt32: return "UInt32"; - case Int64: return "Int64"; - case UInt64: return "UInt64"; - case Int8Ptr: return "Int8Ptr"; - case UInt8Ptr: return "UInt8Ptr"; - 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); - } +Autoarr_declare(kerepTypeDescriptor) +Autoarr_define(kerepTypeDescriptor) + +// type descriptors are stored here during initialization +Autoarr(kerepTypeDescriptor)* __kerepTypeDescriptors=NULL; +// here type descriptors are stored when initialization is complited +kerepTypeDescriptor* typeDescriptors=NULL; +kerepTypeId kerepTypeId_last=-1; + +typedef enum{ + NotInitialized, Initializing, Initialized +} kerepTypeDescriptorsState; +kerepTypeDescriptorsState initState=NotInitialized; + +void kerepTypeDescriptors_beginInit(){ + __kerepTypeDescriptors=Autoarr_create(kerepTypeDescriptor, 256, 256); + if(__kerepTypeDescriptors==NULL) throw(ERR_NULLPTR); } -// 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); - } +void kerepTypeDescriptors_endInit(){ + typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors); + Autoarr_free(__kerepTypeDescriptors); + if(typeDescriptors==NULL) throw(ERR_NULLPTR); +} +void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){ + kerepTypeDescriptor typeDesc={ + .name=name, + .size=size, + .free_members=free_members, + .id=++kerepTypeId_last + }; + Autoarr_add(__kerepTypeDescriptors, typeDesc); } -#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; +kerepTypeDescriptor typeDescriptor_get(kerepTypeId id){ + return typeDescriptors[id]; } - -void printuni(Unitype v){ - char* s=sprintuni(v); - fputs(s, stdout); - free(s); -} \ No newline at end of file diff --git a/src/base/types.c.old b/src/base/types.c.old new file mode 100644 index 0000000..3c6ef1e --- /dev/null +++ b/src/base/types.c.old @@ -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); +} \ No newline at end of file diff --git a/src/base/types.h b/src/base/types.h index e34d95f..2722e17 100644 --- a/src/base/types.h +++ b/src/base/types.h @@ -6,52 +6,55 @@ extern "C" { #include "std.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; -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 +typedef uint16 kerepTypeId; +typedef struct kerepTypeDescriptor{ + void (*free_members)(void*); // NULL or function which frees all struct members + char* name; + kerepTypeId id; + uint16 size; +} kerepTypeDescriptor; -const char* my_type_name(my_type t); +#define kerepType_declare(NAME)\ + extern kerepTypeId kerepTypeId_##NAME -typedef struct Unitype{ - union { - int64 Int64; - uint64 UInt64; - double Float64; - bool Bool; - void* VoidPtr; - }; - my_type type; -} Unitype; +extern kerepTypeId kerepTypeId_last; +void __kerepType_register(char* name, int16 size, void (*free_members)(void*)); -static const Unitype UniNull={.VoidPtr=NULL,.type=Null}; -static const Unitype UniTrue={.Bool=true,.type=Bool}; -static const Unitype UniFalse={.Bool=false,.type=Bool}; +#define kerepType_register(TYPE, NAME, FREE_MEMBERS_FUNC)\ + __kerepType_register(#NAME, sizeof(TYPE), FREE_MEMBERS_FUNC);\ + kerepTypeId_##NAME=kerepTypeId_last; -#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL} -#define UniPtr(TYPE,VAL) (Unitype){.type=TYPE,.VoidPtr=VAL} +void kerepTypeDescriptors_beginInit(); +void kerepTypeDescriptors_endInit(); +kerepTypeDescriptor typeDescriptor_get(kerepTypeId id); -// frees VoidPtr value or does nothing if type isn't pointer -void Unitype_free(Unitype u); -void printuni(Unitype v); -char* sprintuni(Unitype v); +kerepType_declare(Null); + +kerepType_declare(Char); +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 } diff --git a/src/base/unitype.c b/src/base/unitype.c new file mode 100644 index 0000000..a2feb13 --- /dev/null +++ b/src/base/unitype.c @@ -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); } \ No newline at end of file diff --git a/src/base/unitype.h b/src/base/unitype.h new file mode 100644 index 0000000..65c953f --- /dev/null +++ b/src/base/unitype.h @@ -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 \ No newline at end of file