/* Web Polygraph       http://www.web-polygraph.org/
 * (C) 2003-2006 The Measurement Factory
 * Licensed under the Apache License, Version 2.0 */

#ifndef POLYGRAPH__CSM_BODYITER_H
#define POLYGRAPH__CSM_BODYITER_H

#include "xstd/Rnd.h"
#include "base/ObjId.h"

class WrBuf;
class ContentCfg;

class BodyIter {
	public:
		BodyIter();
		virtual ~BodyIter();

		virtual void reset();

		void contentCfg(const ContentCfg *cfg) { theContentCfg = cfg; }
		void oidCfg(const ObjId &anOid, int aHash) { theOid = anOid; theContentHash = aHash; }
		void contentSize(Size aContentSize);

		virtual void start(WrBuf *aBuf);
		virtual void stop() {}

		const ObjId &oid() const { return theOid; }
		const ContentCfg *contentCfg() const { return theContentCfg; }
		int contentHash() const { return theContentHash; }
		Size contentSize() const { return theContentSize; }
		Size builtSize() const { return theBuiltSize; }
		Size offSeed() const { return theContentHash; }

		bool canPour() const; // can add to buffer and buffer has space
		bool pouredAll() const; // has someting to add to buffer
		bool pour();  // fills the buffer; false if unrecoverable error

		operator void *() const { return pouredAll() ? 0 : (void*)-1; }

	protected:
		void pourPrefix();
		virtual bool pourBody() = 0;
		Size sizeLeft() const { return theContentSize.known() ?
			theContentSize - theBuiltSize : theContentSize; }

	protected:
		const ContentCfg *theContentCfg;
		ObjId theOid;  // XXX: replace with a pointer when ObjRec is removed
		WrBuf *theBuf;
		RndGen theRng;
		Size theContentSize;
		int theContentHash;

		Size theBuiltSize;
};

#endif


syntax highlighted by Code2HTML, v. 0.9.1