Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

FEAPIExamplePluginSpectral/RingBuffer.h

Go to the documentation of this file.
00001 
00002 //     /*! \file RingBuffer.h: \brief interface of the CRingBuffer class. */
00003 //
00004 //        Copyright (c) 2004-2007  
00005 //        zplane.development
00006 //        Flohrer Lerch Schwerdtfeger GbR
00007 //        All rights reserved.
00008 //
00009 //        Redistribution and use in source and binary forms, with or without 
00010 //        modification, are permitted provided that the following conditions 
00011 //        are met:
00012 //
00013 //        *   Redistributions of source code must retain the above copyright 
00014 //            notice, this list of conditions and the following disclaimer. 
00015 //        *   Redistributions in binary form must reproduce the above 
00016 //            copyright notice, this list of conditions and the following 
00017 //            disclaimer in the documentation and/or other materials 
00018 //            provided with the distribution. 
00019 //        *   Neither the name of the FEAPI development team nor the names 
00020 //            of its contributors may be used to endorse or promote products 
00021 //            derived from this software without specific prior written 
00022 //            permission. 
00023 //
00024 //        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00025 //        "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00026 //        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
00027 //        FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
00028 //        COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
00029 //        INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
00030 //        BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00031 //        LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00032 //        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00033 //        LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
00034 //        ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00035 //        POSSIBILITY OF SUCH DAMAGE.
00036 //
00038 
00039 #if !defined(__RINGBUFFER_HEADER_INCLUDED__)
00040 #define __RINGBUFFER_HEADER_INCLUDED__
00041 
00042 #ifdef WIN32
00043 #define INLINE __forceinline
00044 #else
00045 #define INLINE
00046 #endif
00047 
00049 static INLINE unsigned int Int2PowTwo (int iValue)
00050 {
00051     unsigned int    iOrder = 0;
00052 
00053     while (iValue>>iOrder)
00054         iOrder++;
00055 
00056     if (!(iValue%(1<<(iOrder-1))))
00057         iOrder--;
00058     
00059     return (1<<(iOrder));
00060 }
00061 
00062 //********************************************************************
00063 //  @class:             CRingBuffer <template>
00064 //  @author:            Tim Flohrer
00065 //  @notes:             implements a Ringbuffer
00066 //  @creation date:     18.7.2001
00067 //  @last modified:     
00068 //********************************************************************
00069 
00070 template <class T>  class CRingBuffer 
00071 {
00072     
00073 private:
00074     
00075     unsigned int    m_uiReadIndex,
00076             m_uiWriteIndex,
00077             m_uiTmpIndex;
00078 
00079     unsigned int    m_uiSize,
00080             m_uiMask;
00081 
00082     T       *m_ptBuffer;
00083 
00084 
00085 public:
00086     static int   CreateInstance  (CRingBuffer*& pCRingBuffer, unsigned int uiLength)
00087     {
00088         int  rErr    = 0;
00089         pCRingBuffer    = 0;
00090     
00091         // create instance
00092         pCRingBuffer    = new CRingBuffer (uiLength);
00093     
00094         if (pCRingBuffer == NULL)
00095             rErr        = -1;
00096         else if (!pCRingBuffer->m_ptBuffer)
00097         {
00098             rErr        = -1;
00099             delete pCRingBuffer;
00100             pCRingBuffer= 0;
00101         }
00102 
00103         return rErr;
00104     };
00105 
00106     static int   DestroyInstance (CRingBuffer*& pCRingBuffer)
00107     {
00108         if (!pCRingBuffer)
00109             return -1;
00110 
00111         delete pCRingBuffer;
00112         pCRingBuffer    = 0;
00113 
00114         return 0;
00115     };
00116 
00117 //********************************************************************
00118 //  @method:            CRingBuffer 
00119 //  @parameter:         unsigned int uiNewSize      desired size of the ring buffer
00120 //  @result:            
00121 //  @author:            Tim Flohrer
00122 //  @notes:             constructor of the CRingbuffer class
00123 //  @creation date:     18.7.2001
00124 //  @last modified:     
00125 //********************************************************************
00126     CRingBuffer(unsigned int uiNewSize)
00127     {
00128 //      m_uiSize            =   (unsigned int)pow(2,1+(int)(log(uiNewSize)/log(2)));
00129         m_uiSize            =   Int2PowTwo(uiNewSize);
00130         m_uiMask            =   m_uiSize-1;
00131         m_uiReadIndex       =   0;
00132         m_uiWriteIndex      =   0;
00133         m_uiTmpIndex        =   0;
00134         m_ptBuffer          =   0;
00135         m_ptBuffer          =   new T[m_uiSize];
00136         for( unsigned int i=0; i<m_uiSize; m_ptBuffer[i++] = 0);
00137     }
00138     
00139 
00140 //********************************************************************
00141 //  @method:            ~CRingBuffer
00142 //  @parameter:         
00143 //  @result:            
00144 //  @author:            Tim Flohrer
00145 //  @notes:             destructor
00146 //  @creation date:     18.7.2001
00147 //  @last modified:     
00148 //********************************************************************
00149     ~CRingBuffer()
00150     {
00151         if (m_ptBuffer)
00152             delete [] m_ptBuffer;
00153     }
00154     
00155     
00156 //********************************************************************
00157 //  @method:            GetOff
00158 //  @parameter:         int iIndex
00159 //  @result:            T
00160 //  @author:            Tim Flohrer
00161 //  @notes:             gets an item at offset iIndex from the
00162 //                      current read pointer, without modifying 
00163 //  @creation date:     18.7.2001
00164 //  @last modified:     
00165 //********************************************************************
00166 INLINE T GetOff(int iIndex)
00167     {       
00168         return m_ptBuffer[(m_uiReadIndex + iIndex + m_uiSize) & m_uiMask];
00169     }
00170 
00171 INLINE T GetOffW(int iIndex)
00172     {       
00173         return m_ptBuffer[(m_uiWriteIndex + iIndex + m_uiSize) & m_uiMask];
00174     }
00175 
00176 
00177 //********************************************************************
00178 //  @method:            PutOff
00179 //  @parameter:         T tItem
00180 //                      int iIndex
00181 //  @result:            
00182 //  @author:            Tim Flohrer
00183 //  @notes:             puts an item at offset iIndex from the
00184 //                      current write pointer, without modifying
00185 //  @creation date:     18.7.2001
00186 //  @last modified:     
00187 //********************************************************************
00188 INLINE void PutOff(T tItem,int iIndex)
00189     {
00190         m_ptBuffer[(m_uiWriteIndex + iIndex + m_uiSize) & m_uiMask] =   tItem;
00191     }
00192     
00193 //********************************************************************
00194 //  @method:            GetOffMod
00195 //  @parameter:         int iIndex
00196 //  @result:            T
00197 //  @author:            Tim Flohrer
00198 //  @notes:             gets an item at offset iIndex from the
00199 //                      current read pointer, with modifying 
00200 //  @creation date:     18.7.2001
00201 //  @last modified:     
00202 //********************************************************************
00203 INLINE T GetOffMod(int iIndex)
00204     {       
00205         m_uiReadIndex               =   ((m_uiReadIndex + iIndex + m_uiSize) & m_uiMask);
00206         return m_ptBuffer[ m_uiReadIndex ];
00207     }
00208     
00209     
00210 //********************************************************************
00211 //  @method:            PutOffMod
00212 //  @parameter:         T tItem
00213 //                      int iIndex
00214 //  @result:            
00215 //  @author:            Tim Flohrer
00216 //  @notes:             puts an item at offset iIndex from the
00217 //                      current write pointer, with modifying
00218 //  @creation date:     18.7.2001
00219 //  @last modified:     
00220 //********************************************************************
00221 INLINE void PutOffMod(T tItem,int iIndex)
00222     {
00223         m_uiWriteIndex              =   (m_uiWriteIndex + iIndex + m_uiSize) & m_uiMask;
00224         m_ptBuffer[m_uiWriteIndex]  =   tItem;
00225     }
00226     
00227 
00228 //********************************************************************
00229 //  @method:            Get
00230 //  @parameter:         
00231 //  @result:            T
00232 //  @author:            Tim Flohrer
00233 //  @notes:             gets an item at current read pointer
00234 //  @creation date:     18.7.2001
00235 //  @last modified:     
00236 //********************************************************************  
00237 INLINE T Get()
00238     {       
00239         return m_ptBuffer[ m_uiReadIndex ];
00240     }
00241     
00242 
00243 //********************************************************************
00244 //  @method:            Put
00245 //  @parameter:         T tItem
00246 //  @result:            
00247 //  @author:            Tim Flohrer
00248 //  @notes:             puts an item at current read pointer
00249 //  @creation date:     18.7.2001
00250 //  @last modified:     
00251 //********************************************************************  
00252 INLINE void Put(T tItem)
00253     {
00254         m_ptBuffer[ m_uiWriteIndex ]    =   tItem;
00255     }
00256     
00257 
00258 //********************************************************************
00259 //  @method:            GetPostInc
00260 //  @parameter:         
00261 //  @result:            T
00262 //  @author:            Tim Flohrer
00263 //  @notes:             gets an item at the current read pointer
00264 //                      and increments the read pointer afterwards
00265 //  @creation date:     18.7.2001
00266 //  @last modified:     
00267 //********************************************************************
00268 INLINE T GetPostInc()
00269     {       
00270         m_uiTmpIndex    = m_uiReadIndex;
00271         m_uiReadIndex   = (m_uiReadIndex + 1) & m_uiMask; 
00272         return m_ptBuffer[ m_uiTmpIndex ];
00273     }
00274 
00275 INLINE void GetPostInc (T* ptBuffer, int iNumOfItems)
00276     {
00277         if (iNumOfItems <= 0)
00278             return;
00279         m_uiTmpIndex    = m_uiReadIndex;
00280         m_uiReadIndex   = (m_uiReadIndex + iNumOfItems) & m_uiMask; 
00281         if (m_uiTmpIndex + iNumOfItems <= m_uiSize)
00282         {
00283             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * iNumOfItems);
00284             return;
00285         }
00286         else
00287         {
00288             ZASSERT (m_uiSize - m_uiTmpIndex <= 0);
00289             ZASSERT (iNumOfItems - m_uiSize + m_uiTmpIndex <= 0);
00290             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * (m_uiSize - m_uiTmpIndex));
00291             memcpy (&ptBuffer[m_uiSize - m_uiTmpIndex], m_ptBuffer, sizeof(T) * (iNumOfItems - m_uiSize + m_uiTmpIndex));
00292             return;
00293         }
00294     }
00295 INLINE void GetOffPostInc(T* ptBuffer, int iNumOfItems, int iIndex)
00296     {       
00297         if (iNumOfItems <= 0)
00298             return;
00299         m_uiTmpIndex    = ((m_uiReadIndex + iIndex + m_uiSize) & m_uiMask);
00300         m_uiReadIndex   = (m_uiReadIndex + iNumOfItems) & m_uiMask; 
00301         if (m_uiTmpIndex + iNumOfItems <= m_uiSize)
00302         {
00303             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * iNumOfItems);
00304             return;
00305         }
00306         else
00307         {
00308             ZASSERT (m_uiSize - m_uiTmpIndex <= 0);
00309             ZASSERT (iNumOfItems - m_uiSize + m_uiTmpIndex <= 0);
00310             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * (m_uiSize - m_uiTmpIndex));
00311             memcpy (&ptBuffer[m_uiSize - m_uiTmpIndex], m_ptBuffer, sizeof(T) * (iNumOfItems - m_uiSize + m_uiTmpIndex));
00312             return;
00313         }
00314     }
00315 INLINE void GetOff(T* ptBuffer, int iNumOfItems, int iIndex)
00316     {       
00317         if (iNumOfItems <= 0)
00318             return;
00319         m_uiTmpIndex    = ((m_uiReadIndex + iIndex + m_uiSize) & m_uiMask);
00320         if (m_uiTmpIndex + iNumOfItems <= m_uiSize)
00321         {
00322             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * iNumOfItems);
00323             return;
00324         }
00325         else
00326         {
00327             ZASSERT (m_uiSize - m_uiTmpIndex <= 0);
00328             ZASSERT (iNumOfItems - m_uiSize + m_uiTmpIndex <= 0);
00329             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * (m_uiSize - m_uiTmpIndex));
00330             memcpy (&ptBuffer[m_uiSize - m_uiTmpIndex], m_ptBuffer, sizeof(T) * (iNumOfItems - m_uiSize + m_uiTmpIndex));
00331             return;
00332         }
00333     }
00334 INLINE void GetOffW(T* ptBuffer, int iNumOfItems, int iIndex)
00335     {       
00336         if (iNumOfItems <= 0)
00337             return;
00338         m_uiTmpIndex    = ((m_uiWriteIndex + iIndex + m_uiSize) & m_uiMask);
00339         if (m_uiTmpIndex + iNumOfItems <= m_uiSize)
00340         {
00341             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * iNumOfItems);
00342             return;
00343         }
00344         else
00345         {
00346             ZASSERT (m_uiSize - m_uiTmpIndex <= 0);
00347             ZASSERT (iNumOfItems - m_uiSize + m_uiTmpIndex <= 0);
00348             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * (m_uiSize - m_uiTmpIndex));
00349             memcpy (&ptBuffer[m_uiSize - m_uiTmpIndex], m_ptBuffer, sizeof(T) * (iNumOfItems - m_uiSize + m_uiTmpIndex));
00350             return;
00351         }
00352     }
00353     
00354 //********************************************************************
00355 //  @method:            PutPostInc
00356 //  @parameter:         T tItem
00357 //  @result:            
00358 //  @author:            Tim Flohrer
00359 //  @notes:             puts an item at the current read pointer
00360 //                      and increments the write pointer afterwards
00361 //  @creation date:     18.7.2001
00362 //  @last modified:     
00363 //********************************************************************
00364 INLINE void PutPostInc(T tItem)
00365     {
00366         m_uiTmpIndex    = m_uiWriteIndex;
00367         m_uiWriteIndex = (m_uiWriteIndex + 1) & m_uiMask;
00368         m_ptBuffer[ m_uiTmpIndex ]  =   tItem;
00369     }
00370 
00371 
00372 INLINE void PutPostInc(const T *ptItem, int iNumOfItems)
00373     {
00374         if (iNumOfItems <= 0)
00375             return;
00376         m_uiTmpIndex    = m_uiWriteIndex;
00377         m_uiWriteIndex = (m_uiWriteIndex + iNumOfItems) & m_uiMask;
00378         if (m_uiTmpIndex + iNumOfItems <= m_uiSize)
00379         {
00380             memcpy (&m_ptBuffer[ m_uiTmpIndex ], ptItem, sizeof(T) * iNumOfItems);
00381             return;
00382         }
00383         else
00384         {
00385             ZASSERT (m_uiSize - m_uiTmpIndex <= 0);
00386             ZASSERT (iNumOfItems - m_uiSize + m_uiTmpIndex <= 0);
00387             memcpy (&m_ptBuffer[ m_uiTmpIndex ], ptItem, sizeof(T) * (m_uiSize - m_uiTmpIndex));
00388             memcpy (m_ptBuffer, &ptItem[m_uiSize - m_uiTmpIndex], sizeof(T) * (iNumOfItems - m_uiSize + m_uiTmpIndex));
00389             return;
00390         }
00391 }
00392     
00393 
00394 //********************************************************************
00395 //  @method:            GetPreInc
00396 //  @parameter:         
00397 //  @result:            T
00398 //  @author:            Tim Flohrer
00399 //  @notes:             increments the read pointer and 
00400 //                      gets an item at the current read pointer
00401 //  @creation date:     18.7.2001
00402 //  @last modified:     
00403 //********************************************************************
00404 INLINE T GetPreInc()
00405     {       
00406         m_uiReadIndex = (m_uiReadIndex + 1) & m_uiMask;
00407         return m_ptBuffer[ m_uiReadIndex ];
00408     }
00409     
00410 
00411 //********************************************************************
00412 //  @method:            PutPreInc
00413 //  @parameter:         T tItem
00414 //  @result:            
00415 //  @author:            Tim Flohrer
00416 //  @notes:             increments the write pointer and 
00417 //                      puts an item at the current write pointer
00418 //  @creation date:     18.7.2001
00419 //  @last modified:     
00420 //********************************************************************
00421 INLINE void PutPreInc(T tItem)
00422     {
00423         m_uiWriteIndex = (m_uiWriteIndex + 1) & m_uiMask;
00424         m_ptBuffer[ m_uiWriteIndex ]    =   tItem;
00425     }
00426     
00427 
00428 //********************************************************************
00429 //  @method:            GetPostDec
00430 //  @parameter:         
00431 //  @result:            T
00432 //  @author:            Tim Flohrer
00433 //  @notes:             gets an item at the current read pointer
00434 //                      and Decrements the read pointer afterwards
00435 //  @creation date:     18.7.2001
00436 //  @last modified:     
00437 //********************************************************************
00438 INLINE T GetPostDec()
00439     {   
00440         m_uiTmpIndex    = m_uiReadIndex;
00441         m_uiReadIndex   = (m_uiReadIndex - 1 + m_uiSize) & m_uiMask;
00442         return m_ptBuffer[ m_uiTmpIndex ];
00443     }
00444     
00445 //********************************************************************
00446 //  @method:            PutPostDec
00447 //  @parameter:         T tItem
00448 //  @result:            
00449 //  @author:            Tim Flohrer
00450 //  @notes:             puts an item at the current read pointer
00451 //                      and Decrements the write pointer afterwards
00452 //  @creation date:     18.7.2001
00453 //  @last modified:     
00454 //********************************************************************
00455 INLINE void PutPostDec(T tItem)
00456     {
00457         m_uiTmpIndex    = m_uiWriteIndex;
00458         m_uiWriteIndex  = (m_uiWriteIndex - 1 + m_uiSize) & m_uiMask;       
00459         m_ptBuffer[ m_uiTmpIndex ]  =   tItem;
00460     }
00461     
00462 
00463 //********************************************************************
00464 //  @method:            GetPreDec
00465 //  @parameter:         
00466 //  @result:            T
00467 //  @author:            Tim Flohrer
00468 //  @notes:             Decrements the read pointer and 
00469 //                      gets an item at the current read pointer
00470 //  @creation date:     18.7.2001
00471 //  @last modified:     
00472 //********************************************************************
00473 INLINE T GetPreDec()
00474     {       
00475         m_uiReadIndex = (m_uiReadIndex - 1 + m_uiSize) & m_uiMask;
00476         return m_ptBuffer[ m_uiReadIndex ];
00477     }
00478     
00479 
00480 
00481 //********************************************************************
00482 //  @method:            PutPreDec
00483 //  @parameter:         T tItem
00484 //  @result:            
00485 //  @author:            Tim Flohrer
00486 //  @notes:             Decrements the write pointer and 
00487 //                      puts an item at the current write pointer
00488 //  @creation date:     18.7.2001
00489 //  @last modified:     
00490 //********************************************************************
00491 INLINE void PutPreDec(T tItem)
00492     {
00493         m_uiWriteIndex = (m_uiWriteIndex - 1 + m_uiSize) & m_uiMask;
00494         m_ptBuffer[ m_uiWriteIndex ]    =   tItem;
00495     }
00496     
00497 
00498 //********************************************************************
00499 //  @method:            GetReadPos
00500 //  @parameter:         
00501 //  @result:            int
00502 //  @author:            Tim Flohrer
00503 //  @notes:             returns current read pointer position
00504 //  @creation date:     18.7.2001
00505 //  @last modified:     
00506 //********************************************************************
00507 INLINE int  GetReadPos()
00508     {
00509         return m_uiReadIndex;
00510     }
00511     
00512     
00513 //********************************************************************
00514 //  @method:            GetWritePos
00515 //  @parameter:         
00516 //  @result:            int
00517 //  @author:            Tim Flohrer
00518 //  @notes:             returns current write pointer position
00519 //  @creation date:     18.7.2001
00520 //  @last modified:     
00521 //********************************************************************
00522 INLINE int  GetWritePos()
00523     {
00524         return m_uiWriteIndex;
00525     }
00526     
00527     
00528 //********************************************************************
00529 //  @method:            SetReadPos
00530 //  @parameter:         int iIndex
00531 //  @result:            
00532 //  @author:            Tim Flohrer
00533 //  @notes:             sets the read pointer
00534 //  @creation date:     18.7.2001
00535 //  @last modified:     
00536 //********************************************************************
00537 INLINE void SetReadPos(int iIndex)
00538     {
00539         m_uiReadIndex   = (iIndex + m_uiSize) & m_uiMask; 
00540     }
00541     
00542 
00543 //********************************************************************
00544 //  @method:            SetWritePos
00545 //  @parameter:         int iIndex  
00546 //  @result:            
00547 //  @author:            Tim Flohrer
00548 //  @notes:             sets the write pointer
00549 //  @creation date:     18.7.2001
00550 //  @last modified:     
00551 //********************************************************************  
00552 INLINE void SetWritePos(int iIndex)
00553     {
00554         m_uiWriteIndex  = (iIndex + m_uiSize) & m_uiMask;
00555     }
00556     
00557     
00558 
00559 //********************************************************************
00560 //  @method:            GetSamplesInBuffer
00561 //  @parameter:         
00562 //  @result:            
00563 //  @author:            Tim Flohrer
00564 //  @notes:             returns number of samples in buffer
00565 //                      i.e. the difference between the read and write pointer  
00566 //  @creation date:     18.7.2001
00567 //  @last modified:     
00568 //********************************************************************
00569 INLINE int  GetSamplesInBuffer()
00570     {
00571         int iCount = m_uiWriteIndex - m_uiReadIndex;
00572         
00573         return (iCount < 0 ? m_uiSize + iCount : iCount);
00574     }   
00575 
00576 INLINE void   Reset()
00577     {
00578         m_uiReadIndex   =   0;
00579         m_uiWriteIndex  =   0;
00580         m_uiTmpIndex    =   0;
00581         for( unsigned int i=0; i<m_uiSize; m_ptBuffer[i++] = 0);
00582     }
00583 
00584 };
00585 
00586 #endif //   __RINGBUFFER_HEADER_INCLUDED__
00587 
00588 

Generated on Fri Mar 23 10:28:54 2007 for FEAPI Plugin Documentation by  doxygen 1.3.9.1