1 /** \file
2  * \brief Definition of exception classes
3  *
4  * \author Carsten Gutwenger, Markus Chimani
5  *
6  * \par License:
7  * This file is part of the Open Graph Drawing Framework (OGDF).
8  *
9  * \par
10  * Copyright (C)<br>
11  * See README.md in the OGDF root directory for details.
12  *
13  * \par
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * Version 2 or 3 as published by the Free Software Foundation;
17  * see the file LICENSE.txt included in the packaging of this file
18  * for details.
19  *
20  * \par
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * \par
27  * You should have received a copy of the GNU General Public
28  * License along with this program; if not, see
29  * http://www.gnu.org/copyleft/gpl.html
30  */
31 
32 #pragma once
33 
34 #include <ogdf/basic/basic.h>
35 #include <ogdf/basic/Logger.h>
36 
37 namespace ogdf {
38 
39 //! @name Throwing exceptions
40 //! @{
41 
42 //! Flushes some output streams
43 #define OGDF_FLUSH_OUTPUTS \
44 	std::cout << std::flush, ::ogdf::Logger::sfout() << std::flush
45 
46 /**
47  * Replacement for \c throw.
48  * This macro is used to throw an exception and pass the file name
49  * and line number of the location in the source file (in Debug mode only).
50  * @param CLASS is the name of the exception class.
51  * @param PARAM is an additional parameter (like the error code) required
52  *        by the exception calls.
53  * @ingroup macros
54  */
55 #define OGDF_THROW_PARAM(CLASS, PARAM) \
56 	OGDF_FLUSH_OUTPUTS, throw CLASS(PARAM)
57 
58 /**
59  * Replacement for \c throw.
60  * This macro is used to throw an exception and pass the file name
61  * and line number of the location in the source file (in Debug mode only).
62  * @param CLASS is the name of the exception class.
63  * @ingroup macros
64  */
65 #define OGDF_THROW(CLASS) \
66 	OGDF_FLUSH_OUTPUTS, throw CLASS()
67 
68 #ifdef OGDF_DEBUG
69 # undef OGDF_THROW_PARAM
70 # define OGDF_THROW_PARAM(CLASS, PARAM) \
71 	OGDF_FLUSH_OUTPUTS, throw CLASS(PARAM, __FILE__, __LINE__)
72 # undef OGDF_THROW
73 # define OGDF_THROW(CLASS) \
74 	OGDF_FLUSH_OUTPUTS, throw CLASS(__FILE__, __LINE__)
75 #endif
76 
77 //! @}
78 
79 //! Code for an internal failure condition
80 /**
81  * @ingroup exceptions
82  *
83  * \see AlgorithmFailureException
84  */
85 enum class AlgorithmFailureCode {
86 	Unknown,
87 	IllegalParameter, //!< function parameter is illegal
88 	NoFlow,           //!< min-cost flow could not find a legal flow
89 	Sort,             //!< sequence not sorted
90 	Label,            //!< labelling failed
91 	ExternalFace,     //!< external face not correct
92 	ForbiddenCrossing,//!< crossing forbidden but necessary
93 	TimelimitExceeded,//!< it took too long
94 	NoSolutionFound,  //!< couldn't solve the problem
95 	IndexOutOfBounds, //!< index out of bounds
96 
97 	// The following codes are used by Abacus (think about changing them to
98 	// more error describing codes)
99 	PrimalBound,
100 	DualBound,
101 	NotInteger,
102 	Buffer,
103 	AddVar,
104 	Sorter,
105 	Phase,
106 	Active,
107 	NoSolution,
108 	MakeFeasible,
109 	Guarantee,
110 	BranchingVariable,
111 	Strategy,
112 	CloseHalf,
113 	StandardPool,
114 	Variable,
115 	LpIf,
116 	Lp,
117 	Bstack,
118 	LpStatus,
119 	BranchingRule,
120 	FixSet,
121 	LpSub,
122 	String,
123 	Constraint,
124 	Pool,
125 	Global,
126 	FsVarStat,
127 	LpVarStat,
128 	OsiIf,
129 	ConBranchRule,
130 	Timer,
131 	Array,
132 	Csense,
133 	BPrioQueue,
134 	FixCand,
135 	BHeap,
136 	Poolslot,
137 	SparVec,
138 	Convar,
139 	Ostream,
140 	Hash,
141 	Paramaster,
142 	InfeasCon,
143 
144 	STOP              // INSERT NEW CODES BEFORE afcSTOP!
145 };
146 
147 //! Code for the library which was intended to get used, but its use is not supported.
148 /**
149  * @ingroup exceptions
150  * \see LibraryNotSupportedException
151  */
152 enum class LibraryNotSupportedCode {
153 	Unknown,
154 	Coin,                          //!< COIN not supported
155 	Abacus,                        //!< ABACUS not supported
156 	FunctionNotImplemented,        //!< the used library doesn't support that function
157 	MissingCallbackImplementation, //
158 	STOP                           // INSERT NEW CODES BEFORE nscSTOP!
159 };
160 
161 //! Base class of all ogdf exceptions.
162 /**
163  * @ingroup exceptions
164  */
165 class OGDF_EXPORT Exception {
166 
167 private:
168 
169 	const char *m_file; //!< Source file where exception occurred.
170 	int         m_line; //!< Line number where exception occurred.
171 
172 public:
173 	//! Constructs an exception.
174 	/**
175 	 * @param file is the name of the source file where exception was thrown.
176 	 * @param line is the line number in the source file where the exception was thrown.
177 	 */
178 	explicit Exception(const char *file = nullptr, int line = -1) :
m_file(file)179 		m_file(file),
180 		m_line(line)
181 		{ }
182 
183 	//! Returns the name of the source file where exception was thrown.
184 	/**
185 	 * Returns a null pointer if the name of the source file is unknown.
186 	 */
file()187 	const char *file() const { return m_file; }
188 
189 	//! Returns the line number where the exception was thrown.
190 	/**
191 	 * Returns -1 if the line number is unknown.
192 	 */
line()193 	int line() const { return m_line; }
194 };
195 
196 
197 //! %Exception thrown when result of cast is 0.
198 /**
199 * @ingroup exceptions
200 */
201 class OGDF_EXPORT DynamicCastFailedException : public Exception {
202 
203 public:
204 	//! Constructs a dynamic cast failed exception.
Exception(file,line)205 	explicit DynamicCastFailedException(const char *file = nullptr, int line = -1) : Exception(file, line) {}
206 };
207 
208 
209 //! %Exception thrown when not enough memory is available to execute an algorithm.
210 /**
211 * @ingroup exceptions
212 */
213 class OGDF_EXPORT InsufficientMemoryException : public Exception {
214 
215 public:
216 	//! Constructs an insufficient memory exception.
Exception(file,line)217 	explicit InsufficientMemoryException(const char *file = nullptr, int line = -1) : Exception(file, line) {}
218 };
219 
220 
221 //! %Exception thrown when a required standard comparer has not been specialized.
222 /**
223  * @ingroup exceptions
224  *
225  * The default implementation of StdComparer<E> throws this exception, since it
226  * provides no meaningful implementation of comparer methods. You need to specialize
227  * this class for the types you want to use with sorting and searching methods (like
228  * quicksort and binary search).
229  */
230 class OGDF_EXPORT NoStdComparerException : public Exception {
231 
232 public:
233 	//! Constructs a no standard comparer available exception.
Exception(file,line)234 	explicit NoStdComparerException(const char *file = nullptr, int line = -1) : Exception(file, line) {}
235 };
236 
237 
238 //! %Exception thrown when a data type is not supported by a generic function.
239 /**
240 * @ingroup exceptions
241 */
242 class OGDF_EXPORT TypeNotSupportedException : public Exception {
243 
244 public:
245 	//! Constructs a type-not-supported exception.
Exception(file,line)246 	explicit TypeNotSupportedException(const char *file = nullptr, int line = -1) : Exception(file, line) {}
247 };
248 
249 //! %Exception thrown when an algorithm realizes an internal bug that prevents it from continuing.
250 /**
251 * @ingroup exceptions
252 */
253 class OGDF_EXPORT AlgorithmFailureException : public Exception
254 {
255 public:
256 
257 	//! Constructs an algorithm failure exception.
258 	explicit AlgorithmFailureException(AlgorithmFailureCode code,
259 		const char *file = nullptr,
260 		int line = -1) :
Exception(file,line)261 	Exception(file, line),
262 	m_exceptionCode(code)
263 	{}
264 
265 	//! Constructs an algorithm failure exception.
266 	explicit AlgorithmFailureException(
267 		const char *file = nullptr,
268 		int line = -1) :
Exception(file,line)269 	Exception(file, line),
270 	m_exceptionCode(AlgorithmFailureCode::Unknown)
271 	{}
272 
273 	//! Returns the error code of the exception.
exceptionCode()274 	AlgorithmFailureCode exceptionCode() const { return m_exceptionCode; }
275 
276 private:
277 	AlgorithmFailureCode m_exceptionCode; //!< The error code specifying the exception.
278 };
279 
280 //! %Exception thrown when an external library shall be used which is not supported.
281 /**
282 * @ingroup exceptions
283 */
284 class OGDF_EXPORT LibraryNotSupportedException : public Exception {
285 	public:
286 	//! Constructs a library not supported exception.
287 		explicit LibraryNotSupportedException(LibraryNotSupportedCode code,
288 			const char *file = nullptr,
289 			int line = -1) :
Exception(file,line)290 		Exception(file, line),
291 		m_exceptionCode(code)
292 		{}
293 
294 	//! Constructs a library not supported exception.
295 		explicit LibraryNotSupportedException(
296 			const char *file = nullptr,
297 			int line = -1) :
Exception(file,line)298 		Exception(file, line),
299 		m_exceptionCode(LibraryNotSupportedCode::Unknown)
300 		{}
301 
302 	//! Returns the error code of the exception.
exceptionCode()303 	LibraryNotSupportedCode exceptionCode() const { return m_exceptionCode; }
304 
305 private:
306 	LibraryNotSupportedCode m_exceptionCode; //!< The error code specifying the exception.
307 };
308 
309 }
310