]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
utils: Provide a CALLBACK macro, similar to METHOD, but for void* callbacks
authorMartin Willi <martin@revosec.ch>
Wed, 22 Jan 2014 15:55:27 +0000 (16:55 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 7 May 2014 12:13:34 +0000 (14:13 +0200)
Using the same mechanism as the METHOD macro, the CALLBACK macro defines
a hybrid function signature. It strictly uses a weak void* for the first
function parameter, in contrast to the dynamic METHOD object "this" type.

src/libstrongswan/utils/utils.h

index 75b31ec1b364125a66710fb8814d5ab1ef078039..53bfceda6d8181f7dc26d9871217b7cecde70747 100644 (file)
@@ -203,6 +203,19 @@ static inline bool memeq(const void *x, const void *y, size_t len)
        static typeof(name) *_##name = (typeof(name)*)name; \
        static ret name(this, ##__VA_ARGS__)
 
+/**
+ * Callback declaration/definition macro, allowing casted first parameter.
+ *
+ * This is very similar to METHOD, but instead of casting the first parameter
+ * to a public interface, it uses a void*. This allows type safe definition
+ * of a callback function, while using the real type for the first parameter.
+ */
+#define CALLBACK(name, ret, param1, ...) \
+       static ret _cb_##name(union {void *_generic; param1;} \
+       __attribute__((transparent_union)), ##__VA_ARGS__); \
+       static typeof(_cb_##name) *name = (typeof(_cb_##name)*)_cb_##name; \
+       static ret _cb_##name(param1, ##__VA_ARGS__)
+
 /**
  * Architecture independent bitfield definition helpers (at least with GCC).
  *