From: Martin Willi Date: Tue, 8 Dec 2009 15:12:16 +0000 (+0100) Subject: Added a METHOD() macro to define methods with both public and private signatures X-Git-Tag: 4.3.6~102 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1a1ff9d1276a48a8c0bc0732a6ef7364b2e0e01f;p=thirdparty%2Fstrongswan.git Added a METHOD() macro to define methods with both public and private signatures --- diff --git a/src/libstrongswan/utils.h b/src/libstrongswan/utils.h index ebc67db004..f07e835824 100644 --- a/src/libstrongswan/utils.h +++ b/src/libstrongswan/utils.h @@ -114,6 +114,19 @@ #define INIT(this, ...) { (this) = malloc(sizeof(*this)); \ *(this) = (typeof(*this)){ __VA_ARGS__ }; } +/** + * Method declaration/definition macro, providing private and public interface. + * + * Defines a method name with this as first parameter and a return value ret, + * and an alias for this method with a _ prefix, having the this argument + * safely casted to the public interface iface. + * _name is provided a function pointer, but will get optimized out by GCC. + */ +#define METHOD(iface, name, ret, this, ...) \ + static ret name(union {iface *_public; this;} __attribute__((transparent_union)), ##__VA_ARGS__); \ + const static typeof(name) *_##name = (const typeof(name)*)name; \ + static ret name(this, ##__VA_ARGS__) + /** * Macro to allocate a sized type. */