safethrow() and Maybe struct

This commit is contained in:
Timerix22 2022-04-05 23:57:08 +03:00
parent 3940eeb2a7
commit a9b1545024
15 changed files with 174 additions and 59 deletions

View File

@ -171,7 +171,6 @@ Unitype __ParseValue(DeserializeSharedData* shared, string str){
sprintf(err,"can't parse to int: <%s>",_c); sprintf(err,"can't parse to int: <%s>",_c);
throw(err); throw(err);
} }
free(_c); free(_c);
return Uni(Int64,li); return Uni(Int64,li);
} }

View File

@ -3,7 +3,7 @@ TESTS=$(wildcard tests/*c) $(wildcard tests/**/*.c)
OUTDIR=bin OUTDIR=bin
CMP=gcc CMP=gcc
all: clear_c clear_bin build_test build_lib all: clear_c clear_bin build_test
clear_c: clear_c:
clear clear

View File

@ -10,23 +10,26 @@ extern "C" {
#include "mystr.h" #include "mystr.h"
// executes codeblock and prints execution time // executes codeblock and prints execution time
/*#define optime(opname,repeats,codeblock) ({\ #ifdef CLOCK_REALTIME //non-standard high-precision clock
struct timespec start, stop;\ #define optime(opname,repeats,codeblock) ({\
clock_gettime(CLOCK_REALTIME, &start);\ struct timespec start, stop;\
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\ clock_gettime(CLOCK_REALTIME, &start);\
(codeblock);\ for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\
clock_gettime(CLOCK_REALTIME, &stop);\ (codeblock);\
double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\ clock_gettime(CLOCK_REALTIME, &stop);\
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\
})*/ printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
#define optime(opname,repeats,codeblock) ({\ })
clock_t start=clock();\ #else //
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\ #define optime(opname,repeats,codeblock) ({\
(codeblock);\ clock_t start=clock();\
clock_t stop=clock();\ for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\
double t=(double)(stop-start)/CLOCKS_PER_SEC/repeats;\ (codeblock);\
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ clock_t stop=clock();\
}) double t=(double)(stop-start)/CLOCKS_PER_SEC/repeats;\
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
})
#endif
#if __cplusplus #if __cplusplus
} }

View File

@ -1,7 +1,8 @@
#include "std.h" #include "std.h"
#include "errors.h" #include "errors.h"
#include "mystr.h"
const char* errname(err_t err){ char* errname(err_t err){
switch(err){ switch(err){
case SUCCESS: return "SUCCESS"; case SUCCESS: return "SUCCESS";
case ERR_MAXLENGTH: return "ERR_MAXLENGTH"; case ERR_MAXLENGTH: return "ERR_MAXLENGTH";
@ -14,14 +15,29 @@ const char* errname(err_t err){
} }
} }
void _throwint(int err, const char* srcfile, int line, const char* funcname){ #define ERRMSG_MAXLENGTH 1024
if(err){ // SUCCESS=0 is not an error
printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errname(err)); char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
exit(err); char* rezult=malloc(ERRMSG_MAXLENGTH);
} sprintf(rezult,"[%s:%d] %s() throwed error: %s",srcfile,line,funcname,errmsg);
else printf("\e[93m[%s:%d %s] throwed SUCCESS as an error",srcfile,line,funcname); return rezult;
} }
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname){
printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errmesg); char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
exit(255); char* rezult=malloc(cptr_length(errmsg)+ERRMSG_MAXLENGTH);
sprintf(rezult,"%s\n \\___[%s:%d] %s()",errmsg,srcfile,line,funcname);
free(errmsg);
return rezult;
} }
void Maybe_free(Maybe e){
free(e.errmsg);
Unitype_free(e.value);
}
void printMaybe(Maybe e){
if(e.errmsg) printf("%s\n",e.errmsg);
else printuni(e.value);
}
char* __doNothing(char* a) {return a;}
char* __unknownErr() {return "UNKNOWN ERROR";}

View File

