aboutsummaryrefslogtreecommitdiff
path: root/include/armnn/Exceptions.hpp
blob: f8e0b430a62ddf80c20caf452295a153802ac8db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#pragma once

#include <sstream>
#include <stdexcept>
#include <string>

namespace armnn
{

struct CheckLocation
{
    const char* m_Function;
    const char* m_File;
    unsigned int m_Line;

    CheckLocation(const char* func,
                  const char* file,
                  unsigned int line)
    : m_Function{func}
    , m_File{file}
    , m_Line{line}
    {
    }

    std::string AsString() const
    {
        std::stringstream ss;
        ss << " at function " << m_Function
           << " [" << m_File << ':' << m_Line << "]";
        return ss.str();
    }

    std::string FileLine() const
    {
        std::stringstream ss;
        ss << " [" << m_File << ':' << m_Line << "]";
        return ss.str();
    }
};

/// Base class for all ArmNN exceptions so that users can filter to just those.
class Exception : public std::exception
{
public:
    explicit Exception(const std::string& message);

    // exception with context
    explicit Exception(const std::string& message,
                       const CheckLocation& location);

    // preserving previous exception context
    // and adding local context information
    explicit Exception(const Exception& other,
                       const std::string& message,
                       const CheckLocation& location);

    virtual const char* what() const noexcept override;

private:
    std::string m_Message;
};

class ClRuntimeUnavailableException : public Exception
{
public:
    using Exception::Exception;
};

class InvalidArgumentException : public Exception
{
public:
    using Exception::Exception;
};

class FileNotFoundException : public Exception
{
public:
    using Exception::Exception;
};

class ParseException : public Exception
{
public:
    using Exception::Exception;
};

class UnimplementedException : public Exception
{
public:
    using Exception::Exception;
    UnimplementedException();
};

class LayerValidationException : public Exception
{
    using Exception::Exception;
};

class GraphValidationException : public Exception
{
    using Exception::Exception;
};

class BadOptionalAccessException : public Exception
{
    using Exception::Exception;
};

class RuntimeException : public Exception
{
    using Exception::Exception;
};

class MemoryImportException : public Exception
{
    using Exception::Exception;
};

class MemoryExportException : public Exception
{
    using Exception::Exception;
};

template <typename ExceptionType>
void ConditionalThrow(bool condition, const std::string& message)
{
    if (!condition)
    {
        throw ExceptionType(message);
    }
}

///
/// ComparedType must support:
///   operator==(const ComparedType&)
///   operator<<(ostream&, const ComparedType&)
///
template <typename ExceptionType, typename ComparedType>
void ConditionalThrowIfNotEqual(const std::string& message,
                                const ComparedType& leftHandSide,
                                const ComparedType& rightHandSide)
{
    if (!(leftHandSide == rightHandSide))
    {
        std::stringstream ss;
        ss << message << " : " << leftHandSide << " != " << rightHandSide;
        throw ExceptionType(ss.str());
    }
}

} // namespace armnn

#define CHECK_LOCATION() armnn::CheckLocation(__func__, __FILE__, __LINE__)