#ifndef DIE_H_INCLUDED #define DIE_H_INCLUDED // die is used like this: // // if (n<3) // die << "How did n get as small as " << n << "?"; // // It will print the message to cerr, and exit(1). // // A newline is *not* required; it is supplied automatically. // // Implementation features: // • die is a macro that creates a temporary Death object whose // destructor is called at the semicolon, the message is displayed, // and exit is called. // • operator<< as a const method, not a free function // • conditional compilation for non-standard acquisition of argv[0] // • string pasting // • const char *const // • __func__ #define die Death(__func__, __FILE__, __LINE__) #include // exit() #include // cerr #if __hpux extern "C" const char **__argv_value; // HP-UX program name #elif __linux #include // Linux program name #else #error Cannot display argv[0]. #endif class Death { const char *const func, *const file; const int line; public: Death(const char *fu, const char *fi, int li) : func(fu), file(fi), line(li) { *this << "\nFatal error: "; } ~Death() { *this << "\n" #if __hpux "Program: " << __argv_value[0] << "\n" #elif __linux "Program: " << program_invocation_name << "\n" #endif "Function: " << func << "\n" "File: " << file << "\n" "Line: " << line << "\n" "Exiting.\n"; std::exit(1); } template std::ostream& operator<<(T const &item) const { return std::cerr << item; } }; #endif /* DIE_H_INCLUDED */