@ -4,20 +4,51 @@
extern "C" { extern "C" {
#endif #endif
#include "std.h"
#include "types.h"
typedef enum err_t { typedef enum err_t {
SUCCESS, //not an error SUCCESS, //not an error
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR
} err_t; } err_t;
const char* errname(err_t err); char* errname(err_t err);
void _throwint(int err, const char* srcfile, int line, const char* funcname) ; char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname); char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
#pragma GCC diagnostic ignored "-Wint-conversion"
#define throw(E) \ typedef struct Maybe{
CHOOSE(IFTYPE(E,int), _throwint(E,__FILE__,__LINE__,__func__), \ Unitype value;
CHOOSE(IFTYPE(E,char[]), _throwstr(E,__FILE__,__LINE__,__func__), \ char* errmsg;
printf("\e[31m[%s:%d/%s] UNKNOWN ERROR\n",__FILE__,__LINE__,__func__))) } Maybe;
static const Maybe MaybeNull={.value.type=Null, .value.VoidPtr=NULL,.errmsg=NULL};
void Maybe_free(Maybe e);
void printMaybe(Maybe e);
#define throwNext(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
return _rezult;\
}else
#define SUCCESS(REZLT) (Maybe){.errmsg=NULL, .value=REZLT}
#define __RETURN_EXCEPTION(ERRMSG) return (Maybe){.errmsg=ERRMSG, .value=UniNull}
#define __EXIT(ERRMSG) ({ printf("\e[91m%s\e[0m \n", ERRMSG); exit(128); })
char* __doNothing(char* a);
char* __unknownErr( );
#define __stringify_err(E) _Generic(\
(E),\
char*: __doNothing,\
int: errname,\
default: __unknownErr\
)(E)
#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
#define safethrow(E) __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
#if __cplusplus #if __cplusplus
} }

View File

