diff --git a/desktop-shell/shell.c b/desktop-shell/shell.c index 8b7a23ad..ea3c4535 100644 --- a/desktop-shell/shell.c +++ b/desktop-shell/shell.c @@ -47,10 +47,6 @@ #define DEFAULT_NUM_WORKSPACES 1 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200 -#ifndef static_assert -#define static_assert(cond, msg) -#endif - struct focus_state { struct desktop_shell *shell; struct weston_seat *seat; diff --git a/shared/helpers.h b/shared/helpers.h index 46f745d1..0ebcc4a4 100644 --- a/shared/helpers.h +++ b/shared/helpers.h @@ -100,6 +100,40 @@ extern "C" { (type *)( (char *)__mptr - offsetof(type,member) );}) #endif +/** + * Build-time static assertion support + * + * A build-time equivalent to assert(), will generate a compilation error + * if the supplied condition does not evaluate true. + * + * The following example demonstrates use of static_assert to ensure that + * arrays which are supposed to mirror each other have a consistent + * size. + * + * This is only a fallback definition; support must be provided by the + * compiler itself. + * + * @code + * int small[4]; + * long expanded[4]; + * + * static_assert(ARRAY_LENGTH(small) == ARRAY_LENGTH(expanded), + * "size mismatch between small and expanded arrays"); + * for (i = 0; i < ARRAY_LENGTH(small); i++) + * expanded[i] = small[4]; + * @endcode + * + * @param condition Expression to check for truth + * @param msg Message to print on failure + */ +#ifndef static_assert +# ifdef _Static_assert +# define static_assert(cond, msg) _Static_assert(cond, msg) +# else +# define static_assert(cond, msg) +# endif +#endif + #ifdef __cplusplus } #endif diff --git a/tests/weston-test-desktop-shell.c b/tests/weston-test-desktop-shell.c index de844251..c780316d 100644 --- a/tests/weston-test-desktop-shell.c +++ b/tests/weston-test-desktop-shell.c @@ -41,10 +41,6 @@ #include "shared/helpers.h" #include "libweston-desktop/libweston-desktop.h" -#ifndef static_assert -#define static_assert(cond, msg) -#endif - struct desktest_shell { struct wl_listener compositor_destroy_listener; struct weston_desktop *desktop;