| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <string> |
| 14 |
#include <sstream> |
| 15 |
|
| 16 |
#include "eckit/log/CodeLocation.h" |
| 17 |
|
| 18 |
#include "atlas/library/config.h" |
| 19 |
|
| 20 |
namespace atlas { |
| 21 |
|
| 22 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 23 |
[[noreturn]] void throw_NotImplemented(const eckit::CodeLocation&); |
| 24 |
[[noreturn]] void throw_NotImplemented(const std::string&, const eckit::CodeLocation&); |
| 25 |
|
| 26 |
[[noreturn]] void throw_AssertionFailed(const std::string&); |
| 27 |
[[noreturn]] void throw_AssertionFailed(const std::string&, const eckit::CodeLocation&); |
| 28 |
[[noreturn]] void throw_AssertionFailed(const std::string&, const std::string&, const eckit::CodeLocation&); |
| 29 |
|
| 30 |
[[noreturn]] void throw_Exception(const std::string&); |
| 31 |
[[noreturn]] void throw_Exception(const std::string&, const eckit::CodeLocation&); |
| 32 |
|
| 33 |
[[noreturn]] void throw_CantOpenFile(const std::string&); |
| 34 |
[[noreturn]] void throw_CantOpenFile(const std::string&, const eckit::CodeLocation&); |
| 35 |
|
| 36 |
[[noreturn]] void throw_OutOfRange(const std::string& varname, idx_t index, idx_t size); |
| 37 |
[[noreturn]] void throw_OutOfRange(const std::string& varname, idx_t index, idx_t size, const eckit::CodeLocation&); |
| 38 |
[[noreturn]] void throw_OutOfRange(idx_t index, idx_t size); |
| 39 |
[[noreturn]] void throw_OutOfRange(idx_t index, idx_t size, const eckit::CodeLocation&); |
| 40 |
|
| 41 |
namespace detail { |
| 42 |
inline void Assert(bool success, const char* code, const char* file, int line, const char* func) { |
| 43 |
if (not success) { |
| 44 |
throw_AssertionFailed(code, eckit::CodeLocation(file, line, func)); |
| 45 |
} |
| 46 |
} |
| 47 |
inline void Assert(bool success, const char* code, const std::string& msg, const char* file, int line, |
| 48 |
const char* func) { |
| 49 |
if (not success) { |
| 50 |
throw_AssertionFailed(code, msg, eckit::CodeLocation(file, line, func)); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
} // namespace detail |
| 55 |
} // namespace atlas |
| 56 |
|
| 57 |
#include "atlas/library/detail/BlackMagic.h" |
| 58 |
|
| 59 |
#define ATLAS_NOTIMPLEMENTED ::atlas::throw_NotImplemented(Here()) |
| 60 |
|
| 61 |
|
| 62 |
#define ATLAS_ASSERT_NOMSG(a) \ |
| 63 |
static_cast<void>(0), (a) ? (void)0 : ::atlas::detail::Assert(bool(a), #a, __FILE__, __LINE__, __func__) |
| 64 |
#define ATLAS_ASSERT_MSG(a, m) \ |
| 65 |
static_cast<void>(0), (a) ? (void)0 : ::atlas::detail::Assert(bool(a), #a, m, __FILE__, __LINE__, __func__) |
| 66 |
|
| 67 |
#define ATLAS_ASSERT(...) __ATLAS_SPLICE(__ATLAS_ASSERT_, __ATLAS_NARG(__VA_ARGS__))(__VA_ARGS__) |
| 68 |
#define __ATLAS_ASSERT_1 ATLAS_ASSERT_NOMSG |
| 69 |
#define __ATLAS_ASSERT_2 ATLAS_ASSERT_MSG |
| 70 |
|
| 71 |
#define ATLAS_THROW_EXCEPTION(WHAT) \ |
| 72 |
do { \ |
| 73 |
std::ostringstream ss; \ |
| 74 |
ss << WHAT; \ |
| 75 |
::atlas::throw_Exception(ss.str(), Here()); \ |
| 76 |
} while (false) |
| 77 |
|
| 78 |
#endif |