libcppwrap
A collection of C++ wrappers for native APIs
Loading...
Searching...
No Matches
assert.hpp
Go to the documentation of this file.
1//
2// libcppwrap - A collection of C++ wrappers for native APIs
3// Copyright (C) 2021-2023 David A. Norris <danorris@gmail.com>
4// Published under the MIT license - https://opensource.org/licenses/MIT
5//
6
7#pragma once
8
9#include <cerrno>
10#include <system_error>
11
12namespace w
13{
24 template <typename T>
25 T throw_if_eq(T value, T x, const char *message)
26 {
27 if (value == x)
28 throw std::system_error(errno, std::generic_category(), message);
29 return value;
30 }
31
42 template <typename T>
43 T throw_if_lt(T value, T x, const char *message)
44 {
45 if (value < x)
46 throw std::system_error(errno, std::generic_category(), message);
47 return value;
48 }
49
60 template <typename T>
61 T throw_if_ne(T value, T x, const char *message)
62 {
63 if (value != x)
64 throw std::system_error(errno, std::generic_category(), message);
65 return value;
66 }
67
77 template <typename T>
78 T throw_if_nz(T value, const char *message)
79 {
80 if (value != 0)
81 throw std::system_error(errno, std::generic_category(), message);
82 return value;
83 }
84
94 template <typename T>
95 T throw_if_z(T value, const char *message)
96 {
97 if (value == 0)
98 throw std::system_error(errno, std::generic_category(), message);
99 return value;
100 }
101}
T generic_category(T... args)
Definition: assert.hpp:13
T throw_if_ne(T value, T x, const char *message)
Throw a std::system_error with errno if value != x, otherwise return value.
Definition: assert.hpp:61
T throw_if_eq(T value, T x, const char *message)
Throw a std::system_error with errno if value == x, otherwise return value.
Definition: assert.hpp:25
T throw_if_lt(T value, T x, const char *message)
Throw a std::system_error with errno if value < x, otherwise return value.
Definition: assert.hpp:43
T throw_if_z(T value, const char *message)
Throw a std::system_error with errno if value == 0, otherwise return value.
Definition: assert.hpp:95
T throw_if_nz(T value, const char *message)
Throw a std::system_error with errno if value != 0, otherwise return value.
Definition: assert.hpp:78