]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/TextException.h
Boilerplate: update copyright blurbs on src/
[thirdparty/squid.git] / src / base / TextException.h
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 #ifndef SQUID__TEXTEXCEPTION_H
10 #define SQUID__TEXTEXCEPTION_H
11
12 // Origin: xstd/TextException
13
14 #include <exception>
15
16 static unsigned int FileNameHashCached(const char *fname);
17
18 // simple exception to report custom errors
19 // we may want to change the interface to be able to report system errors
20
21 class TextException: public std::exception
22 {
23
24 public:
25 TextException();
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();
29
30 // unique exception ID for transaction error detail logging
31 unsigned int id() const { return theId; }
32
33 virtual const char *what() const throw();
34
35 TextException& operator=(const TextException &right);
36
37 public:
38 char *message; // read-only
39
40 protected:
41 /// a small integer hash value to semi-uniquely identify the source file
42 static unsigned int FileNameHash(const char *fname);
43
44 // optional location information
45 const char *theFileName;
46 int theLineNo;
47 unsigned int theId;
48
49 friend unsigned int FileNameHashCached(const char *fname);
50 };
51
52 //inline
53 //ostream &operator <<(ostream &os, const TextException &exx) {
54 // return exx.print(os);
55 //}
56
57 /// caches the result of FileNameHash() for each translation unit
58 static unsigned int
59 FileNameHashCached(const char *fname)
60 {
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
65 lastFname = fname;
66 lastHash = TextException::FileNameHash(fname);
67 }
68 return lastHash;
69 }
70
71 /// Avoids "defined but not used" warnings for FileNameHashCached
72 class FileNameHashCacheUser
73 {
74 bool use(void *ptr=NULL) { return ptr != (void*)&FileNameHashCached; }
75 };
76
77 #if !defined(TexcHere)
78 # define TexcHere(msg) TextException((msg), __FILE__, __LINE__, \
79 (FileNameHashCached(__FILE__)<<14) | (__LINE__ & 0x3FFF))
80 #endif
81
82 void Throw(const char *message, const char *fileName, int lineNo, unsigned int id);
83
84 // Must(condition) is like assert(condition) but throws an exception instead
85 #if !defined(Must)
86 # define Must(cond) ((cond) ? \
87 (void)0 : \
88 (void)Throw(#cond, __FILE__, __LINE__, \
89 (FileNameHashCached(__FILE__)<<14) | (__LINE__ & 0x3FFF)))
90 #endif
91
92 #endif /* SQUID__TEXTEXCEPTION_H */