summaryrefslogtreecommitdiff
path: root/src/util/RegisterKeyedFunction.c
blob: d2f088befd8411b0dfb9f904d1d258e547dccd46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
 /*@@
   @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 "cctk_Flesh.h"
#include "RegisterKeyedFunction.h"

static const char *rcsid = "$Header$";

CCTK_FILEVERSION(util_RegisterKeyedFunction_c);

 /*@@
   @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[])(void), 
                          int min, int max, 
                          int key, void (*func)(void))
{
  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;
}

 /*@@
   @routine    CreateKeyedFunctionArray
   @date       Tue Sep 29 11:16:51 1998
   @author     Tom Goodale
   @desc 

   This creates a keyed function array and initialises it to NULL.

   Function which returns a pointer to a pointer to a function which returns void.
   (An array of pointers to functions which return void.
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
void  (**(CreateKeyedFunctionArray(int size)))(void)
{
  void (**array)(void);
  int i;

  /* Allocate the memory. */
  array = (void (**)(void))malloc(size*sizeof(void (*)(void)));

  if(array)
  {
    /* Initialise the data. */
    for(i = 0; i < size; i++)
    {
      array[i] = NULL;
    };
  };

  return array;
}
    


#ifdef TEST_KEYED_FUNCTIONS

static void (**functions)(void);

void RegisterTestFunction(int key, void (*func)(void))
{
  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)();

  functions = CreateKeyedFunctionArray(3);

  if(!functions)
  {
    fprintf(stderr, "Function array is still null !\n");

    exit(1);
  };


  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