aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/process_io.cpp
blob: ec7dbc9a777b7fa8f8f3ed06ba80394c9d57ad82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "winapi-common" project.
// For details, see https://github.com/egor-tensin/winapi-common.
// Distributed under the MIT License.

#include <winapi/file.hpp>
#include <winapi/handle.hpp>
#include <winapi/path.hpp>
#include <winapi/process_io.hpp>

#include <boost/config.hpp>

#include <string>
#include <utility>

namespace winapi {
namespace process {

Stdin::Stdin() : Stream{Handle::std_in()} {}

Stdout::Stdout() : Stream{Handle::std_out()} {}

Stderr::Stderr() : Stream{Handle::std_err()} {}

Stdin::Stdin(const std::string& path) : Stream{File::open_r(path)} {}

Stdin::Stdin(const CanonicalPath& path) : Stream{File::open_r(path)} {}

Stdout::Stdout(const std::string& path) : Stream{File::open_w(path)} {}

Stdout::Stdout(const CanonicalPath& path) : Stream{File::open_w(path)} {}

Stderr::Stderr(const std::string& path) : Stream{File::open_w(path)} {}

Stderr::Stderr(const CanonicalPath& path) : Stream{File::open_w(path)} {}

Stdin::Stdin(Pipe& pipe) : Stream{std::move(pipe.read_end())} {
    pipe.write_end().dont_inherit();
}

Stdout::Stdout(Pipe& pipe) : Stream{std::move(pipe.write_end())} {
    pipe.read_end().dont_inherit();
}

Stderr::Stderr(Pipe& pipe) : Stream{std::move(pipe.write_end())} {
    pipe.read_end().dont_inherit();
}

Stream::Stream(Stream&& other) BOOST_NOEXCEPT_OR_NOTHROW {
    swap(other);
}

Stream& Stream::operator=(Stream other) BOOST_NOEXCEPT_OR_NOTHROW {
    swap(other);
    return *this;
}

void Stream::swap(Stream& other) BOOST_NOEXCEPT_OR_NOTHROW {
    using std::swap;
    swap(handle, other.handle);
}

void swap(Stream& a, Stream& b) BOOST_NOEXCEPT_OR_NOTHROW {
    a.swap(b);
}

Stdin::Stdin(Stdin&& other) BOOST_NOEXCEPT_OR_NOTHROW : Stream{std::move(other)} {}

Stdin& Stdin::operator=(Stdin other) BOOST_NOEXCEPT_OR_NOTHROW {
    Stream::operator=(std::move(other));
    return *this;
}

Stdout::Stdout(Stdout&& other) BOOST_NOEXCEPT_OR_NOTHROW : Stream{std::move(other)} {}

Stdout& Stdout::operator=(Stdout other) BOOST_NOEXCEPT_OR_NOTHROW {
    Stream::operator=(std::move(other));
    return *this;
}

Stderr::Stderr(Stderr&& other) BOOST_NOEXCEPT_OR_NOTHROW : Stream{std::move(other)} {}

Stderr& Stderr::operator=(Stderr other) BOOST_NOEXCEPT_OR_NOTHROW {
    Stream::operator=(std::move(other));
    return *this;
}

void IO::close() {
    std_in.handle.close();
    std_out.handle.close();
    std_err.handle.close();
}

IO::IO(IO&& other) BOOST_NOEXCEPT_OR_NOTHROW {
    swap(other);
}

IO& IO::operator=(IO other) BOOST_NOEXCEPT_OR_NOTHROW {
    swap(other);
    return *this;
}

void IO::swap(IO& other) BOOST_NOEXCEPT_OR_NOTHROW {
    using std::swap;
    swap(std_in, other.std_in);
    swap(std_out, other.std_out);
    swap(std_err, other.std_err);
}

void swap(IO& a, IO& b) BOOST_NOEXCEPT_OR_NOTHROW {
    a.swap(b);
}

} // namespace process
} // namespace winapi