Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compiler / oops / include / oops / InternalExn.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __OOPS_INTERNAL_EXN_H__
18 #define __OOPS_INTERNAL_EXN_H__
19
20 #include <exception>
21 #include <string>
22
23 /// @ brief throw internal exception with message
24 #define INTERNAL_EXN(msg) throw oops::InternalExn(__FILE__, __LINE__, msg)
25
26 /// @ brief throw internal exception with message and value
27 #define INTERNAL_EXN_V(msg, val) throw oops::InternalExn(__FILE__, __LINE__, msg, val)
28
29 namespace oops
30 {
31
32 template <typename T> uint32_t to_uint32(T a) { return static_cast<uint32_t>(a); }
33
34 /**
35  * @brief Exception caused by internal error
36  *
37  * Note: Please use the above MACROs
38  */
39 class InternalExn : public std::exception
40 {
41 public:
42   InternalExn(const char *filename, const int line, const std::string &msg)
43       : _filename(filename), _line(to_uint32(line)), _msg(msg)
44   {
45     construct_full_msg();
46   }
47
48   explicit InternalExn(const char *filename, const int line, const std::string &msg, uint32_t val)
49       : _filename(filename), _line(to_uint32(line)), _msg(msg + ": " + std::to_string(val))
50   {
51     construct_full_msg();
52   }
53
54   explicit InternalExn(const char *filename, const int line, const std::string &msg,
55                        const std::string &val)
56       : _filename(filename), _line(to_uint32(line)), _msg(msg + ": " + val)
57   {
58     construct_full_msg();
59   }
60
61   const char *what() const noexcept override { return _full_msg.c_str(); }
62
63 private:
64   const std::string _filename;
65   const uint32_t _line;
66   const std::string _msg;
67
68 private:
69   void construct_full_msg()
70   {
71     _full_msg =
72         "Internal Exception. " + _msg + " [" + _filename + ":" + std::to_string(_line) + "]";
73   }
74
75   std::string _full_msg;
76 };
77
78 } // namespace oops
79
80 #endif // __OOPS_INTERNAL_EXN_H__