]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/TextException.h
9e97acdfa78f757d85fafacdcf3b6963e9bedc1e
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 #ifndef SQUID__TEXTEXCEPTION_H
10 #define SQUID__TEXTEXCEPTION_H
12 // Origin: xstd/TextException
16 static unsigned int FileNameHashCached(const char *fname
);
18 // simple exception to report custom errors
19 // we may want to change the interface to be able to report system errors
21 class TextException
: public std::exception
26 TextException(const char *aMessage
, const char *aFileName
= 0, int aLineNo
= -1, unsigned int anId
=0);
27 TextException(const TextException
& right
);
28 virtual ~TextException() throw();
30 // unique exception ID for transaction error detail logging
31 unsigned int id() const { return theId
; }
33 virtual const char *what() const throw();
35 TextException
& operator=(const TextException
&right
);
38 char *message
; // read-only
41 /// a small integer hash value to semi-uniquely identify the source file
42 static unsigned int FileNameHash(const char *fname
);
44 // optional location information
45 const char *theFileName
;
49 friend unsigned int FileNameHashCached(const char *fname
);
53 //ostream &operator <<(ostream &os, const TextException &exx) {
54 // return exx.print(os);
57 /// caches the result of FileNameHash() for each translation unit
59 FileNameHashCached(const char *fname
)
61 static const char *lastFname
= 0;
62 static int lastHash
= 0;
63 // __FILE__ changes when we #include files
64 if (lastFname
!= fname
) { // cheap pointer comparison
66 lastHash
= TextException::FileNameHash(fname
);
71 /// Avoids "defined but not used" warnings for FileNameHashCached
72 class FileNameHashCacheUser
74 bool use(void *ptr
=NULL
) { return ptr
!= (void*)&FileNameHashCached
; }
77 #if !defined(TexcHere)
78 # define TexcHere(msg) TextException((msg), __FILE__, __LINE__, \
79 (FileNameHashCached(__FILE__)<<14) | (__LINE__ & 0x3FFF))
82 void Throw(const char *message
, const char *fileName
, int lineNo
, unsigned int id
);
84 // Must(condition) is like assert(condition) but throws an exception instead
86 # define Must(cond) ((cond) ? \
88 (void)Throw(#cond, __FILE__, __LINE__, \
89 (FileNameHashCached(__FILE__)<<14) | (__LINE__ & 0x3FFF)))
92 #endif /* SQUID__TEXTEXCEPTION_H */