--------------------- PatchSet 1531 Date: 2005/08/19 20:18:07 Author: rousskov Branch: squid3-icap Tag: (none) Log: - Added initial implementation of an exception that carries origin filename/lineno information with it. - Added Must(cond) that is like assert(cond) but throws an exception instead of dumping core. Currently, ICAPXaction is using TextExceptions and Must()s to handle errors. Members: src/TextException.cc:1.1->1.1.2.1 src/TextException.h:1.1->1.1.2.1 --- /dev/null Wed Feb 14 13:33:00 2007 +++ squid3/src/TextException.cc Wed Feb 14 13:34:51 2007 @@ -0,0 +1,25 @@ +#include "squid.h" +#include "TextException.h" + +TextException::TextException(const char *aMsg, const char *aFileName, int aLineNo): + message(xstrdup(aMsg)), theFileName(aFileName), theLineNo(aLineNo) { +} + +TextException::~TextException() { + xfree(message); +} + +void Throw(const char *message, const char *fileName, int lineNo) { + + // or should we let the exception recepient print the exception instead? + + if (fileName) + debugs(0, 3, fileName << ':' << lineNo << ": exception"); + + if (message) + debugs(0, 3, ": " << message << "\n"); + else + debugs(0, 3, "." << "\n"); + + throw TextException(message, fileName, lineNo); +} --- /dev/null Wed Feb 14 13:33:00 2007 +++ squid3/src/TextException.h Wed Feb 14 13:34:51 2007 @@ -0,0 +1,43 @@ +#ifndef SQUID__TEXTEXCEPTION_H +#define SQUID__TEXTEXCEPTION_H + +// Origin: xstd/TextException + + +// simple exception to report custom errors +// we may want to change the interface to be able to report system errors +class TextException { + public: + TextException(const char *aMessage, const char *aFileName = 0, int aLineNo = -1); + ~TextException(); + + // ostream &print(ostream &os) const; + + public: + char *message; // read-only + + protected: + // optional location information + const char *theFileName; + int theLineNo; +}; + +//inline +//ostream &operator <<(ostream &os, const TextException &exx) { +// return exx.print(os); +//} + +#if !defined(TexcHere) +# define TexcHere(msg) TextException((msg), __FILE__, __LINE__) +#endif + +extern void Throw(const char *message, const char *fileName, int lineNo); + +// Must(condition) is like assert(condition) but throws an exception instead +#if !defined(Must) +# define Must(cond) ((cond) ? \ + (void)0 : \ + (void)Throw(#cond, __FILE__, __LINE__)) +#endif + +#endif /* SQUID__TEXTEXCEPTION_H */