#ifndef BIWAK_BUFFER_RING_CPP
#define BIWAK_BUFFER_RING_CPP
//----------------------------------------------------------------------------
///
/// \brief  Buffer ring
///
///         There must be only exist one producer and one consumer at the same
///         time
///
///         Empty:                        2 Entries:
///         [0 ] [1*] [2 ] [3 ] [4 ]      [0 ] [1*] [2*] [3 ] [4 ]
///               |    \--m_top                 |         \--m_top
///               \-- m_bottom                  \-- m_bottom
///
///         Full:
///         [0*] [1*] [2*] [3*] [4*]
///               |    \--m_top
///               \-- m_bottom
///
/// \date   20221222
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Includes-----------------------------------------------------------------


//---Own------------------------------

#include <lepto/ring.h>
//#include <biwak/timer.h>


//---Defines------------------------------------------------------------------


//---Declaration--------------------------------------------------------------

struct SBuffer{
   //bool used;
   int size;
   void *data;
   // lrtimer_t timestamp;
};

class CBufferRing: public CRing<SBuffer>
{
   private:
#if 0
      struct SBuffer{
         bool used;
         int size;
         void *data;
      };
      SBuffer  *m_buffers;
      int m_bufferSize;
      int m_bufferCount;

      //int m_topBuffer;
      //int m_bottomBuffer;
      int m_currentBuffer;
      int m_usedBuffer;
#endif
      int m_maxBufferSize;

   public:

      CBufferRing(int bufferSize, int buffers)
         :CRing(buffers)
         ,m_maxBufferSize(bufferSize)
         //,m_bufferCount(buffers)
         //,m_bottomBuffer(0)
         //,m_topBuffer(0)
         //,m_currentBuffer(0)
         //,m_usedBuffer(0)
      {
         //m_buffers=new SBuffer[buffers];


         for(int i1=0; i1<buffers; i1++)
         {
            SBuffer &buffer=rawEntry(i1);
            //buffer.used=0;
            buffer.data=malloc(bufferSize);
            buffer.size=0;
         }
      }
      int getMaxBufferSize() const
      {
         return(m_maxBufferSize);
      }
      // exceptionally push an buffer first and then fill it with data
      void *pushBuffer( int size )
      {
         SBuffer *buffer=topEntry();

         lDebugAssert( size <= m_maxBufferSize );

         if(!buffer)
         {
            return(nullptr);
         }
         buffer->size=size;
         // buffer->timestamp=lrNow();
         pushEntry();
         return( buffer->data );
      }
      void *getTopData() const
      {
         /*
         if( ! topEntry() )
         {
            return(nullptr);
         }
         */
         return( topEntry()->data );
      }
      void *getBottomData() const
      {
         if( bottomEntry() )
         {
            return( bottomEntry()->data );
         }
         else
         {
            return(nullptr);
         }
      }
      int getBottomSize() const
      {
         lDebugAssert( bottomEntry() );
         return(  bottomEntry()->size);
      }
      void dropBuffer()
      {
         lDebugAssert( m_bottomPos >= 0 );
         dropEntry();
      }
      int usedBuffers() const
      {
         return( getEntryCount() );
      }
};


//---fin----------------------------------------------------------------------
#endif // ? ! BIWAK_BUFFER_RING_CPP
