Document return values
[ACE_TAO.git] / ACE / ace / IOStream_T.h
blob0ac556978a59c526177a2dfd39999ca5f351c15c
1 /* -*- C++ -*- */
3 //=============================================================================
4 /**
5 * @file IOStream_T.h
7 * @author James CE Johnson <jcej@lads.com>
8 * @author Jim Crossley <jim@lads.com>
10 * This file should not be included directly by application
11 * code. Instead, it should include "ace/IOStream.h". That's because
12 * we only put some conditional compilations in that file.
14 //=============================================================================
16 #ifndef ACE_IOSTREAM_T_H
17 #define ACE_IOSTREAM_T_H
18 #include /**/ "ace/pre.h"
20 #include "ace/IOStream.h"
22 #if !defined (ACE_LACKS_PRAGMA_ONCE)
23 # pragma once
24 #endif /* ACE_LACKS_PRAGMA_ONCE */
26 #if !defined (ACE_LACKS_ACE_IOSTREAM)
28 # include "ace/INET_Addr.h"
29 # include "ace/Global_Macros.h"
31 # if defined (ACE_LACKS_IOSTREAM_FX)
32 # include "ace/os_include/os_ctype.h"
33 # endif /**/
35 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
37 template <class STREAM> STREAM & operator>> (STREAM &stream, ACE_Quoted_String &str);
38 template <class STREAM> STREAM & operator<< (STREAM &stream, ACE_Quoted_String &str);
40 template <class STREAM>
41 class ACE_Streambuf_T : public ACE_Streambuf
43 public:
44 /**
45 * We will be given a STREAM by the iostream object which creates
46 * us. See the ACE_IOStream template for how that works. Like
47 * other streambuf objects, we can be input-only, output-only or
48 * both.
50 ACE_Streambuf_T (STREAM *peer,
51 u_int streambuf_size = ACE_STREAMBUF_SIZE,
52 int io_mode = ios::in | ios::out);
54 virtual ssize_t send (char *buf, ssize_t len);
56 virtual ssize_t recv (char *buf,
57 ssize_t len,
58 ACE_Time_Value *tv = 0);
60 virtual ssize_t recv (char *buf,
61 ssize_t len,
62 int flags,
63 ACE_Time_Value * tv = 0);
65 virtual ssize_t recv_n (char *buf,
66 ssize_t len,
67 int flags = 0,
68 ACE_Time_Value *tv = 0);
70 protected:
71 virtual ACE_HANDLE get_handle ();
73 /// This will be our ACE_SOCK_Stream or similar object.
74 STREAM *peer_;
77 /**
78 * @class ACE_IOStream
80 * @brief A template adapter for creating an iostream-like object using
81 * an ACE IPC Stream for the actual I/O. Iostreams use an
82 * underlying streambuf object for the IO interface. The
83 * iostream class and derivatives provide you with a host of
84 * convenient operators that access the streambuf.
86 * We inherit all characteristics of iostream and your <STREAM>
87 * class. When you create a new class from this template, you
88 * can use it anywhere you would have used your original
89 * <STREAM> class.
90 * To create an iostream for your favorite ACE IPC class (e.g.,
91 * ACE_SOCK_Stream), feed that class to this template's
92 * <STREAM> parameter, e.g.,
93 * typedef ACE_Svc_Handler<ACE_SOCK_iostream,
94 * ACE_INET_Addr, ACE_NULL_SYNCH>
95 * Service_Handler;
96 * Because the operators in the iostream class are not virtual,
97 * you cannot easily provide overloads in your custom
98 * ACE_IOStream classes. To make these things work correctly,
99 * you need to overload ALL operators of the ACE_IOStream you
100 * create. I've attempted to do that here to make things easier
101 * for you but there are no guarantees.
102 * In the iostream.cpp file is an example of why it is necessary
103 * to overload all of the get/put operators when you want to
104 * customize only one or two.
106 template <class STREAM>
107 class ACE_IOStream : public iostream, public STREAM
109 public:
110 ACE_IOStream (STREAM &stream,
111 u_int streambuf_size = ACE_STREAMBUF_SIZE);
114 * The default constructor. This will initialize your STREAM and
115 * then setup the iostream baseclass to use a custom streambuf based
116 * on STREAM.
118 ACE_IOStream (u_int streambuf_size = ACE_STREAMBUF_SIZE);
120 /// We have to get rid of the <streambuf_> ourselves since we gave it
121 /// to the <iostream> base class;
122 virtual ~ACE_IOStream ();
124 /// The only ambiguity in the multiple inheritance is the <close>
125 /// function.
126 virtual int close ();
129 * Returns 1 if we're at the end of the <STREAM>, i.e., if the
130 * connection has closed down or an error has occurred, else 0.
131 * Under the covers, <eof> calls the streambuf's @a timeout function
132 * which will reset the timeout flag. As as result, you should save
133 * the return of <eof> and check it instead of calling <eof>
134 * successively.
136 int eof () const;
139 * A simple string operator. The base <iostream> has them for char*
140 * but that isn't always the best thing for a <String>. If we don't
141 * provide our own here, we may not get what we want.
143 virtual ACE_IOStream<STREAM> &operator>> (ACE_IOStream_String &v);
145 /// The converse of the <String::put> operator.
146 virtual ACE_IOStream<STREAM> &operator<< (ACE_IOStream_String &v);
148 // = Using the macros to provide get/set operators.
149 GETPUT_FUNC_SET (ACE_IOStream<STREAM>)
151 # if defined (ACE_LACKS_IOSTREAM_FX)
152 virtual int ipfx (int noskip = 0)
154 if (good ())
156 if (tie () != 0)
157 tie ()->flush ();
158 if (!noskip && flags () & skipws)
160 int ch;
161 while (isspace (ch = rdbuf ()->sbumpc ()))
162 continue;
163 if (ch != EOF)
164 rdbuf ()->sputbackc (ch);
166 if (good ())
167 return 1;
169 setstate (failbit);
170 return (0);
172 virtual int ipfx0 () // Optimized ipfx(0)
173 { return ipfx (0); }
174 virtual int ipfx1 () // Optimized ipfx(1)
176 if (good ())
178 if (tie () != 0)
179 tie ()->flush ();
180 if (good ())
181 return 1;
183 setstate (failbit);
184 return (0);
186 virtual void isfx () { return; }
187 virtual int opfx ()
189 if (good () && tie () != 0)
190 tie ()->flush ();
191 return good ();
193 virtual void osfx () { if (flags () & unitbuf) flush (); }
194 # else
195 # if defined (__GNUC__)
196 virtual int ipfx0 () { return iostream::ipfx0 (); } // Optimized ipfx(0)
197 virtual int ipfx1 () { return iostream::ipfx1 (); } // Optimized ipfx(1)
198 # else
199 virtual int ipfx0 () { return iostream::ipfx (0); }
200 virtual int ipfx1 () { return iostream::ipfx (1); }
201 # endif /* __GNUC__ */
202 virtual int ipfx (int need = 0) { return iostream::ipfx (need); }
203 virtual void isfx () { iostream::isfx (); }
204 virtual int opfx () { return iostream::opfx (); }
205 virtual void osfx () { iostream::osfx (); }
206 # endif /* ACE_LACKS_IOSTREAM_FX */
208 /// Allow the programmer to provide a timeout for read operations.
209 /// Give it a pointer to NULL to block forever.
210 ACE_IOStream<STREAM> & operator>> (ACE_Time_Value *&tv);
212 protected:
213 /// This is where all of the action takes place. The streambuf_ is
214 /// the interface to the underlying STREAM.
215 ACE_Streambuf_T<STREAM> *streambuf_;
217 private:
218 // We move these into the private section so that they cannot be
219 // used by the application programmer. This is necessary because
220 // streambuf_ will be buffering IO on the STREAM object. If these
221 // functions were used in your program, there is a danger of getting
222 // the datastream out of sync.
223 ssize_t send (...) = delete;
224 ssize_t recv (...) = delete;
225 ssize_t send_n (...) = delete;
226 ssize_t recv_n (...) = delete;
230 * @class ACE_SOCK_Dgram_SC
232 * @brief "Dgram_SC" is short for "Datagram Self-Contained."
234 * Datagrams don't have the notion of a "peer". Each send and
235 * receive on a datagram can go to a different peer if you want.
236 * If you're using datagrams for stream activity, you probably
237 * want 'em all to go to (and come from) the same place. That's
238 * what this class is for. Here, we keep an address object so
239 * that we can remember who last sent us data. When we write
240 * back, we're then able to write back to that same address.
242 template <class STREAM>
243 class ACE_SOCK_Dgram_SC : public STREAM
245 public:
246 ACE_SOCK_Dgram_SC ();
247 ACE_SOCK_Dgram_SC (STREAM &source,
248 ACE_INET_Addr &dest);
249 ssize_t send_n (char *buf, ssize_t len);
250 ssize_t recv (char *buf,
251 ssize_t len,
252 ACE_Time_Value *tv = 0);
253 ssize_t recv (char *buf,
254 ssize_t len,
255 int flags,
256 ACE_Time_Value *tv = 0);
257 ssize_t recv_n (char *buf,
258 ssize_t len,
259 int flags = 0,
260 ACE_Time_Value *tv = 0);
261 int get_remote_addr (ACE_INET_Addr &addr) const;
263 protected:
264 ACE_INET_Addr peer_;
267 ACE_END_VERSIONED_NAMESPACE_DECL
269 #if defined (__ACE_INLINE__)
270 # include "ace/IOStream_T.inl"
271 #endif /* __ACE_INLINE__ */
273 #include "ace/IOStream_T.cpp"
275 #include /**/ "ace/post.h"
276 #endif /* ACE_IOSTREAM_T_H */