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