ONPOSIX
2.0
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Enumerator
Friends
Macros
Pages
Pipe.hpp
Go to the documentation of this file.
1
/*
2
* Pipe.hpp
3
*
4
* Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5
*
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
15
*
16
* You should have received a copy of the GNU Library General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
*/
20
21
#ifndef PIPE_HPP_
22
#define PIPE_HPP_
23
24
#include "
PosixDescriptor.hpp
"
25
26
#include <unistd.h>
27
#include <limits.h>
28
29
30
namespace
onposix {
31
32
/**
33
* \brief Posix pipe.
34
*
35
* This class wraps two PosixDescriptors because in some circumstances
36
* (e.g., after a fork()) the code wants to explicitly close one of the
37
* endpoints.
38
* The getReadDescriptor() and getWriteDescriptor() methods allow to get
39
* the PosixDescriptor and explicitly close it.
40
*/
41
class
Pipe
{
42
43
PosixDescriptor
*
read_
;
44
PosixDescriptor
*
write_
;
45
46
// Disable default copy constructor
47
Pipe
(
const
Pipe
&);
48
49
public
:
50
Pipe
();
51
52
/**
53
* \brief Destructor.
54
*
55
* The destructor of PosixDescriptor will close the descriptor.
56
*/
57
virtual
~Pipe
() {
58
if
(
write_
!= 0)
59
delete
write_
;
60
if
(
read_
!= 0)
61
delete
read_
;
62
}
63
64
/**
65
* \brief Get the write endpoint
66
*
67
* This method is useful if we want to explicitly close the read
68
* endpoint.
69
* @return A pointer to the PosixDescriptor for reading
70
*/
71
PosixDescriptor
*
getReadDescriptor
() {
72
return
read_
;
73
}
74
75
/**
76
* \brief Get the read endpoint
77
*
78
* This method is useful if we want to explicitly close the write
79
* endpoint.
80
* @return A pointer to the PosixDescriptor for writing
81
*/
82
PosixDescriptor
*
getWriteDescriptor
() {
83
return
write_
;
84
}
85
86
/**
87
* \brief Method to read from the pipe and fill a buffer.
88
*
89
* Note: this method may block current thread if data is not
90
* available.
91
* The buffer is filled with the read data.
92
* @param b Pointer to the buffer to be filled
93
* @param size Number of bytes that must be read
94
* @return -1 in case of error; the number of bytes read otherwise
95
*/
96
inline
int
read
(
Buffer
* b,
size_t
size) {
97
return
read_
->
read
(b, size);
98
}
99
100
/**
101
* \brief Method to read from the pipe.
102
*
103
* Note: this method may block current thread if data is not
104
* available.
105
* The buffer is filled with the read data.
106
* @param p Pointer to the memory space to be filled
107
* @param size Number of bytes that must be read
108
* @return -1 in case of error; the number of bytes read otherwise
109
*/
110
inline
int
read
(
void
* p,
size_t
size) {
111
return
read_
->
read
(p, size);
112
}
113
114
/**
115
* \brief Method to write data in a buffer to the pipe.
116
*
117
* Note: this method may block current thread if data cannot
118
* be written.
119
* @param b Pointer to the buffer to be filled
120
* @param size Number of bytes that must be written
121
* @return -1 in case of error; the number of bytes read otherwise
122
*/
123
inline
int
write
(
Buffer
* b,
size_t
size) {
124
return
write_
->
write
(b, size);
125
}
126
127
/**
128
* \brief Method to write in the pipe.
129
*
130
* Note: this method may block current thread if data cannot
131
* be written.
132
* @param p Pointer to the memory space containing data
133
* @param size Number of bytes that must be written
134
* @return -1 in case of error; the number of bytes read otherwise
135
*/
136
inline
int
write
(
const
void
* p,
size_t
size) {
137
return
write_
->
write
(p, size);
138
}
139
140
/**
141
* \brief Method to write a string in the pipe.
142
*
143
* Note: this method may block current thread if data cannot
144
* be written.
145
* @param string to be written
146
* @return -1 in case of error; the number of bytes read otherwise
147
*/
148
inline
int
write
(
const
std::string& s) {
149
return
write_
->
write
(s);
150
}
151
152
/**
153
* \brief Method to close the pipe.
154
*
155
* Note: currently there is no method to re-open the pipe.
156
*/
157
inline
void
close
(){
158
write_
->
close
();
159
read_
->
close
();
160
}
161
162
/**
163
* \brief Get posix capacity of the pipe
164
*
165
* @return capacity of the pipe based on the Posix standard
166
*/
167
static
inline
long
int
getPosixCapacity
() {
168
return
_POSIX_PIPE_BUF;
169
}
170
171
/**
172
* \brief Get real capacity of the pipe
173
*
174
* @return real capacity of the pipe
175
*/
176
inline
long
int
getRealCapacity
()
const
{
177
return
fpathconf(
read_
->
getDescriptorNumber
(),
178
_PC_PIPE_BUF);
179
}
180
};
181
182
}
/* onposix */
183
184
#endif
/* PIPE_HPP_ */
include
Pipe.hpp
Generated on Wed Apr 30 2014 15:27:24 for ONPOSIX by
1.8.1.2