| 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 |
#include <cstdlib> |
| 12 |
|
| 13 |
#include "eckit/exception/Exceptions.h" |
| 14 |
|
| 15 |
#include "atlas/runtime/Exception.h" |
| 16 |
|
| 17 |
namespace atlas { |
| 18 |
|
| 19 |
void throw_NotImplemented(const eckit::CodeLocation& loc) { |
| 20 |
throw eckit::NotImplemented(loc); |
| 21 |
} |
| 22 |
|
| 23 |
void throw_NotImplemented(const std::string& msg, const eckit::CodeLocation& loc) { |
| 24 |
throw eckit::NotImplemented(msg, loc); |
| 25 |
} |
| 26 |
|
| 27 |
void throw_AssertionFailed(const std::string& msg) { |
| 28 |
#if ATLAS_ECKIT_VERSION_AT_LEAST(1, 18, 5) |
| 29 |
throw_AssertionFailed(msg, eckit::CodeLocation{}); |
| 30 |
#else |
| 31 |
throw eckit::AssertionFailed(msg); |
| 32 |
#endif |
| 33 |
} |
| 34 |
|
| 35 |
void throw_AssertionFailed(const std::string& msg, const eckit::CodeLocation& loc) { |
| 36 |
#if ATLAS_ECKIT_VERSION_AT_LEAST(1, 18, 5) |
| 37 |
if (loc) { |
| 38 |
eckit::handle_assert(msg, loc); |
| 39 |
} |
| 40 |
else { |
| 41 |
eckit::handle_assert(msg, eckit::CodeLocation{"unspecified file", 0, "unspecified function"}); |
| 42 |
} |
| 43 |
std::abort(); // should never reach here, but makes sure we will never return from this |
| 44 |
#else |
| 45 |
throw eckit::AssertionFailed(msg, loc); |
| 46 |
#endif |
| 47 |
} |
| 48 |
|
| 49 |
void throw_AssertionFailed(const std::string& code, const std::string& msg, const eckit::CodeLocation& loc) { |
| 50 |
std::ostringstream ss; |
| 51 |
ss << " [[ " << code << " ]]\n" << msg; |
| 52 |
throw_AssertionFailed(ss.str(), loc); |
| 53 |
} |
| 54 |
|
| 55 |
void throw_Exception(const std::string& msg) { |
| 56 |
throw eckit::Exception(msg); |
| 57 |
} |
| 58 |
|
| 59 |
void throw_Exception(const std::string& msg, const eckit::CodeLocation& loc) { |
| 60 |
throw eckit::Exception(msg, loc); |
| 61 |
} |
| 62 |
|
| 63 |
void throw_CantOpenFile(const std::string& file) { |
| 64 |
throw eckit::CantOpenFile(file); |
| 65 |
} |
| 66 |
|
| 67 |
void throw_CantOpenFile(const std::string& file, const eckit::CodeLocation& loc) { |
| 68 |
throw eckit::CantOpenFile(file, loc); |
| 69 |
} |
| 70 |
|
| 71 |
void throw_OutOfRange(const std::string& varname, idx_t index, idx_t size) { |
| 72 |
std::ostringstream ss; |
| 73 |
ss << "OutOfRange: Tried to access " << varname << " index " << index << " but maximum allowed index is " |
| 74 |
<< size - 1; |
| 75 |
throw eckit::Exception(ss.str()); |
| 76 |
} |
| 77 |
|
| 78 |
void throw_OutOfRange(const std::string& varname, idx_t index, idx_t size, const eckit::CodeLocation& loc) { |
| 79 |
std::ostringstream ss; |
| 80 |
if (index < 0) { |
| 81 |
ss << "OutOfRange: Tried to access index " << index << " of " << varname << " of size " << size |
| 82 |
<< ". Negative index is invalid."; |
| 83 |
} |
| 84 |
else { |
| 85 |
ss << "OutOfRange: Tried to access index " << index << " of " << varname << " of size " << size |
| 86 |
<< ". The index exceeds the maximum value " << size-1; |
| 87 |
} |
| 88 |
throw eckit::Exception(ss.str(), loc); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
void throw_OutOfRange(idx_t index, idx_t size) { |
| 93 |
std::ostringstream ss; |
| 94 |
ss << "OutOfRange: Tried to access index " << index << " but maximum allowed index is " << size - 1; |
| 95 |
throw eckit::Exception(ss.str()); |
| 96 |
} |
| 97 |
|
| 98 |
void throw_OutOfRange(idx_t index, idx_t size, const eckit::CodeLocation& loc) { |
| 99 |
std::ostringstream ss; |
| 100 |
ss << "OutOfRange: Tried to access index " << index << " but maximum allowed index is " << size - 1; |
| 101 |
throw eckit::Exception(ss.str(), loc); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
} // namespace atlas |