Loading...
Searching...
No Matches
include
core
error.h
Go to the documentation of this file.
1
#ifndef _BBM_ERROR_H_
2
#define _BBM_ERROR_H_
3
4
#include <stdexcept>
5
#include "
util/macro_util.h
"
6
7
/***********************************************************************/
8
/*! \file error.h
9
\brief Predefined exceptions for common errors
10
************************************************************************/
11
12
namespace
bbm
{
13
/*********************************************************************/
14
/* \brief Helper class for throwing a static assert in the 'else' condition
15
of a 'if constexpr' statement. Directly using 'false' in the static_expression
16
always yields a complile error, even if the else condition was not taken.
17
A work around is to make the condition of the static_assert dependent
18
on a template type. Example:
19
20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
21
template<typename T>
22
T foo(T t)
23
{
24
if constexpr (std::same_as<T, float>) { return t; }
25
else static_assert( dependent_false_v<T>, "T is not a float");
26
}
27
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28
29
***********************************************************************/
30
template
<
typename
... T>
struct
dependent_false
:
public
std::false_type {};
31
32
template
<
typename
... T>
33
static
constexpr
bool
dependent_false_v
=
dependent_false
<T...>
::value
;
34
35
template
<
typename
... T>
36
static
constexpr
bool
dependent_true_v
= !
dependent_false
<T...>
::value
;
37
}
// end bbm namespace
38
39
#define BBM_ERROR_MSG(...) BBM_STRINGIFY( __VA_ARGS__ (__FILE__ : __LINE__). )
40
41
//! @{ \name Predefined Run-time Errors
42
#define bbm_incomplete_init std::runtime_error(BBM_ERROR_MSG(BBM: accessing incompletely initialized object))
43
#define bbm_unassigned_ref std::runtime_error(BBM_ERROR_MSG(BBM: Unassigned references cannot be cast/read))
44
#define bbm_out_of_range std::runtime_error(BBM_ERROR_MSG(BBM: index out of range))
45
#define bbm_size_error std::runtime_error(BBM_ERROR_MSG(BBM: size error))
46
#define bbm_unevaluable std::runtime_error(BBM_ERROR_MSG(BBM: cannot evaluate expression))
47
//! @}
48
49
//! @{ \name Predefined Compile-time Errors
50
#define BBM_SIZE_MISMATCH "BBM: size mismatch"
51
#define BBM_NO_MATCH "BBM: no matching argument found"
52
#define BBM_NOT_DIFF "BBM: type does not support gradients"
53
#define BBM_INVALID_LAMBDA "BBM: invalid lambda signature"
54
#define BBM_CONST_ASSIGNMENT "BBM: cannot assign to a const type"
55
//! @}
56
57
#endif
/* _BBM_ERROR_H_ */
macro_util.h
General macro utilities.
bbm
Definition:
aggregatebsdf.h:29
bbm::dependent_true_v
static constexpr bool dependent_true_v
Definition:
error.h:36
bbm::dependent_false_v
static constexpr bool dependent_false_v
Definition:
error.h:33
bbm::value
decltype(auto) value(T &&t)
return the value of an attribute, or if not an attribute the object
Definition:
attribute_value.h:20
bbm::dependent_false
Definition:
error.h:30