]> git.saurik.com Git - wxWidgets.git/blame - include/wx/unix/pipe.h
Fix warnings about implicit float or double to int conversions in wxMSW.
[wxWidgets.git] / include / wx / unix / pipe.h
CommitLineData
e2478fde
VZ
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$
77ffb593 8// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
65571936 9// Licence: wxWindows licence
e2478fde
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_UNIX_PIPE_H_
13#define _WX_UNIX_PIPE_H_
14
15#include <unistd.h>
cd794127 16#include <fcntl.h>
e2478fde 17
af848193
VS
18#include "wx/log.h"
19#include "wx/intl.h"
20
e2478fde
VZ
21// ----------------------------------------------------------------------------
22// wxPipe: this class encapsulates pipe() system call
23// ----------------------------------------------------------------------------
24
25class wxPipe
26{
27public:
28 // the symbolic names for the pipe ends
29 enum Direction
30 {
31 Read,
e4edebc0 32 Write
e2478fde
VZ
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
8e6efd1f 50 return false;
e2478fde
VZ
51 }
52
8e6efd1f 53 return true;
e2478fde
VZ
54 }
55
b8353201
VZ
56 // switch the given end of the pipe to non-blocking IO
57 bool MakeNonBlocking(Direction which)
58 {
59 const int flags = fcntl(m_fds[which], F_GETFL, 0);
60 if ( flags == -1 )
61 return false;
62
63 return fcntl(m_fds[which], F_SETFL, flags | O_NONBLOCK) == 0;
64 }
65
e2478fde
VZ
66 // return TRUE if we were created successfully
67 bool IsOk() const { return m_fds[Read] != INVALID_FD; }
68
69 // return the descriptor for one of the pipe ends
7fa4dc1c 70 int operator[](Direction which) const { return m_fds[which]; }
e2478fde
VZ
71
72 // detach a descriptor, meaning that the pipe dtor won't close it, and
73 // return it
74 int Detach(Direction which)
75 {
e2478fde
VZ
76 int fd = m_fds[which];
77 m_fds[which] = INVALID_FD;
78
79 return fd;
80 }
81
82 // close the pipe descriptors
83 void Close()
84 {
85 for ( size_t n = 0; n < WXSIZEOF(m_fds); n++ )
86 {
87 if ( m_fds[n] != INVALID_FD )
ca7aeeb7 88 {
e2478fde 89 close(m_fds[n]);
ca7aeeb7
VZ
90 m_fds[n] = INVALID_FD;
91 }
e2478fde
VZ
92 }
93 }
94
95 // dtor closes the pipe descriptors
96 ~wxPipe() { Close(); }
97
98private:
e4edebc0 99 int m_fds[2];
e2478fde
VZ
100};
101
102#endif // _WX_UNIX_PIPE_H_
103