diff options
author | Hans Ulrich Niedermann <hun@n-dimensional.de> | 2021-11-11 23:39:47 +0100 |
---|---|---|
committer | Hans Ulrich Niedermann <hun@n-dimensional.de> | 2021-11-11 23:41:48 +0100 |
commit | a154a91599ad5d16a3e6067e7552ea1e12dad48c (patch) | |
tree | 59d68d5e443209e0092d90b72d3815a99487d216 /libgphoto2_port | |
parent | 4a0d9dcb1fffd226925da095684dfce2184fe430 (diff) | |
download | libgphoto2-a154a91599ad5d16a3e6067e7552ea1e12dad48c.tar.gz |
Add compiletime-assert.h for COMPILETIME_ASSERT() macro
C11 has added static_assert(EXPR, MSG) to assert.h, but C99
only knows runtime assertions using assert(EXPR). This
defines the COMPILETIME_ASSERT(EXPR) macro which works with
both C11 and later using static_assert(), and another way
if compiling using a pre-C11 language standard.
Diffstat (limited to 'libgphoto2_port')
-rw-r--r-- | libgphoto2_port/libgphoto2_port/Makefile.am | 3 | ||||
-rw-r--r-- | libgphoto2_port/libgphoto2_port/compiletime-assert.h | 92 |
2 files changed, 95 insertions, 0 deletions
diff --git a/libgphoto2_port/libgphoto2_port/Makefile.am b/libgphoto2_port/libgphoto2_port/Makefile.am index dfbad73a9..0119253e0 100644 --- a/libgphoto2_port/libgphoto2_port/Makefile.am +++ b/libgphoto2_port/libgphoto2_port/Makefile.am @@ -6,6 +6,9 @@ EXTRA_DIST = EXTRA_DIST += i18n.h +EXTRA_DIST += compiletime-assert.h + + lib_LTLIBRARIES = libgphoto2_port.la libgphoto2_port_la_CFLAGS = libgphoto2_port_la_CPPFLAGS = diff --git a/libgphoto2_port/libgphoto2_port/compiletime-assert.h b/libgphoto2_port/libgphoto2_port/compiletime-assert.h new file mode 100644 index 000000000..4c046e76e --- /dev/null +++ b/libgphoto2_port/libgphoto2_port/compiletime-assert.h @@ -0,0 +1,92 @@ +/** \file libgphoto2_port/compiletime-assert.h + * \brief Allow compile time assertions with or without C11. + * + * C11 has added static_assert(EXPR, MSG) to assert.h, but C99 only + * knows runtime assertions using assert(EXPR). This defines + * COMPILETIME_ASSERT(EXPR) which works with both C99 and C11 and + * later. + * + * \author Copyright (C) 2010-2021 Hans Ulrich Niedermann <hun@n-dimensional.de> + * + * \copyright GNU Lesser General Public License 2 or later + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#ifndef LIBGPHOTO2_PORT_COMPILETIME_ASSERT_H +#define LIBGPHOTO2_PORT_COMPILETIME_ASSERT_H + + +#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) +/* C11 or later */ + +#include <assert.h> + +/** Compiletime assertion for use inside a function. */ +#define COMPILETIME_ASSERT(CONDITION) \ + static_assert((CONDITION), #CONDITION) + +/** Compiletime assertion for use outside a function. */ +#define BARE_COMPILETIME_ASSERT(CONDITION) \ + static_assert((CONDITION), #CONDITION) + +#else +/* before C11 */ + +/** Compiletime assertion for use inside a function. */ +#define COMPILETIME_ASSERT(CONDITION) \ + switch (0) { /* error here means assertion has failed */ \ + case 0: /* error here means assertion has failed */ \ + case (CONDITION): /* error here means assertion has failed */ \ + break; \ + } + + +/** \internal used by BARE_COMPILETIME_ASSERT() macro */ +#define MAKE_BARE_COMPILETIME_ASSERT_NAME \ + MAKE_BARE_COMPILETIME_ASSERT_NAME1(COMPILETIME_ASSERT_fails_in_line, \ + __LINE__) + + +/** \internal used by BARE_COMPILETIME_ASSERT() macro */ +#define MAKE_BARE_COMPILETIME_ASSERT_NAME1(BASE, PARAM) \ + MAKE_BARE_COMPILETIME_ASSERT_NAME2(BASE, PARAM) + + +/** \internal used by BARE_COMPILETIME_ASSERT() macro */ +#define MAKE_BARE_COMPILETIME_ASSERT_NAME2(BASE, PARAM) \ + BASE ## _ ## PARAM + + +/** Compiletime assertion for use outside a function. */ +#define BARE_COMPILETIME_ASSERT(CONDITION) \ + void MAKE_BARE_COMPILETIME_ASSERT_NAME(void); \ + void MAKE_BARE_COMPILETIME_ASSERT_NAME(void) \ + { \ + COMPILETIME_ASSERT(CONDITION); \ + } + +#endif /* after/before C11 */ + + +#endif /* !defined(LIBGPHOTO2_PORT_COMPILETIME_ASSERT_H) */ + +/* + * Local Variables: + * c-file-style:"linux" + * indent-tabs-mode:t + * End: + */ |