summaryrefslogtreecommitdiff
path: root/src/util/RegisterKeyedFunction.c
diff options
context:
space:
mode:
authorgoodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac>1998-09-29 07:58:59 +0000
committergoodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac>1998-09-29 07:58:59 +0000
commit20ee8bd50c4254719e22612d2414556fe040ccb0 (patch)
treef8537a8d0d9dd8dcf01586b4c46b1b55116d4ef1 /src/util/RegisterKeyedFunction.c
parent2ef2d714406fb3b741e56b1609d0d76bfdcd1af0 (diff)
Source file for keyed function stuff.
git-svn-id: http://svn.cactuscode.org/flesh/trunk@9 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src/util/RegisterKeyedFunction.c')
-rw-r--r--src/util/RegisterKeyedFunction.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/util/RegisterKeyedFunction.c b/src/util/RegisterKeyedFunction.c
new file mode 100644
index 00000000..cda2a5a2
--- /dev/null
+++ b/src/util/RegisterKeyedFunction.c
@@ -0,0 +1,119 @@
+ /*@@
+ @file RegisterKeyedFunction.c
+ @date Tue Sep 29 09:39:55 1998
+ @author Tom Goodale
+ @desc
+ Routines to register keyed functions.
+ @enddesc
+ @@*/
+#include <stdio.h>
+#include <stdlib.h>
+#include "RegisterKeyedFunction.h"
+
+static char *rcsid = "$Id$";
+
+ /*@@
+ @routine RegisterKeyedFunction
+ @date Tue Sep 29 09:41:08 1998
+ @author Tom Goodale
+ @desc
+ Registers a function with a key between the minimum and maximum (inclusive).
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+
+@@*/
+int RegisterKeyedFunction(void (*array[])(),
+ int min, int max,
+ int key, void (*func)())
+{
+ int return_code;
+
+ if(key >= min && key <= max)
+ {
+ if(array[key] == NULL)
+ {
+ array[key] = func;
+ return_code = REG_KEYED_FUNCTION_SUCCESS;
+ }
+ else
+ {
+ return_code = REG_KEYED_FUNCTION_ALREADY_ASSIGNED;
+ };
+ }
+ else
+ {
+ return_code = REG_KEYED_FUNCTION_RANGE_ERROR;
+ };
+
+ return return_code;
+}
+
+
+#ifdef TEST_KEYED_FUNCTIONS
+
+static void (*functions[])() = {NULL, NULL, NULL};
+
+void RegisterTestFunction(int key, void (*func)())
+{
+ int retcode;
+ if((retcode = RegisterKeyedFunction(functions, 0, 2, key, func)) == 1)
+ {
+ fprintf(stderr, "Test function %d already registered.\n", key);
+ }
+ else if(retcode == 2)
+ {
+ fprintf(stderr, "Unknown test function %d\n", key);
+ }
+}
+
+#define CREATE_FUNC(x) void function ## _ ## x (void) { printf("I'm function %d\n", x); }
+
+CREATE_FUNC(0)
+CREATE_FUNC(1)
+CREATE_FUNC(2)
+CREATE_FUNC(3)
+CREATE_FUNC(4)
+
+#define FUNC(x) function ## _ ## x
+
+#define REGTEST(x) RegisterTestFunction(x, FUNC(x))
+
+int main(int argc, char *argv[])
+{
+ int i;
+ void (*test)();
+
+ REGTEST(0);
+ REGTEST(1);
+ REGTEST(2);
+ REGTEST(3);
+ REGTEST(4);
+
+ REGTEST(0);
+ REGTEST(1);
+ REGTEST(2);
+ REGTEST(3);
+ REGTEST(4);
+
+ for(i = 0; i < 3; i++)
+ {
+ test = functions[i];
+
+ if(test)
+ {
+ test();
+ }
+ else
+ {
+ printf("Error test function %d is null !!!\n", i);
+ };
+
+ };
+
+}
+
+#endif