Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

debug.h

Go to the documentation of this file.
00001 /*!
00002   \file   debug.h
00003   \author Klaus Holst
00004   \date   Jan 2005
00005   
00006   \brief  Debug class. Implements much safer arrays.
00007     
00008 */
00009 
00010 #ifndef _DEBUG_H_
00011 #define _DEBUG_H_
00012 
00013 /* All debug-print-statements are removed by preprocessor
00014    when NDEBUG is defined. Debug-printing goes to stderr. 
00015    dbg1 through dbg5 are used in a printf-like manner and
00016    dbg() is used to output the single argument using the
00017    c++ stream-object cerr. */
00018 #ifndef NDEBUG
00019 
00020 # include <iostream>
00021 # include <stdio.h>
00022 # include <vector>
00023 # include <cassert>
00024 
00025 # define dbg1(msg) fprintf(stderr, msg)
00026 # define dbg2(fmt,a1) fprintf(stderr, fmt, a1)
00027 # define dbg3(fmt,a1,a2) fprintf(stderr, fmt, a1, a2)
00028 # define dbg4(fmt,a1,a2,a3) fprintf(stderr, fmt, a1, a2, a3)
00029 # define dbg5(fmt,a1,a2,a3,a4) fprintf(stderr, fmt, a1, a2, a3, a4)
00030 # define dbg(data) { std::cerr << (data); std::cerr.flush(); }
00031 
00032 // This class asserts all subscripts to std::vector unless NDEBUG is
00033 // defined, in which case it has zero overhead.
00034 template <class T>
00035 class safevector : public std::vector<T> {
00036  public:
00037   safevector() {}
00038   safevector(unsigned size):std::vector<T>(size) {}
00039   safevector(unsigned size, T const& value):std::vector<T>(size, value) {}
00040   safevector(std::vector<T> const& array): std::vector<T>(array) {}
00041   safevector(T const* begin, T const* end): std::vector<T>(begin, end) {}
00042   safevector(typename std::vector<T>::const_iterator begin, 
00043              typename std::vector<T>::const_iterator end): std::vector<T>(begin, end) {}
00044 
00045   inline typename std::vector<T>::const_reference operator[](unsigned idx)const {
00046     assert(idx < this->size());
00047     return std::vector<T>::operator[](idx);
00048   }
00049   inline typename std::vector<T>::reference operator[](unsigned idx) {
00050     assert(idx < this->size());
00051     return std::vector<T>::operator[](idx);
00052   }
00053 
00054   inline void insert(typename std::vector<T>::iterator pos, T const& value) {
00055     assert(pos >= this->begin() && pos <= this->end());
00056     std::vector<T>::insert(pos, value);
00057   }
00058 
00059   inline void insert(typename std::vector<T>::iterator pos, 
00060                      typename std::vector<T>::const_iterator from, 
00061                      typename std::vector<T>::const_iterator to) {
00062     assert(pos >= this->begin() && pos <= this->end());
00063     assert(from <= to);
00064     std::vector<T>::insert(pos, from, to);
00065   }
00066 
00067 };
00068 
00069 #else //!NDEBUG
00070 
00071 # define dbg1(msg) {}
00072 # define dbg2(fmt,a1) {} 
00073 # define dbg3(fmt,a1,a2) {}
00074 # define dbg4(fmt,a1,a2,a3) {}
00075 # define dbg5(fmt,a1,a2,a3,a4) {} 
00076 # define dbg(data) {} 
00077 
00078 # define safevector std::vector
00079 
00080 #endif//NDEBUG
00081 #endif//_DEBUG_H_

Generated on Tue Feb 14 16:05:52 2006 for estfunc by doxygen 1.3.6