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 
20  : exception(EC, nullptr, Msg) {}
21 
22 // new SYCL 2020 constructors
24 
25 exception::exception(int EV, const std::error_category &ECat,
26  const char *WhatArg)
27  : exception({EV, ECat}, nullptr, std::string(WhatArg)) {}
28 
29 exception::exception(int EV, const std::error_category &ECat)
30  : exception({EV, ECat}, nullptr, "") {}
31 
33  const std::string &WhatArg)
34  : exception(EC, std::make_shared<context>(Ctx), WhatArg) {}
35 
36 exception::exception(context Ctx, std::error_code EC, const char *WhatArg)
37  : exception(Ctx, EC, std::string(WhatArg)) {}
38 
40  : exception(Ctx, EC, "") {}
41 
42 exception::exception(context Ctx, int EV, const std::error_category &ECat,
43  const char *WhatArg)
44  : exception(Ctx, {EV, ECat}, std::string(WhatArg)) {}
45 
46 exception::exception(context Ctx, int EV, const std::error_category &ECat,
47  const std::string &WhatArg)
48  : exception(Ctx, {EV, ECat}, WhatArg) {}
49 
50 exception::exception(context Ctx, int EV, const std::error_category &ECat)
51  : exception(Ctx, EV, ECat, "") {}
52 
53 // protected base constructor for all SYCL 2020 constructors
54 exception::exception(std::error_code EC, std::shared_ptr<context> SharedPtrCtx,
55  const char *WhatArg)
56  : MMsg(std::make_shared<std::string>(WhatArg)),
57  MPIErr(PI_ERROR_INVALID_VALUE), MContext(SharedPtrCtx), MErrC(EC) {
59 }
60 
62 
63 const std::error_code &exception::code() const noexcept { return MErrC; }
64 
65 const std::error_category &exception::category() const noexcept {
66  return code().category();
67 }
68 
69 const char *exception::what() const noexcept { return MMsg->c_str(); }
70 
71 bool exception::has_context() const noexcept { return (MContext != nullptr); }
72 
74  if (!has_context())
75  throw sycl::exception(sycl::errc::invalid);
76 
77  return *MContext;
78 }
79 
80 cl_int exception::get_cl_code() const { return MPIErr; }
81 
82 const std::error_category &sycl_category() noexcept {
83  static const detail::SYCLCategory SYCLCategoryObj;
84  return SYCLCategoryObj;
85 }
86 
88  return {static_cast<int>(Err), sycl_category()};
89 }
90 
91 } // namespace _V1
92 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51
void TraceEventXPTI(const char *Message)
static GlobalHandler & instance()
bool has_context() const noexcept
Definition: exception.cpp:71
context get_context() const
Definition: exception.cpp:73
const char * what() const noexcept final
Definition: exception.cpp:69
const std::error_code & code() const noexcept
Definition: exception.cpp:63
cl_int get_cl_code() const
Definition: exception.cpp:80
virtual ~exception()
Definition: exception.cpp:61
const std::error_category & category() const noexcept
Definition: exception.cpp:65
exception(std::error_code, const char *Msg)
Definition: exception.cpp:19
std::int32_t cl_int
Definition: aliases.hpp:134
const std::error_category & sycl_category() noexcept
Definition: exception.cpp:82
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
Definition: exception.cpp:87
Definition: access.hpp:18
error_code
Definition: defs.hpp:59
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324