Assignments
Assignments
exceptions.h
Go to the documentation of this file.
1/*
2Copyright (c) 2019, Michael Kazhdan
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
8Redistributions of source code must retain the above copyright notice, this list of
9conditions and the following disclaimer. Redistributions in binary form must reproduce
10the above copyright notice, this list of conditions and the following disclaimer
11in the documentation and/or other materials provided with the distribution.
12
13Neither the name of the Johns Hopkins University nor the names of its contributors
14may be used to endorse or promote products derived from this software without specific
15prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26DAMAGE.
27*/
28
29#ifndef EXCEPTIONS_INCLUDED
30#define EXCEPTIONS_INCLUDED
31
32#include <exception>
33#include <string>
34#include <sstream>
35#include <iostream>
36
37#define VERBOSE_MESSAGING
38
39#ifdef _WIN32
40#ifndef MSVC_WARNING
41#define DO_PRAGMA(x) _Pragma (#x)
42#define MSVC_WARNING(x) DO_PRAGMA( message( "[WARNING] " #x ) )
43#endif // MSVC_WARNING
44#else // !_WIN32
45#define MSVC_WARNING(x)
46#endif // _WIN32
47
48namespace Util
49{
50 template< typename ... Arguments > void _AddToMessageStream( std::stringstream &stream , Arguments ... arguments );
51 inline void _AddToMessageStream( std::stringstream &stream ){ return; }
52 template< typename Argument , typename ... Arguments > void _AddToMessageStream( std::stringstream &stream , Argument argument , Arguments ... arguments )
53 {
54 stream << argument;
55 _AddToMessageStream( stream , arguments ... );
56 }
57
58#ifdef VERBOSE_MESSAGING
59 template< typename ... Arguments >
60 std::string MakeMessageString( std::string header , std::string fileName , int line , std::string functionName , Arguments ... arguments )
61 {
62 size_t headerSize = header.size();
63 std::stringstream stream;
64
65 // The first line is the header, the file name , and the line number
66 stream << header << " " << fileName << " (Line " << line << ")" << std::endl;
67
68 // Inset the second line by the size of the header and write the function name
69 for( size_t i=0 ; i<=headerSize ; i++ ) stream << " ";
70 stream << functionName << std::endl;
71
72 // Inset the third line by the size of the header and write the rest
73 for( size_t i=0 ; i<=headerSize ; i++ ) stream << " ";
74 _AddToMessageStream( stream , arguments ... );
75
76 return stream.str();
77 }
78
79 struct Exception : public std::exception
80 {
81 const char *what( void ) const noexcept { return _message.c_str(); }
82 template< typename ... Args >
83 Exception( const char *fileName , int line , const char *functionName , Args ... args )
84 {
85 _message = MakeMessageString( "[EXCEPTION]" , fileName , line , functionName , args ... );
86 }
87 private:
88 std::string _message;
89 };
90
91 template< typename ... Args > void Throw( const char *fileName , int line , const char *functionName , Args ... args ){ throw Exception( fileName , line , functionName , args ... ); }
92 template< typename ... Args >
93 void Warn( const char *fileName , int line , const char *functionName , Args ... args )
94 {
95 std::cerr << MakeMessageString( "[WARNING]" , fileName , line , functionName , args ... ) << std::endl;
96 }
97
98 template< typename ... Args >
99 void ErrorOut( const char *fileName , int line , const char *functionName , Args ... args )
100 {
101 std::cerr << MakeMessageString( "[ERROR]" , fileName , line , functionName , args ... ) << std::endl;
102 exit( 0 );
103 }
104#else // !VERBOSE_MESSAGING
105 template< typename ... Arguments >
106 std::string MakeMessageString( std::string header , std::string functionName , Arguments ... arguments )
107 {
108 std::stringstream stream;
109
110 // The first line is the header, the file name , and the line number
111 stream << header << " " << functionName << ": ";
112
113 _AddToMessageStream( stream , arguments ... );
114
115 return stream.str();
116 }
117
118 struct Exception : public std::exception
119 {
120 const char *what( void ) const noexcept { return _message.c_str(); }
121 template< typename ... Args >
122 Exception( const char *functionName , Args ... args )
123 {
124 _message = MakeMessageString( "[EXCEPTION]" , functionName , args ... );
125 }
126 private:
127 std::string _message;
128 };
129 template< typename ... Args > void Throw( const char *functionName , Args ... args ){ throw Exception( functionName , args ... ); }
130 template< typename ... Args >
131 void Warn( const char *functionName , Args ... args )
132 {
133 std::cerr << MakeMessageString( "[WARNING]" , functionName , args ... ) << std::endl;
134 }
135 template< typename ... Args >
136 void ErrorOut( const char *functionName , Args ... args )
137 {
138 std::cerr << MakeMessageString( "[WARNING]" , functionName , args ... ) << std::endl;
139 exit( 0 );
140 }
141#endif // VERBOSE_MESSAGING
142}
143#ifdef VERBOSE_MESSAGING
144#ifndef WARN
145#define WARN( ... ) Util::Warn( __FILE__ , __LINE__ , __FUNCTION__ , __VA_ARGS__ )
146#endif // WARN
147#ifndef WARN_ONCE
148#define WARN_ONCE( ... ) { static bool firstTime = true ; if( firstTime ) Util::Warn( __FILE__ , __LINE__ , __FUNCTION__ , __VA_ARGS__ ) ; firstTime = false; }
149#endif // WARN_ONCE
150#ifndef THROW
151#define THROW( ... ) Util::Throw( __FILE__ , __LINE__ , __FUNCTION__ , __VA_ARGS__ )
152#endif // THROW
153#ifndef ERROR_OUT
154#define ERROR_OUT( ... ) Util::ErrorOut( __FILE__ , __LINE__ , __FUNCTION__ , __VA_ARGS__ )
155#endif // ERROR_OUT
156#else // !VERBOSE_MESSAGING
157#ifndef WARN
158#define WARN( ... ) Util::Warn( __FUNCTION__ , __VA_ARGS__ )
159#endif // WARN
160#ifndef WARN_ONCE
161#define WARN_ONCE( ... ) { static bool firstTime = true ; if( firstTime ) Util::Warn( __FUNCTION__ , __VA_ARGS__ ) ; firstTime = false; }
162#endif // WARN_ONCE
163#ifndef THROW
164#define THROW( ... ) Util::Throw( __FUNCTION__ , __VA_ARGS__ )
165#endif // THROW
166#ifndef ERROR_OUT
167#define ERROR_OUT( ... ) Util::ErrorOut( __FUNCTION__ , __VA_ARGS__ )
168#endif // ERROR_OUT
169#endif // VERBOSE_MESSAGING
170#endif // EXCEPTIONS_INCLUDED
Definition: algebra.h:7
void _AddToMessageStream(std::stringstream &stream, Arguments ... arguments)
void Warn(const char *fileName, int line, const char *functionName, Args ... args)
Definition: exceptions.h:93
void Throw(const char *fileName, int line, const char *functionName, Args ... args)
Definition: exceptions.h:91
std::string MakeMessageString(std::string header, std::string fileName, int line, std::string functionName, Arguments ... arguments)
Definition: exceptions.h:60
void ErrorOut(const char *fileName, int line, const char *functionName, Args ... args)
Definition: exceptions.h:99
Definition: exceptions.h:80
std::string _message
Definition: exceptions.h:88
Exception(const char *fileName, int line, const char *functionName, Args ... args)
Definition: exceptions.h:83
const char * what(void) const noexcept
Definition: exceptions.h:81