Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Handle.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_IO_HANDLE_HPP
18#define TOOLBOX_IO_HANDLE_HPP
19
20#include <utility> // swap<>
21
22#include <unistd.h> // close()
23
24namespace toolbox {
25inline namespace io {
26
27template <typename PolicyT>
29 public:
30 using Id = typename PolicyT::Id;
31
32 static constexpr Id invalid() noexcept { return PolicyT::invalid(); }
33
34 constexpr BasicHandle(std::nullptr_t = nullptr) noexcept {
35 } // NOLINT(hicpp-explicit-conversions)
36 constexpr BasicHandle(Id id) noexcept // NOLINT(hicpp-explicit-conversions)
37 : id_{id}
38 {
39 }
41 {
42 if (id_ != invalid()) {
43 PolicyT::close(id_);
44 }
45 }
46
47 // Copy.
48 BasicHandle(const BasicHandle&) = delete;
50
51 // Move.
53 : id_{rhs.id_}
54 {
55 rhs.id_ = invalid();
56 }
58 {
59 reset();
60 swap(rhs);
61 return *this;
62 }
63
64 constexpr bool empty() const noexcept { return id_ == invalid(); }
65 constexpr explicit operator bool() const noexcept { return id_ != invalid(); }
66
67 constexpr Id get() const noexcept { return id_; }
68 constexpr Id operator*() const noexcept { return get(); }
69
71 {
72 const auto id = id_;
73 id_ = invalid();
74 return id;
75 }
76 void reset(std::nullptr_t = nullptr) noexcept { reset(invalid()); }
77 void reset(Id id) noexcept
78 {
79 std::swap(id_, id);
80 if (id != invalid()) {
81 PolicyT::close(id);
82 }
83 }
84 void swap(BasicHandle& rhs) noexcept { std::swap(id_, rhs.id_); }
85
86 private:
87 Id id_{invalid()};
88};
89
90template <typename PolicyT>
92{
93 return lhs.get() == rhs.get();
94}
95
96template <typename PolicyT>
98{
99 return !(lhs == rhs);
100}
101
103 using Id = int;
104 static constexpr int invalid() noexcept { return -1; }
105 static void close(int fd) noexcept { ::close(fd); }
106};
107
109
110} // namespace io
111} // namespace toolbox
112
113#endif // TOOLBOX_IO_HANDLE_HPP
constexpr BasicHandle(std::nullptr_t=nullptr) noexcept
Definition Handle.hpp:34
static constexpr Id invalid() noexcept
Definition Handle.hpp:32
constexpr Id operator*() const noexcept
Definition Handle.hpp:68
constexpr bool empty() const noexcept
Definition Handle.hpp:64
constexpr Id get() const noexcept
Definition Handle.hpp:67
BasicHandle(const BasicHandle &)=delete
typename PolicyT::Id Id
Definition Handle.hpp:30
Id release() noexcept
Definition Handle.hpp:70
BasicHandle & operator=(const BasicHandle &)=delete
constexpr BasicHandle(Id id) noexcept
Definition Handle.hpp:36
void reset(Id id) noexcept
Definition Handle.hpp:77
void reset(std::nullptr_t=nullptr) noexcept
Definition Handle.hpp:76
void swap(BasicHandle &rhs) noexcept
Definition Handle.hpp:84
constexpr BasicHandle(BasicHandle &&rhs) noexcept
Definition Handle.hpp:52
BasicHandle & operator=(BasicHandle &&rhs) noexcept
Definition Handle.hpp:57
constexpr bool operator==(const BasicHandle< PolicyT > &lhs, const BasicHandle< PolicyT > &rhs)
Definition Handle.hpp:91
constexpr bool operator!=(const BasicHandle< PolicyT > &lhs, const BasicHandle< PolicyT > &rhs)
Definition Handle.hpp:97
constexpr auto bind() noexcept
Definition Slot.hpp:92
static constexpr int invalid() noexcept
Definition Handle.hpp:104
static void close(int fd) noexcept
Definition Handle.hpp:105