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
constexpr
BasicHandle
(
Id
id
)
noexcept
36
: id_{
id
}
37
{
38
}
39
~BasicHandle
()
40
{
41
if
(id_ !=
invalid
()) {
42
PolicyT::close(id_);
43
}
44
}
45
46
// Copy.
47
BasicHandle
(
const
BasicHandle
&) =
delete
;
48
BasicHandle
&
operator=
(
const
BasicHandle
&) =
delete
;
49
50
// Move.
51
constexpr
BasicHandle
(
BasicHandle
&&
rhs
)
noexcept
52
: id_{
rhs
.id_}
53
{
54
rhs
.id_ =
invalid
();
55
}
56
BasicHandle
&
operator=
(
BasicHandle
&&
rhs
)
noexcept
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
69
Id
release
()
noexcept
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
89
template
<
typename
PolicyT>
90
constexpr
bool
operator==
(
const
BasicHandle<PolicyT>
&
lhs
,
const
BasicHandle<PolicyT>
&
rhs
)
91
{
92
return
lhs
.get() ==
rhs
.get();
93
}
94
95
template
<
typename
PolicyT>
96
constexpr
bool
operator!=
(
const
BasicHandle<PolicyT>
&
lhs
,
const
BasicHandle<PolicyT>
&
rhs
)
97
{
98
return
!(
lhs
==
rhs
);
99
}
100
101
struct
FilePolicy
{
102
using
Id
=
int
;
103
static
constexpr
int
invalid
()
noexcept
{
return
-1; }
104
static
void
close
(
int
fd)
noexcept
{
::close
(fd); }
105
};
106
107
using
FileHandle
=
BasicHandle<FilePolicy>
;
108
109
}
// namespace io
110
}
// namespace toolbox
111
112
#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:67
toolbox::io::BasicHandle::empty
constexpr bool empty() const noexcept
Definition
Handle.hpp:63
toolbox::io::BasicHandle::get
constexpr Id get() const noexcept
Definition
Handle.hpp:66
toolbox::io::BasicHandle::BasicHandle
BasicHandle(const BasicHandle &)=delete
toolbox::io::BasicHandle::~BasicHandle
~BasicHandle()
Definition
Handle.hpp:39
toolbox::io::BasicHandle::Id
typename PolicyT::Id Id
Definition
Handle.hpp:30
toolbox::io::BasicHandle::release
Id release() noexcept
Definition
Handle.hpp:69
toolbox::io::BasicHandle::operator=
BasicHandle & operator=(const BasicHandle &)=delete
toolbox::io::BasicHandle::BasicHandle
constexpr BasicHandle(Id id) noexcept
Definition
Handle.hpp:35
toolbox::io::BasicHandle::reset
void reset(Id id) noexcept
Definition
Handle.hpp:76
toolbox::io::BasicHandle::reset
void reset(std::nullptr_t=nullptr) noexcept
Definition
Handle.hpp:75
toolbox::io::BasicHandle::swap
void swap(BasicHandle &rhs) noexcept
Definition
Handle.hpp:83
toolbox::io::BasicHandle::BasicHandle
constexpr BasicHandle(BasicHandle &&rhs) noexcept
Definition
Handle.hpp:51
toolbox::io::BasicHandle::operator=
BasicHandle & operator=(BasicHandle &&rhs) noexcept
Definition
Handle.hpp:56
toolbox::io::operator==
constexpr bool operator==(const BasicHandle< PolicyT > &lhs, const BasicHandle< PolicyT > &rhs)
Definition
Handle.hpp:90
toolbox::io::operator!=
constexpr bool operator!=(const BasicHandle< PolicyT > &lhs, const BasicHandle< PolicyT > &rhs)
Definition
Handle.hpp:96
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:97
toolbox
Definition
Benchmark.cpp:27
toolbox::FilePolicy
Definition
Handle.hpp:101
toolbox::io::FilePolicy::invalid
static constexpr int invalid() noexcept
Definition
Handle.hpp:103
toolbox::io::FilePolicy::Id
int Id
Definition
Handle.hpp:102
toolbox::io::FilePolicy::close
static void close(int fd) noexcept
Definition
Handle.hpp:104
Generated by
1.9.8