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);
throw(err);
}
free(_c);
return Uni(Int64,li);
}

View File

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

View File

@ -10,7 +10,8 @@ extern "C" {
#include "mystr.h"
// executes codeblock and prints execution time
/*#define optime(opname,repeats,codeblock) ({\
#ifdef CLOCK_REALTIME //non-standard high-precision clock
#define optime(opname,repeats,codeblock) ({\
struct timespec start, stop;\
clock_gettime(CLOCK_REALTIME, &start);\
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\
@ -18,15 +19,17 @@ extern "C" {
clock_gettime(CLOCK_REALTIME, &stop);\
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) ({\
})
#else //
#define optime(opname,repeats,codeblock) ({\
clock_t start=clock();\
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\
(codeblock);\
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
}

View File

@ -1,7 +1,8 @@
#include "std.h"
#include "errors.h"
#include "mystr.h"
const char* errname(err_t err){
char* errname(err_t err){
switch(err){
case SUCCESS: return "SUCCESS";
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){
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));
exit(err);
}
else printf("\e[93m[%s:%d %s] throwed SUCCESS as an error",srcfile,line,funcname);
#define ERRMSG_MAXLENGTH 1024
char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
char* rezult=malloc(ERRMSG_MAXLENGTH);
sprintf(rezult,"[%s:%d] %s() throwed error: %s",srcfile,line,funcname,errmsg);
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);
exit(255);
char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
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" {
#endif
#include "std.h"
#include "types.h"
typedef enum err_t {
SUCCESS, //not an error
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR
} 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) ;
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname);
#pragma GCC diagnostic ignored "-Wint-conversion"
#define throw(E) \
CHOOSE(IFTYPE(E,int), _throwint(E,__FILE__,__LINE__,__func__), \
CHOOSE(IFTYPE(E,char[]), _throwstr(E,__FILE__,__LINE__,__func__), \
printf("\e[31m[%s:%d/%s] UNKNOWN ERROR\n",__FILE__,__LINE__,__func__)))
char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
typedef struct Maybe{
Unitype value;
char* errmsg;
} 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
}

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)
char* string_cpToCptr(string str){
char* cptr=malloc(str.length*sizeof(char)+1);
if(str.length==0) return NULL;
char* cptr=malloc(str.length*sizeof(char)+1);
cptr[str.length]=0;
while(str.length-->0)
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>
string string_reverse(string s){
if(s.length==0) return s;
string r={malloc(s.length), s.length};
for(uint32 i=0; i<s.length; i++)
r.ptr[i]=s.ptr[s.length-i-1];

View File

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

View File

@ -115,16 +115,22 @@ void Unitype_free(Unitype u){
}
}
void printuni(Unitype v){
void sprintuni(char* buf,Unitype v){
switch (v.type) {
case Null: printf("{Null}");break;
case Float64: printf("{%s : %lf}",my_type_name(v.type),v.Float64);break;
case Char: printf("{%s : '%c'}",my_type_name(v.type),v.Char);break;
case Null: sprintf(buf, "{Null}");break;
case Float64: sprintf(buf, "{%s : %lf}",my_type_name(v.type),v.Float64);break;
case Char: sprintf(buf, "{%s : '%c'}",my_type_name(v.type),v.Char);break;
case Bool:
case UInt64: printf("{%s : %lu}",my_type_name(v.type),v.UInt64);break;
case Int64: printf("{%s : %ld}",my_type_name(v.type),v.Int64);break;
case CharPtr: printf("{%s : \"%s\"}",my_type_name(v.type),(char*)v.VoidPtr);break;
default: printf("{%s : %p}",my_type_name(v.type),v.VoidPtr);break;
case UInt64: sprintf(buf, "{%s : %lu}",my_type_name(v.type),v.UInt64);break;
case Int64: sprintf(buf, "{%s : %ld}",my_type_name(v.type),v.Int64);break;
case CharPtr: 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;
}
}
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
void Unitype_free(Unitype u);
void printuni(Unitype v);
void sprintuni(char* s, Unitype v);
#if __cplusplus
}

View File

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

View File

@ -125,5 +125,14 @@
<ClCompile Include="DtsodParser\DtsodV24_exported.c">
<Filter>Исходные файлы</Filter>
</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>
</Project>

View File

@ -6,6 +6,7 @@ void test_all(){
test_hashtable();
test_string();
test_dtsod();
test_safethrow();
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");
char c[]="0123456789abcdef";
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");
char* p=string_cpToCptr(s);
printf("\e[92mstring_cpToCptr() -> \"%s\"\n",p);
printf("\e[94mstring_cpToCptr() -> \e[92m\"%s\"\n",p);
free(p);
free(s.ptr);
}));

View File

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