#ifndef _INCLUDED_L3_ARREX_H #define _INCLUDED_L3_ARREX_H // *Array Exceptions* Copyright (C) Krzysztof Bosak, 2000-11-10...2000-11-20 // All rights reserved. // kbosak@box43.pl // http://www.kbosak.prv.pl // neither nor are not widely implemented, as for 2000-11-19. #include #ifndef NDEBUG #include #endif #ifdef USE_ARRAY_EXCEPTIONS class ArrayEx { protected: static char * const _description; static const unsigned MAX_DESCRIPTION_LENGTH; inline ArrayEx(const char * const strf, int strl, int arrlen) { // protected constructor: you can create an object derived from this class assert(strf!=NULL); assert(_description!=NULL); sprintf(const_cast(_description), "Compilation stardate is %s, %s.\n" "Array Exception thrown in file %s at line %d.\n" "Last recorded array size was %d.\n", __DATE__, __TIME__, strf, strl, arrlen); assert( strlen(_description)+1 < MAX_DESCRIPTION_LENGTH ); } friend ostream& operator<<(ostream& str, const ArrayEx&) { assert(ArrayEx::_description!=NULL); return str<(_description)+strlen(_description), "Out Of Memory, tried to allocate %d cell(s), %d byte(s) each (%dKB total).\n", cells, cellsize, static_cast(cells*cellsize/1024.0+0.5)); } }; class SentinelOverwrittenEx: public ArrayEx { public: inline SentinelOverwrittenEx(const char * const strf, int strl, int arrlen) : ArrayEx(strf, strl, arrlen) { assert( strlen(_description)+1 < MAX_DESCRIPTION_LENGTH ); sprintf(const_cast(_description)+strlen(_description), "One of two terminating sentinels were overwritten.\n"); } }; class BadIndexEx: public ArrayEx { public: inline BadIndexEx(int bad_array_index, const char * const strf, int strl, int arrlen) : ArrayEx(strf, strl, arrlen) { assert( strlen(_description)+1 < MAX_DESCRIPTION_LENGTH ); sprintf(const_cast(_description)+strlen(_description), "Unauthorized access out of array boundary, used index: %d.\n", bad_array_index); } }; class TooLowIndexEx: public BadIndexEx { public: inline TooLowIndexEx(int bad_array_index, const char * const strf, int strl, int arrlen) : BadIndexEx(bad_array_index, strf, strl, arrlen) { assert( strlen(_description)+1 < MAX_DESCRIPTION_LENGTH ); sprintf(const_cast(_description)+strlen(_description), "Too low array index.\n"); } }; class TooBigIndexEx: public BadIndexEx { public: inline TooBigIndexEx(int bad_array_index, const char * const strf, int strl, int arrlen) : BadIndexEx(bad_array_index, strf, strl, arrlen) { assert( strlen(_description)+1 < MAX_DESCRIPTION_LENGTH ); sprintf(const_cast(_description)+strlen(_description), "Too big array index.\n"); } }; /* class ReadingUninitializedScalarEx: public ArrayEx { public: inline ReadingUninitializedScalarEx(const char * const strf, int strl, int element_index) : ArrayEx(strf, strl, element_index) { assert( strlen(_description)+1 < MAX_DESCRIPTION_LENGTH ); sprintf(const_cast(_description)+strlen(_description), "Attempt to read an uninitialized scalar value in array.\n"); } }; */ #endif // USE_ARRAY_EXCEPTIONS #endif //_INCLUDED_L3_ARREX_H