src/core/assert.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3182 61fe7fe81ebd
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2006 INRIA
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License version 2 as
     7  * published by the Free Software Foundation;
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  *
    18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  */
    20 #ifndef ASSERT_H
    21 #define ASSERT_H
    22 
    23 #ifdef NS3_ASSERT_ENABLE
    24 
    25 #include <iostream>
    26 
    27 /**
    28  * \ingroup core
    29  * \defgroup debugging Debugging
    30  */
    31 /**
    32  * \ingroup debugging
    33  * \defgroup assert Assert
    34  *
    35  * \brief assert functions and macros
    36  *
    37  * The assert macros are used to verify
    38  * at runtime that a certain condition is true. If it is
    39  * not true, the program halts. These checks are built
    40  * into the program only in debugging builds. They are
    41  * removed in optimized builds.
    42  */
    43 
    44 /**
    45  * \ingroup assert
    46  * \param condition condition to verifiy.
    47  *
    48  * At runtime, in debugging builds, if this condition is not
    49  * true, the program prints the source file, line number and 
    50  * unverified condition and halts by dereferencing a null pointer.
    51  */
    52 #define NS_ASSERT(condition)                                    \
    53   do                                                            \
    54     {                                                           \
    55       if (!(condition))                                         \
    56         {                                                       \
    57           std::cerr << "assert failed. file=" << __FILE__ <<    \
    58             ", line=" << __LINE__ << ", cond=\""#condition <<   \
    59             "\"" << std::endl;                                  \
    60           int *a = 0;                                           \
    61           *a = 0;                                               \
    62         }                                                       \
    63     }                                                           \
    64   while (false)
    65 
    66 
    67 /**
    68  * \ingroup assert
    69  * \param condition condition to verifiy.
    70  * \param message message to output
    71  *
    72  * At runtime, in debugging builds, if this condition is not
    73  * true, the program prints the message to output and
    74  * halts by dereferencing a null pointer.
    75  */
    76 #define NS_ASSERT_MSG(condition, message)       \
    77   do                                            \
    78     {                                           \
    79       if (!(condition))                         \
    80         {                                       \
    81           std::cerr << message << std::endl;    \
    82           int *a = 0;                           \
    83           *a = 0;                               \
    84         }                                       \
    85     }                                           \
    86   while (false)
    87 
    88 #else /* NS3_ASSERT_ENABLE */
    89 
    90 #define NS_ASSERT(cond)
    91 #define NS_ASSERT_MSG(cond,msg)
    92 
    93 #endif /* NS3_ASSERT_ENABLE */
    94 
    95 #endif /* ASSERT_H */