Coverage Report

Created: 2025-09-27 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wxwidgets/include/wx/unix/pipe.h
Line
Count
Source
1
///////////////////////////////////////////////////////////////////////////////
2
// Name:        wx/unix/pipe.h
3
// Purpose:     wxPipe class
4
// Author:      Vadim Zeitlin
5
// Created:     24.06.2003 (extracted from src/unix/utilsunx.cpp)
6
// Copyright:   (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
7
// Licence:     wxWindows licence
8
///////////////////////////////////////////////////////////////////////////////
9
10
#ifndef _WX_UNIX_PIPE_H_
11
#define _WX_UNIX_PIPE_H_
12
13
#include <unistd.h>
14
#include <fcntl.h>
15
16
#include "wx/log.h"
17
#include "wx/intl.h"
18
19
// ----------------------------------------------------------------------------
20
// wxPipe: this class encapsulates pipe() system call
21
// ----------------------------------------------------------------------------
22
23
class wxPipe
24
{
25
public:
26
    // the symbolic names for the pipe ends
27
    enum Direction
28
    {
29
        Read,
30
        Write
31
    };
32
33
    enum
34
    {
35
        INVALID_FD = -1
36
    };
37
38
    // default ctor doesn't do anything
39
0
    wxPipe() { m_fds[Read] = m_fds[Write] = INVALID_FD; }
40
41
    // create the pipe, return TRUE if ok, FALSE on error
42
    bool Create()
43
0
    {
44
0
        if ( pipe(m_fds) == -1 )
45
0
        {
46
0
            wxLogSysError(wxGetTranslation("Pipe creation failed"));
47
48
0
            return false;
49
0
        }
50
51
0
        return true;
52
0
    }
53
54
    // switch the given end of the pipe to non-blocking IO
55
    bool MakeNonBlocking(Direction which)
56
0
    {
57
0
        const int flags = fcntl(m_fds[which], F_GETFL, 0);
58
0
        if ( flags == -1 )
59
0
            return false;
60
61
0
        return fcntl(m_fds[which], F_SETFL, flags | O_NONBLOCK) == 0;
62
0
    }
63
64
    // return TRUE if we were created successfully
65
0
    bool IsOk() const { return m_fds[Read] != INVALID_FD; }
66
67
    // return the descriptor for one of the pipe ends
68
0
    int operator[](Direction which) const { return m_fds[which]; }
69
70
    // detach a descriptor, meaning that the pipe dtor won't close it, and
71
    // return it
72
    int Detach(Direction which)
73
0
    {
74
0
        int fd = m_fds[which];
75
0
        m_fds[which] = INVALID_FD;
76
77
0
        return fd;
78
0
    }
79
80
    // close the pipe descriptors
81
    void Close()
82
0
    {
83
0
        for ( size_t n = 0; n < WXSIZEOF(m_fds); n++ )
84
0
        {
85
0
            if ( m_fds[n] != INVALID_FD )
86
0
            {
87
0
                close(m_fds[n]);
88
0
                m_fds[n] = INVALID_FD;
89
0
            }
90
0
        }
91
0
    }
92
93
    // dtor closes the pipe descriptors
94
0
    ~wxPipe() { Close(); }
95
96
private:
97
    int m_fds[2];
98
};
99
100
#endif // _WX_UNIX_PIPE_H_
101