File: executeiohandler.h

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (136 lines) | stat: -rw-r--r-- 4,000 bytes parent folder | download | duplicates (10)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
///////////////////////////////////////////////////////////////////////////////
// Name:        wx/unix/private/executeiohandler.h
// Purpose:     IO handler class for the FD used by wxExecute() under Unix
// Author:      Rob Bresalier, Vadim Zeitlin
// Created:     2013-01-06
// Copyright:   (c) 2013 Rob Bresalier, Vadim Zeitlin
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#ifndef _WX_UNIX_PRIVATE_EXECUTEIOHANDLER_H_
#define _WX_UNIX_PRIVATE_EXECUTEIOHANDLER_H_

#include "wx/private/streamtempinput.h"

// This class handles IO events on the pipe FD connected to the child process
// stdout/stderr and is used by wxExecute().
//
// Currently it can derive from either wxEventLoopSourceHandler or
// wxFDIOHandler depending on the kind of dispatcher/event loop it is used
// with. In the future, when we get rid of wxFDIOHandler entirely, it will
// derive from wxEventLoopSourceHandler only.
template <class T>
class wxExecuteIOHandlerBase : public T
{
public:
    wxExecuteIOHandlerBase(int fd, wxStreamTempInputBuffer& buf)
        : m_fd(fd),
          m_buf(buf)
    {
        m_callbackDisabled = false;
    }

    // Called when the associated descriptor is available for reading.
    virtual void OnReadWaiting()
    {
        // Sync process, process all data coming at us from the pipe so that
        // the pipe does not get full and cause a deadlock situation.
        m_buf.Update();

        if ( m_buf.Eof() )
            DisableCallback();
    }

    // These methods are never called as we only monitor the associated FD for
    // reading, but we still must implement them as they're pure virtual in the
    // base class.
    virtual void OnWriteWaiting() { }
    virtual void OnExceptionWaiting() { }

    // Disable any future calls to our OnReadWaiting(), can be called when
    // we're sure that no more input is forthcoming.
    void DisableCallback()
    {
        if ( !m_callbackDisabled )
        {
            m_callbackDisabled = true;

            DoDisable();
        }
    }

protected:
    const int m_fd;

private:
    virtual void DoDisable() = 0;

    wxStreamTempInputBuffer& m_buf;

    // If true, DisableCallback() had been already called.
    bool m_callbackDisabled;

    wxDECLARE_NO_COPY_CLASS(wxExecuteIOHandlerBase);
};

// This is the version used with wxFDIODispatcher, which must be passed to the
// ctor in order to register this handler with it.
class wxExecuteFDIOHandler : public wxExecuteIOHandlerBase<wxFDIOHandler>
{
public:
    wxExecuteFDIOHandler(wxFDIODispatcher& dispatcher,
                         int fd,
                         wxStreamTempInputBuffer& buf)
        : wxExecuteIOHandlerBase<wxFDIOHandler>(fd, buf),
          m_dispatcher(dispatcher)
    {
        dispatcher.RegisterFD(fd, this, wxFDIO_INPUT);
    }

    virtual ~wxExecuteFDIOHandler()
    {
        DisableCallback();
    }

private:
    virtual void DoDisable()
    {
        m_dispatcher.UnregisterFD(m_fd);
    }

    wxFDIODispatcher& m_dispatcher;

    wxDECLARE_NO_COPY_CLASS(wxExecuteFDIOHandler);
};

// And this is the version used with an event loop. As AddSourceForFD() is
// static, we don't require passing the event loop to the ctor but an event
// loop must be running to handle our events.
class wxExecuteEventLoopSourceHandler
    : public wxExecuteIOHandlerBase<wxEventLoopSourceHandler>
{
public:
    wxExecuteEventLoopSourceHandler(int fd, wxStreamTempInputBuffer& buf)
        : wxExecuteIOHandlerBase<wxEventLoopSourceHandler>(fd, buf)
    {
        m_source = wxEventLoop::AddSourceForFD(fd, this, wxEVENT_SOURCE_INPUT);
    }

    virtual ~wxExecuteEventLoopSourceHandler()
    {
        DisableCallback();
    }

private:
    virtual void DoDisable()
    {
        delete m_source;
        m_source = NULL;
    }

    wxEventLoopSource* m_source;

    wxDECLARE_NO_COPY_CLASS(wxExecuteEventLoopSourceHandler);
};

#endif // _WX_UNIX_PRIVATE_EXECUTEIOHANDLER_H_