aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/include/winapi/process.hpp
blob: 2614c0f7788eb49f27ce7365de6d7d87aef1af23 (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
// 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.

#pragma once

#include "cmd_line.hpp"
#include "handle.hpp"
#include "stream.hpp"

#include <boost/config.hpp>

#include <utility>

namespace winapi {

class Process {
public:
    struct IO {
        IO() = default;

        void close();

        process::Stdin std_in;
        process::Stdout std_out;
        process::Stderr std_err;

        // VS 2013 won't generate these automatically.

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

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

        void 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);
        }

        IO(const IO&) = delete;
    };

    static Process create(const CommandLine&);
    static Process create(const CommandLine&, IO);

    void wait();

private:
    explicit Process(Handle&& handle) : m_handle{std::move(handle)} {}

    Handle m_handle;
};

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

} // namespace winapi

namespace std {

template <>
inline void swap(winapi::Process::IO& a, winapi::Process::IO& b) BOOST_NOEXCEPT_OR_NOTHROW {
    a.swap(b);
}

} // namespace std