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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 * Quincy Tse <quincy.tse@nicta.com.au>
19 */
20#ifndef NS_ASSERT_H
21#define NS_ASSERT_H
22
23/**
24 * \file
25 * \ingroup assert
26 * NS_ASSERT() and NS_ASSERT_MSG() macro definitions
27 */
28
29/**
30 * \ingroup debugging
31 * \defgroup assert Assertions
32 *
33 * \brief Assert functions and macros
34 *
35 * The assert macros are used to verify
36 * at runtime that a certain condition is true. If it is
37 * not true, the program halts. These checks are built
38 * into the program only in debugging builds. They are
39 * removed in optimized builds.
40 *
41 * These macro are intended to check certain conditions
42 * to be true. Do not put code that also have side effects
43 * that your program relies on (eg. code that advances
44 * an iterator and return false at end of file) because
45 * the code will not be executed on release builds!!
46 *
47 * If assertion-style checks are required for release
48 * builds, use NS_ABORT_UNLESS and NS_ABORT_MSG_UNLESS.
49 */
50
51#ifdef NS3_ASSERT_ENABLE
52
53#include "fatal-error.h"
54
55#include <iostream>
56
57/**
58 * \ingroup assert
59 *
60 * At runtime, in debugging builds, if this condition is not
61 * true, the program prints the source file, line number and
62 * unverified condition and halts by calling std::terminate
63 *
64 * \param [in] condition Condition to verify.
65 */
66#define NS_ASSERT(condition) \
67 do \
68 { \
69 if (!(condition)) \
70 { \
71 std::cerr << "NS_ASSERT failed, cond=\"" << #condition << "\", "; \
72 NS_FATAL_ERROR_NO_MSG(); \
73 } \
74 } while (false)
75
76/**
77 * \ingroup assert
78 *
79 * At runtime, in debugging builds, if this condition is not
80 * true, the program prints the message to output and
81 * halts by calling std::terminate.
82 *
83 * \param [in] condition Condition to verify.
84 * \param [in] message Message to output
85 */
86#define NS_ASSERT_MSG(condition, message) \
87 do \
88 { \
89 if (!(condition)) \
90 { \
91 std::cerr << "NS_ASSERT failed, cond=\"" << #condition << "\", "; \
92 NS_FATAL_ERROR(message); \
93 } \
94 } while (false)
95
96#else /* NS3_ASSERT_ENABLE */
97
98// NOTE: The no-op macros are not inserted into the final code.
99// However, the use of sizeof() allows the compiler to silently check if the condition is
100// syntactically valid.
101
102#define NS_ASSERT(condition) \
103 do \
104 { \
105 (void)sizeof(condition); \
106 } while (false)
107
108#define NS_ASSERT_MSG(condition, message) \
109 do \
110 { \
111 (void)sizeof(condition); \
112 } while (false)
113
114#endif /* NS3_ASSERT_ENABLE */
115
116#endif /* ASSERT_H */
NS_FATAL_x macro definitions.