1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
// ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2018, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Knut Reinert or the FU Berlin nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: Enrico Siragusa <enrico.siragusa@fu-berlin.de>
// ==========================================================================
// Definition of basic exceptions.
// ==========================================================================
#ifndef SEQAN_BASIC_BASIC_EXCEPTION_H_
#define SEQAN_BASIC_BASIC_EXCEPTION_H_
// ============================================================================
// Prerequisites
// ============================================================================
#include <typeinfo>
#include <exception>
#include <stdexcept>
#if !defined(STDLIB_VS)
#include <cxxabi.h>
#endif
namespace seqan {
// ============================================================================
// Forwards
// ============================================================================
template <typename T>
struct Tag;
//struct Nothing_;
//typedef Tag<Nothing_> Nothing;
// ============================================================================
// Macros
// ============================================================================
/*!
* @defgroup ExceptionHandling SeqAn Exception Handling
* @brief Macros supporting exception handling on various platforms.
*/
/*!
* @macro ExceptionHandling#SEQAN_EXCEPTIONS
* @headerfile <seqan/basic.h>
* @brief Determines whether exceptions are enabled or not.
*
* @signature #define SEQAN_EXCEPTIONS
*
* @see ExceptionHandling#SEQAN_TRY
* @see ExceptionHandling#SEQAN_CATCH
* @see ExceptionHandling#SEQAN_THROW
* @see Exception
*/
#define SEQAN_EXCEPTIONS __EXCEPTIONS
/*!
* @macro ExceptionHandling#SEQAN_TRY
* @headerfile <seqan/basic.h>
* @brief Replaces the C++ try keyword.
*
* @signature SEQAN_TRY {} SEQAN_CATCH() {}
*
* When exceptions are disabled, i.e. SEQAN_EXCEPTIONS is set to false, the code inside the try block is always executed.
*
* @see ExceptionHandling#SEQAN_CATCH
* @see ExceptionHandling#SEQAN_THROW
* @see Exception
*
* @section Examples
*
* @code{.cpp}
*
* SEQAN_TRY
* {
* SEQAN_THROW(Exception)
* }
* SEQAN_CATCH(Exception const & e)
* {
* std::cerr << e.what() << std::endl;
* }
*
* @endcode
*/
/*!
* @macro ExceptionHandling#SEQAN_CATCH
* @headerfile <seqan/basic.h>
* @brief Replaces the C++ catch keyword.
*
* @signature SEQAN_TRY {} SEQAN_CATCH() {}
*
* When exceptions are disabled, i.e. SEQAN_EXCEPTIONS is set to false, the code inside the catch block is never executed.
*
* @see ExceptionHandling#SEQAN_TRY
* @see ExceptionHandling#SEQAN_THROW
* @see Exception
*
* @section Examples
*
* See @link ExceptionHandling#SEQAN_TRY @endlink for a full example.
*/
/*!
* @macro ExceptionHandling#SEQAN_THROW
* @headerfile <seqan/basic.h>
* @brief Replaces the C++ throw keyword.
*
* @signature SEQAN_THROW(Exception);
*
* When exceptions are disabled, i.e. AssertMacros#SEQAN_EXCEPTIONS is set to false, the macro turns into SEQAN_FAIL.
*
* @see ExceptionHandling#SEQAN_TRY
* @see ExceptionHandling#SEQAN_CATCH
* @see AssertMacros#SEQAN_FAIL
* @see Exception
*
* @section Examples
*
* See @link ExceptionHandling#SEQAN_TRY @endlink for a full example.
*/
#ifdef SEQAN_EXCEPTIONS
#define SEQAN_TRY try
#define SEQAN_CATCH(E) catch(E)
#define SEQAN_THROW(E) throw E
#define SEQAN_RETHROW throw
#else
#define SEQAN_TRY if (true)
#define SEQAN_CATCH(E) if (false)
//#define SEQAN_CATCH(E) for (E ; false; )
#define SEQAN_THROW(E) SEQAN_FAIL(#E)
#define SEQAN_RETHROW
#endif // #ifdef SEQAN_EXCEPTIONS
// ============================================================================
// Exceptions
// ============================================================================
// ----------------------------------------------------------------------------
// Basic Exception
// ----------------------------------------------------------------------------
/*!
* @class Exception
* @headerfile <seqan/basic.h>
* @brief Generic SeqAn exception.
* @signature typedef std::exception Exception;
*
* @fn Exception::Exception
* @brief Constructor.
*
* @signature Exception::Exception(msg);
* @param[in] msg The message as a <tt>std::string</tt>.
*/
typedef std::exception Exception;
// ----------------------------------------------------------------------------
// Exception BadAlloc
// ----------------------------------------------------------------------------
/*!
* @class BadAlloc
* @headerfile <seqan/basic.h>
* @brief Generic SeqAn exception.
* @signature typedef std::bad_alloc BadAlloc;
*
* @fn BadAlloc::BadAlloc
* @brief Constructor.
*
* @signature BadAlloc::BadAlloc(msg);
* @param[in] msg The message as a <tt>std::string</tt>.
*/
typedef std::bad_alloc BadAlloc;
// ----------------------------------------------------------------------------
// Exception BadCast
// ----------------------------------------------------------------------------
/*!
* @class BadCast
* @headerfile <seqan/basic.h>
* @brief Generic SeqAn exception.
* @signature typedef std::bad_cast BadCast;
*
* @fn BadCast::BadCast
* @brief Constructor.
*
* @signature BadCast::BadCast(msg);
* @param[in] msg The message as a <tt>std::string</tt>.
*/
typedef std::bad_cast BadCast;
// ----------------------------------------------------------------------------
// Exceptions Bad*
// ----------------------------------------------------------------------------
// NOTE(esiragusa): These exceptions can be introduced as long as we need them.
//typedef std::bad_exception BadException;
//typedef std::bad_typeid BadTypeId;
//typedef std::bad_function_call BadFunctionCall;
//typedef std::bad_weak_ptr BadWeakPtr;
// ----------------------------------------------------------------------------
// Exception RuntimeError
// ----------------------------------------------------------------------------
/*!
* @class RuntimeError
* @headerfile <seqan/basic.h>
* @brief Runtime error exception.
* @signature typedef std::runtime_error RuntimeError;
*
*
* @fn RuntimeError::RuntimeError
* @brief Constructor.
*
* @signature RuntimeError::RuntimeError(msg);
* @param[in] msg The message as a <tt>std::string</tt>.
*/
typedef std::runtime_error RuntimeError;
// ----------------------------------------------------------------------------
// Exception LogicError
// ----------------------------------------------------------------------------
// NOTE(esiragusa): Always prefer SEQAN_ASSERT to logic error exceptions.
//typedef std::logic_error LogicError;
// ============================================================================
// Metafunctions
// ============================================================================
// ----------------------------------------------------------------------------
// Metafunction ExceptionMessage
// ----------------------------------------------------------------------------
template <typename T, typename TSpec = void>
struct ExceptionMessage
{
static const std::string VALUE;
};
template <typename T, typename TSpec>
const std::string ExceptionMessage<T, TSpec>::VALUE;
// ----------------------------------------------------------------------------
// Function getExceptionMessage()
// ----------------------------------------------------------------------------
template <typename TFunctor, typename TContext>
inline std::string const &
getExceptionMessage(TFunctor const &, TContext const &)
{
return ExceptionMessage<TFunctor, TContext>::VALUE;
}
// ============================================================================
// Functors
// ============================================================================
// ----------------------------------------------------------------------------
// Functor AssertFunctor
// ----------------------------------------------------------------------------
template <typename TFunctor, typename TException, typename TContext = Nothing, bool RETURN_VALUE = false>
struct AssertFunctor
{
TFunctor func;
AssertFunctor() {}
AssertFunctor(TFunctor & func) :
func(func)
{}
std::string escapeChar(unsigned char val)
{
if (val <= '\r')
{
static const char * const escapeCodes[14] = {
"\\0", "\\1", "\\2", "\\3", "\\4", "\\5", "\\6", "\\a",
"\\b", "\\t", "\\n", "\\v", "\\f", "\\r" };
return std::string(escapeCodes[val]);
}
else if (' ' <= val && val < 128u)
return std::string() + (char)val;
else
{
char buffer[6]; // 5 + 1, e.g. "\0xff" + trailing zero
snprintf(buffer, 6, "\\%#2x", (unsigned)val);
return std::string(buffer);
}
}
template <typename TValue>
bool operator() (TValue const & val)
{
if (SEQAN_UNLIKELY(!func(val)))
throw TException(std::string("Unexpected character '") + escapeChar(val) + "' found. " +
getExceptionMessage(func, TContext()));
return RETURN_VALUE;
}
};
// ============================================================================
// Functions
// ============================================================================
// ----------------------------------------------------------------------------
// Function globalExceptionHandler()
// ----------------------------------------------------------------------------
#if defined(SEQAN_EXCEPTIONS) && defined(SEQAN_GLOBAL_EXCEPTION_HANDLER)
// Declare global exception handler.
inline static void globalExceptionHandler()
{
SEQAN_TRY
{
SEQAN_RETHROW;
}
SEQAN_CATCH(Exception & e)
{
SEQAN_FAIL("Uncaught exception of type %s: %s", toCString(Demangler<Exception>(e)), e.what());
}
SEQAN_CATCH(...)
{
SEQAN_FAIL("Uncaught exception of unknown type.\n");
}
}
// Install global exception handler.
static const std::terminate_handler _globalExceptionHandler SEQAN_UNUSED = std::set_terminate(globalExceptionHandler);
#endif // #if defined(SEQAN_EXCEPTIONS) && defined(SEQAN_GLOBAL_EXCEPTION_HANDLER)
} // namespace seqan
#endif // #ifndef SEQAN_BASIC_BASIC_EXCEPTION_H_
|