pars 0.2.1
Loading...
Searching...
No Matches
err.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include "clev/err.h"
33
34#include <nng/nng.h>
35
36#include <expected>
37
38namespace nngxx
39{
40
41enum class err : int;
42
43[[nodiscard]] inline static const std::error_category& error_category() noexcept
44{
45 static struct : std::error_category
46 {
47 virtual const char* name() const noexcept override { return "nngxx"; }
48
49 virtual std::string message(int e) const override
50 {
51 return nng_strerror(e);
52 }
53 } error_category;
54
55 return error_category;
56}
57
58[[nodiscard]] inline std::error_code make_error_code(err e) noexcept
59{
60 return std::error_code(static_cast<int>(e), error_category());
61}
62
63} // namespace nngxx
64
65template<>
66struct std::is_error_code_enum<nngxx::err> : std::true_type
67{
68};
69
70namespace nngxx
71{
72
73enum class err
74{
76 intr = NNG_EINTR,
77 nomem = NNG_ENOMEM,
78 inval = NNG_EINVAL,
79 busy = NNG_EBUSY,
80 timedout = NNG_ETIMEDOUT,
81 connrefused = NNG_ECONNREFUSED,
82 closed = NNG_ECLOSED,
83 again = NNG_EAGAIN,
84 notsup = NNG_ENOTSUP,
85 addrinuse = NNG_EADDRINUSE,
86 state = NNG_ESTATE,
87 noent = NNG_ENOENT,
88 proto = NNG_EPROTO,
89 unreachable = NNG_EUNREACHABLE,
90 addrinval = NNG_EADDRINVAL,
91 perm = NNG_EPERM,
92 msgsize = NNG_EMSGSIZE,
93 connaborted = NNG_ECONNABORTED,
94 connreset = NNG_ECONNRESET,
95 canceled = NNG_ECANCELED,
96 nofiles = NNG_ENOFILES,
97 nospc = NNG_ENOSPC,
98 exist = NNG_EEXIST,
99 readonly = NNG_EREADONLY,
100 writeonly = NNG_EWRITEONLY,
101 crypto = NNG_ECRYPTO,
102 peerauth = NNG_EPEERAUTH,
103 noarg = NNG_ENOARG,
104 ambiguous = NNG_EAMBIGUOUS,
105 badtype = NNG_EBADTYPE,
106 connshut = NNG_ECONNSHUT,
107 internal = NNG_EINTERNAL,
108 syserr = NNG_ESYSERR,
109 tranerr = NNG_ETRANERR
110};
111
112template<typename ret_t, typename arg_t, typename... args_t>
114make(ret_t (*f)(arg_t, args_t...), args_t... args) noexcept
115{
116 std::remove_pointer_t<arg_t> x;
117
118 return clev::make_expected<err>(f(&x, args...)).transform_to(std::move(x));
119}
120
121template<typename ret_t, typename... args_t>
122 requires(!std::is_void_v<ret_t>)
123[[nodiscard]] inline clev::expected<void> invoke(ret_t (*f)(args_t...),
124 args_t... args) noexcept
125{
126 return clev::make_expected<err>(f(args...));
127}
128
129template<typename... args_t>
130[[nodiscard]] inline clev::expected<void> invoke(void (*f)(args_t...),
131 args_t... args) noexcept
132{
133 f(args...);
134
135 return {};
136}
137
138} // namespace nngxx
139
140static_assert(std::convertible_to<nngxx::err, std::error_code>);
141
142static_assert(std::constructible_from<std::error_code, nngxx::err>);
clev::expected< void > make_expected(const int e) noexcept
Definition err.h:240
Definition aio.h:35
std::error_code make_error_code(err e) noexcept
Definition err.h:58
clev::expected< std::remove_pointer_t< arg_t > > make(ret_t(*f)(arg_t, args_t...), args_t... args) noexcept
Definition err.h:114
err
Definition err.h:74
@ exist
Definition err.h:98
@ nospc
Definition err.h:97
@ notsup
Definition err.h:84
@ inval
Definition err.h:78
@ ambiguous
Definition err.h:104
@ success
Definition err.h:75
@ noent
Definition err.h:87
@ readonly
Definition err.h:99
@ proto
Definition err.h:88
@ closed
Definition err.h:82
@ badtype
Definition err.h:105
@ connreset
Definition err.h:94
@ nomem
Definition err.h:77
@ connshut
Definition err.h:106
@ crypto
Definition err.h:101
@ again
Definition err.h:83
@ timedout
Definition err.h:80
@ intr
Definition err.h:76
@ busy
Definition err.h:79
@ addrinval
Definition err.h:90
@ state
Definition err.h:86
@ unreachable
Definition err.h:89
@ connaborted
Definition err.h:93
@ tranerr
Definition err.h:109
@ syserr
Definition err.h:108
@ addrinuse
Definition err.h:85
@ perm
Definition err.h:91
@ nofiles
Definition err.h:96
@ internal
Definition err.h:107
@ canceled
Definition err.h:95
@ connrefused
Definition err.h:81
@ noarg
Definition err.h:103
@ writeonly
Definition err.h:100
@ peerauth
Definition err.h:102
@ msgsize
Definition err.h:92
clev::expected< void > invoke(ret_t(*f)(args_t...), args_t... args) noexcept
Definition err.h:123
auto transform_to(to_value_t &&v) &&noexcept
Definition err.h:119