Publishing 2019 R2 content (#223)
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / include / api_impl.h
1 /*
2 // Copyright (c) 2016 Intel Corporation
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 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #pragma once
19
20 #include "api/C/cldnn.h"
21 #include "api/CPP/cldnn_defs.h"
22
23 #include <functional>
24 #include <stdexcept>
25 #include <string>
26
27 #define API_CAST(api_type, impl_type)                                                        \
28     inline api_type api_cast(impl_type* value) { return reinterpret_cast<api_type>(value); } \
29     inline impl_type* api_cast(api_type value) { return reinterpret_cast<impl_type*>(value); }
30
31 namespace cldnn {
32 struct last_err {
33     /// @breif Sets the message of last error
34     void set_last_error_message(const std::string& msg) { _msg = msg; }
35
36     void set_last_exception(const std::exception& ex) { _msg = ex.what(); }
37
38     /// @breif Gets the message of last error
39     const std::string& get_last_error_message() { return _msg; }
40     static last_err& instance();
41
42 private:
43     std::string _msg;
44     last_err() : _msg("Operation succeed") {}
45 };
46
47 // float <--> half convertors
48 float half_to_float(uint16_t value);
49 uint16_t float_to_half(float value);
50 }  // namespace cldnn
51
52 template <typename T>
53 T exception_handler(cldnn_status default_error,
54                     cldnn_status* status,
55                     const T& default_result,
56                     std::function<T()> func) {
57     // NOTE for implementer: status should not be modified after successful func() call
58     try {
59         if (status)
60             *status = CLDNN_SUCCESS;
61         return func();
62     } catch (const cldnn::error& err) {
63         if (status)
64             *status = err.status();
65         cldnn::last_err::instance().set_last_exception(err);
66
67 #ifndef NDEBUG
68         static_cast<void>(default_result);
69         throw;
70 #endif
71     } catch (const std::exception& err) {
72         if (status)
73             *status = default_error;
74         cldnn::last_err::instance().set_last_exception(err);
75
76 #ifndef NDEBUG
77         static_cast<void>(default_result);
78         throw;
79 #endif
80     } catch (...) {
81         if (status)
82             *status = default_error;
83         cldnn::last_err::instance().set_last_error_message("error unknown");
84
85 #ifndef NDEBUG
86         static_cast<void>(default_result);
87         throw;
88 #endif
89     }
90
91 #ifdef NDEBUG
92     return default_result;
93 #endif
94 }
95
96 inline void exception_handler(cldnn_status default_error, cldnn_status* status, std::function<void()> func) {
97     // NOTE for implementer: status should not be modified after successful func() call
98     try {
99         if (status)
100             *status = CLDNN_SUCCESS;
101         func();
102     } catch (const cldnn::error& err) {
103         if (status)
104             *status = err.status();
105         cldnn::last_err::instance().set_last_exception(err);
106 #ifndef NDEBUG
107         throw;
108 #endif
109     } catch (const std::exception& err) {
110         if (status)
111             *status = default_error;
112         cldnn::last_err::instance().set_last_exception(err);
113
114 #ifndef NDEBUG
115         throw;
116 #endif
117     } catch (...) {
118         if (status)
119             *status = default_error;
120         cldnn::last_err::instance().set_last_error_message("error unknown");
121 #ifndef NDEBUG
122         throw;
123 #endif
124     }
125 }