Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

BufferedSink.h

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 
00003    Copyright (c) 2000 Tyrell Corporation. All rights reserved.
00004 
00005    Tyrell DarkIce
00006 
00007    File     : BufferedSink.h
00008    Version  : $Revision: 1.8 $
00009    Author   : $Author: jbebel $
00010    Location : $Source: /cvsroot/darkice/darkice/src/BufferedSink.h,v $
00011    
00012    Copyright notice:
00013 
00014     This program is free software; you can redistribute it and/or
00015     modify it under the terms of the GNU General Public License  
00016     as published by the Free Software Foundation; either version 2
00017     of the License, or (at your option) any later version.
00018    
00019     This program is distributed in the hope that it will be useful,
00020     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00022     GNU General Public License for more details.
00023    
00024     You should have received a copy of the GNU General Public License
00025     along with this program; if not, write to the Free Software
00026     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028 ------------------------------------------------------------------------------*/
00029 #ifndef BUFFERED_SINK_H
00030 #define BUFFERED_SINK_H
00031 
00032 #ifndef __cplusplus
00033 #error This is a C++ include file
00034 #endif
00035 
00036 
00037 /* ============================================================ include files */
00038 
00039 #include "Ref.h"
00040 #include "Reporter.h"
00041 #include "Sink.h"
00042 
00043 
00044 /* ================================================================ constants */
00045 
00046 
00047 /* =================================================================== macros */
00048 
00049 
00050 /* =============================================================== data types */
00051 
00061 class BufferedSink : public Sink, public virtual Reporter
00062 {
00063     private:
00064 
00068         unsigned char     * buffer;
00069 
00073         unsigned char     * bufferEnd;
00074 
00078         unsigned int        bufferSize;
00079 
00083         unsigned int        peak;
00084         
00089         unsigned int        chunkSize;
00090 
00095         unsigned int        misalignment;
00096 
00100         unsigned char     * inp;
00101 
00105         unsigned char     * outp;
00106 
00107 
00111         Ref<Sink>           sink;
00112 
00121         void
00122         init (  Sink              * sink,
00123                 unsigned int        size,
00124                 unsigned int        chunkSize )         throw ( Exception );
00125 
00131         void
00132         strip ( void )                                  throw ( Exception );
00133 
00142         inline unsigned char *
00143         slidePointer (
00144                         unsigned char * p,
00145                         unsigned int    offset )        throw ()
00146         {
00147             p += offset;
00148             while ( p >= bufferEnd ) {
00149                 p -= bufferSize;
00150             }
00151 
00152             return p;
00153         }
00154 
00160         inline void
00161         updatePeak ( void )                             throw ()
00162         {
00163             unsigned int    u;
00164 
00165             u = outp <= inp ? inp - outp : (bufferEnd - outp) + (inp - buffer);
00166             if ( peak < u ) {
00167                 peak = u;
00168                 reportEvent( 4, "BufferedSink, new peak:", peak);
00169                 reportEvent( 4, "BufferedSink, remaining:", bufferSize - peak);
00170             }
00171         }
00172 
00180         inline bool
00181         align ( void )                                      throw ( Exception )
00182         {
00183             char    b[] = { 0 };
00184 
00185             while ( misalignment ) {
00186                 if ( sink->canWrite( 0, 0) ) {
00187                     unsigned int    ret;
00188                     
00189                     if ( !(ret = sink->write( b, 1)) ) {
00190                         return false;
00191                     }
00192                     --misalignment;
00193 
00194                 } else {
00195                     return false;
00196                 }
00197             }
00198 
00199             return true;
00200         }
00201 
00202 
00203     protected:
00204 
00210         inline
00211         BufferedSink ( void )                       throw ( Exception )
00212         {
00213             throw Exception( __FILE__, __LINE__);
00214         }
00215 
00221         inline unsigned int
00222         getSize ( void ) const                      throw ()
00223         {
00224             return bufferSize;
00225         }
00226 
00236         unsigned int
00237         store (     const void    * buffer,
00238                     unsigned int    bufferSize )    throw ( Exception );
00239 
00240 
00241     public:
00242 
00252         inline 
00253         BufferedSink (  Sink              * sink,
00254                         unsigned int        size,
00255                         unsigned int        chunkSize = 1 ) throw ( Exception )
00256         {
00257             init( sink, size, chunkSize);
00258         }
00259 
00266         BufferedSink (  const BufferedSink &  buffer )  throw ( Exception );
00267 
00273         inline virtual
00274         ~BufferedSink ( void )                          throw ( Exception )
00275         {
00276             strip();
00277         }
00278 
00286         virtual BufferedSink &
00287         operator= ( const BufferedSink &    bs )        throw ( Exception );
00288 
00294         inline unsigned int
00295         getPeak ( void ) const                          throw ()
00296         {
00297             return peak;
00298         }
00299 
00306         inline virtual bool
00307         open ( void )                                   throw ( Exception )
00308         {
00309             return sink->open();
00310         }
00311 
00317         inline virtual bool
00318         isOpen ( void ) const                           throw ()
00319         {
00320             return sink->isOpen();
00321         }
00322 
00332         inline virtual bool
00333         canWrite (     unsigned int    sec,
00334                        unsigned int    usec )           throw ( Exception )
00335         {
00336             return true;
00337         }
00338 
00351         virtual unsigned int
00352         write (    const void    * buf,
00353                    unsigned int    len )                throw ( Exception );
00354 
00361         inline virtual void
00362         flush ( void )                                  throw ( Exception )
00363         {
00364             unsigned char   b[1];
00365 
00366             write( b, 0);
00367         }
00368 
00374         virtual void
00375         close ( void )                                  throw ( Exception );
00376 };
00377 
00378 
00379 /* ================================================= external data structures */
00380 
00381 
00382 /* ====================================================== function prototypes */
00383 
00384 
00385 
00386 #endif  /* BUFFERED_SINK_H */
00387 
00388 
00389 /*------------------------------------------------------------------------------
00390  
00391   $Source: /cvsroot/darkice/darkice/src/BufferedSink.h,v $
00392 
00393   $Log: BufferedSink.h,v $
00394   Revision 1.8  2005/04/03 05:01:46  jbebel
00395   Fix peak reporting to report new peak rather than previous
00396 
00397   Revision 1.7  2002/07/21 08:47:06  darkeye
00398   some exception cleanup (throw clauses in function declarations)
00399 
00400   Revision 1.6  2002/05/28 12:35:41  darkeye
00401   code cleanup: compiles under gcc-c++ 3.1, using -pedantic option
00402 
00403   Revision 1.5  2000/11/15 18:08:42  darkeye
00404   added multiple verbosity-level event reporting and verbosity command
00405   line option
00406 
00407   Revision 1.4  2000/11/11 12:33:13  darkeye
00408   added kdoc-style documentation
00409 
00410   Revision 1.3  2000/11/10 20:16:21  darkeye
00411   first real tests with multiple streaming
00412 
00413   Revision 1.2  2000/11/05 17:37:24  darkeye
00414   removed clone() functions
00415 
00416   Revision 1.1.1.1  2000/11/05 10:05:48  darkeye
00417   initial version
00418 
00419   
00420 ------------------------------------------------------------------------------*/
00421 

Generated on Sat Oct 22 13:17:03 2005 for DarkIce by  doxygen 1.4.4