3 //=============================================================================
7 * @author James CE Johnson <jcej@lads.com>
8 * @author Jim Crossley <jim@lads.com>
10 //=============================================================================
12 #ifndef ACE_IOSTREAM_H
13 #define ACE_IOSTREAM_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/ACE_export.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 // Needed on Windows for streambuf
23 // FUZZ: disable check_for_streams_include
24 #include "ace/streams.h"
26 // This is a temporary restriction - ACE_IOStream is only enabled if the
27 // compiler does not supply the standard C++ library (and standard iostreams)
28 // or, if it does, the platform is explicitly set to use old iostreams
29 // by its config.h file.
30 // This restriction is recorded in Bugzilla entry 857.
31 #if defined (ACE_HAS_STANDARD_CPP_LIBRARY) && (ACE_HAS_STANDARD_CPP_LIBRARY == 1)
32 # if !defined (ACE_USES_OLD_IOSTREAMS) && !defined (ACE_LACKS_ACE_IOSTREAM)
33 # define ACE_LACKS_ACE_IOSTREAM
34 # endif /* !ACE_USES_OLD_IOSTREAMS && !ACE_LACKS_ACE_IOSTREAM */
35 #endif /* ACE_HAS_STANDARD_CPP_LIBRARY */
37 #if !defined (ACE_LACKS_ACE_IOSTREAM)
39 # if defined (ACE_HAS_STRING_CLASS)
40 # if defined (ACE_WIN32) && defined (_MSC_VER)
41 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
42 typedef CString ACE_IOStream_String
;
43 ACE_END_VERSIONED_NAMESPACE_DECL
45 # if !defined (ACE_HAS_STDCPP_STL_INCLUDES)
46 #include /**/ <String.h>
47 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
48 typedef String ACE_IOStream_String
;
49 ACE_END_VERSIONED_NAMESPACE_DECL
51 # include /**/ <string>
53 # if defined(ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB)
54 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
55 typedef std::string ACE_IOStream_String
;
56 ACE_END_VERSIONED_NAMESPACE_DECL
58 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
59 typedef string ACE_IOStream_String
;
60 ACE_END_VERSIONED_NAMESPACE_DECL
61 # endif /* ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB */
62 # endif /* ! ACE_HAS_STDCPP_STL_INCLUDES */
63 # endif /* ACE_WIN32 && defined (_MSC_VER) */
65 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
67 class ACE_Export ACE_Quoted_String
: public ACE_IOStream_String
70 inline ACE_Quoted_String (void) { *this = ""; }
71 inline ACE_Quoted_String (const char *c
) { *this = ACE_IOStream_String (c
); }
72 inline ACE_Quoted_String (const ACE_IOStream_String
&s
) { *this = s
; }
73 inline ACE_Quoted_String
&operator= (const ACE_IOStream_String
& s
)
75 return (ACE_Quoted_String
&) ACE_IOStream_String::operator= (s
);
77 inline ACE_Quoted_String
&operator = (const char c
) {
78 return (ACE_Quoted_String
&) ACE_IOStream_String::operator= (c
);
80 inline ACE_Quoted_String
&operator = (const char *c
) {
81 return (ACE_Quoted_String
&) ACE_IOStream_String::operator= (c
);
83 inline bool operator < (const ACE_Quoted_String
&s
) const {
84 return *(ACE_IOStream_String
*) this < (ACE_IOStream_String
) s
;
86 # if defined (ACE_WIN32) && defined (_MSC_VER)
87 inline int length (void) { return this->GetLength (); }
88 # endif /* ACE_WIN32 && defined (_MSC_VER) */
91 ACE_END_VERSIONED_NAMESPACE_DECL
93 # endif /* ACE_HAS_STRING_CLASS */
95 # include "ace/Time_Value.h"
96 # include "ace/os_include/sys/os_types.h"
98 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
101 * @class ACE_Streambuf
103 * @brief Create your custom streambuf by providing and ACE_*_Stream
104 * object to this template. I have tested it with
105 * ACE_SOCK_Stream and it should work fine for others as well.
107 * For any iostream object, the real work is done by the
108 * underlying streambuf class. That is what we create here.
109 * A streambuf has an internal buffer area into which data is
110 * read and written as the iostream requests and provides data.
111 * At some point during the read process, the iostream will
112 * realize that the streambuf has no more data. The underflow
113 * function of the streambuf is then called.
114 * Likewise, during the write process, the iostream will
115 * eventually notice that the streabuf's buffer has become full
116 * and will invoke the overflow function.
117 * The empty/full state of the read/write "buffers" are
118 * controled by two sets pointers. One set is dedicated to
119 * read, the other to write. These pointers, in turn, reference
120 * a common buffer that is to be shared by both read and write
121 * operations. It is this common buffer to which data is
122 * written and from which it is read.
123 * The common buffer is used by functions of the streambuf as
124 * well as the iostream. Because of this and the fact that it
125 * is "shared" by both read and write operators, there is a
126 * danger of data corruption if read and write operations are
127 * allowed to take place "at the same time".
128 * To prevent data corruption, we manipulate the read and write
129 * pointer sets so that the streambuf is in either a read-mode
130 * or write-mode at all times and can never be in both modes at
132 * In the constructor: set the read and write sets to NULL This
133 * causes the underflow or overflow operators to be invoked at
134 * the first IO activity of the iostream.
135 * In the underflow function we arrange for the common buffer to
136 * reference our read buffer and for the write pointer set to be
137 * disabled. If a write operation is performed by the iostream
138 * this will cause the overflow function to be invoked.
139 * In the overflow function we arrange for the common buffer to
140 * reference our write buffer and for the read pointer set to be
141 * disabled. This causes the underflow function to be invoked
142 * when the iostream "changes our mode".
143 * The overflow function will also invoke the send_n function to
144 * flush the buffered data to our peer. Similarly, the sync and
145 * syncout functions will cause send_n to be invoked to send the
147 * Since socket's and the like do not support seeking, there can
148 * be no method for "syncing" the input. However, since we
149 * maintain separate read/write buffers, no data is lost by
150 * "syncing" the input. It simply remains buffered.
152 class ACE_Export ACE_Streambuf
: public streambuf
157 * If the default allocation strategy were used the common buffer
158 * would be deleted when the object destructs. Since we are
159 * providing separate read/write buffers, it is up to us to manage
162 virtual ~ACE_Streambuf (void);
164 /// Get the current Time_Value pointer and provide a new one.
165 ACE_Time_Value
*recv_timeout (ACE_Time_Value
*tv
= 0);
168 * Use this to allocate a new/different buffer for put operations.
169 * If you do not provide a buffer pointer, one will be allocated.
170 * That is the preferred method. If you do provide a buffer, the
171 * size must match that being used by the get buffer. If
172 * successful, you will receive a pointer to the current put buffer.
173 * It is your responsibility to delete this memory when you are done
176 char *reset_put_buffer (char *newBuffer
= 0,
177 u_int _streambuf_size
= 0,
180 /// Return the number of bytes to be 'put' onto the stream media.
181 /// pbase + put_avail = pptr
182 u_int
put_avail (void);
185 * Use this to allocate a new/different buffer for get operations.
186 * If you do not provide a buffer pointer, one will be allocated.
187 * That is the preferred method. If you do provide a buffer, the
188 * size must match that being used by the put buffer. If
189 * successful, you will receive a pointer to the current get buffer.
190 * It is your responsibility to delete this memory when you are done
193 char *reset_get_buffer (char *newBuffer
= 0,
194 u_int _streambuf_size
= 0,
198 /// Return the number of bytes not yet gotten. eback + get_waiting =
200 u_int
get_waiting (void);
202 /// Return the number of bytes in the get area (includes some already
203 /// gotten); eback + get_avail = egptr
204 u_int
get_avail (void);
206 /// Query the streambuf for the size of its buffers.
207 u_int
streambuf_size (void);
209 /// Did we take an error because of an IO operation timeout?
210 /// @note Invoking this resets the flag.
211 u_char
timeout (void);
214 ACE_Streambuf (u_int streambuf_size
,
217 /// Sync both input and output. See syncin/syncout below for
219 virtual int sync (void);
221 // = Signatures for the underflow/overflow discussed above.
222 virtual int underflow (void);
224 /// The overflow function receives the character which caused the
226 virtual int overflow (int c
= EOF
);
228 /// Resets the <base> pointer and streambuf mode. This is used
229 /// internally when get/put buffers are allocatd.
230 void reset_base (void);
233 // = Two pointer sets for manipulating the read/write areas.
241 // = With cur_mode_ we keep track of our current IO mode.
243 // This helps us to optimize the underflow/overflow functions.
245 const u_char get_mode_
;
246 const u_char put_mode_
;
248 /// mode tells us if we're working for an istream, ostream, or
252 /// This defines the size of the input and output buffers. It can be
253 /// set by the object constructor.
254 const u_int streambuf_size_
;
256 /// Did we take an error because of an IO operation timeout?
259 /// We want to allow the user to provide Time_Value pointers to
260 /// prevent infinite blocking while waiting to receive data.
261 ACE_Time_Value recv_timeout_value_
;
262 ACE_Time_Value
*recv_timeout_
;
265 * syncin is called when the input needs to be synced with the
266 * source file. In a filebuf, this results in the <seek> system
267 * call being used. We can't do that on socket-like connections, so
268 * this does basically nothing. That's safe because we have a
269 * separate read buffer to maintain the already-read data. In a
270 * filebuf, the single common buffer is used forcing the <seek>
275 /// syncout() is called when the output needs to be flushed. This is
276 /// easily done by calling the peer's send_n function.
279 /// flushbuf() is the worker of syncout. It is a separate function
280 /// because it gets used sometimes in different context.
284 * fillbuf is called in a couple of places. This is the worker of
285 * underflow. It will attempt to fill the read buffer from the
291 * Used by fillbuf and others to get exactly one byte from the peer.
292 * recv_n is used to be sure we block until something is available.
293 * It is virtual because we really need to override it for
294 * datagram-derived objects.
296 virtual int get_one_byte (void);
299 * Stream connections and "unconnected connections" (ie --
300 * datagrams) need to work just a little differently. We derive
301 * custom Streambuf objects for them and provide these functions at
304 virtual ssize_t
send (char *buf
,
306 virtual ssize_t
recv (char *buf
,
308 ACE_Time_Value
*tv
= 0) = 0;
309 virtual ssize_t
recv (char *buf
,
312 ACE_Time_Value
*tv
= 0) = 0;
313 virtual ssize_t
recv_n (char *buf
,
316 ACE_Time_Value
*tv
= 0) = 0;
318 virtual ACE_HANDLE
get_handle (void);
320 # if defined (ACE_HAS_STANDARD_CPP_LIBRARY) && (ACE_HAS_STANDARD_CPP_LIBRARY != 0) && !defined (ACE_USES_OLD_IOSTREAMS)
321 char *base (void) const
323 return cur_mode_
== get_mode_
? eback_saved_
324 : cur_mode_
== put_mode_
? pbase_saved_
327 char *ebuf (void) const
329 return cur_mode_
== 0 ? 0 : base () + streambuf_size_
;
332 int blen (void) const
334 return streambuf_size_
;
337 void setb (char* b
, char* eb
, int /* a */=0)
339 setbuf (b
, (eb
- b
));
342 int out_waiting (void)
344 return pptr () - pbase ();
346 # endif /* ACE_HAS_STANDARD_CPP_LIBRARY */
349 ACE_END_VERSIONED_NAMESPACE_DECL
351 ///////////////////////////////////////////////////////////////////////////
353 // These typedefs are provided by G++ (on some systems?) without the
354 // trailing '_'. Since we can't count on 'em, I've defined them to
355 // what GNU wants here.
357 typedef ios
& (*__manip_
)(ios
&);
358 typedef istream
& (*__imanip_
)(istream
&);
359 typedef ostream
& (*__omanip_
)(ostream
&);
361 // Trying to do something like is shown below instead of using the
362 // __*manip typedefs causes Linux do segfault when "<<endl" is done.
364 // virtual MT& operator<<(ios& (*func)(ios&)) { (*func)(*this); return *this; }
366 // This macro defines the get operator for class MT into datatype DT.
367 // We will use it below to quickly override most (all?) iostream get
368 // operators. Notice how the <ipfx> and <isfx> functions are used.
370 #define GET_SIG(MT,DT) inline virtual MT& operator>> (DT v)
371 # if (defined (__SUNPRO_CC) && __SUNPRO_CC > 0x510)
375 (*((istream*)this)) >> (v); \
384 iostream::operator>> (v); \
390 #define GET_PROT(MT,DT,CODE) GET_SIG(MT,DT) CODE
391 #define GET_FUNC(MT,DT) GET_PROT(MT,DT,GET_CODE)
393 // This macro defines the put operator for class MT into datatype DT.
394 // We will use it below to quickly override most (all?) iostream put
395 // operators. Notice how the <opfx> and <osfx> functions are used.
397 #define PUT_SIG(MT,DT) inline virtual MT& operator<< (DT v)
398 # if (defined (__SUNPRO_CC) && __SUNPRO_CC > 0x510)
402 (*((ostream *) this)) << (v); \
411 iostream::operator<< (v); \
417 #define PUT_PROT(MT,DT,CODE) PUT_SIG(MT,DT) CODE
418 #define PUT_FUNC(MT,DT) PUT_PROT(MT,DT,PUT_CODE)
421 // These are necessary in case somebody wants to derive from us and
422 // override one of these with a custom approach.
424 # if defined (ACE_LACKS_CHAR_RIGHT_SHIFTS)
425 #define GET_FUNC_SET0(MT,CODE,CODE2) \
426 GET_PROT(MT,short &,CODE) \
427 GET_PROT(MT,u_short &,CODE) \
428 GET_PROT(MT,int &,CODE) \
429 GET_PROT(MT,u_int &,CODE) \
430 GET_PROT(MT,long &,CODE) \
431 GET_PROT(MT,u_long &,CODE) \
432 GET_PROT(MT,float &,CODE) \
433 GET_PROT(MT,double &,CODE) \
434 inline virtual MT& operator>>(__omanip_ func) CODE2 \
435 inline virtual MT& operator>>(__manip_ func) CODE2
437 #define GET_FUNC_SET0(MT,CODE,CODE2) \
438 GET_PROT(MT,short &,CODE) \
439 GET_PROT(MT,u_short &,CODE) \
440 GET_PROT(MT,int &,CODE) \
441 GET_PROT(MT,u_int &,CODE) \
442 GET_PROT(MT,long &,CODE) \
443 GET_PROT(MT,u_long &,CODE) \
444 GET_PROT(MT,float &,CODE) \
445 GET_PROT(MT,double &,CODE) \
446 GET_PROT(MT,char &,CODE) \
447 GET_PROT(MT,u_char &,CODE) \
448 GET_PROT(MT,char *,CODE) \
449 GET_PROT(MT,u_char *,CODE) \
450 inline virtual MT& operator>>(__omanip_ func) CODE2 \
451 inline virtual MT& operator>>(__manip_ func) CODE2
454 #define PUT_FUNC_SET0(MT,CODE,CODE2) \
455 PUT_PROT(MT,short,CODE) \
456 PUT_PROT(MT,u_short,CODE) \
457 PUT_PROT(MT,int,CODE) \
458 PUT_PROT(MT,u_int,CODE) \
459 PUT_PROT(MT,long,CODE) \
460 PUT_PROT(MT,u_long,CODE) \
461 PUT_PROT(MT,float,CODE) \
462 PUT_PROT(MT,double,CODE) \
463 PUT_PROT(MT,char,CODE) \
464 PUT_PROT(MT,u_char,CODE) \
465 PUT_PROT(MT,const char *,CODE) \
466 PUT_PROT(MT,u_char *,CODE) \
467 PUT_PROT(MT,void *,CODE) \
468 inline virtual MT& operator<<(__omanip_ func) CODE2 \
469 inline virtual MT& operator<<(__manip_ func) CODE2
471 # if defined (ACE_LACKS_SIGNED_CHAR)
472 #define GET_FUNC_SET1(MT,CODE,CODE2) GET_FUNC_SET0(MT,CODE,CODE2)
473 #define PUT_FUNC_SET1(MT,CODE,CODE2) PUT_FUNC_SET0(MT,CODE,CODE2)
475 #define GET_FUNC_SET1(MT,CODE,CODE2) \
476 GET_PROT(MT,signed char &,CODE) \
477 GET_PROT(MT,signed char *,CODE) \
478 GET_FUNC_SET0(MT,CODE,CODE2)
480 #define PUT_FUNC_SET1(MT,CODE,CODE2) \
481 PUT_FUNC(MT,signed char) \
482 PUT_FUNC(MT,const signed char *) \
483 PUT_FUNC_SET0(MT,CODE,CODE2)
484 # endif /* ACE_LACKS_SIGNED_CHAR */
486 #define GET_MANIP_CODE { if (ipfx ()) { (*func) (*this); } isfx (); return *this; }
487 #define PUT_MANIP_CODE { if (opfx ()) { (*func) (*this); } osfx (); return *this; }
489 #define GET_FUNC_SET(MT) GET_FUNC_SET1(MT,GET_CODE,GET_MANIP_CODE)
490 #define PUT_FUNC_SET(MT) PUT_FUNC_SET1(MT,PUT_CODE,PUT_MANIP_CODE)
491 #define GETPUT_FUNC_SET(MT) GET_FUNC_SET(MT) PUT_FUNC_SET(MT)
493 #define GET_SIG_SET(MT) GET_FUNC_SET1(MT,= 0;,= 0;)
494 #define PUT_SIG_SET(MT) PUT_FUNC_SET1(MT,= 0;,= 0;)
495 #define GETPUT_SIG_SET(MT) GET_SIG_SET(MT) PUT_SIG_SET(MT)
497 // Include the templates here.
498 # include "ace/IOStream_T.h"
499 #endif /* !ACE_LACKS_ACE_IOSTREAM && ACE_USES_OLD_IOSTREAMS */
501 #include /**/ "ace/post.h"
502 #endif /* ACE_IOSTREAM_H */