Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
io
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
24
namespace
toolbox
{
25
inline
namespace
io {
26
27
template
<
typename
PolicyT>
28
class
BasicHandle
{
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
}
40
~BasicHandle
()
41
{
42
if
(id_ !=
invalid
()) {
43
PolicyT::close(id_);
44
}
45
}
46
47
// Copy.
48
BasicHandle
(
const
BasicHandle
&) =
delete
;
49
BasicHandle
&
operator=
(
const
BasicHandle
&) =
delete
;
50
51
// Move.
52
constexpr
BasicHandle
(
BasicHandle
&&
rhs
)
noexcept
53
: id_{
rhs
.id_}
54
{
55
rhs
.id_ =
invalid
();
56
}
57
BasicHandle
&
operator=
(
BasicHandle
&&
rhs
)
noexcept
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
70
Id
release
()
noexcept
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
90
template
<
typename
PolicyT>
91
constexpr
bool
operator==
(
const
BasicHandle<PolicyT>
&
lhs
,
const
BasicHandle<PolicyT>
&
rhs
)
92
{
93
return
lhs
.get() ==
rhs
.get();
94
}
95
96
template
<
typename
PolicyT>
97
constexpr
bool
operator!=
(
const
BasicHandle<PolicyT>
&
lhs
,
const
BasicHandle<PolicyT>
&
rhs
)
98
{
99
return
!(
lhs
==
rhs
);
100
}
101
102
struct
FilePolicy
{
103
using
Id
=
int
;
104
static
constexpr
int
invalid
()
noexcept
{
return
-1; }
105
static
void
close
(
int
fd)
noexcept
{
::close
(fd); }
106
};
107
108
using
FileHandle
=
BasicHandle<FilePolicy>
;
109
110
}
// namespace io
111
}
// namespace toolbox
112
113
#endif
// TOOLBOX_IO_HANDLE_HPP
toolbox::BasicHandle
Definition
Handle.hpp:28
toolbox::io::BasicHandle::BasicHandle
constexpr BasicHandle(std::nullptr_t=nullptr) noexcept
Definition
Handle.hpp:34
toolbox::io::BasicHandle::invalid
static constexpr Id invalid() noexcept
Definition
Handle.hpp:32
toolbox::io::BasicHandle::operator*
constexpr Id operator*() const noexcept
Definition
Handle.hpp:68
toolbox::io::BasicHandle::empty
constexpr bool empty() const noexcept
Definition
Handle.hpp:64
toolbox::io::BasicHandle::get
constexpr Id get() const noexcept
Definition
Handle.hpp:67
toolbox::io::BasicHandle::BasicHandle
BasicHandle(const BasicHandle &)=delete
toolbox::io::BasicHandle::~BasicHandle
~BasicHandle()
Definition
Handle.hpp:40
toolbox::io::BasicHandle::Id
typename PolicyT::Id Id
Definition
Handle.hpp:30
toolbox::io::BasicHandle::release
Id release() noexcept
Definition
Handle.hpp:70
toolbox::io::BasicHandle::operator=
BasicHandle & operator=(const BasicHandle &)=delete
toolbox::io::BasicHandle::BasicHandle
constexpr BasicHandle(Id id) noexcept
Definition
Handle.hpp:36
toolbox::io::BasicHandle::reset
void reset(Id id) noexcept
Definition
Handle.hpp:77
toolbox::io::BasicHandle::reset
void reset(std::nullptr_t=nullptr) noexcept
Definition
Handle.hpp:76
toolbox::io::BasicHandle::swap
void swap(BasicHandle &rhs) noexcept
Definition
Handle.hpp:84
toolbox::io::BasicHandle::BasicHandle
constexpr BasicHandle(BasicHandle &&rhs) noexcept
Definition
Handle.hpp:52
toolbox::io::BasicHandle::operator=
BasicHandle & operator=(BasicHandle &&rhs) noexcept
Definition
Handle.hpp:57
toolbox::io::operator==
constexpr bool operator==(const BasicHandle< PolicyT > &lhs, const BasicHandle< PolicyT > &rhs)
Definition
Handle.hpp:91
toolbox::io::operator!=
constexpr bool operator!=(const BasicHandle< PolicyT > &lhs, const BasicHandle< PolicyT > &rhs)
Definition
Handle.hpp:97
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox
Definition
Benchmark.cpp:26
toolbox::FilePolicy
Definition
Handle.hpp:102
toolbox::io::FilePolicy::invalid
static constexpr int invalid() noexcept
Definition
Handle.hpp:104
toolbox::io::FilePolicy::Id
int Id
Definition
Handle.hpp:103
toolbox::io::FilePolicy::close
static void close(int fd) noexcept
Definition
Handle.hpp:105
Generated by
1.9.8