From 7bf3ea3a879a76cd7d468833be5789822e65280a Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 9 Dec 2023 12:40:45 +0100 Subject: [PATCH] Update Function Object interface to make interface trivially copiable There is no need to perform a copy-protected type as there is no init/clear for this type. It is always created through an instance. --- README.md | 4 +++- m-funcobj.h | 4 ++-- tests/test-mfuncobj.c | 6 ++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a5a3f8c2..745ee38d 100644 --- a/README.md +++ b/README.md @@ -8008,7 +8008,8 @@ Example: { sumint_t s; sumint_init_with(s, 16); - int n = f(sumint_as_interface(s)); + interface_t interface = sumint_as_interface(s); + int n = f(interface); printf("sum=%d\n", n); sumint_clear(s); } @@ -8037,6 +8038,7 @@ It will define the following type and functions: Type representing an interface to such function object. There is only one method for such type (see below). +It is a pointer to an hidded structure and can be assigned. ##### retcode\_type name\_call(name\_t interface, type\_of\_param1, type\_of\_param 2, ...) diff --git a/m-funcobj.h b/m-funcobj.h index 26f2a48c..7b5401e8 100644 --- a/m-funcobj.h +++ b/m-funcobj.h @@ -156,7 +156,7 @@ \ typedef struct M_F(name, _s) { \ M_F(name, _callback_ct) callback; \ - } interface_t[1]; \ + } *interface_t; \ \ /* Internal type for oplist & instance */ \ typedef interface_t M_F(name, _ct); \ @@ -192,7 +192,7 @@ \ typedef struct M_F(name, _s) { \ M_F(name, _callback_ct) callback; \ - } interface_t[1]; \ + } *interface_t; \ \ /* Internal type for oplist & instance */ \ typedef interface_t M_F(name, _ct); \ diff --git a/tests/test-mfuncobj.c b/tests/test-mfuncobj.c index 4ae5bf25..03deb698 100644 --- a/tests/test-mfuncobj.c +++ b/tests/test-mfuncobj.c @@ -85,7 +85,8 @@ static void test_instance11(void) instance11_t cmp; int n = 2; instance11_init_with(cmp, -1, STRING_CTE("INIT") ); - int x = interface1_call(instance11_as_interface(cmp), 10, &n); + interface1_t interface = instance11_as_interface(cmp); + int x = interface1_call(interface, 10, &n); assert(x == -8); assert(string_equal_str_p(cmp->c, "CALLED")); instance11_clear(cmp); @@ -111,7 +112,8 @@ static void test_instance21(void) { instance21_t cmp; instance21_init(cmp); - int x = interface2_call(instance21_as_interface(cmp)); + interface2_t interface = instance21_as_interface(cmp); + int x = interface2_call(interface); assert(x == 1); instance21_clear(cmp); }