type_macros.hpp
Go to the documentation of this file.
1#ifndef ERT_TYPE_MACROS_H
2#define ERT_TYPE_MACROS_H
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8#include <ert/util/util.h>
9
10
11/*****************************************************************/
47#define UTIL_IS_INSTANCE_FUNCTION(type , TYPE_ID) \
48bool type ## _is_instance( const void * __arg ) { \
49 if (__arg == NULL) \
50 return false; \
51 else { \
52 const type ## _type * arg = (const type ## _type * ) __arg; \
53 if ( arg->__type_id == TYPE_ID) \
54 return true; \
55 else \
56 return false; \
57 } \
58}
59
60
61#define UTIL_IS_INSTANCE_HEADER(type) bool type ## _is_instance( const void * __arg )
62
63
64#define UTIL_SAFE_CAST_FUNCTION(type , TYPE_ID) \
65type ## _type * type ## _safe_cast( void * __arg ) { \
66 if (__arg == NULL) { \
67 util_abort("%s: runtime cast failed - tried to dereference NULL\n",__func__); \
68 return NULL; \
69 } \
70 { \
71 type ## _type * arg = (type ## _type *) __arg; \
72 if ( arg->__type_id == TYPE_ID) \
73 return arg; \
74 else { \
75 util_abort("%s: runtime cast failed: Got ID:%d Expected ID:%d \n", __func__ , arg->__type_id , TYPE_ID); \
76 return NULL; \
77 } \
78 } \
79}
80#define UTIL_SAFE_CAST_HEADER( type ) type ## _type * type ## _safe_cast( void * __arg )
81
82
83#define UTIL_SAFE_CAST_FUNCTION_CONST(type , TYPE_ID) \
84const type ## _type * type ## _safe_cast_const( const void * __arg ) { \
85 if (__arg == NULL) { \
86 util_abort("%s: runtime cast failed - tried to dereference NULL\n",__func__); \
87 return NULL; \
88 } \
89 { \
90 const type ## _type * arg = (const type ## _type *) __arg; \
91 if ( arg->__type_id == TYPE_ID) \
92 return arg; \
93 else { \
94 util_abort("%s: runtime cast failed: Got ID:%d Expected ID:%d \n", __func__ , arg->__type_id , TYPE_ID); \
95 return NULL; \
96 } \
97 } \
98}
99#define UTIL_SAFE_CAST_HEADER_CONST( type ) const type ## _type * type ## _safe_cast_const( const void * __arg )
100
101
102
103
104#define UTIL_TRY_CAST_FUNCTION(type , TYPE_ID) \
105type ## _type * type ## _try_cast( void * __arg ) { \
106 if (__arg == NULL) \
107 return NULL; \
108 { \
109 type ## _type * arg = (type ## _type *) __arg; \
110 if ( arg->__type_id == TYPE_ID) \
111 return arg; \
112 else \
113 return NULL; \
114 } \
115}
116#define UTIL_TRY_CAST_HEADER( type ) type ## _type * type ## _try_cast( void * __arg )
117
118
119#define UTIL_TRY_CAST_FUNCTION_CONST(type , TYPE_ID) \
120const type ## _type * type ## _try_cast_const( const void * __arg ) { \
121 if (__arg == NULL) \
122 return NULL; \
123 { \
124 const type ## _type * arg = (type ## _type *) __arg; \
125 if ( arg->__type_id == TYPE_ID) \
126 return arg; \
127 else \
128 return NULL; \
129 } \
130}
131#define UTIL_TRY_CAST_HEADER_CONST( type ) const type ## _type * type ## _try_cast_const( const void * __arg )
132
133
134
135
136#define UTIL_TYPE_ID_DECLARATION int __type_id
137#define UTIL_TYPE_ID_INIT(var , TYPE_ID) var->__type_id = TYPE_ID;
138
139
140
141#ifdef __cplusplus
142}
143#endif
144#endif