File: event_error.h

package info (click to toggle)
libthrust 1.17.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,900 kB
  • sloc: ansic: 29,519; cpp: 23,989; python: 1,421; sh: 811; perl: 460; makefile: 112
file content (164 lines) | stat: -rw-r--r-- 4,450 bytes parent folder | download | duplicates (4)
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
158
159
160
161
162
163
164
/*
 *  Copyright 2008-2018 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/// \file thrust/detail/event_error.h
/// \brief \c thrust::future and thrust::future error handling types and codes.

#pragma once

#include <thrust/detail/config.h>
#include <thrust/detail/cpp14_required.h>

#if THRUST_CPP_DIALECT >= 2014

#include <thrust/detail/type_traits.h>
#include <thrust/system/error_code.h>

#include <stdexcept>

THRUST_NAMESPACE_BEGIN

enum class event_errc
{
  unknown_event_error
, no_state
, no_content
, last_event_error
};

/// \return <tt>error_code(static_cast<int>(e), event_category())</tt>
inline error_code make_error_code(event_errc e);

/// \return <tt>error_condition(static_cast<int>(e), event_category())</tt>.
inline error_condition make_error_condition(event_errc e);

struct event_error_category : error_category
{
  event_error_category() = default;

  virtual char const* name() const
  {
    return "event";
  }

  virtual std::string message(int ev) const
  {
    switch (static_cast<event_errc>(ev))
    {
      case event_errc::no_state:
      {
        return "no_state: an operation that requires an event or future to have "
               "a stream or content has been performed on a event or future "
               "without either, e.g. a moved-from or default constructed event "
               "or future (an event or future may have been consumed more than "
               "once)";
      }
      case event_errc::no_content:
      {
        return "no_content: an operation that requires a future to have content "
               "has been performed on future without any, e.g. a moved-from, "
               "default constructed, or `thrust::new_stream` constructed future "
               "(a future may have been consumed more than once)";
      }
      default:
      {
        return "unknown_event_error: an unknown error with a future "
               "object has occurred";
      }
    };
  }

  virtual error_condition default_error_condition(int ev) const
  {
    if (
         event_errc::last_event_error
         >
         static_cast<event_errc>(ev)
       )
      return make_error_condition(static_cast<event_errc>(ev));

    return system_category().default_error_condition(ev);
  }
};

/// Obtains a reference to the static error category object for the errors
/// related to futures and promises. The object is required to override the
/// virtual function error_category::name() to return a pointer to the string
/// "event". It is used to identify error codes provided in the
/// exceptions of type event_error.
inline error_category const& event_category()
{
  static const event_error_category result;
  return result;
}

namespace system
{
/// Specialization of \p is_error_code_enum for \p event_errc.
template<> struct is_error_code_enum<event_errc> : true_type {};
} // end system

/// \return <tt>error_code(static_cast<int>(e), event_category())</tt>
inline error_code make_error_code(event_errc e)
{
  return error_code(static_cast<int>(e), event_category());
}

/// \return <tt>error_condition(static_cast<int>(e), event_category())</tt>.
inline error_condition make_error_condition(event_errc e)
{
  return error_condition(static_cast<int>(e), event_category());
}

struct event_error : std::logic_error
{
  __host__
  explicit event_error(error_code ec)
    : std::logic_error(ec.message()), ec_(ec)
  {}

  __host__
  explicit event_error(event_errc e)
    : event_error(make_error_code(e))
  {}

  __host__
  error_code const& code() const noexcept
  {
    return ec_;
  }

  __host__
  virtual ~event_error() noexcept {}

private:
  error_code ec_;
};

inline bool operator==(event_error const& lhs, event_error const& rhs) noexcept
{
  return lhs.code() == rhs.code();
}

inline bool operator<(event_error const& lhs, event_error const& rhs) noexcept
{
  return lhs.code() < rhs.code();
}

THRUST_NAMESPACE_END

#endif // C++14