IOStream.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    IOStream.h
00006  *
00007  *  IOStream.h,v 4.72 2006/05/30 11:10:37 jwillemsen Exp
00008  *
00009  *  @author James CE Johnson <jcej@lads.com>
00010  *  @author Jim Crossley <jim@lads.com>
00011  */
00012 //=============================================================================
00013 
00014 #ifndef ACE_IOSTREAM_H
00015 #define ACE_IOSTREAM_H
00016 #include /**/ "ace/pre.h"
00017 
00018 #include "ace/ACE_export.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 // Needed on Windows for streambuf
00025 // FUZZ: disable check_for_streams_include
00026 #include "ace/streams.h"
00027 
00028 // This is a temporary restriction - ACE_IOStream is only enabled if the
00029 // compiler does not supply the standard C++ library (and standard iostreams)
00030 // or, if it does, the platform is explicitly set to use old iostreams
00031 // by its config.h file.
00032 // This restriction is recorded in Bugzilla entry 857.
00033 #if defined (ACE_HAS_STANDARD_CPP_LIBRARY) && (ACE_HAS_STANDARD_CPP_LIBRARY == 1)
00034 #  if !defined (ACE_USES_OLD_IOSTREAMS) && !defined (ACE_LACKS_ACE_IOSTREAM)
00035 #    define ACE_LACKS_ACE_IOSTREAM
00036 #  endif /* !ACE_USES_OLD_IOSTREAMS && !ACE_LACKS_ACE_IOSTREAM */
00037 #endif /* ACE_HAS_STANDARD_CPP_LIBRARY */
00038 
00039 #if !defined (ACE_LACKS_ACE_IOSTREAM)
00040 
00041 #  if defined (ACE_HAS_STRING_CLASS)
00042 #    if defined (ACE_WIN32) && defined (_MSC_VER)
00043 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00044 typedef CString ACE_IOStream_String;
00045 ACE_END_VERSIONED_NAMESPACE_DECL
00046 #    else
00047 #      if !defined (ACE_HAS_STDCPP_STL_INCLUDES)
00048 #include /**/ <String.h>
00049 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00050 typedef String ACE_IOStream_String;
00051 ACE_END_VERSIONED_NAMESPACE_DECL
00052 #      else
00053 #        include /**/ <string>
00054 
00055 #        if defined(ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB)
00056 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00057 typedef std::string ACE_IOStream_String;
00058 ACE_END_VERSIONED_NAMESPACE_DECL
00059 #        else
00060 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00061 typedef string ACE_IOStream_String;
00062 ACE_END_VERSIONED_NAMESPACE_DECL
00063 #        endif /* ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB */
00064 #      endif /* ! ACE_HAS_STDCPP_STL_INCLUDES */
00065 #    endif /* ACE_WIN32 && defined (_MSC_VER) */
00066 
00067 #    if defined (__DECCXX_VER)
00068 #      if __DECCXX_VER < 50700000
00069 #        include /**/ <stl_macros>
00070 #      else
00071 #        include /**/ <stdcomp>
00072 #      endif /* __DECCXX_VER < 50700000 */
00073 #    endif /* __DECCXX_VER */
00074 
00075 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00076 
00077 class ACE_Export ACE_Quoted_String : public ACE_IOStream_String
00078 {
00079 public:
00080   inline ACE_Quoted_String (void) { *this = ""; }
00081   inline ACE_Quoted_String (const char *c) { *this = ACE_IOStream_String (c); }
00082   inline ACE_Quoted_String (const ACE_IOStream_String &s) { *this = s; }
00083   inline ACE_Quoted_String &operator= (const ACE_IOStream_String& s)
00084   {
00085     return (ACE_Quoted_String &) ACE_IOStream_String::operator= (s);
00086   }
00087   inline ACE_Quoted_String &operator = (const char c) {
00088     return (ACE_Quoted_String &) ACE_IOStream_String::operator= (c);
00089   }
00090   inline ACE_Quoted_String &operator = (const char *c) {
00091     return (ACE_Quoted_String &) ACE_IOStream_String::operator= (c);
00092   }
00093   inline bool operator < (const ACE_Quoted_String &s) const {
00094     return *(ACE_IOStream_String *) this < (ACE_IOStream_String) s;
00095   }
00096 #    if defined (ACE_WIN32) && defined (_MSC_VER)
00097   inline int length (void) { return this->GetLength (); }
00098 #    endif /* ACE_WIN32 && defined (_MSC_VER) */
00099 };
00100 
00101 ACE_END_VERSIONED_NAMESPACE_DECL
00102 
00103 #  endif /* ACE_HAS_STRING_CLASS */
00104 
00105 #  include "ace/Time_Value.h"
00106 #  include "ace/os_include/sys/os_types.h"
00107 
00108 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00109 
00110 /**
00111  * @class ACE_Streambuf
00112  *
00113  * @brief Create your custom streambuf by providing and ACE_*_Stream
00114  * object to this template.  I have tested it with
00115  * ACE_SOCK_Stream and it should work fine for others as well.
00116  *
00117  * For any iostream object, the real work is done by the
00118  * underlying streambuf class.  That is what we create here.
00119  * A streambuf has an internal buffer area into which data is
00120  * read and written as the iostream requests and provides data.
00121  * At some point during the read process, the iostream will
00122  * realize that the streambuf has no more data.  The underflow
00123  * function of the streambuf is then called.
00124  * Likewise, during the write process, the iostream will
00125  * eventually notice that the streabuf's buffer has become full
00126  * and will invoke the overflow function.
00127  * The empty/full state of the read/write "buffers" are
00128  * controled by two sets pointers.  One set is dedicated to
00129  * read, the other to write.  These pointers, in turn, reference
00130  * a common buffer that is to be shared by both read and write
00131  * operations.  It is this common buffer to which data is
00132  * written and from which it is read.
00133  * The common buffer is used by functions of the streambuf as
00134  * well as the iostream.  Because of this and the fact that it
00135  * is "shared" by both read and write operators, there is a
00136  * danger of data corruption if read and write operations are
00137  * allowed to take place "at the same time".
00138  * To prevent data corruption, we manipulate the read and write
00139  * pointer sets so that the streambuf is in either a read-mode
00140  * or write-mode at all times and can never be in both modes at
00141  * the same time.
00142  * In the constructor: set the read and write sets to NULL This
00143  * causes the underflow or overflow operators to be invoked at
00144  * the first IO activity of the iostream.
00145  * In the underflow function we arrange for the common buffer to
00146  * reference our read buffer and for the write pointer set to be
00147  * disabled.  If a write operation is performed by the iostream
00148  * this will cause the overflow function to be invoked.
00149  * In the overflow function we arrange for the common buffer to
00150  * reference our write buffer and for the read pointer set to be
00151  * disabled.  This causes the underflow function to be invoked
00152  * when the iostream "changes our mode".
00153  * The overflow function will also invoke the send_n function to
00154  * flush the buffered data to our peer.  Similarly, the sync and
00155  * syncout functions will cause send_n to be invoked to send the
00156  * data.
00157  * Since socket's and the like do not support seeking, there can
00158  * be no method for "syncing" the input.  However, since we
00159  * maintain separate read/write buffers, no data is lost by
00160  * "syncing" the input.  It simply remains buffered.
00161  */
00162 class ACE_Export ACE_Streambuf : public streambuf
00163 {
00164 public:
00165 
00166   /**
00167    * If the default allocation strategey were used the common buffer
00168    * would be deleted when the object destructs.  Since we are
00169    * providing separate read/write buffers, it is up to us to manage
00170    * their memory.
00171    */
00172   virtual ~ACE_Streambuf (void);
00173 
00174   /// Get the current Time_Value pointer and provide a new one.
00175   ACE_Time_Value *recv_timeout (ACE_Time_Value *tv = 0);
00176 
00177   /**
00178    * Use this to allocate a new/different buffer for put operations.
00179    * If you do not provide a buffer pointer, one will be allocated.
00180    * That is the preferred method.  If you do provide a buffer, the
00181    * size must match that being used by the get buffer.  If
00182    * successful, you will receive a pointer to the current put buffer.
00183    * It is your responsibility to delete this memory when you are done
00184    * with it.
00185    */
00186   char *reset_put_buffer (char *newBuffer = 0,
00187                           u_int _streambuf_size = 0,
00188                           u_int _pptr = 0 );
00189 
00190   /// Return the number of bytes to be 'put' onto the stream media.
00191   ///    pbase + put_avail = pptr
00192   u_int put_avail (void);
00193 
00194   /**
00195    * Use this to allocate a new/different buffer for get operations.
00196    * If you do not provide a buffer pointer, one will be allocated.
00197    * That is the preferred method.  If you do provide a buffer, the
00198    * size must match that being used by the put buffer.  If
00199    * successful, you will receive a pointer to the current get buffer.
00200    * It is your responsibility to delete this memory when you are done
00201    * with it.
00202    */
00203   char *reset_get_buffer (char *newBuffer = 0,
00204                           u_int _streambuf_size = 0,
00205                           u_int _gptr = 0,
00206                           u_int _egptr = 0);
00207 
00208   /// Return the number of bytes not yet gotten.  eback + get_waiting =
00209   /// gptr
00210   u_int get_waiting (void);
00211 
00212   /// Return the number of bytes in the get area (includes some already
00213   /// gotten); eback + get_avail = egptr
00214   u_int get_avail (void);
00215 
00216   /// Query the streambuf for the size of its buffers.
00217   u_int streambuf_size (void);
00218 
00219   /// Did we take an error because of an IO operation timeout?
00220   /// @note Invoking this resets the flag.
00221   u_char timeout (void);
00222 
00223 protected:
00224   ACE_Streambuf (u_int streambuf_size,
00225                  int io_mode);
00226 
00227   /// Sync both input and output. See syncin/syncout below for
00228   /// descriptions.
00229   virtual int sync (void);
00230 
00231   // = Signatures for the underflow/overflow discussed above.
00232   virtual int underflow (void);
00233 
00234   /// The overflow function receives the character which caused the
00235   /// overflow.
00236   virtual int overflow (int c = EOF);
00237 
00238   /// Resets the <base> pointer and streambuf mode.  This is used
00239   /// internally when get/put buffers are allocatd.
00240   void reset_base (void);
00241 
00242 protected:
00243   // = Two pointer sets for manipulating the read/write areas.
00244   char *eback_saved_;
00245   char *gptr_saved_;
00246   char *egptr_saved_;
00247   char *pbase_saved_;
00248   char *pptr_saved_;
00249   char *epptr_saved_;
00250 
00251   // = With cur_mode_ we keep track of our current IO mode.
00252 
00253   // This helps us to optimize the underflow/overflow functions.
00254   u_char cur_mode_;
00255   const u_char get_mode_;
00256   const u_char put_mode_;
00257 
00258   /// mode tells us if we're working for an istream, ostream, or
00259   /// iostream.
00260   int mode_;
00261 
00262   /// This defines the size of the input and output buffers.  It can be
00263   /// set by the object constructor.
00264   const u_int streambuf_size_;
00265 
00266   /// Did we take an error because of an IO operation timeout?
00267   u_char timeout_;
00268 
00269   /// We want to allow the user to provide Time_Value pointers to
00270   /// prevent infinite blocking while waiting to receive data.
00271   ACE_Time_Value recv_timeout_value_;
00272   ACE_Time_Value *recv_timeout_;
00273 
00274   /**
00275    * syncin is called when the input needs to be synced with the
00276    * source file.  In a filebuf, this results in the <seek> system
00277    * call being used.  We can't do that on socket-like connections, so
00278    * this does basically nothing.  That's safe because we have a
00279    * separate read buffer to maintain the already-read data.  In a
00280    * filebuf, the single common buffer is used forcing the <seek>
00281    * call.
00282    */
00283   int syncin (void);
00284 
00285   /// syncout is called when the output needs to be flushed.  This is
00286   /// easily done by calling the peer's send_n function.
00287   int syncout (void);
00288 
00289   /// flushbuf is the worker of syncout.  It is a separate function
00290   /// because it gets used sometimes in different context.
00291   int flushbuf (void);
00292 
00293   /**
00294    * fillbuf is called in a couple of places.  This is the worker of
00295    * underflow.  It will attempt to fill the read buffer from the
00296    * peer.
00297    */
00298   int fillbuf (void);
00299 
00300   /**
00301    * Used by fillbuf and others to get exactly one byte from the peer.
00302    * recv_n is used to be sure we block until something is available.
00303    * It is virtual because we really need to override it for
00304    * datagram-derived objects.
00305    */
00306   virtual int get_one_byte (void);
00307 
00308   /**
00309    * Stream connections and "unconnected connections" (ie --
00310    * datagrams) need to work just a little differently.  We derive
00311    * custom Streambuf objects for them and provide these functions at
00312    * that time.
00313    */
00314   virtual ssize_t send (char *buf,
00315                         ssize_t len) = 0;
00316   virtual ssize_t recv (char *buf,
00317                         ssize_t len,
00318                         ACE_Time_Value *tv = 0) = 0;
00319   virtual ssize_t recv (char *buf,
00320                         ssize_t len,
00321                         int flags,
00322                         ACE_Time_Value *tv = 0) = 0;
00323   virtual ssize_t recv_n (char *buf,
00324                           ssize_t len,
00325                           int flags = 0,
00326                           ACE_Time_Value *tv = 0) = 0;
00327 
00328   virtual ACE_HANDLE get_handle (void);
00329 
00330 #  if defined (ACE_HAS_STANDARD_CPP_LIBRARY) && (ACE_HAS_STANDARD_CPP_LIBRARY != 0) && !defined (ACE_USES_OLD_IOSTREAMS)
00331   char *base (void) const
00332     {
00333       return cur_mode_ == get_mode_ ? eback_saved_
00334         : cur_mode_ == put_mode_ ? pbase_saved_
00335         : 0;
00336     }
00337   char *ebuf (void) const
00338     {
00339       return cur_mode_ == 0 ? 0 : base () + streambuf_size_;
00340     }
00341 
00342   int blen (void) const
00343     {
00344       return streambuf_size_;
00345     }
00346 
00347   void setb (char* b, char* eb, int /* a */=0)
00348     {
00349       setbuf (b, (eb - b));
00350     }
00351 
00352   int out_waiting (void)
00353     {
00354       return pptr () - pbase ();
00355     }
00356 #  endif /* ACE_HAS_STANDARD_CPP_LIBRARY */
00357 };
00358 
00359 ACE_END_VERSIONED_NAMESPACE_DECL
00360 
00361 ///////////////////////////////////////////////////////////////////////////
00362 
00363 // These typedefs are provided by G++ (on some systems?) without the
00364 // trailing '_'.  Since we can't count on 'em, I've defined them to
00365 // what GNU wants here.
00366 //
00367 typedef ios& (*__manip_)(ios&);
00368 typedef istream& (*__imanip_)(istream&);
00369 typedef ostream& (*__omanip_)(ostream&);
00370 
00371 // Trying to do something like is shown below instead of using the
00372 // __*manip typedefs causes Linux do segfault when "<<endl" is done.
00373 //
00374 //        virtual MT& operator<<(ios& (*func)(ios&))  { (*func)(*this); return *this; }
00375 
00376 // This macro defines the get operator for class MT into datatype DT.
00377 // We will use it below to quickly override most (all?)  iostream get
00378 // operators.  Notice how the <ipfx> and <isfx> functions are used.
00379 
00380 #define GET_SIG(MT,DT)          inline virtual MT& operator>> (DT v)
00381 #  if (defined (__SUNPRO_CC) && __SUNPRO_CC > 0x510)
00382 #define GET_CODE {                      \
00383         if (ipfx (0))                                   \
00384         {                                               \
00385                 (*((istream*)this)) >> (v);             \
00386         }                                               \
00387         isfx ();                                        \
00388         return *this;                                   \
00389         }
00390 #  else
00391 #define GET_CODE {                      \
00392         if (ipfx (0))                                   \
00393         {                                               \
00394                 iostream::operator>> (v);               \
00395         }                                               \
00396         isfx ();                                        \
00397         return *this;                                   \
00398         }
00399 #  endif
00400 #define GET_PROT(MT,DT,CODE)    GET_SIG(MT,DT)  CODE
00401 #define GET_FUNC(MT,DT)         GET_PROT(MT,DT,GET_CODE)
00402 
00403 // This macro defines the put operator for class MT into datatype DT.
00404 // We will use it below to quickly override most (all?)  iostream put
00405 // operators.  Notice how the <opfx> and <osfx> functions are used.
00406 
00407 #define PUT_SIG(MT,DT)          inline virtual MT& operator<< (DT v)
00408 #  if (defined (__SUNPRO_CC) && __SUNPRO_CC > 0x510)
00409 #define PUT_CODE {                      \
00410         if (opfx ())                                    \
00411         {                                               \
00412                 (*((ostream *) this)) << (v);            \
00413         }                                               \
00414         osfx ();                                        \
00415         return *this;                                   \
00416         }
00417 #  else
00418 #define PUT_CODE {                      \
00419         if (opfx ())                                    \
00420         {                                               \
00421                 iostream::operator<< (v);               \
00422         }                                               \
00423         osfx ();                                        \
00424         return *this;                                   \
00425         }
00426 #  endif
00427 #define PUT_PROT(MT,DT,CODE)    PUT_SIG(MT,DT)  CODE
00428 #define PUT_FUNC(MT,DT)         PUT_PROT(MT,DT,PUT_CODE)
00429 
00430 
00431 // These are necessary in case somebody wants to derive from us and
00432 // override one of these with a custom approach.
00433 
00434 #  if defined (ACE_LACKS_CHAR_RIGHT_SHIFTS)
00435 #define GET_FUNC_SET0(MT,CODE,CODE2) \
00436         GET_PROT(MT,short &,CODE) \
00437         GET_PROT(MT,u_short &,CODE) \
00438         GET_PROT(MT,int &,CODE) \
00439         GET_PROT(MT,u_int &,CODE) \
00440         GET_PROT(MT,long &,CODE) \
00441         GET_PROT(MT,u_long &,CODE) \
00442         GET_PROT(MT,float &,CODE) \
00443         GET_PROT(MT,double &,CODE) \
00444         inline virtual MT& operator>>(__omanip_ func) CODE2 \
00445         inline virtual MT& operator>>(__manip_ func)  CODE2
00446 #  else
00447 #define GET_FUNC_SET0(MT,CODE,CODE2) \
00448         GET_PROT(MT,short &,CODE) \
00449         GET_PROT(MT,u_short &,CODE) \
00450         GET_PROT(MT,int &,CODE) \
00451         GET_PROT(MT,u_int &,CODE) \
00452         GET_PROT(MT,long &,CODE) \
00453         GET_PROT(MT,u_long &,CODE) \
00454         GET_PROT(MT,float &,CODE) \
00455         GET_PROT(MT,double &,CODE) \
00456         GET_PROT(MT,char &,CODE) \
00457         GET_PROT(MT,u_char &,CODE) \
00458         GET_PROT(MT,char *,CODE) \
00459         GET_PROT(MT,u_char *,CODE) \
00460         inline virtual MT& operator>>(__omanip_ func) CODE2 \
00461         inline virtual MT& operator>>(__manip_ func)  CODE2
00462 #  endif
00463 
00464 #define PUT_FUNC_SET0(MT,CODE,CODE2) \
00465         PUT_PROT(MT,short,CODE) \
00466         PUT_PROT(MT,u_short,CODE) \
00467         PUT_PROT(MT,int,CODE) \
00468         PUT_PROT(MT,u_int,CODE) \
00469         PUT_PROT(MT,long,CODE) \
00470         PUT_PROT(MT,u_long,CODE) \
00471         PUT_PROT(MT,float,CODE) \
00472         PUT_PROT(MT,double,CODE) \
00473         PUT_PROT(MT,char,CODE) \
00474         PUT_PROT(MT,u_char,CODE) \
00475         PUT_PROT(MT,const char *,CODE) \
00476         PUT_PROT(MT,u_char *,CODE) \
00477         PUT_PROT(MT,void *,CODE) \
00478         inline virtual MT& operator<<(__omanip_ func) CODE2 \
00479         inline virtual MT& operator<<(__manip_ func)  CODE2
00480 
00481 #  if defined (ACE_LACKS_SIGNED_CHAR)
00482   #define GET_FUNC_SET1(MT,CODE,CODE2) GET_FUNC_SET0(MT,CODE,CODE2)
00483   #define PUT_FUNC_SET1(MT,CODE,CODE2) PUT_FUNC_SET0(MT,CODE,CODE2)
00484 #  else
00485   #define GET_FUNC_SET1(MT,CODE,CODE2) \
00486           GET_PROT(MT,signed char &,CODE) \
00487           GET_PROT(MT,signed char *,CODE) \
00488           GET_FUNC_SET0(MT,CODE,CODE2)
00489 
00490   #define PUT_FUNC_SET1(MT,CODE,CODE2) \
00491           PUT_FUNC(MT,signed char) \
00492           PUT_FUNC(MT,const signed char *) \
00493           PUT_FUNC_SET0(MT,CODE,CODE2)
00494 #  endif /* ACE_LACKS_SIGNED_CHAR */
00495 
00496 #define GET_MANIP_CODE  { if (ipfx ()) { (*func) (*this); } isfx (); return *this; }
00497 #define PUT_MANIP_CODE  { if (opfx ()) { (*func) (*this); } osfx (); return *this; }
00498 
00499 #define GET_FUNC_SET(MT)        GET_FUNC_SET1(MT,GET_CODE,GET_MANIP_CODE)
00500 #define PUT_FUNC_SET(MT)        PUT_FUNC_SET1(MT,PUT_CODE,PUT_MANIP_CODE)
00501 #define GETPUT_FUNC_SET(MT)     GET_FUNC_SET(MT) PUT_FUNC_SET(MT)
00502 
00503 #define GET_SIG_SET(MT)         GET_FUNC_SET1(MT,= 0;,= 0;)
00504 #define PUT_SIG_SET(MT)         PUT_FUNC_SET1(MT,= 0;,= 0;)
00505 #define GETPUT_SIG_SET(MT)      GET_SIG_SET(MT) PUT_SIG_SET(MT)
00506 
00507 // Include the templates here.
00508 #  include "ace/IOStream_T.h"
00509 #endif /* !ACE_LACKS_ACE_IOSTREAM && ACE_USES_OLD_IOSTREAMS */
00510 
00511 #include /**/ "ace/post.h"
00512 #endif /* ACE_IOSTREAM_H */

Generated on Thu Nov 9 09:41:52 2006 for ACE by doxygen 1.3.6