]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/pipe.h
wxBase/GUI separation: 1st step, wxMSW should build, all the rest is broken
[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@wxwindows.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 // ----------------------------------------------------------------------------
18 // wxPipe: this class encapsulates pipe() system call
19 // ----------------------------------------------------------------------------
20
21 class wxPipe
22 {
23 public:
24 // the symbolic names for the pipe ends
25 enum Direction
26 {
27 Read,
28 Write
29 };
30
31 enum
32 {
33 INVALID_FD = -1
34 };
35
36 // default ctor doesn't do anything
37 wxPipe() { m_fds[Read] = m_fds[Write] = INVALID_FD; }
38
39 // create the pipe, return TRUE if ok, FALSE on error
40 bool Create()
41 {
42 if ( pipe(m_fds) == -1 )
43 {
44 wxLogSysError(_("Pipe creation failed"));
45
46 return FALSE;
47 }
48
49 return TRUE;
50 }
51
52 // return TRUE if we were created successfully
53 bool IsOk() const { return m_fds[Read] != INVALID_FD; }
54
55 // return the descriptor for one of the pipe ends
56 int operator[](Direction which) const
57 {
58 wxASSERT_MSG( which >= 0 && (size_t)which < WXSIZEOF(m_fds),
59 _T("invalid pipe index") );
60
61 return m_fds[which];
62 }
63
64 // detach a descriptor, meaning that the pipe dtor won't close it, and
65 // return it
66 int Detach(Direction which)
67 {
68 wxASSERT_MSG( which >= 0 && (size_t)which < WXSIZEOF(m_fds),
69 _T("invalid pipe index") );
70
71 int fd = m_fds[which];
72 m_fds[which] = INVALID_FD;
73
74 return fd;
75 }
76
77 // close the pipe descriptors
78 void Close()
79 {
80 for ( size_t n = 0; n < WXSIZEOF(m_fds); n++ )
81 {
82 if ( m_fds[n] != INVALID_FD )
83 close(m_fds[n]);
84 }
85 }
86
87 // dtor closes the pipe descriptors
88 ~wxPipe() { Close(); }
89
90 private:
91 int m_fds[2];
92 };
93
94 #endif // _WX_UNIX_PIPE_H_
95