pars 0.2.1.99
Loading...
Searching...
No Matches
opt_getter.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 "nngxx/concept.h"
33#include "nngxx/err.h"
34#include "nngxx/opt.h"
35
36namespace nngxx
37{
38
39template<opt opt_v, nng_with_get_opt_c nng_t>
41{
42 using nng_type = nng_t;
43
44 static constexpr auto opt = opt_to_nng_value<opt_v>();
45
46 static constexpr auto overload = opt_to_nng_overload<opt_v>();
47
49
50 [[nodiscard]] inline clev::expected<return_type>
51 operator()(nng_type obj) noexcept
52 {
53 int (*get)(nng_type, const char*, return_type*);
54
55 if constexpr (std::is_same_v<nng_type, nng_socket>)
56 {
57 if constexpr (overload == opt_overload::boolean)
58 get = nng_socket_get_bool;
59 else if constexpr (overload == opt_overload::integer)
60 get = nng_socket_get_int;
61 else if constexpr (overload == opt_overload::size)
62 get = nng_socket_get_size;
63 else if constexpr (overload == opt_overload::uint64)
64 get = nng_socket_get_uint64;
65 else if constexpr (overload == opt_overload::string)
66 get = nng_socket_get_string;
67 else if constexpr (overload == opt_overload::duration)
68 get = nng_socket_get_ms;
69 else if constexpr (overload == opt_overload::sockaddr)
70 get = nng_socket_get_addr;
71 }
72 else if constexpr (std::is_same_v<nng_type, nng_ctx>)
73 {
74 if constexpr (overload == opt_overload::boolean)
75 get = nng_ctx_get_bool;
76 else if constexpr (overload == opt_overload::integer)
77 get = nng_ctx_get_int;
78 else if constexpr (overload == opt_overload::size)
79 get = nng_ctx_get_size;
80 else if constexpr (overload == opt_overload::uint64)
81 get = nng_ctx_get_uint64;
82 else if constexpr (overload == opt_overload::string)
83 get = nng_ctx_get_string;
84 else if constexpr (overload == opt_overload::duration)
85 get = nng_ctx_get_ms;
86 }
87 else if constexpr (std::is_same_v<nng_type, nng_listener>)
88 {
89 if constexpr (overload == opt_overload::boolean)
90 get = nng_listener_get_bool;
91 else if constexpr (overload == opt_overload::integer)
92 get = nng_listener_get_int;
93 else if constexpr (overload == opt_overload::size)
94 get = nng_listener_get_size;
95 else if constexpr (overload == opt_overload::uint64)
96 get = nng_listener_get_uint64;
97 else if constexpr (overload == opt_overload::string)
98 get = nng_listener_get_string;
99 else if constexpr (overload == opt_overload::duration)
100 get = nng_listener_get_ms;
101 else if constexpr (overload == opt_overload::sockaddr)
102 get = nng_listener_get_addr;
103 }
104 else if constexpr (std::is_same_v<nng_type, nng_dialer>)
105 {
106 if constexpr (overload == opt_overload::boolean)
107 get = nng_dialer_get_bool;
108 else if constexpr (overload == opt_overload::integer)
109 get = nng_dialer_get_int;
110 else if constexpr (overload == opt_overload::size)
111 get = nng_dialer_get_size;
112 else if constexpr (overload == opt_overload::uint64)
113 get = nng_dialer_get_uint64;
114 else if constexpr (overload == opt_overload::string)
115 get = nng_dialer_get_string;
116 else if constexpr (overload == opt_overload::duration)
117 get = nng_dialer_get_ms;
118 else if constexpr (overload == opt_overload::sockaddr)
119 get = nng_dialer_get_addr;
120 }
121 else if constexpr (std::is_same_v<nng_type, nng_pipe>)
122 {
123 if constexpr (overload == opt_overload::boolean)
124 get = nng_pipe_get_bool;
125 else if constexpr (overload == opt_overload::integer)
126 get = nng_pipe_get_int;
127 else if constexpr (overload == opt_overload::size)
128 get = nng_pipe_get_size;
129 else if constexpr (overload == opt_overload::uint64)
130 get = nng_pipe_get_uint64;
131 else if constexpr (overload == opt_overload::string)
132 get = nng_pipe_get_string;
133 else if constexpr (overload == opt_overload::duration)
134 get = nng_pipe_get_ms;
135 else if constexpr (overload == opt_overload::sockaddr)
136 get = nng_pipe_get_addr;
137 }
138
139 return_type val;
140
141 return nngxx::invoke(get, obj, opt, &val).transform_to(std::move(val));
142 }
143};
144
145} // namespace nngxx
Definition aio.h:35
opt_to_nng_type< o >::return_type opt_to_nng_type_t
Definition opt.h:260
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
static constexpr auto overload
Definition opt_getter.h:46
opt_to_nng_type_t< opt_v > return_type
Definition opt_getter.h:48
static constexpr auto opt
Definition opt_getter.h:44
clev::expected< return_type > operator()(nng_type obj) noexcept
Definition opt_getter.h:51