new TypeId system
This commit is contained in:
parent
11c5b57856
commit
2e378d1458
@ -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);
|
||||
|
||||
|
||||
@ -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 : \
|
||||
|
||||
@ -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<length; i++)\
|
||||
array[i]=__Autoarr_get_##type(ar, i);\
|
||||
return array;\
|
||||
}\
|
||||
\
|
||||
__functions_list_t_##type __functions_list_##type={\
|
||||
&__Autoarr_add_##type,\
|
||||
&__Autoarr_get_##type,\
|
||||
&__Autoarr_getptr_##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){\
|
||||
|
||||
@ -11,6 +11,8 @@ typedef struct Hashtable{
|
||||
uint8 hein; // height=HT_HEIGHTS[hein]
|
||||
Autoarr(KVPair)** rows; // Autoarr[height]
|
||||
} Hashtable;
|
||||
kerepType_declare(Hashtable);
|
||||
kerepType_declare(HashtablePtr);
|
||||
|
||||
Hashtable* Hashtable_create();
|
||||
void Hashtable_free(Hashtable* ht);
|
||||
|
||||
@ -11,6 +11,8 @@ typedef struct SearchTreeNode{
|
||||
struct SearchTreeNode**** branches; // *STNode[8][8][4]
|
||||
Unitype value;
|
||||
} STNode;
|
||||
kerepType_declare(STNode);
|
||||
kerepType_declare(STNodePtr);
|
||||
|
||||
STNode* STNode_create();
|
||||
void STNode_free(STNode* node);
|
||||
|
||||
@ -5,10 +5,12 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#include "std.h"
|
||||
#include "types.h"
|
||||
#include "errors.h"
|
||||
#include "cptr.h"
|
||||
#include "optime.h"
|
||||
#include "types.h"
|
||||
#include "unitype.h"
|
||||
#include "init.h"
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
66
src/base/init.c
Normal file
66
src/base/init.c
Normal 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
12
src/base/init.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// call this between kerepTypesInitBegin() and kerepTypesInitEnd()
|
||||
void kerepInit();
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -12,10 +12,19 @@ extern "C" {
|
||||
#include <time.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)
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
178
src/base/types.c
178
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;
|
||||
}
|
||||
|
||||
void printuni(Unitype v){
|
||||
char* s=sprintuni(v);
|
||||
fputs(s, stdout);
|
||||
free(s);
|
||||
kerepTypeDescriptor typeDescriptor_get(kerepTypeId id){
|
||||
return typeDescriptors[id];
|
||||
}
|
||||
113
src/base/types.c.old
Normal file
113
src/base/types.c.old
Normal 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);
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
10
src/base/unitype.c
Normal file
10
src/base/unitype.c
Normal 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
50
src/base/unitype.h
Normal 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
|
||||
Loading…
Reference in New Issue
Block a user