Chops Net IP
Loading...
Searching...
No Matches
wp_access.hpp
Go to the documentation of this file.
1
19#ifndef WP_ACCESS_HPP_INCLUDED
20#define WP_ACCESS_HPP_INCLUDED
21
22#include <memory> // std::weak_ptr, std::shared_ptr
23#include <system_error>
24
25#include "nonstd/expected.hpp"
26
28
29namespace chops {
30namespace net {
31namespace detail {
32
33template <typename R, typename WP, typename F>
34auto wp_access(const WP& wp, F&& func) ->
35 nonstd::expected<R, std::error_code> {
36 if (auto sp = wp.lock()) {
37 return func(sp);
38 }
39 return nonstd::make_unexpected(std::make_error_code(net_ip_errc::weak_ptr_expired));
40}
41
42template <typename WP, typename F>
43auto wp_access_void(const WP& wp, F&& func) ->
44 nonstd::expected<void, std::error_code> {
45 if (auto sp = wp.lock()) {
46 auto r = func(sp);
47 if (r) {
48 return nonstd::make_unexpected(r);
49 }
50 return { };
51 }
52 return nonstd::make_unexpected(std::make_error_code(net_ip_errc::weak_ptr_expired));
53}
54
55} // end detail namespace
56} // end net namespace
57} // end chops namespace
58
59#endif
60
Error codes, exception class, and error category within Chops net_ip library.