]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/pipe.h
split wxRegion(wxBitmap) ctor into two ctors with clearer semantics
[wxWidgets.git] / include / wx / unix / pipe.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/pipe.h
3 // Purpose: wxPipe class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 24.06.2003 (extracted from src/unix/utilsunx.cpp)
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_UNIX_PIPE_H_
13 #define _WX_UNIX_PIPE_H_
14
15 #include <unistd.h>
16
17 #include "wx/log.h"
18 #include "wx/intl.h"
19
20 // ----------------------------------------------------------------------------
21 // wxPipe: this class encapsulates pipe() system call
22 // ----------------------------------------------------------------------------
23
24 class wxPipe
25 {
26 public:
27 // the symbolic names for the pipe ends
28 enum Direction
29 {
30 Read,
31 Write,
32 Direction_Max
33 };
34
35 enum
36 {
37 INVALID_FD = -1
38 };
39
40 // default ctor doesn't do anything
41 wxPipe() { m_fds[Read] = m_fds[Write] = INVALID_FD; }
42
43 // create the pipe, return TRUE if ok, FALSE on error
44 bool Create()
45 {
46 if ( pipe(m_fds) == -1 )
47 {
48 wxLogSysError(_("Pipe creation failed"));
49
50 return FALSE;
51 }
52
53 return TRUE;
54 }
55
56 // return TRUE if we were created successfully
57 bool IsOk() const { return m_fds[Read] != INVALID_FD; }
58
59 // return the descriptor for one of the pipe ends
60 int operator[](Direction which) const { return m_fds[which]; }
61
62 // detach a descriptor, meaning that the pipe dtor won't close it, and
63 // return it
64 int Detach(Direction which)
65 {
66 int fd = m_fds[which];
67 m_fds[which] = INVALID_FD;
68
69 return fd;
70 }
71
72 // close the pipe descriptors
73 void Close()
74 {
75 for ( size_t n = 0; n < WXSIZEOF(m_fds); n++ )
76 {
77 if ( m_fds[n] != INVALID_FD )
78 close(m_fds[n]);
79 }
80 }
81
82 // dtor closes the pipe descriptors
83 ~wxPipe() { Close(); }
84
85 private:
86 int m_fds[Direction_Max];
87 };
88
89 #if wxUSE_STREAMS
90
91 #include "wx/wfstream.h"
92
93 // ----------------------------------------------------------------------------
94 // wxPipeInputStream: stream for reading from a pipe
95 // ----------------------------------------------------------------------------
96
97 class wxPipeInputStream : public wxFileInputStream
98 {
99 public:
100 wxPipeInputStream(int fd) : wxFileInputStream(fd) { }
101
102 // return TRUE if the pipe is still opened
103 bool IsOpened() const { return !Eof(); }
104
105 // return TRUE if we have anything to read, don't block
106 virtual bool CanRead() const;
107 };
108
109 #endif // wxUSE_STREAMS
110
111 #endif // _WX_UNIX_PIPE_H_
112