DPC++ Runtime
Runtime libraries for oneAPI DPC++
exception.cpp
Go to the documentation of this file.
1 //==---------------- exception.cpp - SYCL exception ------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // 4.9.2 Exception Class Interface
11 #include <sycl/context.hpp>
12 #include <sycl/exception.hpp>
13 
14 #include <cstring>
15 
16 namespace sycl {
17 inline namespace _V1 {
18 
19 exception::exception(std::error_code EC, const char *Msg)
20  : exception(EC, nullptr, Msg) {}
21 
22 exception::exception(std::error_code EC, const std::string &Msg)
23  : exception(EC, nullptr, Msg) {}
24 
25 // new SYCL 2020 constructors
26 exception::exception(std::error_code EC) : exception(EC, nullptr, "") {}
27 
28 exception::exception(int EV, const std::error_category &ECat,
29  const std::string &WhatArg)
30  : exception({EV, ECat}, nullptr, WhatArg) {}
31 
32 exception::exception(int EV, const std::error_category &ECat,
33  const char *WhatArg)
34  : exception({EV, ECat}, nullptr, std::string(WhatArg)) {}
35 
36 exception::exception(int EV, const std::error_category &ECat)
37  : exception({EV, ECat}, nullptr, "") {}
38 
39 exception::exception(context Ctx, std::error_code EC,
40  const std::string &WhatArg)
41  : exception(EC, std::make_shared<context>(Ctx), WhatArg) {}
42 
43 exception::exception(context Ctx, std::error_code EC, const char *WhatArg)
44  : exception(Ctx, EC, std::string(WhatArg)) {}
45 
46 exception::exception(context Ctx, std::error_code EC)
47  : exception(Ctx, EC, "") {}
48 
49 exception::exception(context Ctx, int EV, const std::error_category &ECat,
50  const char *WhatArg)
51  : exception(Ctx, {EV, ECat}, std::string(WhatArg)) {}
52 
53 exception::exception(context Ctx, int EV, const std::error_category &ECat,
54  const std::string &WhatArg)
55  : exception(Ctx, {EV, ECat}, WhatArg) {}
56 
57 exception::exception(context Ctx, int EV, const std::error_category &ECat)
58  : exception(Ctx, EV, ECat, "") {}
59 
60 // protected base constructor for all SYCL 2020 constructors
61 exception::exception(std::error_code EC, std::shared_ptr<context> SharedPtrCtx,
62  const std::string &WhatArg)
63  : MMsg(std::make_shared<std::string>(WhatArg)),
64  MPIErr(PI_ERROR_INVALID_VALUE), MContext(SharedPtrCtx), MErrC(EC) {
66 }
67 
69 
70 const std::error_code &exception::code() const noexcept { return MErrC; }
71 
72 const std::error_category &exception::category() const noexcept {
73  return code().category();
74 }
75 
76 const char *exception::what() const noexcept { return MMsg->c_str(); }
77 
78 bool exception::has_context() const noexcept { return (MContext != nullptr); }
79 
81  if (!has_context())
82  throw sycl::exception(sycl::errc::invalid);
83 
84  return *MContext;
85 }
86 
87 cl_int exception::get_cl_code() const { return MPIErr; }
88 
89 const std::error_category &sycl_category() noexcept {
90  static const detail::SYCLCategory SYCLCategoryObj;
91  return SYCLCategoryObj;
92 }
93 
94 std::error_code make_error_code(sycl::errc Err) noexcept {
95  return {static_cast<int>(Err), sycl_category()};
96 }
97 
98 } // namespace _V1
99 } // namespace sycl
sycl::_V1::detail::SYCLCategory
Definition: exception.hpp:58
sycl::_V1::opencl::cl_int
std::int32_t cl_int
Definition: aliases.hpp:134
sycl::_V1::sycl_category
const std::error_category & sycl_category() noexcept
Definition: exception.cpp:89
sycl::_V1::exception::get_context
context get_context() const
Definition: exception.cpp:80
sycl::_V1::make_error_code
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
Definition: exception.cpp:94
sycl::_V1::detail::GlobalHandler::instance
static GlobalHandler & instance()
Definition: global_handler.cpp:125
sycl::_V1::exception::~exception
virtual ~exception()
Definition: exception.cpp:68
sycl
Definition: access.hpp:18
sycl::_V1::exception::has_context
bool has_context() const noexcept
Definition: exception.cpp:78
sycl::_V1::exception::get_cl_code
cl_int get_cl_code() const
Definition: exception.cpp:87
sycl::_V1::exception::code
const std::error_code & code() const noexcept
Definition: exception.cpp:70
sycl::_V1::detail::GlobalHandler::TraceEventXPTI
void TraceEventXPTI(const char *Message)
Definition: global_handler.cpp:87
sycl::_V1::exception
Definition: exception.hpp:68
global_handler.hpp
sycl::_V1::errc
errc
Definition: exception.hpp:32
sycl::_V1::exception::exception
exception(std::error_code, const char *Msg)
Definition: exception.cpp:19
sycl::_V1::exception::category
const std::error_category & category() const noexcept
Definition: exception.cpp:72
exception.hpp
std
Definition: accessor.hpp:4139
context.hpp
sycl::_V1::exception::what
const char * what() const noexcept final
Definition: exception.cpp:76
sycl::_V1::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51