TextException.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 class SBuf;
17 
18 static unsigned int FileNameHashCached(const char *fname);
19 
20 // simple exception to report custom errors
21 // we may want to change the interface to be able to report system errors
22 
23 class TextException: public std::exception
24 {
25 
26 public:
27  TextException();
28  TextException(const char *aMessage, const char *aFileName = 0, int aLineNo = -1, unsigned int anId =0);
29  TextException(SBuf aMessage, const char *aFileName = 0, int aLineNo = -1, unsigned int anId =0);
30  TextException(const TextException& right);
31  virtual ~TextException() throw();
32 
33  // unique exception ID for transaction error detail logging
34  unsigned int id() const { return theId; }
35 
36  virtual const char *what() const throw();
37 
38  TextException& operator=(const TextException &right);
39 
40 public:
41  char *message; // read-only
42 
43 protected:
45  static unsigned int FileNameHash(const char *fname);
46 
47  // optional location information
48  const char *theFileName;
49  int theLineNo;
50  unsigned int theId;
51 
52  friend unsigned int FileNameHashCached(const char *fname);
53 };
54 
55 //inline
56 //ostream &operator <<(ostream &os, const TextException &exx) {
57 // return exx.print(os);
58 //}
59 
61 static unsigned int
62 FileNameHashCached(const char *fname)
63 {
64  static const char *lastFname = 0;
65  static int lastHash = 0;
66  // __FILE__ changes when we #include files
67  if (lastFname != fname) { // cheap pointer comparison
68  lastFname = fname;
69  lastHash = TextException::FileNameHash(fname);
70  }
71  return lastHash;
72 }
73 
76 {
77  bool use(void *ptr=NULL) { return ptr != (void*)&FileNameHashCached; }
78 };
79 
80 #if !defined(TexcHere)
81 # define TexcHere(msg) TextException((msg), __FILE__, __LINE__, \
82  (FileNameHashCached(__FILE__)<<14) | (__LINE__ & 0x3FFF))
83 #endif
84 
85 void Throw(const char *message, const char *fileName, int lineNo, unsigned int id);
86 
87 // Must(condition) is like assert(condition) but throws an exception instead
88 #if !defined(Must)
89 # define Must(cond) ((cond) ? \
90  (void)0 : \
91  (void)Throw(#cond, __FILE__, __LINE__, \
92  (FileNameHashCached(__FILE__)<<14) | (__LINE__ & 0x3FFF)))
93 #endif
94 
95 #endif /* SQUID__TEXTEXCEPTION_H */
96 
void Throw(const char *message, const char *fileName, int lineNo, unsigned int id)
const char * theFileName
Definition: TextException.h:48
Definition: SBuf.h:87
Avoids "defined but not used" warnings for FileNameHashCached.
Definition: TextException.h:75
virtual ~TextException()
static unsigned int FileNameHashCached(const char *fname)
caches the result of FileNameHash() for each translation unit
Definition: TextException.h:62
static unsigned int FileNameHash(const char *fname)
a small integer hash value to semi-uniquely identify the source file
unsigned int id() const
Definition: TextException.h:34
unsigned int theId
Definition: TextException.h:50
#define protected
friend unsigned int FileNameHashCached(const char *fname)
caches the result of FileNameHash() for each translation unit
Definition: TextException.h:62
virtual const char * what() const
bool use(void *ptr=NULL)
Definition: TextException.h:77
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors