#if !defined(__alffnbuf_h) // Sentry, use file only if it's not already included.
#define __alffnbuf_h
/*  Project Biblio-Tec

	 SUBSYSTEM:    TEC30r.dll Library (TecLib)
	 FILE:         alffbuff.h
	 AUTHOR:       Alcindo Ferreira Filho

	 OVERVIEW:
	 ALFFBuffer (Berkeley Ring Buffer Based) Class definition.
    This kind of buffer is an circular buffer that MUST be
    well controlled for not overlapping its beggining point
    when its endpoint is not reached correctly.......

    COPYRIGHT:    © 1998,2005
                  Alcindo Ferreira Filho
                  <alcindo@inttec.com.br>
                  © 1988
                  University of California

    LICENSE:      Biblio-Tec license (BiblTec.txt)
                  University of California (see below)
*/
#include "alffobj.h"
#include <mem.h>
#include <windows.h>

/* Internal macros */
#if	!defined(MIN)
#define	MIN(a,b)   (((a)<(b))? (a):(b))

#endif	/* !defined(MIN) */


#define	ring_subtract(a,b)	((((int)(a))-((int)(b)) >= 0)? \
											  (a)-(b): (((a)-(b)) + size))


#define	ring_increment(a,c)	(((a)+(c) < top)? \

											  (a)+(c) : (((a)+(c)) - size))


#define	ring_decrement(a,c)	(((a)-(c) >= bottom)? \
											  (a)-(c) : (((a)-(c)) - size))


#define	ring_empty ((consume == supply) && \
											  (consumetime >= supplytime))


#define	ring_full ((supply == consume) && \

											  (supplytime > consumetime))


/*----------------------------------------------------------------*/

/*
 * Copyright (c) 1988 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the University of California, Berkeley.  The name of the
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *	@(#)ring.h	1.8 (Berkeley) 6/29/88
 */

/*-------------------------------------------------------------------*/

/*
 * This defines a structure for a ring buffer.
 *
 * The circular buffer has two parts:
 *
 *		full:	  [consume, supply)
 *		empty:  [supply, consume)
 *
 *
 */

typedef struct
{     char *consume,	/* where data comes out of */
				*supply,	/* where data comes in to */
				*bottom,	/* lowest address in buffer */
				*top,		/* highest address+1 in buffer */
				*mark;	/* marker (user defined) */
	 int	 	size;		/* size in bytes of buffer */

	 unsigned long	consumetime,	 /* help us keep straight full, */

						supplytime;     /* empty, etc. */
} Ring;


//{{ALFFBuffer}}
class _TECLIBCLASS ALFFBuffer : public Ring, public ALFFObject
{

  public:
	 ALFFBuffer(int bufSize);
	 virtual ~ALFFBuffer();
    char far* GetClassName() {return "ALFFBuffer";}

	 /* Ring Buffer Reset Routine */
	 void ring_reset();

	 /* Data movement routines */
	 void ring_supply_data(char *buffer, int count);
	 int  ring_consume_data(char *buffer, int count);


	 /* Buffer state transition routines */

	 void	ring_supplied(int count);
	 void ring_consumed(int count);


	 /* Buffer Position Mark routines */

	 void	ring_mark();
	 bool ring_at_mark();
	 void ring_clear_mark();


	 /* Buffer state query routines */
	 int ring_empty_count();
	 int ring_empty_consecutive();
	 int ring_full_count();
	 int ring_full_consecutive();


  protected:
	 char *ringBuf;

	 /*
	  * The following is a clock, used to determine full, empty, etc.
	  * There is some trickiness here.  Since the ring buffers are initialized
	  * to ZERO on allocation, we need to make sure, when interpreting the
	  * clock, that when the times are EQUAL, then the buffer is FULL.
	  */

	 unsigned long ring_clock;

};

typedef ALFFBuffer* LPALFFBUF;

//{{ALFFBuffer}}


#endif

