summaryrefslogtreecommitdiff
path: root/libgphoto2_port/libgphoto2_port/compiletime-assert.h
blob: 4c046e76e010a77b0c8e130c8d4e5aac8878f82d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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:
 */