@ -37,8 +37,8 @@ char* char_multiply(char c, uint32 n){
//copies str content to new char pointer value (adding '\0' at the end) //copies str content to new char pointer value (adding '\0' at the end)
char* string_cpToCptr(string str){ char* string_cpToCptr(string str){
char* cptr=malloc(str.length*sizeof(char)+1);
if(str.length==0) return NULL; if(str.length==0) return NULL;
char* cptr=malloc(str.length*sizeof(char)+1);
cptr[str.length]=0; cptr[str.length]=0;
while(str.length-->0) while(str.length-->0)
cptr[str.length]=str.ptr[str.length]; cptr[str.length]=str.ptr[str.length];
@ -69,6 +69,7 @@ bool string_compare(string str0, string str1){
//creates new string which is reversed variant of <s> //creates new string which is reversed variant of <s>
string string_reverse(string s){ string string_reverse(string s){
if(s.length==0) return s;
string r={malloc(s.length), s.length}; string r={malloc(s.length), s.length};
for(uint32 i=0; i<s.length; i++) for(uint32 i=0; i<s.length; i++)
r.ptr[i]=s.ptr[s.length-i-1]; r.ptr[i]=s.ptr[s.length-i-1];

View File

@ -10,6 +10,7 @@ extern "C" {
#include <stdbool.h> #include <stdbool.h>
#include <locale.h> #include <locale.h>
#include <time.h> #include <time.h>
#include <setjmp.h>
#define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N) #define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N)

View File

@ -115,16 +115,22 @@ void Unitype_free(Unitype u){
} }
} }
void sprintuni(char* buf,Unitype v){
void printuni(Unitype v){
switch (v.type) { switch (v.type) {
case Null: printf("{Null}");break; case Null: sprintf(buf, "{Null}");break;
case Float64: printf("{%s : %lf}",my_type_name(v.type),v.Float64);break; case Float64: sprintf(buf, "{%s : %lf}",my_type_name(v.type),v.Float64);break;
case Char: printf("{%s : '%c'}",my_type_name(v.type),v.Char);break; case Char: sprintf(buf, "{%s : '%c'}",my_type_name(v.type),v.Char);break;
case Bool: case Bool:
case UInt64: printf("{%s : %lu}",my_type_name(v.type),v.UInt64);break; case UInt64: sprintf(buf, "{%s : %lu}",my_type_name(v.type),v.UInt64);break;
case Int64: printf("{%s : %ld}",my_type_name(v.type),v.Int64);break; case Int64: sprintf(buf, "{%s : %ld}",my_type_name(v.type),v.Int64);break;
case CharPtr: printf("{%s : \"%s\"}",my_type_name(v.type),(char*)v.VoidPtr);break; case CharPtr: sprintf(buf, "{%s : \"%s\"}",my_type_name(v.type),(char*)v.VoidPtr);break;
default: printf("{%s : %p}",my_type_name(v.type),v.VoidPtr);break; default: sprintf(buf, "{%s : %p}",my_type_name(v.type),v.VoidPtr);break;
} }
} }
void printuni(Unitype v){
char* s=malloc(64);
sprintuni(s,v);
fputs(s, stdout);
free(s);
}

View File

@ -51,6 +51,7 @@ static const Unitype UniFalse={.Bool=false,.type=Bool};
//frees VoidPtr value or does nothing if type isn't pointer //frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u); void Unitype_free(Unitype u);
void printuni(Unitype v); void printuni(Unitype v);
void sprintuni(char* s, Unitype v);
#if __cplusplus #if __cplusplus
} }

View File

@ -75,27 +75,27 @@
</ImportGroup> </ImportGroup>
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<OutDir>bin-$(Platform)\$(Configuration)\</OutDir> <OutDir>bin\$(Configuration)-$(Platform)\</OutDir>
<IntDir>obj-$(Platform)\$(Configuration)\</IntDir> <IntDir>obj\$(Configuration)-$(Platform)\</IntDir>
<RunCodeAnalysis>true</RunCodeAnalysis> <RunCodeAnalysis>true</RunCodeAnalysis>
<ManagedAssembly>false</ManagedAssembly> <ManagedAssembly>false</ManagedAssembly>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>bin-$(Platform)\$(Configuration)\</OutDir> <OutDir>bin\$(Configuration)-$(Platform)\</OutDir>
<IntDir>obj-$(Platform)\$(Configuration)\</IntDir> <IntDir>obj\$(Configuration)-$(Platform)\</IntDir>
<RunCodeAnalysis>true</RunCodeAnalysis> <RunCodeAnalysis>true</RunCodeAnalysis>
<ManagedAssembly>false</ManagedAssembly> <ManagedAssembly>false</ManagedAssembly>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<RunCodeAnalysis>true</RunCodeAnalysis> <RunCodeAnalysis>true</RunCodeAnalysis>
<OutDir>bin-$(Platform)\$(Configuration)\</OutDir> <OutDir>bin\$(Configuration)-$(Platform)\</OutDir>
<IntDir>obj-$(Platform)\$(Configuration)\</IntDir> <IntDir>obj\$(Configuration)-$(Platform)\</IntDir>
<ManagedAssembly>false</ManagedAssembly> <ManagedAssembly>false</ManagedAssembly>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<RunCodeAnalysis>true</RunCodeAnalysis> <RunCodeAnalysis>true</RunCodeAnalysis>
<OutDir>bin-$(Platform)\$(Configuration)\</OutDir> <OutDir>bin\$(Configuration)-$(Platform)\</OutDir>
<IntDir>obj-$(Platform)\$(Configuration)\</IntDir> <IntDir>obj\$(Configuration)-$(Platform)\</IntDir>
<ManagedAssembly>false</ManagedAssembly> <ManagedAssembly>false</ManagedAssembly>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@ -220,6 +220,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Autoarr\Autoarr.c" /> <ClCompile Include="Autoarr\Autoarr.c" />
<ClCompile Include="Autoarr\Autoarr_KVPair_exported.c" />
<ClCompile Include="Autoarr\Autoarr_Unitype_exported.c" />
<ClCompile Include="Autoarr\StringBuilder.c" /> <ClCompile Include="Autoarr\StringBuilder.c" />
<ClCompile Include="base\errors.c" /> <ClCompile Include="base\errors.c" />
<ClCompile Include="base\mystr.c" /> <ClCompile Include="base\mystr.c" />
@ -237,6 +239,7 @@
<ClCompile Include="tests\test_dtsod.c" /> <ClCompile Include="tests\test_dtsod.c" />
<ClCompile Include="tests\test_hashtable.c" /> <ClCompile Include="tests\test_hashtable.c" />
<ClCompile Include="tests\test_marshalling.c" /> <ClCompile Include="tests\test_marshalling.c" />
<ClCompile Include="tests\test_safethrow.c" />
<ClCompile Include="tests\test_searchtree.c" /> <ClCompile Include="tests\test_searchtree.c" />
<ClCompile Include="tests\test_string.c" /> <ClCompile Include="tests\test_string.c" />
</ItemGroup> </ItemGroup>

View File

@ -125,5 +125,14 @@
<ClCompile Include="DtsodParser\DtsodV24_exported.c"> <ClCompile Include="DtsodParser\DtsodV24_exported.c">
<Filter>Исходные файлы</Filter> <Filter>Исходные файлы</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Autoarr\Autoarr_KVPair_exported.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="Autoarr\Autoarr_Unitype_exported.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="tests\test_safethrow.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -6,6 +6,7 @@ void test_all(){
test_hashtable(); test_hashtable();
test_string(); test_string();
test_dtsod(); test_dtsod();
test_safethrow();
printf("\e[96m---------------------------------------\e[0m\n"); printf("\e[96m---------------------------------------\e[0m\n");
} }

43
tests/test_safethrow.c Normal file
View File

@ -0,0 +1,43 @@
#include "tests.h"
Maybe dont_throw(){
return SUCCESS(Uni(UInt64, 9/2));
}
Maybe throw_error(){
safethrow("test exception");
}
Maybe throw_errcode(){
safethrow(ERR_NULLPTR);
}
Maybe test_maybe(){
printf("\e[94mdont_throw returns \e[92m");
throwNext(dont_throw(),rez0)
printMaybe(rez0);
printf("\n");
throwNext(throw_error(),rez1)
printMaybe(rez1);
throw("test_maybe failed");
return MaybeNull;
}
Maybe c(){ throwNext(throw_errcode(),_) return MaybeNull; }
Maybe b(){ throwNext(c(),_) return MaybeNull; }
Maybe a(){ throwNext(b(),_) return MaybeNull; }
void test_safethrow(){
printf("\e[96m-----------[test_safethrow]-----------\n");
optime("test_safethrow", 1, ({
Maybe e=test_maybe();
printf("\e[94mthrow_error:\n\e[92m");
printMaybe(e);
Maybe_free(e);
printf("\e[94mthrow_errcode:\n\e[92m");
e=a();
printMaybe(e);
Maybe_free(e);
}));
}

View File

@ -6,10 +6,10 @@ void test_string(){
printf("\e[96m-------------[test_string]-------------\n"); printf("\e[96m-------------[test_string]-------------\n");
char c[]="0123456789abcdef"; char c[]="0123456789abcdef";
string s=string_cpFromCharPtr(c); string s=string_cpFromCharPtr(c);
printf("\e[92m\"%s\" -> string_cpFromCharPtr()\n",c); printf("\e[92m\"%s\" \e[94m-> string_cpFromCharPtr()\n",c);
if(s.length!=16) throw("string created with incorrect length"); if(s.length!=16) throw("string created with incorrect length");
char* p=string_cpToCptr(s); char* p=string_cpToCptr(s);
printf("\e[92mstring_cpToCptr() -> \"%s\"\n",p); printf("\e[94mstring_cpToCptr() -> \e[92m\"%s\"\n",p);
free(p); free(p);
free(s.ptr); free(s.ptr);
})); }));

View File

@ -9,3 +9,4 @@ void test_autoarr();
void test_hashtable(); void test_hashtable();
void test_string(); void test_string();
void test_dtsod(); void test_dtsod();
void test_safethrow();