Skip to content

Commit e0df06f

Browse files
committed
fu update: rework error keywords: make them look like function call
1 parent e37dd39 commit e0df06f

File tree

6 files changed

+137
-117
lines changed

6 files changed

+137
-117
lines changed

src/fu_util/fm_util.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,27 @@
4545
#define fm__xor_11 0
4646

4747
#define fm_if(x, y, ...) fm_cat(fm__if_, x)(y, __VA_ARGS__)
48-
#define fm_iif(x) fm_cat(fm__if_, x)
4948
#define fm__if_1(y, ...) y
5049
#define fm__if_0(y, ...) __VA_ARGS__
5150
#define fm_when(x) fm_cat(fm__when_, x)
5251
#define fm__when_1(...) __VA_ARGS__
5352
#define fm__when_0(...)
53+
#define fm_iif(x) fm_cat(fm__iif_, x)
54+
#define fm__iif_1(...) __VA_ARGS__ fm_empty
55+
#define fm__iif_0(...) fm_expand
5456

5557
#define fm_va_comma(...) \
5658
fm_cat(fm__va_comma_, fm_va_01(__VA_ARGS__))()
5759
#define fm__va_comma_0()
5860
#define fm__va_comma_1() ,
5961

6062
#define fm_or_default(...) \
61-
fm_cat(fm__or_default_, fm_va_01(__VA_ARGS__))(__VA_ARGS__)
62-
#define fm__or_default_0(...) fm_expand
63-
#define fm__or_default_1(...) __VA_ARGS__ fm_empty
63+
fm_iif(fm_va_01(__VA_ARGS__))(__VA_ARGS__)
6464

6565
#define fm__primitive_compare(x, y) fm_is_tuple(COMPARE_##x(COMPARE_##y)(()))
6666
#define fm__is_comparable(x) fm_is_tuple(fm_cat(COMPARE_,x)(()))
6767
#define fm_not_equal(x, y) \
68-
fm_iif(fm_and(fm__is_comparable(x),fm__is_comparable(y))) \
69-
(fm__primitive_compare, 1 fm_empty)(x, y)
68+
fm_if(fm_and(fm__is_comparable(x),fm__is_comparable(y)), fm__primitive_compare, 1 fm_empty)(x, y)
7069
#define fm_equal(x, y) \
7170
fm_compl(fm_not_equal(x, y))
7271

@@ -133,10 +132,11 @@
133132

134133
// recursion handle
135134
#define fm_defer(id) id fm_empty()
136-
#define fm_recurs(id) id fm_defer(fm_empty)()
135+
#define fm_recurs(id) id fm_empty fm_empty() ()
136+
#define fm_recurs2(a,b) fm_cat fm_empty fm_empty() () (a,b)
137137

