blob: 29d874cd0598fa5e981c8a68d8057a9b24da558e [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#pragma once
6
7#include <stdexcept>
8#include <string>
surmeh013537c2c2018-05-18 16:31:43 +01009#include <sstream>
telsoa014fcda012018-03-09 14:13:49 +000010
11namespace armnn
12{
13
telsoa01c577f2c2018-08-31 09:22:23 +010014struct 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.
telsoa014fcda012018-03-09 14:13:49 +000046class Exception : public std::exception
47{
48public:
49 explicit Exception(const std::string& message);
50
David Beck3cc9a622018-10-12 10:38:31 +010051 // 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
telsoa014fcda012018-03-09 14:13:49 +000061 virtual const char* what() const noexcept override;
62
63private:
64 std::string m_Message;
65};
66
67class ClRuntimeUnavailableException : public Exception
68{
69public:
70 using Exception::Exception;
71};
72
73class InvalidArgumentException : public Exception
74{
75public:
76 using Exception::Exception;
77};
78
79class FileNotFoundException : public Exception
80{
81public:
82 using Exception::Exception;
83};
84
85class ParseException : public Exception
86{
87public:
88 using Exception::Exception;
89};
90
91class UnimplementedException : public Exception
92{
93public:
94 using Exception::Exception;
95 UnimplementedException();
96};
97
98class LayerValidationException : public Exception
99{
100 using Exception::Exception;
101};
102
103class GraphValidationException : public Exception
104{
105 using Exception::Exception;
106};
107
David Beck5eec11d2018-10-04 15:43:17 +0100108class BadOptionalAccessException : public Exception
109{
110 using Exception::Exception;
111};
112
telsoa014fcda012018-03-09 14:13:49 +0000113template <typename ExceptionType>
114void ConditionalThrow(bool condition, const std::string& message)
115{
116 if (!condition)
117 {
118 throw ExceptionType(message);
119 }
120}
121
surmeh013537c2c2018-05-18 16:31:43 +0100122///
123/// ComparedType must support:
124/// operator==(const ComparedType&)
125/// operator<<(ostream&, const ComparedType&)
126///
127template <typename ExceptionType, typename ComparedType>
128void 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
telsoa01c577f2c2018-08-31 09:22:23 +0100140} // namespace armnn
141
142#define CHECK_LOCATION() armnn::CheckLocation(__func__, __FILE__, __LINE__)