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 constexpr BasicHandle(Id id) noexcept
36 : id_{id}
37 {
38 }
40 {
41 if (id_ != invalid()) {
42 PolicyT::close(id_);
43 }
44 }
45
46 // Copy.
47 BasicHandle(const BasicHandle&) = delete;
49
50 // Move.
52 : id_{rhs.id_}
53 {
54 rhs.id_ = invalid();
55 }
57 {
58 reset();
59 swap(rhs);
60 return *this;
61 }
62
63 constexpr bool empty() const noexcept { return id_ == invalid(); }
64 constexpr explicit operator bool() const noexcept { return id_ != invalid(); }
65
66 constexpr Id get() const noexcept { return id_; }
67 constexpr Id operator*() const noexcept { return get(); }
68
70 {
71 const auto id = id_;
72 id_ = invalid();
73 return id;
74 }
75 void reset(std::nullptr_t = nullptr) noexcept { reset(invalid()); }
76 void reset(Id id) noexcept
77 {
78 std::swap(id_, id);
79 if (id != invalid()) {
80 PolicyT::close(id);
81 }
82 }
83 void swap(BasicHandle& rhs) noexcept { std::swap(id_, rhs.id_); }
84
85 private:
86 Id id_{invalid()};
87};
88
89template <typename PolicyT>
91{
92 return lhs.get() == rhs.get();
93}
94
95template <typename PolicyT>
97{
98 return !(lhs == rhs);
99}
100
102 using Id = int;
103 static constexpr int invalid() noexcept { return -1; }
104 static void close(int fd) noexcept { ::close(fd); }
105};
106
108
109} // namespace io
110} // namespace toolbox
111
112#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:67
constexpr bool empty() const noexcept
Definition Handle.hpp:63
constexpr Id get() const noexcept
Definition Handle.hpp:66
BasicHandle(const BasicHandle &)=delete
typename PolicyT::Id Id
Definition Handle.hpp:30
Id release() noexcept
Definition Handle.hpp:69
BasicHandle & operator=(const BasicHandle &)=delete
constexpr BasicHandle(Id id) noexcept
Definition Handle.hpp:35
void reset(Id id) noexcept
Definition Handle.hpp:76
void reset(std::nullptr_t=nullptr) noexcept
Definition Handle.hpp:75
void swap(BasicHandle &rhs) noexcept
Definition Handle.hpp:83
constexpr BasicHandle(BasicHandle &&rhs) noexcept
Definition Handle.hpp:51
BasicHandle & operator=(BasicHandle &&rhs) noexcept
Definition Handle.hpp:56
constexpr bool operator==(const BasicHandle< PolicyT > &lhs, const BasicHandle< PolicyT > &rhs)
Definition Handle.hpp:90
constexpr bool operator!=(const BasicHandle< PolicyT > &lhs, const BasicHandle< PolicyT > &rhs)
Definition Handle.hpp:96
constexpr auto bind() noexcept
Definition Slot.hpp:97
static constexpr int invalid() noexcept
Definition Handle.hpp:103
static void close(int fd) noexcept
Definition Handle.hpp:104