PIPS
newgen_generic_function.h
Go to the documentation of this file.
1 /*
2 
3  $Id: newgen_generic_function.h 1357 2016-03-02 08:18:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of NewGen.
8 
9  NewGen is free software: you can redistribute it and/or modify it under the
10  terms of the GNU General Public License as published by the Free Software
11  Foundation, either version 3 of the License, or any later version.
12 
13  NewGen is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16  License for more details.
17 
18  You should have received a copy of the GNU General Public License along with
19  NewGen. If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifndef NEWGEN_GENERIC_FUNCTION_INCLUDED
24 #define NEWGEN_GENERIC_FUNCTION_INCLUDED
25 
26 /* Add the __attribute__ ((unused)) for gcc to avoid warning if the
27  functions are not used. */
28 #define GENERIC_STATIC_OBJECT(PREFIX, name, type) \
29  static type name##_object = type##_undefined; \
30  PREFIX bool __attribute__ ((unused)) name##_undefined_p(void) { \
31  return name##_object==type##_undefined; \
32  } \
33  PREFIX void __attribute__ ((unused)) reset_##name(void) { \
34  message_assert("must reset sg defined", !name##_undefined_p()); \
35  name##_object=type##_undefined; \
36  } \
37  PREFIX void __attribute__ ((unused)) error_reset_##name(void) { \
38  name##_object=type##_undefined; \
39  } \
40  PREFIX void __attribute__ ((unused)) set_##name(type o) { \
41  message_assert("must set sg undefined", name##_undefined_p()); \
42  name##_object=o; \
43  } \
44  PREFIX type __attribute__ ((unused)) get_##name(void) { \
45  message_assert("must get sg defined", !name##_undefined_p()); \
46  return name##_object; \
47  }
48 
49 #define GENERIC_STATIC_STATUS(PREFIX, name, type, init, cloze) \
50  GENERIC_STATIC_OBJECT(PREFIX, name, type) \
51  PREFIX void __attribute__ ((unused)) init_##name(void) { \
52  message_assert("must initialize sg undefined", name##_undefined_p()); \
53  name##_object = init; \
54  } \
55  PREFIX void __attribute__ ((unused)) close_##name(void) { \
56  message_assert("must close sg defined", !name##_undefined_p()); \
57  cloze(name##_object); name##_object = type##_undefined; \
58 }
59 
60 /* The idea here is to have a static function the name of which is
61  * name, and which is a newgen function (that is a ->).
62  * It embeds the status of some function related to the manipulated
63  * data, with the {INIT,SET,RESET,GET,CLOSE} functions.
64  * Plus the STORE, UPDATE and LOAD operators. and BOUND_P predicate.
65  * This could replace all generic_mappings in PIPS, if the mapping
66  * types are declared to newgen. It would also ease the db management.
67  */
68 
69 /* STORE and LOAD are prefered to extend and apply because it sounds
70  * like the generic mappings, and it feels as a status/static thing
71  */
72 #define GENERIC_FUNCTION(PREFIX, name, type) \
73  GENERIC_STATIC_STATUS(PREFIX, name, type, make_##type(), free_##type) \
74  PREFIX void __attribute__ ((unused)) store_##name(type##_key_type k, type##_value_type v) { \
75  extend_##type(name##_object, k, v); \
76  } \
77  PREFIX void __attribute__ ((unused)) update_##name(type##_key_type k, type##_value_type v) { \
78  update_##type(name##_object, k, v); \
79  } \
80  PREFIX type##_value_type __attribute__ ((unused)) load_##name(type##_key_type k) { \
81  return(apply_##type(name##_object, k)); \
82  } \
83  PREFIX type##_value_type __attribute__ ((unused)) delete_##name(type##_key_type k) { \
84  return(delete_##type(name##_object, k)); \
85  } \
86  PREFIX bool __attribute__ ((unused)) bound_##name##_p(type##_key_type k) { \
87  return(bound_##type##_p(name##_object, k)); \
88  } \
89  PREFIX void __attribute__ ((unused)) store_or_update_##name(type##_key_type k, type##_value_type v) { \
90  if (bound_##name##_p(k)) \
91  update_##name(k, v); \
92  else \
93  store_##name(k, v); \
94  }
95 
96 #define GENERIC_LOCAL_FUNCTION(name, type)\
97  GENERIC_FUNCTION(static, name, type)
98 
99 #define GENERIC_GLOBAL_FUNCTION(name, type)\
100  GENERIC_FUNCTION(extern, name, type)
101 
102 #endif /* NEWGEN_GENERIC_FUNCTION_INCLUDED */