macros.h

Go to the documentation of this file.
00001 // Debugging support implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2005, 2006
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /** @file debug/macros.h
00032  *  This file is a GNU debug extension to the Standard C++ Library.
00033  */
00034 
00035 #ifndef _GLIBCXX_DEBUG_MACROS_H
00036 #define _GLIBCXX_DEBUG_MACROS_H 1
00037 
00038 /**
00039  * Macros used by the implementation to verify certain
00040  * properties. These macros may only be used directly by the debug
00041  * wrappers. Note that these are macros (instead of the more obviously
00042  * "correct" choice of making them functions) because we need line and
00043  * file information at the call site, to minimize the distance between
00044  * the user error and where the error is reported.
00045  *
00046  */
00047 #define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage)             \
00048   do                                    \
00049   {                                 \
00050     if (! (_Condition))                         \
00051       __gnu_debug::_Error_formatter::_M_at(__FILE__, __LINE__)          \
00052       ._ErrorMessage._M_error();                    \
00053   } while (false)
00054 
00055 // Verify that [_First, _Last) forms a valid iterator range.
00056 #define __glibcxx_check_valid_range(_First,_Last)           \
00057 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__valid_range(_First, _Last),    \
00058               _M_message(__gnu_debug::__msg_valid_range)    \
00059               ._M_iterator(_First, #_First)         \
00060               ._M_iterator(_Last, #_Last))
00061 
00062 /** Verify that we can insert into *this with the iterator _Position.
00063  *  Insertion into a container at a specific position requires that
00064  *  the iterator be nonsingular (i.e., either dereferenceable or
00065  *  past-the-end) and that it reference the sequence we are inserting
00066  *  into. Note that this macro is only valid when the container is a
00067  *  _Safe_sequence and the iterator is a _Safe_iterator.
00068 */
00069 #define __glibcxx_check_insert(_Position)               \
00070 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(),             \
00071               _M_message(__gnu_debug::__msg_insert_singular) \
00072               ._M_sequence(*this, "this")           \
00073               ._M_iterator(_Position, #_Position));     \
00074 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),           \
00075               _M_message(__gnu_debug::__msg_insert_different) \
00076               ._M_sequence(*this, "this")           \
00077               ._M_iterator(_Position, #_Position))
00078 
00079 /** Verify that we can insert the values in the iterator range
00080  *  [_First, _Last) into *this with the iterator _Position.  Insertion
00081  *  into a container at a specific position requires that the iterator
00082  *  be nonsingular (i.e., either dereferenceable or past-the-end),
00083  *  that it reference the sequence we are inserting into, and that the
00084  *  iterator range [_First, Last) is a valid (possibly empty)
00085  *  range. Note that this macro is only valid when the container is a
00086  *  _Safe_sequence and the iterator is a _Safe_iterator.
00087  *
00088  *  @tbd We would like to be able to check for noninterference of
00089  *  _Position and the range [_First, _Last), but that can't (in
00090  *  general) be done.
00091 */
00092 #define __glibcxx_check_insert_range(_Position,_First,_Last)        \
00093 __glibcxx_check_valid_range(_First,_Last);              \
00094 _GLIBCXX_DEBUG_VERIFY(!_Position._M_singular(),             \
00095               _M_message(__gnu_debug::__msg_insert_singular)    \
00096                       ._M_sequence(*this, "this")           \
00097               ._M_iterator(_Position, #_Position));     \
00098 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),           \
00099               _M_message(__gnu_debug::__msg_insert_different)   \
00100               ._M_sequence(*this, "this")           \
00101               ._M_iterator(_Position, #_Position))
00102 
00103 /** Verify that we can erase the element referenced by the iterator
00104  * _Position. We can erase the element if the _Position iterator is
00105  * dereferenceable and references this sequence.
00106 */
00107 #define __glibcxx_check_erase(_Position)                \
00108 _GLIBCXX_DEBUG_VERIFY(_Position._M_dereferenceable(),           \
00109               _M_message(__gnu_debug::__msg_erase_bad)          \
00110                       ._M_sequence(*this, "this")           \
00111               ._M_iterator(_Position, #_Position));     \
00112 _GLIBCXX_DEBUG_VERIFY(_Position._M_attached_to(this),           \
00113               _M_message(__gnu_debug::__msg_erase_different)    \
00114               ._M_sequence(*this, "this")           \
00115               ._M_iterator(_Position, #_Position))
00116 
00117 /** Verify that we can erase the elements in the iterator range
00118  *  [_First, _Last). We can erase the elements if [_First, _Last) is a
00119  *  valid iterator range within this sequence.
00120 */
00121 #define __glibcxx_check_erase_range(_First,_Last)           \
00122 __glibcxx_check_valid_range(_First,_Last);              \
00123 _GLIBCXX_DEBUG_VERIFY(_First._M_attached_to(this),          \
00124               _M_message(__gnu_debug::__msg_erase_different)    \
00125                       ._M_sequence(*this, "this")           \
00126               ._M_iterator(_First, #_First)         \
00127               ._M_iterator(_Last, #_Last))
00128 
00129 // Verify that the subscript _N is less than the container's size.
00130 #define __glibcxx_check_subscript(_N)                   \
00131 _GLIBCXX_DEBUG_VERIFY(_N < this->size(),                \
00132               _M_message(__gnu_debug::__msg_subscript_oob)      \
00133                       ._M_sequence(*this, "this")           \
00134               ._M_integer(_N, #_N)              \
00135               ._M_integer(this->size(), "size"))
00136 
00137 // Verify that the container is nonempty
00138 #define __glibcxx_check_nonempty()                  \
00139 _GLIBCXX_DEBUG_VERIFY(! this->empty(),                  \
00140               _M_message(__gnu_debug::__msg_empty)          \
00141                       ._M_sequence(*this, "this"))
00142 
00143 // Verify that the < operator for elements in the sequence is a
00144 // StrictWeakOrdering by checking that it is irreflexive.
00145 #define __glibcxx_check_strict_weak_ordering(_First,_Last)  \
00146 _GLIBCXX_DEBUG_ASSERT(_First == _Last || !(*_First < *_First))
00147 
00148 // Verify that the predicate is StrictWeakOrdering by checking that it
00149 // is irreflexive.
00150 #define __glibcxx_check_strict_weak_ordering_pred(_First,_Last,_Pred)   \
00151 _GLIBCXX_DEBUG_ASSERT(_First == _Last || !_Pred(*_First, *_First))
00152 
00153 
00154 // Verify that the iterator range [_First, _Last) is sorted
00155 #define __glibcxx_check_sorted(_First,_Last)                \
00156 __glibcxx_check_valid_range(_First,_Last);              \
00157 __glibcxx_check_strict_weak_ordering(_First,_Last);         \
00158 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last),   \
00159               _M_message(__gnu_debug::__msg_unsorted)           \
00160                       ._M_iterator(_First, #_First)         \
00161               ._M_iterator(_Last, #_Last))
00162 
00163 /** Verify that the iterator range [_First, _Last) is sorted by the
00164     predicate _Pred. */
00165 #define __glibcxx_check_sorted_pred(_First,_Last,_Pred)         \
00166 __glibcxx_check_valid_range(_First,_Last);              \
00167 __glibcxx_check_strict_weak_ordering_pred(_First,_Last,_Pred);          \
00168 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_sorted(_First, _Last, _Pred), \
00169               _M_message(__gnu_debug::__msg_unsorted_pred)      \
00170                       ._M_iterator(_First, #_First)         \
00171               ._M_iterator(_Last, #_Last)           \
00172               ._M_string(#_Pred))
00173 
00174 /** Verify that the iterator range [_First, _Last) is partitioned
00175     w.r.t. the value _Value. */
00176 #define __glibcxx_check_partitioned(_First,_Last,_Value)        \
00177 __glibcxx_check_valid_range(_First,_Last);              \
00178 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned(_First, _Last,   \
00179                                 _Value),    \
00180               _M_message(__gnu_debug::__msg_unpartitioned)      \
00181               ._M_iterator(_First, #_First)         \
00182               ._M_iterator(_Last, #_Last)           \
00183               ._M_string(#_Value))
00184 
00185 /** Verify that the iterator range [_First, _Last) is partitioned
00186     w.r.t. the value _Value and predicate _Pred. */
00187 #define __glibcxx_check_partitioned_pred(_First,_Last,_Value,_Pred) \
00188 __glibcxx_check_valid_range(_First,_Last);              \
00189 _GLIBCXX_DEBUG_VERIFY(__gnu_debug::__check_partitioned(_First, _Last,   \
00190                              _Value, _Pred), \
00191               _M_message(__gnu_debug::__msg_unpartitioned_pred) \
00192               ._M_iterator(_First, #_First)         \
00193               ._M_iterator(_Last, #_Last)           \
00194               ._M_string(#_Pred)                \
00195                       ._M_string(#_Value))
00196 
00197 // Verify that the iterator range [_First, _Last) is a heap
00198 #define __glibcxx_check_heap(_First,_Last)              \
00199 __glibcxx_check_valid_range(_First,_Last);              \
00200 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last),                \
00201               _M_message(__gnu_debug::__msg_not_heap)           \
00202               ._M_iterator(_First, #_First)         \
00203               ._M_iterator(_Last, #_Last))
00204 
00205 /** Verify that the iterator range [_First, _Last) is a heap
00206     w.r.t. the predicate _Pred. */
00207 #define __glibcxx_check_heap_pred(_First,_Last,_Pred)           \
00208 __glibcxx_check_valid_range(_First,_Last);              \
00209 _GLIBCXX_DEBUG_VERIFY(std::__is_heap(_First, _Last, _Pred),     \
00210               _M_message(__gnu_debug::__msg_not_heap_pred)      \
00211                       ._M_iterator(_First, #_First)         \
00212               ._M_iterator(_Last, #_Last)           \
00213               ._M_string(#_Pred))
00214 
00215 #ifdef _GLIBCXX_DEBUG_PEDANTIC
00216 #  define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
00217 #  define __glibcxx_check_string_len(_String,_Len) \
00218        _GLIBCXX_DEBUG_ASSERT(_String != 0 || _Len == 0)
00219 #else
00220 #  define __glibcxx_check_string(_String)
00221 #  define __glibcxx_check_string_len(_String,_Len)
00222 #endif
00223 
00224 #endif

Generated on Thu Nov 1 13:12:04 2007 for libstdc++ by  doxygen 1.5.1