-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathErrorCheck.h
More file actions
163 lines (142 loc) · 5.22 KB
/
ErrorCheck.h
File metadata and controls
163 lines (142 loc) · 5.22 KB
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
// SPDX-FileCopyrightText: Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
//
#pragma once
#include <stdexcept>
#include <string>
#include <iostream>
namespace otk {
namespace error {
/// A generalized error checking mechanism for CUDA device API errors,
/// CUDA runtime API errors and OptiX errors.
// Specialize these two functions for your error types to
// return enum names for the error code and a human readable
// error message for the error code.
/// Return a string giving the name of the error code.
///
/// @param value The error code to be named.
/// @returns A string naming the error code.
///
// ReSharper disable once CppFunctionIsNotImplemented
template <typename T>
std::string getErrorName( T value );
/// Specialization for assertions.
template <>
inline std::string getErrorName( bool value )
{
return value ? "true" : "false";
}
/// Specialization for int status.
template <>
inline std::string getErrorName( int value )
{
return std::to_string( value );
}
/// Return a string giving the error message for an error code.
///
/// @param value The error code to be described.
/// @returns A string describing the error.
///
// ReSharper disable once CppFunctionIsNotImplemented
template <typename T>
std::string getErrorMessage( T value );
/// Specialization for assertions.
template <>
inline std::string getErrorMessage( bool value )
{
return value ? "true" : "false";
}
/// Specialization for integer status.
template <>
inline std::string getErrorMessage( int value )
{
return std::to_string( value );
}
/// Identify an error code as a failure.
///
/// Assume that any non-zero value represents failure;
/// if your error type represents success with some non-zero value,
/// then specialize this function for your distinct error type.
///
/// @param value The error code value to be tested.
/// @returns true if the code represents failure.
///
template <typename T>
bool isFailure( T value )
{
return static_cast<bool>( value );
}
/// Build a complete error string.
///
/// @param error The failed error code to be reported.
/// @param expr The originating source code expression that generated the error.
/// @param file The file name containing the expression.
/// @param line The source line number containing the expression.
/// @param extra Optional additional error text.
template <typename T>
std::string makeErrorString( T error, const char* expr, const char* file, unsigned int line, const char* extra )
{
std::string message{ file };
message += '(' + std::to_string( line ) + "): " + expr + " failed with error " + std::to_string( static_cast<int>( error ) );
const std::string errorName{ getErrorName( error ) };
if( !errorName.empty() )
message += " (" + errorName + ')';
const std::string errorMessage{ getErrorMessage( error ) };
if( !errorMessage.empty() )
message += ' ' + errorMessage;
if( extra != nullptr )
message += std::string( ": " ) + extra;
return message;
}
/// Checks an error code and reports detected failures by throwing std::runtime_error.
///
/// @param result The error code to be tested for failure.
/// @param expr The originating source code expression that generated the error.
/// @param file The file name containing the expression.
/// @param line The source line number containing the expression.
/// @param extra Optional additional error text.
template <typename T>
void checkError( T result, const char* expr, const char* file, unsigned int line, const char* extra )
{
if( isFailure( result ) )
{
throw std::runtime_error( makeErrorString( result, expr, file, line, extra ) );
}
}
/// Checks an error code and reports detected failures via std::cerr.
///
/// @param result The error code to be tested for failure.
/// @param expr The originating source code expression that generated the error.
/// @param file The file name containing the expression.
/// @param line The source line number containing the expression.
/// @param extra Optional additional error text.
template <typename T>
void checkErrorNoThrow( T result, const char* expr, const char* file, unsigned int line, const char* extra )
{
if( isFailure( result ) )
{
try
{
std::cerr << makeErrorString( result, expr, file, line, extra ) << std::endl;
}
catch( ... )
{
}
}
}
} // namespace errorCheck
} // namespace otk
/// Check an expression for error
/// @param expr The source expression to check.
///
#define OTK_ERROR_CHECK( expr ) ::otk::error::checkError( expr, #expr, __FILE__, __LINE__, /*extra=*/nullptr )
#define OTK_ERROR_CHECK_MSG( expr, msg ) ::otk::error::checkError( expr, #expr, __FILE__, __LINE__, msg )
#define OTK_ERROR_CHECK_NOTHROW( expr ) ::otk::error::checkErrorNoThrow( expr, #expr, __FILE__, __LINE__, /*extra=*/nullptr )
#ifndef NDEBUG
// Note that a non-zero value represents failure
#define OTK_ASSERT( expr ) OTK_ERROR_CHECK( !static_cast<bool>( expr ) )
#define OTK_ASSERT_MSG( expr, msg ) OTK_ERROR_CHECK_MSG( !static_cast<bool>( expr ), msg )
#else
#define OTK_ASSERT( expr ) {}
#define OTK_ASSERT_MSG( expr, msg ) {}
#endif