A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
assert.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 INRIA, 2010 NICTA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 * Quincy Tse <quincy.tse@nicta.com.au>
8 */
9#ifndef NS_ASSERT_H
10#define NS_ASSERT_H
11
12/**
13 * \file
14 * \ingroup assert
15 * NS_ASSERT() and NS_ASSERT_MSG() macro definitions
16 */
17
18/**
19 * \ingroup debugging
20 * \defgroup assert Assertions
21 *
22 * \brief Assert functions and macros
23 *
24 * The assert macros are used to verify
25 * at runtime that a certain condition is true. If it is
26 * not true, the program halts. These checks are built
27 * into the program only in debugging builds. They are
28 * removed in optimized builds.
29 *
30 * These macro are intended to check certain conditions
31 * to be true. Do not put code that also have side effects
32 * that your program relies on (eg. code that advances
33 * an iterator and return false at end of file) because
34 * the code will not be executed on release builds!!
35 *
36 * If assertion-style checks are required for release
37 * builds, use NS_ABORT_UNLESS and NS_ABORT_MSG_UNLESS.
38 */
39
40#ifdef NS3_ASSERT_ENABLE
41
42#include "fatal-error.h"
43
44#include <iostream>
45
46/**
47 * \ingroup assert
48 *
49 * At runtime, in debugging builds, if this condition is not
50 * true, the program prints the source file, line number and
51 * unverified condition and halts by calling std::terminate
52 *
53 * \param [in] condition Condition to verify.
54 */
55#define NS_ASSERT(condition) \
56 do \
57 { \
58 if (!(condition)) \
59 { \
60 std::cerr << "NS_ASSERT failed, cond=\"" << #condition << "\", "; \
61 NS_FATAL_ERROR_NO_MSG(); \
62 } \
63 } while (false)
64
65/**
66 * \ingroup assert
67 *
68 * At runtime, in debugging builds, if this condition is not
69 * true, the program prints the message to output and
70 * halts by calling std::terminate.
71 *
72 * \param [in] condition Condition to verify.
73 * \param [in] message Message to output
74 */
75#define NS_ASSERT_MSG(condition, message) \
76 do \
77 { \
78 if (!(condition)) \
79 { \
80 std::cerr << "NS_ASSERT failed, cond=\"" << #condition << "\", "; \
81 NS_FATAL_ERROR(message); \
82 } \
83 } while (false)
84
85#else /* NS3_ASSERT_ENABLE */
86
87// NOTE: The no-op macros are not inserted into the final code.
88// However, the use of sizeof() allows the compiler to silently check if the condition is
89// syntactically valid.
90
91#define NS_ASSERT(condition) \
92 do \
93 { \
94 (void)sizeof(condition); \
95 } while (false)
96
97#define NS_ASSERT_MSG(condition, message) \
98 do \
99 { \
100 (void)sizeof(condition); \
101 } while (false)
102
103#endif /* NS3_ASSERT_ENABLE */
104
105#endif /* ASSERT_H */
NS_FATAL_x macro definitions.