UCX++
error.h
1#pragma once
2#include <cerrno>
3#include <cstddef>
4#include <cstdio>
5#include <cstring>
6#include <stdexcept>
7#include <ucs/type/status.h>
8
9#include <ucp/api/ucp.h>
10
11namespace ucxpp {
12
13constexpr size_t kErrorStringBufferSize = 1024;
14
15static inline void throw_with(const char *message) {
16 throw std::runtime_error(message);
17}
18
19template <class... Args>
20static inline void throw_with(const char *format, Args... args) {
21 char buffer[kErrorStringBufferSize];
22 ::snprintf(buffer, sizeof(buffer), format, args...);
23 throw std::runtime_error(buffer);
24}
25
26static inline void check_ucs_status(ucs_status_t status, const char *message) {
27 if (status == UCS_OK) [[likely]] {
28 return;
29 }
30 if (status == UCS_INPROGRESS) [[likely]] {
31 return;
32 }
33
34 throw_with("%s: %s (status=%d)", message, ::ucs_status_string(status),
35 status);
36}
37
38static inline void check_rc(int rc, const char *message) {
39 if (rc != 0) [[unlikely]] {
40 throw_with("%s: %s (rc=%d)", message, ::strerror(rc), rc);
41 }
42}
43
44static inline void check_ptr(void *ptr, const char *message) {
45 if (ptr == nullptr) [[unlikely]] {
46 throw_with("%s: %s (errno=%d)", message, ::strerror(errno), errno);
47 }
48}
49
50static inline void check_errno(int rc, const char *message) {
51 if (rc < 0) [[unlikely]] {
52 throw_with("%s: %s (errno=%d)", message, ::strerror(errno), errno);
53 }
54}
55
56} // namespace ucxpp