telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include <stdexcept> |
| 8 | #include <string> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 9 | #include <sstream> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | |
| 11 | namespace armnn |
| 12 | { |
| 13 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 14 | struct CheckLocation |
| 15 | { |
| 16 | const char* m_Function; |
| 17 | const char* m_File; |
| 18 | unsigned int m_Line; |
| 19 | |
| 20 | CheckLocation(const char* func, |
| 21 | const char* file, |
| 22 | unsigned int line) |
| 23 | : m_Function{func} |
| 24 | , m_File{file} |
| 25 | , m_Line{line} |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | std::string AsString() const |
| 30 | { |
| 31 | std::stringstream ss; |
| 32 | ss << " at function " << m_Function |
| 33 | << " [" << m_File << ':' << m_Line << "]"; |
| 34 | return ss.str(); |
| 35 | } |
| 36 | |
| 37 | std::string FileLine() const |
| 38 | { |
| 39 | std::stringstream ss; |
| 40 | ss << " [" << m_File << ':' << m_Line << "]"; |
| 41 | return ss.str(); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | /// Base class for all ArmNN exceptions so that users can filter to just those. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 46 | class Exception : public std::exception |
| 47 | { |
| 48 | public: |
| 49 | explicit Exception(const std::string& message); |
| 50 | |
David Beck | 3cc9a62 | 2018-10-12 10:38:31 +0100 | [diff] [blame^] | 51 | // exception with context |
| 52 | explicit Exception(const std::string& message, |
| 53 | const CheckLocation& location); |
| 54 | |
| 55 | // preserving previous exception context |
| 56 | // and adding local context information |
| 57 | explicit Exception(const Exception& other, |
| 58 | const std::string& message, |
| 59 | const CheckLocation& location); |
| 60 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | virtual const char* what() const noexcept override; |
| 62 | |
| 63 | private: |
| 64 | std::string m_Message; |
| 65 | }; |
| 66 | |
| 67 | class ClRuntimeUnavailableException : public Exception |
| 68 | { |
| 69 | public: |
| 70 | using Exception::Exception; |
| 71 | }; |
| 72 | |
| 73 | class InvalidArgumentException : public Exception |
| 74 | { |
| 75 | public: |
| 76 | using Exception::Exception; |
| 77 | }; |
| 78 | |
| 79 | class FileNotFoundException : public Exception |
| 80 | { |
| 81 | public: |
| 82 | using Exception::Exception; |
| 83 | }; |
| 84 | |
| 85 | class ParseException : public Exception |
| 86 | { |
| 87 | public: |
| 88 | using Exception::Exception; |
| 89 | }; |
| 90 | |
| 91 | class UnimplementedException : public Exception |
| 92 | { |
| 93 | public: |
| 94 | using Exception::Exception; |
| 95 | UnimplementedException(); |
| 96 | }; |
| 97 | |
| 98 | class LayerValidationException : public Exception |
| 99 | { |
| 100 | using Exception::Exception; |
| 101 | }; |
| 102 | |
| 103 | class GraphValidationException : public Exception |
| 104 | { |
| 105 | using Exception::Exception; |
| 106 | }; |
| 107 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 108 | class BadOptionalAccessException : public Exception |
| 109 | { |
| 110 | using Exception::Exception; |
| 111 | }; |
| 112 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 113 | template <typename ExceptionType> |
| 114 | void ConditionalThrow(bool condition, const std::string& message) |
| 115 | { |
| 116 | if (!condition) |
| 117 | { |
| 118 | throw ExceptionType(message); |
| 119 | } |
| 120 | } |
| 121 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 122 | /// |
| 123 | /// ComparedType must support: |
| 124 | /// operator==(const ComparedType&) |
| 125 | /// operator<<(ostream&, const ComparedType&) |
| 126 | /// |
| 127 | template <typename ExceptionType, typename ComparedType> |
| 128 | void ConditionalThrowIfNotEqual(const std::string& message, |
| 129 | const ComparedType& leftHandSide, |
| 130 | const ComparedType& rightHandSide) |
| 131 | { |
| 132 | if (!(leftHandSide == rightHandSide)) |
| 133 | { |
| 134 | std::stringstream ss; |
| 135 | ss << message << " : " << leftHandSide << " != " << rightHandSide; |
| 136 | throw ExceptionType(ss.str()); |
| 137 | } |
| 138 | } |
| 139 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 140 | } // namespace armnn |
| 141 | |
| 142 | #define CHECK_LOCATION() armnn::CheckLocation(__func__, __FILE__, __LINE__) |