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

#include "base/polygraph.h"

#include "base/ObjId.h"
#include "base/RndPermut.h"
#include "runtime/IOBuf.h"
#include "csm/ContentCfg.h"
#include "csm/BodyIter.h"


BodyIter::BodyIter() {
	reset();
}

BodyIter::~BodyIter() {
	reset();
}

void BodyIter::contentSize(Size aContentSize) {
	theContentSize = aContentSize;
}

void BodyIter::start(WrBuf *aBuf) {
	Assert(theOid);
	Assert(aBuf);
	theBuf = aBuf;
	theRng.seed(GlbPermut(theContentHash, rndBodyIter));
	theBuiltSize = 0;
}

void BodyIter::reset() {
	theOid.reset();
	theBuf = 0;
	theContentSize = theBuiltSize = Size();
}

bool BodyIter::canPour() const {
	return theBuf && !theBuf->full() && !pouredAll();
}

bool BodyIter::pouredAll() const {
	return theContentSize.known() && theBuiltSize >= theContentSize;
}

bool BodyIter::pour() {
	if (theBuiltSize == 0)
		pourPrefix();
	return pourBody();
}

void BodyIter::pourPrefix() {
	if (Should(theContentCfg && theBuf))
		theBuiltSize += theContentCfg->pourContentPrefix(theOid, *theBuf);
}


syntax highlighted by Code2HTML, v. 0.9.1