Fixed typos
[ACE_TAO.git] / ACE / ace / IOStream_T.h
blob8a8776a79eb159c9a90a71e414d0b1e15f1dac06
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 # if defined (ACE_HAS_STRING_CLASS)
38 template <class STREAM> STREAM & operator>> (STREAM &stream, ACE_Quoted_String &str);
39 template <class STREAM> STREAM & operator<< (STREAM &stream, ACE_Quoted_String &str);
40 # endif /* defined (ACE_HAS_STRING_CLASS) */
42 template <class STREAM>
43 class ACE_Streambuf_T : public ACE_Streambuf
45 public:
46 /**
47 * We will be given a STREAM by the iostream object which creates
48 * us. See the ACE_IOStream template for how that works. Like
49 * other streambuf objects, we can be input-only, output-only or
50 * both.
52 ACE_Streambuf_T (STREAM *peer,
53 u_int streambuf_size = ACE_STREAMBUF_SIZE,
54 int io_mode = ios::in | ios::out);
56 virtual ssize_t send (char *buf, ssize_t len);
58 virtual ssize_t recv (char *buf,
59 ssize_t len,
60 ACE_Time_Value *tv = 0);
62 virtual ssize_t recv (char *buf,
63 ssize_t len,
64 int flags,
65 ACE_Time_Value * tv = 0);
67 virtual ssize_t recv_n (char *buf,
68 ssize_t len,
69 int flags = 0,
70 ACE_Time_Value *tv = 0);
72 protected:
73 virtual ACE_HANDLE get_handle (void);
75 /// This will be our ACE_SOCK_Stream or similar object.
76 STREAM *peer_;
79 /**
80 * @class ACE_IOStream
82 * @brief A template adapter for creating an iostream-like object using
83 * an ACE IPC Stream for the actual I/O. Iostreams use an
84 * underlying streambuf object for the IO interface. The
85 * iostream class and derivatives provide you with a host of
86 * convenient operators that access the streambuf.
88 * We inherit all characteristics of iostream and your <STREAM>
89 * class. When you create a new class from this template, you
90 * can use it anywhere you would have used your original
91 * <STREAM> class.
92 * To create an iostream for your favorite ACE IPC class (e.g.,
93 * ACE_SOCK_Stream), feed that class to this template's
94 * <STREAM> parameter, e.g.,
95 * typedef ACE_Svc_Handler<ACE_SOCK_iostream,
96 * ACE_INET_Addr, ACE_NULL_SYNCH>
97 * Service_Handler;
98 * Because the operators in the iostream class are not virtual,
99 * you cannot easily provide overloads in your custom
100 * ACE_IOStream classes. To make these things work correctly,
101 * you need to overload ALL operators of the ACE_IOStream you
102 * create. I've attempted to do that here to make things easier
103 * for you but there are no guarantees.
104 * In the iostream.cpp file is an example of why it is necessary
105 * to overload all of the get/put operators when you want to
106 * customize only one or two.
108 template <class STREAM>
109 class ACE_IOStream : public iostream, public STREAM
111 public:
112 ACE_IOStream (STREAM &stream,
113 u_int streambuf_size = ACE_STREAMBUF_SIZE);
116 * The default constructor. This will initialize your STREAM and
117 * then setup the iostream baseclass to use a custom streambuf based
118 * on STREAM.
120 ACE_IOStream (u_int streambuf_size = ACE_STREAMBUF_SIZE);
122 /// We have to get rid of the <streambuf_> ourselves since we gave it
123 /// to the <iostream> base class;
124 virtual ~ACE_IOStream (void);
126 /// The only ambiguity in the multiple inheritance is the <close>
127 /// function.
128 virtual int close (void);
131 * Returns 1 if we're at the end of the <STREAM>, i.e., if the
132 * connection has closed down or an error has occurred, else 0.
133 * Under the covers, <eof> calls the streambuf's @a timeout function
134 * which will reset the timeout flag. As as result, you should save
135 * the return of <eof> and check it instead of calling <eof>
136 * successively.
138 int eof (void) const;
140 # if defined (ACE_HAS_STRING_CLASS)
142 * A simple string operator. The base <iostream> has them for char*
143 * but that isn't always the best thing for a <String>. If we don't
144 * provide our own here, we may not get what we want.
146 virtual ACE_IOStream<STREAM> &operator>> (ACE_IOStream_String &v);
148 /// The converse of the <String::put> operator.
149 virtual ACE_IOStream<STREAM> &operator<< (ACE_IOStream_String &v);
151 # endif /* ACE_HAS_STRING_CLASS */
152 // = Using the macros to provide get/set operators.
153 GETPUT_FUNC_SET (ACE_IOStream<STREAM>)
155 # if defined (ACE_LACKS_IOSTREAM_FX)
156 virtual int ipfx (int noskip = 0)
158 if (good ())
160 if (tie () != 0)
161 tie ()->flush ();
162 if (!noskip && flags () & skipws)
164 int ch;
165 while (isspace (ch = rdbuf ()->sbumpc ()))
166 continue;
167 if (ch != EOF)
168 rdbuf ()->sputbackc (ch);
170 if (good ())
171 return 1;
173 # if !defined (ACE_WIN32)
174 // MS VC++ 5.0 doesn't declare setstate.
175 setstate (failbit);
176 # endif /* !ACE_WIN32 */
177 return (0);
179 virtual int ipfx0 (void) { return ipfx (0); } // Optimized ipfx(0)
180 virtual int ipfx1 (void) // Optimized ipfx(1)
182 if (good ())
184 if (tie () != 0)
185 tie ()->flush ();
186 if (good ())
187 return 1;
189 # if !defined (ACE_WIN32)
190 // MS VC++ 5.0 doesn't declare setstate.
191 setstate (failbit);
192 # endif /* !ACE_WIN32 */
193 return (0);
195 virtual void isfx (void) { return; }
196 virtual int opfx (void)
198 if (good () && tie () != 0)
199 tie ()->flush ();
200 return good ();
202 virtual void osfx (void) { if (flags () & unitbuf) flush (); }
203 # else
204 # if defined (__GNUC__)
205 virtual int ipfx0 (void) { return iostream::ipfx0 (); } // Optimized ipfx(0)
206 virtual int ipfx1 (void) { return iostream::ipfx1 (); } // Optimized ipfx(1)
207 # else
208 virtual int ipfx0 (void) { return iostream::ipfx (0); }
209 virtual int ipfx1 (void) { return iostream::ipfx (1); }
210 # endif /* __GNUC__ */
211 virtual int ipfx (int need = 0) { return iostream::ipfx (need); }
212 virtual void isfx (void) { iostream::isfx (); }
213 virtual int opfx (void) { return iostream::opfx (); }
214 virtual void osfx (void) { iostream::osfx (); }
215 # endif /* ACE_LACKS_IOSTREAM_FX */
217 /// Allow the programmer to provide a timeout for read operations.
218 /// Give it a pointer to NULL to block forever.
219 ACE_IOStream<STREAM> & operator>> (ACE_Time_Value *&tv);
221 protected:
222 /// This is where all of the action takes place. The streambuf_ is
223 /// the interface to the underlying STREAM.
224 ACE_Streambuf_T<STREAM> *streambuf_;
226 private:
227 // We move these into the private section so that they cannot be
228 // used by the application programmer. This is necessary because
229 // streambuf_ will be buffering IO on the STREAM object. If these
230 // functions were used in your program, there is a danger of getting
231 // the datastream out of sync.
232 ACE_UNIMPLEMENTED_FUNC (ssize_t send (...))
233 ACE_UNIMPLEMENTED_FUNC (ssize_t recv (...))
234 ACE_UNIMPLEMENTED_FUNC (ssize_t send_n (...))
235 ACE_UNIMPLEMENTED_FUNC (ssize_t recv_n (...))
239 * @class ACE_SOCK_Dgram_SC
241 * @brief "Dgram_SC" is short for "Datagram Self-Contained."
243 * Datagrams don't have the notion of a "peer". Each send and
244 * receive on a datagram can go to a different peer if you want.
245 * If you're using datagrams for stream activity, you probably
246 * want 'em all to go to (and come from) the same place. That's
247 * what this class is for. Here, we keep an address object so
248 * that we can remember who last sent us data. When we write
249 * back, we're then able to write back to that same address.
251 template <class STREAM>
252 class ACE_SOCK_Dgram_SC : public STREAM
254 public:
255 ACE_SOCK_Dgram_SC (void);
256 ACE_SOCK_Dgram_SC (STREAM &source,
257 ACE_INET_Addr &dest);
258 ssize_t send_n (char *buf, ssize_t len);
259 ssize_t recv (char *buf,
260 ssize_t len,
261 ACE_Time_Value *tv = 0);
262 ssize_t recv (char *buf,
263 ssize_t len,
264 int flags,
265 ACE_Time_Value *tv = 0);
266 ssize_t recv_n (char *buf,
267 ssize_t len,
268 int flags = 0,
269 ACE_Time_Value *tv = 0);
270 int get_remote_addr (ACE_INET_Addr &addr) const;
272 protected:
273 ACE_INET_Addr peer_;
276 ACE_END_VERSIONED_NAMESPACE_DECL
278 # if defined (__ACE_INLINE__)
279 # include "ace/IOStream_T.inl"
280 # endif /* __ACE_INLINE__ */
282 # if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
283 # include "ace/IOStream_T.cpp"
284 # endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
286 # if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
287 # pragma implementation ("IOStream_T.cpp")
288 # endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
289 #endif /* ACE_LACKS_ACE_IOSTREAM */
291 #include /**/ "ace/post.h"
292 #endif /* ACE_IOSTREAM_T_H */