vtkAssertUtils.hpp 5.39 KB
Newer Older
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 * @brief A Simple assertion library for self-checks that can be turned on/off
 * at compile time based on the ASSERT_ON flag.
 * @file vtkAssertUtils.hpp
 * @date Jan 26, 2011
 * @author gzagaris
 */

#ifndef VTKASSERTUTILS_HPP_
#define VTKASSERTUTILS_HPP_

class vtkAssertUtils
{

  public:

    /**
     * @brief Checks if the predicate is false.
     * @param predicate the predicate to check.
     * @param file the file name from where this function is called.
     * @param line the line number where this function is called.
     */
    inline static void assertFalse(
        const bool predicate, const char* file, int line )
    {
      #ifdef ASSERT_ON
        if( predicate != false )
        {
          std::cerr << "===========================================\n";
          std::cerr <<  __DATE__ << " " << __TIME__ << std::endl;
          std::cerr << "ERROR: AssertUtils::assertFalse() failed from:\n";
          std::cerr << "FILE: " << file << std::endl;
          std::cerr << "LINE: " << line << std::endl;
          assert( predicate == false );
        }
      #endif
      return;

    }


    /**
     * @brief Checks if the predicate is true.
     * @param predicate the predicate to check.
     * @param file the file name from where this function is called.
     * @param line the line number where this function is called.
     */
    inline static void assertTrue(
        const bool predicate, const char* file, int line )
    {
      #ifdef ASSERT_ON
        if( predicate != true )
        {
          std::cerr << "===========================================\n";
          std::cerr <<  __DATE__ << " " << __TIME__ << std::endl;
          std::cerr << "ERROR: AssertUtils::assertTrue() failed from:\n";
          std::cerr << "FILE: " << file << std::endl;
          std::cerr << "LINE: " << line << std::endl;
          assert( predicate == true );
        }
      #endif
      return;

    }


    /**
     * @brief Checks if the supplied pointer is null.
     * @param ptr the pointer to check.
     * @param file the file name from where this function is called.
     * @param line the line number where this function is called.
     */
    inline static void assertNull(
        const void* ptr, const char* file, int line )
    {

      #ifdef ASSERT_ON
        if( ptr != NULL )
        {
          std::cerr << "===========================================\n";
          std::cerr <<  __DATE__ << " " << __TIME__ << std::endl;
          std::cerr << "ERROR: AssertUtils::assertNull() failed from:\n";
          std::cerr << "FILE: " << file << std::endl;
          std::cerr << "LINE: " << line << std::endl;
          assert( ptr == NULL );
        }
      #endif
      return;

    }

    /**
     * @brief Checks if the supplied pointer is NOT null.
     * @param ptr the pointer to check.
     * @param file the file name from where this function is called.
     * @param line the line number where this function is called.
     */
    inline static void assertNotNull(
        const void *ptr, const char* file, int line )
    {

      #ifdef ASSERT_ON
        if( ptr == NULL )
        {
          std::cerr << "===========================================\n";
          std::cerr <<  __DATE__ << " " << __TIME__ << std::endl;
          std::cerr << "ERROR: AssertUtils::assertNull() failed from:\n";
          std::cerr << "FILE: " << file << std::endl;
          std::cerr << "LINE: " << line << std::endl;
          assert( ptr != NULL );
        }
      #endif
      return;

    }


    /**
     * @brief Checks if the two numbers are equal.
     * @param rhs the number on the right-hand-side.
     * @param lhs the number on the left-hand-side.
     */
    inline static void assertEquals(
        const int rhs, const int lhs, const char* file, int line )
    {

      #ifdef ASSERT_ON
        if( rhs != lhs )
        {
          std::cerr << "===========================================\n";
          std::cerr <<  __DATE__ << " " << __TIME__ << std::endl;
          std::cerr << "ERROR: AssertUtils::assertEquals() failed from:\n";
          std::cerr << "FILE: " << file << std::endl;
          std::cerr << "LINE: " << line << std::endl;
          std::cerr << "num1: " << rhs  << std::endl;
          std::cerr << "num2: " << lhs  << std::endl;
          assert( rhs == lhs );
        }
      #endif
      return;

    }

    /**
     * @brief Checks if a number is within the given range.
     * @param num the number to check.
     * @param lb lower-bound.
     * @param ub upper-bound.
     * @note Checks if  lb <= num <= up, i.e. the check is inclusive
     * of the lower and upper bounds.
     */
    inline static void assertInRange(
        const int num, const int lb, const int ub, const char* file, int line )
    {

      #ifdef ASSERT_ON
        if( !( num >= lb ) || !( num <= ub ) )
        {
          std::cerr << "===========================================\n";
          std::cerr <<  __DATE__ << " " << __TIME__ << std::endl;
          std::cerr << "ERROR: AssertUtils::assertInRange() failed from:\n";
          std::cerr << "FILE: "        << file << std::endl;
          std::cerr << "LINE: "        << line << std::endl;
          std::cerr << "NUMBER: "      << num  << std::endl;
          std::cerr << "LOWER BOUND: " << lb   << std::endl;
          std::cerr << "UPPER BOUND: " << ub   << std::endl;
          assert( num >= lb && num <= ub );
        }
      #endif
      return;

    }

};

#endif /* VTKASSERTUTILS_HPP_ */