138138
#if __STRICT_ANSI__
139-
#define fm__is_emptyfirst(x, ...) fm_iif(fm_is_tuple(x))(0, fm__is_emptyfirst_impl(x))
139+
#define fm__is_emptyfirst(x, ...) fm_if(fm_is_tuple(x), 0, fm__is_emptyfirst_impl(x))
140140
#define fm__is_emptyfirst_impl(x,...) fm_tuple_2((\
141141
fm__is_emptyfirst_do1 x (fm__is_emptyfirst_do2), 1, 0))
142142
#define fm__is_emptyfirst_do1(F) F()
@@ -161,25 +161,24 @@
161161
#define fm_foreach(macro, ...) \
162162
fm_when(fm_va_01(__VA_ARGS__))( \
163163
fm_apply_1(macro, __VA_ARGS__) \
164-
fm_recurs(fm_cat) (fm_, foreach) (\
164+
fm_recurs2(fm_, foreach) (\
165165
macro, fm_tail(__VA_ARGS__) \
166166
) \
167167
)
168168

169169
#define fm_foreach_arg(macro, arg, ...) \
170170
fm_when(fm_va_01(__VA_ARGS__))( \
171171
fm_apply_2(macro, arg, __VA_ARGS__) \
172-
fm_recurs(fm_cat) (fm_, foreach_arg) (\
172+
fm_recurs2(fm_, foreach_arg) (\
173173
macro, arg, fm_tail(__VA_ARGS__) \
174174
) \
175175
)
176176

177-
#define fm_catx(x, y) fm_cat_impl(x, y)
178177
#define fm_foreach_comma(macro, ...) \
179178
fm_when(fm_va_01(__VA_ARGS__))( \
180179
fm_apply_1(macro, __VA_ARGS__\
181180
)fm_if(fm_va_single(__VA_ARGS__), , fm__comma)\
182-
fm_recurs(fm_catx) (fm_, foreach_comma) (\
181+
fm_recurs2(fm_, foreach_comma) (\
183182
macro, fm_tail(__VA_ARGS__) \
184183
) \
185184
)
@@ -188,15 +187,15 @@
188187
#define fm_foreach_tuple(macro, ...) \
189188
fm_when(fm_va_01(__VA_ARGS__))( \
190189
fm_apply_tuple_1(macro, __VA_ARGS__) \
191-
fm_recurs(fm_cat) (fm_, foreach_tuple) (\
190+
fm_recurs2(fm_, foreach_tuple) (\
192191
macro, fm_tail(__VA_ARGS__) \
193192
) \
194193
)
195194

196195
#define fm_foreach_tuple_arg(macro, arg, ...) \
197196
fm_when(fm_va_01(__VA_ARGS__))( \
198197
fm_apply_tuple_2(macro, arg, __VA_ARGS__) \
199-
fm_recurs(fm_cat) (fm_, foreach_tuple_arg) (\
198+
fm_recurs2(fm_, foreach_tuple_arg) (\
200199
macro, arg, fm_tail(__VA_ARGS__) \
201200
) \
202201
)
@@ -205,7 +204,7 @@
205204
fm_when(fm_va_01(__VA_ARGS__))( \
206205
fm_apply_tuple_1(macro, __VA_ARGS__\
207206
)fm_if(fm_va_single(__VA_ARGS__), fm_empty(), fm__comma)\
208-
fm_recurs(fm_cat) (fm_, foreach_tuple_comma) (\
207+
fm_recurs2(fm_, foreach_tuple_comma) (\
209208
macro, fm_tail(__VA_ARGS__) \
210209
) \
211210
)
@@ -214,6 +213,9 @@
214213
#define fm_eval_foreach(macro, ...) \
215214
fm_eval(fm_foreach(macro, __VA_ARGS__))
216215

216+
#define fm_eval_foreach_comma(macro, ...) \
217+
fm_eval(fm_foreach_comma(macro, __VA_ARGS__))
218+
217219
#define fm_eval_foreach_arg(macro, arg, ...) \
218220
fm_eval(fm_foreach_arg(macro, arg, __VA_ARGS__))
219221

src/fu_util/fo_obj.h

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,10 @@ extern fobj_t fobj_swap(fobj_t* var, fobj_t newval);
194194

195195
/*
196196
* fobjDispose should finish all object's activity and release resources.
197-
* It is called automatically before destroying object, but could be
198-
* called manually as well using `fobj_dispose` function. `fobjDispose` could
199-
* not be called directly.
200-
* Therefore after fobjDispose object should be accessible, ie call of any
201-
* method should not be undefined. But it should not be usable, ie should
202-
* not do any meaningful job.
197+
* It is called automatically before destroying object.
203198
*/
204199
#define mth__fobjDispose void
205200
fobj__special_void_method(fobjDispose);
206-
#define $dispose(obj) fobj_dispose(obj)
207-
extern void fobj_dispose(fobj_t);
208-
209-
/* check if object is disposing or was disposed */
210-
extern bool fobj_disposing(fobj_t);
211-
extern bool fobj_disposed(fobj_t);
212201

213202
/*
214203
* returns globally allocated klass name.
@@ -474,7 +463,7 @@ typedef struct fobjBool {
474463
bool b;
475464
} fobjBool;
476465

477-
ft_inline fobjBool* fobj_bool(bool f);
466+
extern fobjBool* fobj_bool(bool f);
478467
#define $B(f) fobj_bool(f)
479468

480469
#define kls__fobjBool mth(fobjRepr, fobjFormat)
@@ -507,7 +496,10 @@ extern fobjStr* fobj_printkv(const char *fmt, ft_slc_fokv_t kv);
507496
* ERRORS
508497
*/
509498

510-
#define iface__err
499+
#define mth___fobjErr_marker_DONT_IMPLEMENT_ME void
500+
fobj__special_void_method(_fobjErr_marker_DONT_IMPLEMENT_ME);
501+
502+
#define iface__err mth(_fobjErr_marker_DONT_IMPLEMENT_ME)
511503
fobj_iface(err);
512504

513505
#define fobj_error_kind(err) fobj__error_kind(err)
@@ -526,8 +518,8 @@ fobj_error_kind(SysErr);
526518

527519
fobj_error_object_key(cause);
528520
fobj_error_int_key(errNo);
529-
fobj_error_cstr_key(errStr);
530-
#define fobj_errno_keys(errno) (errNo, errno), (errStr, ft_strerror(errno))
521+
fobj_error_cstr_key(errNoStr);
522+
#define fobj_errno_keys(errno) (errNo, errno), (errNoStr, ft_strerror(errno))
531523
fobj_error_cstr_key(path);
532524
fobj_error_cstr_key(old_path);
533525
fobj_error_cstr_key(new_path);
@@ -541,19 +533,32 @@ fobj_error_cstr_key(__msgSuffix);
541533
* $err(Type, "Some bad thing happens at {path}", (path, filename))
542534
*/
543535
#define $err(type, ...) fobj_make_err(type, __VA_ARGS__)
536+
/*
537+
* $noerr() - empty error
538+
* $noerr(err) - true, if $isNULL(err)
539+
*/
544540
#define $noerr(...) fm_if(fm_va_01(__VA_ARGS__), $isNULL(__VA_ARGS__), $null(err))
541+
/*
542+
* $haserr(err) - true if $notNULL(err)
543+
*/
545544
#define $haserr(err) $notNULL(err)
546545

547546
/*
548-
* $syserr()
549-
* $syserr("allocation error")
550-
* $syserr("Could not open file {path}", (path, filename))
547+
* $syserr(errno)
548+
* $syserr(errno, "allocation error")
549+
* $syserr(errno, "Could not open file {path}", (path, filename))
551550
*/
552-
#define $syserr(...) fobj_make_syserr(__VA_ARGS__)
551+
#define $syserr(erno, ...) fobj_make_syserr((erno), __VA_ARGS__)
553552

554553
/* fetch key back */
555554
#define $errkey(key, err, ...) fobj__err_getkey(key, err, __VA_ARGS__)
555+
/*
556+
* Get errno stored in `errNo` error key
557+
*/
556558
ft_inline int getErrno(err_i err);
559+
/*
560+
* Get errno string stored in `errNoStr` error key
561+
*/
557562
ft_inline const char* getErrnoStr(err_i err);
558563

559564
/*

src/fu_util/impl/fo_impl.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ fobj__do_dispose(fobj_t self, fobj_header_t *h, fobj_klass_registration_t *kreg)
511511
}
512512
}
513513

514-
void
515-
fobj_dispose(fobj_t self) {
514+
static void
515+
fobj_release(fobj_t self) {
516516
fobj_header_t *h;
517517
fobj_klass_handle_t klass;
518518
fobj_klass_registration_t *kreg;
@@ -528,11 +528,17 @@ fobj_dispose(fobj_t self) {
528528
ft_dbg_assert(klass > 0 && klass <= atload(&fobj_klasses_n));
529529
kreg = &fobj_klasses[klass];
530530

531+
532+
if (__atomic_sub_fetch(&h->rc, 1, __ATOMIC_ACQ_REL) != 0)
533+
return;
534+
if ((atload(&h->flags) & FOBJ_DISPOSING) != 0)
535+
return;
531536
fobj__do_dispose(self, h, kreg);
532537
}
533538

534-
static void
535-
fobj_release(fobj_t self) {
539+
#if 0
540+
void
541+
fobj_dispose(fobj_t self) {
536542
fobj_header_t *h;
537543
fobj_klass_handle_t klass;
538544
fobj_klass_registration_t *kreg;
@@ -548,11 +554,6 @@ fobj_release(fobj_t self) {
548554
ft_dbg_assert(klass > 0 && klass <= atload(&fobj_klasses_n));
549555
kreg = &fobj_klasses[klass];
550556

551-
552-
if (__atomic_sub_fetch(&h->rc, 1, __ATOMIC_ACQ_REL) != 0)
553-
return;
554-
if ((atload(&h->flags) & FOBJ_DISPOSING) != 0)
555-
return;
556557
fobj__do_dispose(self, h, kreg);
557558
}
558559

@@ -580,6 +581,8 @@ fobj_disposed(fobj_t self) {
580581
return (atload(&h->flags) & FOBJ_DISPOSED) != 0;
581582
}
582583

584+
#endif
585+
583586
static fobj_klass_handle_t
584587
fobjBase_fobjKlass(fobj_t self) {
585588
return fobj_real_klass_of(self);
@@ -844,7 +847,7 @@ fobjUInt_fobjFormat(VSelf, ft_strbuf_t *buf, const char *fmt) {
844847
static fobjStr*
845848
fobjFloat_fobjRepr(VSelf) {
846849
Self(fobjFloat);
847-
return fobj_sprintf("%f", self->f);
850+
return fobj_sprintf("$F(%f)", self->f);
848851
}
849852

850853
static void
@@ -865,10 +868,15 @@ fobjFloat_fobjFormat(VSelf, ft_strbuf_t *buf, const char *fmt) {
865868
fobj_format_float(buf, self->f, fmt);
866869
}
867870

868-
static fobjStr* trueRepr = NULL;
869-
static fobjStr* falseRepr = NULL;
870-
static fobjStr* trueStr = NULL;
871-
static fobjStr* falseStr = NULL;
871+
static fobjBool* fobjTrue = NULL;
872+
static fobjBool* fobjFalse = NULL;
873+
static fobjStr* trueRepr = NULL;
874+
static fobjStr* falseRepr = NULL;
875+
876+
fobjBool*
877+
fobj_bool(bool b) {
878+
return b ? fobjTrue : fobjFalse;
879+
}
872880

873881
static fobjStr*
874882
fobjBool_fobjRepr(VSelf) {
@@ -1080,6 +1088,10 @@ fobj__make_err(const char *type,
10801088
return bind_err(err);
10811089
}
10821090

1091+
static void
1092+
fobjErr__fobjErr_marker_DONT_IMPLEMENT_ME(VSelf) {
1093+
}
1094+
10831095
static void
10841096
fobjErr_fobjDispose(VSelf) {
10851097
Self(fobjErr);
@@ -1263,7 +1275,7 @@ fobjBase__kh(void) {
12631275
return khandle;
12641276
}
12651277

1266-
fobj_klass_handle(fobjErr, mth(fobjRepr), varsized(kv));
1278+
fobj_klass_handle(fobjErr, mth(fobjRepr, _fobjErr_marker_DONT_IMPLEMENT_ME), varsized(kv));
12671279
fobj_klass_handle(fobjStr, mth(fobjDispose), varsized(_buf));
12681280
fobj_klass_handle(fobjInt);
12691281
fobj_klass_handle(fobjUInt);
@@ -1298,8 +1310,10 @@ fobj_init(void) {
12981310

12991311
FOBJ_FUNC_ARP();
13001312

1301-
falseStr = $ref($S("false"));
1302-
trueStr = $ref($S("true"));
1313+
fobjTrue = $alloc(fobjBool, .b = true);
1314+
fobjFalse = $alloc(fobjBool, .b = false);
1315+
falseRepr = $ref($S("$B(false)"));
1316+
trueRepr = $ref($S("$B(true)"));
13031317
}
13041318

13051319
void

src/fu_util/impl/fo_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ typedef struct {
254254

255255
#define fobj__special_void_method(meth) \
256256
\
257-
static ft_unused fobj_method_handle_t fobj__nm_mhandle(meth) (void) { \
257+
ft_inline ft_gcc_const fobj_method_handle_t fobj__nm_mhandle(meth) (void) { \
258258
static volatile fobj_method_handle_t hndl = 0; \
259259
fobj_method_handle_t h = hndl; \
260260
if (h) return h; \

src/fu_util/impl/fo_impl2.h

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ fobj_float(double f) {
8484
return $alloc(fobjFloat, .f = f);
8585
}
8686

87-
ft_inline fobjBool*
88-
fobj_bool(bool b) {
89-
return $alloc(fobjBool, .b = b);
90-
}
91-
9287
typedef struct fobjErr fobjErr;
9388
struct fobjErr {
9489
const char* type;
@@ -117,22 +112,24 @@ struct fobjErr {
117112
kvs, ft_arrsz(kvs)); \
118113
})
119114

120-
#define fobj_make_syserr( ...) \
121-
fm_cat(fobj_make_syserr_, fm_va_01(__VA_ARGS__))(__VA_ARGS__)
122-
#define fobj_make_syserr_0(...) ({ \
115+
#define fobj_make_syserr(erno_, ...) \
116+
fm_cat(fobj_make_syserr_, fm_va_01(__VA_ARGS__))((erno_), fm_uniq(erno), __VA_ARGS__)
117+
#define fobj_make_syserr_0(erno_, erno, ...) ({ \
118+
int erno = erno_; \
123119
fobj_err_kv_t kvs[] = { \
124-
{"errNo", ft_mka_i(errno)}, \
125-
{"errStr", ft_mka_s((char*)ft_strerror(errno))}, \
120+
{"errNo", ft_mka_i(erno)}, \
121+
{"errNoStr", ft_mka_s((char*)ft_strerror(erno))}, \
126122
}; \
127123
fobj__make_err(fobj_error_kind_SysErr(), \
128-
ft__srcpos(), "System Error: {errStr}", \
124+
ft__srcpos(), "System Error: {errNoStr}", \
129125
kvs, ft_arrsz(kvs));\
130126
})
131-
#define fobj_make_syserr_1(msg, ...) ({ \
127+
#define fobj_make_syserr_1(erno_, erno, msg, ...) ({ \
128+
int erno = erno_; \
132129
fobj_err_kv_t kvs[] = { \
133-
{"errNo", ft_mka_i(errno)}, \
134-
{"errStr", ft_mka_s((char*)ft_strerror(errno))}, \
135-
{"__msgSuffix", ft_mka_s((char*)": {errStr}")}, \
130+
{"errNo", ft_mka_i(erno)}, \
131+
{"errNoStr", ft_mka_s((char*)ft_strerror(erno))}, \
132+
{"__msgSuffix", ft_mka_s((char*)": {errNoStr}")}, \
136133
fobj__err_transform_kv(__VA_ARGS__) \
137134
}; \
138135
fobj__make_err(fobj_error_kind_SysErr(), \
@@ -146,10 +143,10 @@ extern err_i fobj__make_err(const char *type,
146143
fobj_err_kv_t *kvs,
147144
size_t kvn);
148145

149-
#define fobj__err_transform_kv_do(key, ...) \
150-
fobj__err_mkkv_##key(__VA_ARGS__)
146+
#define fobj__err_transform_kv_do(v) \
147+
fobj__err_mkkv_##v
151148
#define fobj__err_transform_kv(...) \
152-
fm_eval_tuples_comma(fobj__err_transform_kv_do, __VA_ARGS__)
149+
fm_eval_foreach_comma(fobj__err_transform_kv_do, __VA_ARGS__)
153150

154151
#define fobj__err_getkey(key, err, ...) \
155152
fobj__err_getkv_##key(err, fm_or_default(__VA_ARGS__)(NULL))
@@ -161,7 +158,7 @@ getErrno(err_i err) {
161158

162159
ft_inline const char*
163160
getErrnoStr(err_i err) {
164-
return $errkey(errStr, err);
161+
return $errkey(errNoStr, err);
165162
}
166163

167164
ft_inline const char*

0 commit comments

Comments
 (0)