ソースを参照

[future] Adding errs

Vinicius Teshima 11 ヶ月 前
コミット
225ce5c800
2 ファイル変更40 行追加10 行削除
  1. 38 8
      src/future.h
  2. 2 2
      src/future_test.c

+ 38 - 8
src/future.h

@@ -10,20 +10,31 @@ struct future {
 	void *env;
 };
 
-void *future_poll(struct future *p, void *data);
+enum future_err_code {
+	FUTURE_ERR_OK,
+	FUTURE_ERR_MISSING_FUNC
+};
+
+struct future_err {
+	enum future_err_code code;
+	const char *name;
+};
+
+void *future_poll(struct future *p, void *data, struct future_err *err);
+
+void _future_err_set(struct future_err *err, enum future_err_code code);
 
 #if defined(IMP) || defined(POLLABLE_IMP)
 
 #include <stdlib.h>
 
 void *
-future_poll(struct future *p, void *data)
+future_poll(struct future *p, void *data, struct future_err *err)
 {
+	void *res = p->f(p->env, data);
 	if ( p->then != NULL ) {
-		void *res = p->f(p->env, data);
-
 		if ( res == NULL ) {
-			return NULL;
+			goto exit_ok;
 		}
 
 		{
@@ -34,15 +45,34 @@ future_poll(struct future *p, void *data)
 			p->then = new_ftr.then;
 		}
 
-		return NULL;
+		res = NULL;
+		goto exit_ok;
 	}
 
 	if ( p->f == NULL ) {
-		/* TODO: Raire ERR */
+		_future_err_set(err, FUTURE_ERR_MISSING_FUNC);
 		return NULL;
 	}
 
-	return p->f(p->env, data);
+exit_ok:
+	_future_err_set(err, FUTURE_ERR_OK);
+	return res;
+}
+
+void
+_future_err_set(struct future_err *err, enum future_err_code code)
+{
+	if ( err == NULL ) {
+		return ;
+	}
+
+	err->code = code;
+	switch ( code ) {
+#define _FUTURE_ERR_SET_CASE(c) case c: err->name = #c; break
+	_FUTURE_ERR_SET_CASE(FUTURE_ERR_OK);
+	_FUTURE_ERR_SET_CASE(FUTURE_ERR_MISSING_FUNC);
+#undef _FUTURE_ERR_SET_CASE
+	}
 }
 
 #endif /* defined(IMP) || defined(POLLABLE_IMP) */

+ 2 - 2
src/future_test.c

@@ -30,8 +30,8 @@ main(int argc, char *argv[])
 	while ( true ) {
 		void *r1 = NULL;
 		void *r2 = NULL;
-		r1 = future_poll(&counter_f1, NULL);
-		r2 = future_poll(&counter_f2, NULL);
+		r1 = future_poll(&counter_f1, NULL, NULL);
+		r2 = future_poll(&counter_f2, NULL, NULL);
 		if ( r1 != NULL && r2 != NULL ) {
 			break;
 		}