~ubuntu-branches/ubuntu/trusty/libthrust/trusty

« back to all changes in this revision

Viewing changes to system/error_code.h

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Beckmann
  • Date: 2011-05-28 09:32:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110528093248-np3euv5sj7fw3nyv
Tags: upstream-1.4.0
ImportĀ upstreamĀ versionĀ 1.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright 2008-2011 NVIDIA 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
/*! \file error_code.h
 
19
 *  \brief An object used to hold error values, such as those originating from the
 
20
 *         operating system or other low-level application program interfaces.
 
21
 */
 
22
 
 
23
#pragma once
 
24
 
 
25
#include <thrust/detail/config.h>
 
26
#include <thrust/detail/type_traits.h>
 
27
#include <thrust/system/detail/errno.h>
 
28
#include <iostream>
 
29
 
 
30
namespace thrust
 
31
{
 
32
 
 
33
namespace system
 
34
{
 
35
 
 
36
 
 
37
/*! \addtogroup system
 
38
 *  \{
 
39
 */
 
40
 
 
41
class error_condition;
 
42
class error_code;
 
43
 
 
44
/*! A metafunction returning whether or not the parameter is an \p error_code enum.
 
45
 */
 
46
template<typename T> struct is_error_code_enum : public thrust::detail::false_type {};
 
47
 
 
48
/*! A metafunction returning whether or not the parameter is an \p error_condition enum.
 
49
 */
 
50
template<typename T> struct is_error_condition_enum : public thrust::detail::false_type {};
 
51
 
 
52
 
 
53
// XXX N3092 prefers enum class errc { ... }
 
54
namespace errc
 
55
{
 
56
 
 
57
enum errc_t
 
58
{
 
59
  address_family_not_supported       = detail::eafnosupport,
 
60
  address_in_use                     = detail::eaddrinuse,
 
61
  address_not_available              = detail::eaddrnotavail,
 
62
  already_connected                  = detail::eisconn,
 
63
  argument_list_too_long             = detail::e2big,
 
64
  argument_out_of_domain             = detail::edom,
 
65
  bad_address                        = detail::efault,
 
66
  bad_file_descriptor                = detail::ebadf,
 
67
  bad_message                        = detail::ebadmsg,
 
68
  broken_pipe                        = detail::epipe,
 
69
  connection_aborted                 = detail::econnaborted,
 
70
  connection_already_in_progress     = detail::ealready,
 
71
  connection_refused                 = detail::econnrefused,
 
72
  connection_reset                   = detail::econnreset,
 
73
  cross_device_link                  = detail::exdev,
 
74
  destination_address_required       = detail::edestaddrreq,
 
75
  device_or_resource_busy            = detail::ebusy,
 
76
  directory_not_empty                = detail::enotempty,
 
77
  executable_format_error            = detail::enoexec,
 
78
  file_exists                        = detail::eexist,
 
79
  file_too_large                     = detail::efbig,
 
80
  filename_too_long                  = detail::enametoolong,
 
81
  function_not_supported             = detail::enosys,
 
82
  host_unreachable                   = detail::ehostunreach,
 
83
  identifier_removed                 = detail::eidrm,
 
84
  illegal_byte_sequence              = detail::eilseq,
 
85
  inappropriate_io_control_operation = detail::enotty,
 
86
  interrupted                        = detail::eintr,
 
87
  invalid_argument                   = detail::einval,
 
88
  invalid_seek                       = detail::espipe,
 
89
  io_error                           = detail::eio,
 
90
  is_a_directory                     = detail::eisdir,
 
91
  message_size                       = detail::emsgsize,
 
92
  network_down                       = detail::enetdown,
 
93
  network_reset                      = detail::enetreset,
 
94
  network_unreachable                = detail::enetunreach,
 
95
  no_buffer_space                    = detail::enobufs,
 
96
  no_child_process                   = detail::echild,
 
97
  no_link                            = detail::enolink,
 
98
  no_lock_available                  = detail::enolck,
 
99
  no_message_available               = detail::enodata,
 
100
  no_message                         = detail::enomsg,
 
101
  no_protocol_option                 = detail::enoprotoopt,
 
102
  no_space_on_device                 = detail::enospc,
 
103
  no_stream_resources                = detail::enosr,
 
104
  no_such_device_or_address          = detail::enxio,
 
105
  no_such_device                     = detail::enodev,
 
106
  no_such_file_or_directory          = detail::enoent,
 
107
  no_such_process                    = detail::esrch,
 
108
  not_a_directory                    = detail::enotdir,
 
109
  not_a_socket                       = detail::enotsock,
 
110
  not_a_stream                       = detail::enostr,
 
111
  not_connected                      = detail::enotconn,
 
112
  not_enough_memory                  = detail::enomem,
 
113
  not_supported                      = detail::enotsup,
 
114
  operation_canceled                 = detail::ecanceled,
 
115
  operation_in_progress              = detail::einprogress,
 
116
  operation_not_permitted            = detail::eperm,
 
117
  operation_not_supported            = detail::eopnotsupp,
 
118
  operation_would_block              = detail::ewouldblock,
 
119
  owner_dead                         = detail::eownerdead,
 
120
  permission_denied                  = detail::eacces,
 
121
  protocol_error                     = detail::eproto,
 
122
  protocol_not_supported             = detail::eprotonosupport,
 
123
  read_only_file_system              = detail::erofs,
 
124
  resource_deadlock_would_occur      = detail::edeadlk,
 
125
  resource_unavailable_try_again     = detail::eagain,
 
126
  result_out_of_range                = detail::erange,
 
127
  state_not_recoverable              = detail::enotrecoverable,
 
128
  stream_timeout                     = detail::etime,
 
129
  text_file_busy                     = detail::etxtbsy,
 
130
  timed_out                          = detail::etimedout,
 
131
  too_many_files_open_in_system      = detail::enfile,
 
132
  too_many_files_open                = detail::emfile,
 
133
  too_many_links                     = detail::emlink,
 
134
  too_many_symbolic_link_levels      = detail::eloop,
 
135
  value_too_large                    = detail::eoverflow,
 
136
  wrong_protocol_type                = detail::eprototype,
 
137
}; // end errc_t
 
138
 
 
139
} // end namespace errc
 
140
 
 
141
 
 
142
template<> struct is_error_condition_enum<errc::errc_t> : public thrust::detail::true_type {};
 
143
 
 
144
 
 
145
// [19.5.1.1] class error_category
 
146
 
 
147
/*! \brief The class \p error_category serves as a base class for types used to identify the
 
148
 *         source and encoding of a particular category of error code. Classes may be derived
 
149
 *         from \p error_category to support categories of errors in addition to those defined
 
150
 *         in the C++ International Standard.
 
151
 */
 
152
class error_category
 
153
{
 
154
  public:
 
155
    /*! Destructor does nothing.
 
156
     */
 
157
    inline virtual ~error_category(void);
 
158
 
 
159
    // XXX enable upon c++0x
 
160
    // error_category(const error_category &) = delete;
 
161
    // error_category &operator=(const error_category &) = delete;
 
162
 
 
163
    /*! \return A string naming the error category.
 
164
     */
 
165
    inline virtual const char *name(void) const = 0;
 
166
 
 
167
    /*! \return \p error_condition(ev, *this).
 
168
     */
 
169
    inline virtual error_condition default_error_condition(int ev) const;
 
170
 
 
171
    /*! \return <tt>default_error_condition(code) == condition</tt>
 
172
     */
 
173
    inline virtual bool equivalent(int code, const error_condition &condition) const;
 
174
 
 
175
    /*! \return <tt>*this == code.category() && code.value() == condition</tt>
 
176
     */
 
177
    inline virtual bool equivalent(const error_code &code, int condition) const;
 
178
 
 
179
    /*! \return A string that describes the error condition denoted by \p ev.
 
180
     */
 
181
    virtual std::string message(int ev) const = 0;
 
182
 
 
183
    /*! \return <tt>*this == &rhs</tt>
 
184
     */
 
185
    inline bool operator==(const error_category &rhs) const;
 
186
 
 
187
    /*! \return <tt>!(*this == rhs)</tt>
 
188
     */
 
189
    inline bool operator!=(const error_category &rhs) const;
 
190
 
 
191
    /*! \return <tt>less<const error_category*>()(this, &rhs)</tt>
 
192
     *  \note \c less provides a total ordering for pointers.
 
193
     */
 
194
    inline bool operator<(const error_category &rhs) const;
 
195
}; // end error_category
 
196
 
 
197
 
 
198
// [19.5.1.5] error_category objects
 
199
 
 
200
 
 
201
/*! \return A reference to an object of a type derived from class \p error_category.
 
202
 *  \note The object's \p default_error_condition and \p equivalent virtual functions
 
203
 *        shall behave as specified for the class \p error_category. The object's
 
204
 *        \p name virtual function shall return a pointer to the string <tt>"generic"</tt>.
 
205
 */
 
206
inline const error_category &generic_category(void);
 
207
 
 
208
 
 
209
/*! \return A reference to an object of a type derived from class \p error_category.
 
210
 *  \note The object's \p equivalent virtual functions shall behave as specified for
 
211
 *        class \p error_category. The object's \p name virtual function shall return
 
212
 *        a pointer to the string <tt>"system"</tt>. The object's \p default_error_condition
 
213
 *        virtual function shall behave as follows:
 
214
 *
 
215
 *        If the argument <tt>ev</tt> corresponds to a POSIX <tt>errno</tt> value
 
216
 *        \c posv, the function shall return <tt>error_condition(ev,generic_category())</tt>.
 
217
 *        Otherwise, the function shall return <tt>error_condition(ev,system_category())</tt>.
 
218
 *        What constitutes correspondence for any given operating system is unspecified.
 
219
 */
 
220
inline const error_category &system_category(void);
 
221
 
 
222
 
 
223
// [19.5.2] Class error_code
 
224
 
 
225
 
 
226
/*! \brief The class \p error_code describes an object used to hold error code values, such as
 
227
 *         those originating from the operating system or other low-level application program
 
228
 *         interfaces.
 
229
 */
 
230
class error_code
 
231
{
 
232
  public:
 
233
    // [19.5.2.2] constructors:
 
234
 
 
235
    /*! Effects: Constructs an object of type \p error_code.
 
236
     *  \post <tt>value() == 0</tt> and <tt>category() == &system_category()</tt>.
 
237
     */
 
238
    inline error_code(void);
 
239
 
 
240
    /*! Effects: Constructs an object of type \p error_code.
 
241
     *  \post <tt>value() == val</tt> and <tt>category() == &cat</tt>.
 
242
     */
 
243
    inline error_code(int val, const error_category &cat);
 
244
 
 
245
    /*! Effects: Constructs an object of type \p error_code.
 
246
     *  \post <tt>*this == make_error_code(e)</tt>.
 
247
     */
 
248
    template <typename ErrorCodeEnum>
 
249
      error_code(ErrorCodeEnum e
 
250
// XXX WAR msvc's problem with enable_if
 
251
#if THRUST_HOST_COMPILER != THRUST_HOST_COMPILER_MSVC
 
252
        , typename thrust::detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value>::type * = 0
 
253
#endif // THRUST_HOST_COMPILER_MSVC
 
254
        );
 
255
 
 
256
    // [19.5.2.3] modifiers:
 
257
 
 
258
    /*! \post <tt>value() == val</tt> and <tt>category() == &cat</tt>.
 
259
     */
 
260
    inline void assign(int val, const error_category &cat);
 
261
 
 
262
    /*! \post <tt>*this == make_error_code(e)</tt>.
 
263
     */
 
264
    template <typename ErrorCodeEnum>
 
265
// XXX WAR msvc's problem with enable_if
 
266
#if THRUST_HOST_COMPILER != THRUST_HOST_COMPILER_MSVC
 
267
      typename thrust::detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value, error_code>::type &
 
268
#else
 
269
      error_code &
 
270
#endif // THRUST_HOST_COMPILER_MSVC
 
271
        operator=(ErrorCodeEnum e);
 
272
 
 
273
    /*! \post <tt>value() == 0</tt> and <tt>category() == system_category()</tt>.
 
274
     */
 
275
    inline void clear(void);
 
276
 
 
277
    // [19.5.2.4] observers:
 
278
 
 
279
    /*! \return An integral value of this \p error_code object.
 
280
     */
 
281
    inline int value(void) const;
 
282
 
 
283
    /*! \return An \p error_category describing the category of this \p error_code object.
 
284
     */
 
285
    inline const error_category &category(void) const;
 
286
 
 
287
    /*! \return <tt>category().default_error_condition()</tt>.
 
288
     */
 
289
    inline error_condition default_error_condition(void) const;
 
290
 
 
291
    /*! \return <tt>category().message(value())</tt>.
 
292
     */
 
293
    inline std::string message(void) const;
 
294
 
 
295
    // XXX replace the below upon c++0x
 
296
    // inline explicit operator bool (void) const;
 
297
 
 
298
    /*! \return <tt>value() != 0</tt>.
 
299
     */
 
300
    inline operator bool (void) const;
 
301
 
 
302
    /*! \cond
 
303
     */
 
304
  private:
 
305
    int m_val;
 
306
    const error_category *m_cat;
 
307
    /*! \endcond
 
308
     */
 
309
}; // end error_code
 
310
 
 
311
 
 
312
// [19.5.2.5] Class error_code non-member functions
 
313
 
 
314
 
 
315
// XXX replace errc::errc_t with errc upon c++0x
 
316
/*! \return <tt>error_code(static_cast<int>(e), generic_category())</tt>
 
317
 */
 
318
inline error_code make_error_code(errc::errc_t e);
 
319
 
 
320
 
 
321
/*! \return <tt>lhs.category() < rhs.category() || lhs.category() == rhs.category() && lhs.value() < rhs.value()</tt>.
 
322
 */
 
323
inline bool operator<(const error_code &lhs, const error_code &rhs);
 
324
 
 
325
 
 
326
/*! Effects: <tt>os << ec.category().name() << ':' << ec.value()</tt>.
 
327
 */
 
328
template <typename charT, typename traits>
 
329
  std::basic_ostream<charT,traits>&
 
330
    operator<<(std::basic_ostream<charT,traits>& os, const error_code &ec);
 
331
 
 
332
 
 
333
// [19.5.3] class error_condition
 
334
 
 
335
 
 
336
/*! \brief The class \p error_condition describes an object used to hold values identifying
 
337
 *  error conditions.
 
338
 *
 
339
 *  \note \p error_condition values are portable abstractions, while \p error_code values
 
340
 *        are implementation specific.
 
341
 */
 
342
class error_condition
 
343
{
 
344
  public:
 
345
    // [19.5.3.2] constructors
 
346
 
 
347
    /*! Constructs an object of type \p error_condition.
 
348
     *  \post <tt>value() == 0</tt>.
 
349
     *  \post <tt>category() == generic_category()</tt>.
 
350
     */
 
351
    inline error_condition(void);
 
352
 
 
353
    /*! Constructs an object of type \p error_condition.
 
354
     *  \post <tt>value() == val</tt>.
 
355
     *  \post <tt>category() == cat</tt>.
 
356
     */
 
357
    inline error_condition(int val, const error_category &cat);
 
358
 
 
359
    /*! Constructs an object of type \p error_condition.
 
360
     *  \post <tt>*this == make_error_condition(e)</tt>.
 
361
     *  \note This constructor shall not participate in overload resolution unless
 
362
     *        <tt>is_error_condition_enum<ErrorConditionEnum>::value</tt> is <tt>true</tt>.
 
363
     */
 
364
    template<typename ErrorConditionEnum>
 
365
      error_condition(ErrorConditionEnum e
 
366
// XXX WAR msvc's problem with enable_if
 
367
#if THRUST_HOST_COMPILER != THRUST_HOST_COMPILER_MSVC
 
368
        , typename thrust::detail::enable_if<is_error_condition_enum<ErrorConditionEnum>::value>::type * = 0
 
369
#endif // THRUST_HOST_COMPILER != THRUST_HOST_COMPILER_MSVC
 
370
                     );
 
371
 
 
372
    // [19.5.3.3] modifiers
 
373
 
 
374
    /*! Assigns to this \p error_code object from an error value and an \p error_category.
 
375
     *  \param val The new value to return from <tt>value()</tt>.
 
376
     *  \param cat The new \p error_category to return from <tt>category()</tt>.
 
377
     *  \post <tt>value() == val</tt>.
 
378
     *  \post <tt>category() == cat</tt>.
 
379
     */
 
380
    inline void assign(int val, const error_category &cat);
 
381
 
 
382
    /*! Assigns to this \p error_code object from an error condition enumeration.
 
383
     *  \return *this
 
384
     *  \post <tt>*this == make_error_condition(e)</tt>.
 
385
     *  \note This operator shall not participate in overload resolution unless
 
386
     *        <tt>is_error_condition_enum<ErrorConditionEnum>::value</tt> is <tt>true</tt>.
 
387
     */
 
388
    template<typename ErrorConditionEnum>
 
389
// XXX WAR msvc's problem with enable_if
 
390
#if THRUST_HOST_COMPILER != THRUST_HOST_COMPILER_MSVC
 
391
      typename thrust::detail::enable_if<is_error_condition_enum<ErrorConditionEnum>::value, error_condition>::type &
 
392
#else
 
393
      error_condition &
 
394
#endif // THRUST_HOST_COMPILER != THRUST_HOST_COMPILER_MSVC
 
395
        operator=(ErrorConditionEnum e);
 
396
 
 
397
    /*! Clears this \p error_code object.
 
398
     *  \post <tt>value == 0</tt>
 
399
     *  \post <tt>category() == generic_category()</tt>.
 
400
     */
 
401
    inline void clear(void);
 
402
 
 
403
    // [19.5.3.4] observers
 
404
 
 
405
    /*! \return The value encoded by this \p error_condition.
 
406
     */
 
407
    inline int value(void) const;
 
408
 
 
409
    /*! \return A <tt>const</tt> reference to the \p error_category encoded by this \p error_condition.
 
410
     */
 
411
    inline const error_category &category(void) const;
 
412
 
 
413
    /*! \return <tt>category().message(value())</tt>.
 
414
     */
 
415
    inline std::string message(void) const;
 
416
 
 
417
    // XXX replace below with this upon c++0x
 
418
    //explicit operator bool (void) const;
 
419
    
 
420
    /*! \return <tt>value() != 0</tt>.
 
421
     */
 
422
    inline operator bool (void) const;
 
423
 
 
424
    /*! \cond
 
425
     */
 
426
 
 
427
  private:
 
428
    int m_val;
 
429
    const error_category *m_cat;
 
430
 
 
431
    /*! \endcond
 
432
     */
 
433
}; // end error_condition
 
434
 
 
435
 
 
436
 
 
437
// [19.5.3.5] Class error_condition non-member functions
 
438
 
 
439
// XXX replace errc::errc_t with errc upon c++0x
 
440
/*! \return <tt>error_condition(static_cast<int>(e), generic_category())</tt>.
 
441
 */
 
442
inline error_condition make_error_condition(errc::errc_t e);
 
443
 
 
444
 
 
445
/*! \return <tt>lhs.category() < rhs.category() || lhs.category() == rhs.category() && lhs.value() < rhs.value()</tt>.
 
446
 */
 
447
inline bool operator<(const error_condition &lhs, const error_condition &rhs);
 
448
 
 
449
 
 
450
// [19.5.4] Comparison operators
 
451
 
 
452
 
 
453
/*! \return <tt>lhs.category() == rhs.category() && lhs.value() == rhs.value()</tt>.
 
454
 */
 
455
inline bool operator==(const error_code &lhs, const error_code &rhs);
 
456
 
 
457
 
 
458
/*! \return <tt>lhs.category().equivalent(lhs.value(), rhs) || rhs.category().equivalent(lhs,rhs.value())</tt>.
 
459
 */
 
460
inline bool operator==(const error_code &lhs, const error_condition &rhs);
 
461
 
 
462
 
 
463
/*! \return <tt>rhs.category().equivalent(lhs.value(), lhs) || lhs.category().equivalent(rhs, lhs.value())</tt>.
 
464
 */
 
465
inline bool operator==(const error_condition &lhs, const error_code &rhs);
 
466
 
 
467
 
 
468
/*! \return <tt>lhs.category() == rhs.category() && lhs.value() == rhs.value()</tt>
 
469
 */
 
470
inline bool operator==(const error_condition &lhs, const error_condition &rhs);
 
471
 
 
472
 
 
473
/*! \return <tt>!(lhs == rhs)</tt>
 
474
 */
 
475
inline bool operator!=(const error_code &lhs, const error_code &rhs);
 
476
 
 
477
 
 
478
/*! \return <tt>!(lhs == rhs)</tt>
 
479
 */
 
480
inline bool operator!=(const error_code &lhs, const error_condition &rhs);
 
481
 
 
482
 
 
483
/*! \return <tt>!(lhs == rhs)</tt>
 
484
 */
 
485
inline bool operator!=(const error_condition &lhs, const error_code &rhs);
 
486
 
 
487
 
 
488
/*! \return <tt>!(lhs == rhs)</tt>
 
489
 */
 
490
inline bool operator!=(const error_condition &lhs, const error_condition &rhs);
 
491
 
 
492
/*! \} // end system
 
493
 */
 
494
 
 
495
 
 
496
} // end system
 
497
 
 
498
 
 
499
// import names into thrust::
 
500
using system::error_category;
 
501
using system::error_code;
 
502
using system::error_condition;
 
503
using system::is_error_code_enum;
 
504
using system::is_error_condition_enum;
 
505
using system::make_error_code;
 
506
using system::make_error_condition;
 
507
 
 
508
// XXX replace with using system::errc upon c++0x
 
509
namespace errc = system::errc;
 
510
 
 
511
using system::generic_category;
 
512
using system::system_category;
 
513
 
 
514
} // end thrust
 
515
 
 
516
#include <thrust/system/detail/error_category.inl>
 
517
#include <thrust/system/detail/error_code.inl>
 
518
#include <thrust/system/detail/error_condition.inl>
 
519