HawkTracer  0.9.0
event_macros.h
Go to the documentation of this file.
1 #ifndef HAWKTRACER_EVENT_MACROS_H
2 #define HAWKTRACER_EVENT_MACROS_H
3 
4 #include <hawktracer/events.h>
6 #include <hawktracer/registry.h>
7 
8 #include <string.h>
9 
10 #define HT_EVENT_GET_SIZE_FUNCTION(C_TYPE) ht_##C_TYPE##_get_size
11 #define HT_EVENT_SERIALIZE_FUNCTION(C_TYPE) ht_##C_TYPE##_fnc_serialize
12 #define HT_EVENT_REGISTER_KLASS_FUNCTION(C_TYPE) ht_##C_TYPE##_register_event_klass
13 #define HT_EVENT_GET_KLASS_INSTANCE_FUNCTION(C_TYPE) ht_##C_TYPE##_get_event_klass_instance
14 
15 #define HT_EVENT_RUNTIME_SIZEOF_STRUCT(VAR_NAME, FIELD, C_TYPE) HT_EVENT_GET_SIZE_FUNCTION(C_TYPE)(((HT_Event*)&VAR_NAME->FIELD))
16 #define HT_EVENT_RUNTIME_SIZEOF_INTEGER(VAR_NAME, FIELD, C_TYPE) sizeof(VAR_NAME->FIELD)
17 #define HT_EVENT_RUNTIME_SIZEOF_POINTER(VAR_NAME, FIELD, C_TYPE) sizeof(VAR_NAME->FIELD)
18 #define HT_EVENT_RUNTIME_SIZEOF_DOUBLE(VAR_NAME, FIELD, C_TYPE) sizeof(VAR_NAME->FIELD)
19 #define HT_EVENT_RUNTIME_SIZEOF_STRING(VAR_NAME, FIELD, C_TYPE) (VAR_NAME->FIELD ? strlen(VAR_NAME->FIELD) + 1 : 0)
20 #define HT_EVENT_RUNTIME_SIZEOF__(VAR_NAME, DATA_TYPE, C_TYPE, FIELD, ...) +HT_EVENT_RUNTIME_SIZEOF_##DATA_TYPE(VAR_NAME, FIELD, C_TYPE)
21 #define HT_EVENT_RUNTIME_SIZEOF_(...) MKCREFLECT_EXPAND_(HT_EVENT_RUNTIME_SIZEOF__(__VA_ARGS__, 0))
22 #define HT_EVENT_RUNTIME_SIZEOF(X, USER_DATA) HT_EVENT_RUNTIME_SIZEOF_(USER_DATA, MKCREFLECT_EXPAND_VA_ X)
23 
24 #define HT_EVENT_RUNTIME_SERIALIZE_BASE_TYPE_(C_TYPE, FIELD) memcpy(buffer + offset, (char*)&VAR_NAME->FIELD, sizeof(VAR_NAME->FIELD)), offset += sizeof(VAR_NAME->FIELD);
25 
26 #define HT_EVENT_RUNTIME_SERIALIZE_STRUCT(C_TYPE, FIELD) offset += HT_EVENT_SERIALIZE_FUNCTION(C_TYPE)(((HT_Event*)&VAR_NAME->FIELD), buffer);
27 #define HT_EVENT_RUNTIME_SERIALIZE_INTEGER(C_TYPE, FIELD) HT_EVENT_RUNTIME_SERIALIZE_BASE_TYPE_(C_TYPE, FIELD)
28 #define HT_EVENT_RUNTIME_SERIALIZE_POINTER(C_TYPE, FIELD) HT_EVENT_RUNTIME_SERIALIZE_BASE_TYPE_(C_TYPE, FIELD)
29 #define HT_EVENT_RUNTIME_SERIALIZE_DOUBLE(C_TYPE, FIELD) HT_EVENT_RUNTIME_SERIALIZE_BASE_TYPE_(C_TYPE, FIELD)
30 #define HT_EVENT_RUNTIME_SERIALIZE_STRING(C_TYPE, FIELD) do {\
31  size_t len = (VAR_NAME->FIELD) ? strlen(VAR_NAME->FIELD) + 1 : 0; \
32  memcpy(buffer + offset, VAR_NAME->FIELD, len); \
33  offset += len; \
34 } while (0);
35 #define HT_EVENT_RUNTIME_SERIALIZE__(DATA_TYPE, C_TYPE, FIELD, ...) HT_EVENT_RUNTIME_SERIALIZE_##DATA_TYPE(C_TYPE, FIELD)
36 #define HT_EVENT_RUNTIME_SERIALIZE_(...) MKCREFLECT_EXPAND_(HT_EVENT_RUNTIME_SERIALIZE__(__VA_ARGS__, 0))
37 #define HT_EVENT_RUNTIME_SERIALIZE(X, USER_DATA) HT_EVENT_RUNTIME_SERIALIZE_ (MKCREFLECT_EXPAND_VA_ X)
38 
39 
40 #define HT_EVENT_GET_SIZE_FUNCTION_DEF(TYPE_NAME, BASE_TYPE, ...) \
41  size_t HT_EVENT_GET_SIZE_FUNCTION(TYPE_NAME)(HT_Event* VAR_NAME) \
42  { \
43  return MKCREFLECT_FOREACH(HT_EVENT_RUNTIME_SIZEOF, ((TYPE_NAME*)VAR_NAME), (STRUCT, BASE_TYPE, base), __VA_ARGS__); \
44  }
45 #define HT_EVENT_GET_SIZE_FUNCTION_DECL(TYPE_NAME, BASE_TYPE, ...) \
46  HT_API size_t HT_EVENT_GET_SIZE_FUNCTION(TYPE_NAME)(HT_Event* VAR_NAME);\
47 
48 #define HT_EVENT_SERIALIZE_FUNCTION_DEF(TYPE_NAME, BASE_TYPE, ...) \
49  size_t HT_EVENT_SERIALIZE_FUNCTION(TYPE_NAME)(HT_Event* VAR_NAME_, HT_Byte* buffer) \
50  { \
51  size_t offset = 0; \
52  TYPE_NAME* VAR_NAME = (TYPE_NAME*)VAR_NAME_; \
53  MKCREFLECT_FOREACH(HT_EVENT_RUNTIME_SERIALIZE, 0, (STRUCT, BASE_TYPE, base), __VA_ARGS__) \
54  return offset; \
55  }
56 #define HT_EVENT_SERIALIZE_FUNCTION_DECL(TYPE_NAME, BASE_TYPE, ...) \
57  HT_API size_t HT_EVENT_SERIALIZE_FUNCTION(TYPE_NAME)(HT_Event* VAR_NAME_, HT_Byte* buffer);
58 
59 #define HT_EVENT_GET_KLASS_INSTANCE_FUNCTION_DEF(TYPE_NAME) \
60  HT_EventKlass* HT_EVENT_GET_KLASS_INSTANCE_FUNCTION(TYPE_NAME)(void) \
61  { \
62  static HT_EventKlass klass_instance = { \
63  NULL, \
64  HT_EVENT_SERIALIZE_FUNCTION(TYPE_NAME), \
65  HT_EVENT_GET_SIZE_FUNCTION(TYPE_NAME), \
66  HT_INVALID_KLASS_ID \
67  }; \
68  return &klass_instance; \
69  }
70 #define HT_EVENT_GET_KLASS_INSTANCE_FUNCTION_DECL(TYPE_NAME) \
71  HT_API HT_EventKlass* HT_EVENT_GET_KLASS_INSTANCE_FUNCTION(TYPE_NAME)(void);
72 
73 #define HT_EVENT_REGISTER_KLASS_FUNCTION_DEF(TYPE_NAME) \
74  HT_EventKlassId HT_EVENT_REGISTER_KLASS_FUNCTION(TYPE_NAME)(void) \
75  { \
76  HT_EVENT_GET_KLASS_INSTANCE_FUNCTION(TYPE_NAME)()->type_info = mkcreflect_get_##TYPE_NAME##_type_info(); \
77  return ht_registry_register_event_klass(HT_EVENT_GET_KLASS_INSTANCE_FUNCTION(TYPE_NAME)()); \
78  }
79 #define HT_EVENT_REGISTER_KLASS_FUNCTION_DECL(TYPE_NAME) \
80  HT_API HT_EventKlassId HT_EVENT_REGISTER_KLASS_FUNCTION(TYPE_NAME)(void);
81 
82 #define HT_EVENT_DEFINE_STRUCTURE(TYPE_NAME, BASE_TYPE, ...) \
83  MKCREFLECT_DEFINE_STRUCT(TYPE_NAME, \
84  (STRUCT, BASE_TYPE, base), \
85  MKCREFLECT_EXPAND_VA_(__VA_ARGS__))
86 
87 #define HT_EVENT_DECLARATIONS(TYPE_NAME, BASE_TYPE, ...) \
88  HT_EVENT_GET_SIZE_FUNCTION_DECL(TYPE_NAME, BASE_TYPE, __VA_ARGS__) \
89  HT_EVENT_SERIALIZE_FUNCTION_DECL(TYPE_NAME, BASE_TYPE, __VA_ARGS__) \
90  HT_EVENT_GET_KLASS_INSTANCE_FUNCTION_DECL(TYPE_NAME) \
91  HT_EVENT_REGISTER_KLASS_FUNCTION_DECL(TYPE_NAME)
92 
93 #define HT_EVENT_DEFINITIONS_(TYPE_NAME, BASE_TYPE, ...) \
94  HT_EVENT_GET_SIZE_FUNCTION_DEF(TYPE_NAME, BASE_TYPE, __VA_ARGS__) \
95  HT_EVENT_SERIALIZE_FUNCTION_DEF(TYPE_NAME, BASE_TYPE, __VA_ARGS__) \
96  HT_EVENT_GET_KLASS_INSTANCE_FUNCTION_DEF(TYPE_NAME) \
97  HT_EVENT_REGISTER_KLASS_FUNCTION_DEF(TYPE_NAME)
98 
99 #define HT_DECLARE_EVENT_KLASS(TYPE_NAME, BASE_TYPE, ...) \
100  HT_EVENT_DEFINE_STRUCTURE(TYPE_NAME, BASE_TYPE, __VA_ARGS__) \
101  HT_EVENT_DECLARATIONS(TYPE_NAME, BASE_TYPE, __VA_ARGS__) \
102  HT_EVENT_DEFINITIONS(TYPE_NAME, BASE_TYPE, __VA_ARGS__)
103 
104 #ifndef HT_EVENT_DEFINITIONS
105 #define HT_EVENT_DEFINITIONS(TYPE_NAME, BASE_TYPE, ...)
106 #endif /* HT_EVENT_DEFINITIONS */
107 
108 #endif /* HAWKTRACER_EVENT_MACROS_H */