Release 18.08
[platform/upstream/armnn.git] / include / armnn / Exceptions.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5 #pragma once
6
7 #include <stdexcept>
8 #include <string>
9 #include <sstream>
10
11 namespace armnn
12 {
13
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.
46 class Exception : public std::exception
47 {
48 public:
49     explicit Exception(const std::string& message);
50
51     virtual const char* what() const noexcept override;
52
53 private:
54     std::string m_Message;
55 };
56
57 class ClRuntimeUnavailableException : public Exception
58 {
59 public:
60     using Exception::Exception;
61 };
62
63 class InvalidArgumentException : public Exception
64 {
65 public:
66     using Exception::Exception;
67 };
68
69 class FileNotFoundException : public Exception
70 {
71 public:
72     using Exception::Exception;
73 };
74
75 class ParseException : public Exception
76 {
77 public:
78     using Exception::Exception;
79 };
80
81 class UnimplementedException : public Exception
82 {
83 public:
84     using Exception::Exception;
85     UnimplementedException();
86 };
87
88 class LayerValidationException : public Exception
89 {
90     using Exception::Exception;
91 };
92
93 class GraphValidationException : public Exception
94 {
95     using Exception::Exception;
96 };
97
98 template <typename ExceptionType>
99 void ConditionalThrow(bool condition, const std::string& message)
100 {
101     if (!condition)
102     {
103         throw ExceptionType(message);
104     }
105 }
106
107 ///
108 /// ComparedType must support:
109 ///   operator==(const ComparedType&)
110 ///   operator<<(ostream&, const ComparedType&)
111 ///
112 template <typename ExceptionType, typename ComparedType>
113 void ConditionalThrowIfNotEqual(const std::string& message,
114                                 const ComparedType& leftHandSide,
115                                 const ComparedType& rightHandSide)
116 {
117     if (!(leftHandSide == rightHandSide))
118     {
119         std::stringstream ss;
120         ss << message << " : " << leftHandSide << " != " << rightHandSide;
121         throw ExceptionType(ss.str());
122     }
123 }
124
125 } // namespace armnn
126
127 #define CHECK_LOCATION() armnn::CheckLocation(__func__, __FILE__, __LINE__)