-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContractExtensions.h
More file actions
71 lines (63 loc) · 2.51 KB
/
ContractExtensions.h
File metadata and controls
71 lines (63 loc) · 2.51 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
namespace Platform::Exceptions::Contract::Always
{
void ArgumentNotNull(auto argument, const std::string& argumentName, const std::string& message)
requires std::is_pointer_v<decltype(argument)> || std::is_null_pointer_v<decltype(argument)>
{
if (argument == nullptr)
{
throw std::invalid_argument(std::string("Argument ").append(argumentName).append(" is null: ").append(message).append(1, '.'));
}
}
void ArgumentNotNull(auto argument, const std::string& argumentName)
requires std::is_pointer_v<decltype(argument)> || std::is_null_pointer_v<decltype(argument)>
{
if (argument == nullptr)
{
throw std::invalid_argument(std::string("Argument ").append(argumentName).append(" is null."));
}
}
void ArgumentNotNull(auto argument)
requires std::is_pointer_v<decltype(argument)> || std::is_null_pointer_v<decltype(argument)>
{
if (argument == nullptr)
{
throw std::invalid_argument(std::string("Argument is null."));
}
}
void ArgumentMeetsCriteria(auto&& argument, auto&& predicate, const std::string& argumentName, const std::string& message)
requires requires { { predicate(argument) } -> std::integral; }
{
if (not predicate(std::forward<decltype(argument)>(argument)))
{
throw std::invalid_argument(std::string("Invalid ").append(argumentName).append(" argument: ").append(message).append(1, '.'));
}
}
void ArgumentMeetsCriteria(auto&& argument, auto&& predicate, const std::string& argumentName)
{
ArgumentMeetsCriteria(std::forward<decltype(argument)>(argument), predicate, argumentName, std::string("Argument ").append(argumentName).append(" does not meet the criteria."));
}
void ArgumentMeetsCriteria(auto&& argument, auto&& predicate)
{
ArgumentMeetsCriteria(std::forward<decltype(argument)>(argument), predicate, {});
}
}
namespace Platform::Exceptions::Contract::OnDebug
{
#ifdef NDEBUG
#define NDEBUG_CONSTEVAL consteval
#else
#define NDEBUG_CONSTEVAL
#endif
NDEBUG_CONSTEVAL static void ArgumentNotNull(auto&&... args)
#ifdef NDEBUG
noexcept {}
#else
{ Always::ArgumentNotNull(std::forward<decltype(args)>(args)...); }
#endif
NDEBUG_CONSTEVAL static void ArgumentMeetsCriteria(auto&&... args)
#ifdef NDEBUG
noexcept {}
#else
{ Always::ArgumentMeetsCriteria(std::forward<decltype(args)>(args)...); }
#endif
}