#ifndef _FROG_Base_H__
#define _FROG_Base_H__
#include "Utils.h"
namespace FROG{
class Base{
protected :
std::vector daughters_;
Base* mother_;
unsigned int detId_;
char* Name_;
unsigned short NameLength_;
public :
virtual unsigned int chunkId(){ return 55556;}
virtual bool isCompactible() { return true; }
class cmp {
public:
bool operator () (Base* a, Base* b) {return (a->chunkId() < b->chunkId());}
};
Base(){
mother_ = NULL;
}
virtual ~Base(){
for(unsigned int i=0;iwrite();
daughterBufferSize += daughterBuffer[i].position();
}
ByteBuffer toReturn = ByteBuffer(daughterBufferSize);
unsigned int PreviousChunkId = -1; int PreviousSizeBlock = 2;
int Size = 0;
for(unsigned int i=0;ichunkId() == PreviousChunkId && daughters_[i]->isCompactible();
//printf("Compacting = %i\n",(int)Compacting);
//printf("pos = %i\n",(int)daughterBuffer[i].position());
daughterBuffer[i].flip();
//printf("pos = %i\n",(int)daughterBuffer[i].position());
if(Compacting){ daughterBuffer[i].position(6);
}else{ PreviousSizeBlock = toReturn.position()+2; }
Size += daughterBuffer[i].remaining();
//printf("ToReturnpos = %i (%i)\n",toReturn.position(), daughterBuffer[i].position());
toReturn.put(daughterBuffer[i]);
//printf("ToReturnpos = %i (%i)\n",toReturn.position(), daughterBuffer[i].position());
if(Compacting)toReturn.putInt(PreviousSizeBlock, Size );
PreviousChunkId = daughters_[i]->chunkId();
}
//printf("DaughtersData size = %i - position =%i\n",toReturn.capacity(), toReturn.position());
return toReturn;
}
ByteBuffer write(){
ByteBuffer data = writeData();
ByteBuffer daughterData = writeDaughtersData();
ByteBuffer toReturn = ByteBuffer(6 + data.position() + daughterData.position());
toReturn.putUShort(chunkId());
toReturn.putInt(0);
data.flip();
toReturn.put(data);
data.clear();
daughterData.flip();
toReturn.put(daughterData);
daughterData.clear();
toReturn.putInt(2,toReturn.position());
return toReturn;
}
void save(std::string fileName){
ByteBuffer data = write();
FILE* pFile = fopen(fileName.c_str(), "wb");
fwrite(data.buffer,data.position(),1,pFile);
fclose(pFile);
data.clear();
}
const std::vector& daughters() const { return daughters_; }
void setMother(Base* mother){ mother_ = mother;}
Base* getMother(){
return mother_;
}
void addDaughter( Base* dau){
dau->setMother(this);
daughters_.push_back(dau);
}
Base* getDaughter(unsigned int i){
return daughters_[i];
}
unsigned int getDaughterSize(){
return daughters_.size();
}
void deleteDaughter(unsigned int i){
if(i>=daughters_.size())return;
daughters_[i]->deleteDaughters();
delete daughters_[i];
daughters_.erase(daughters_.begin()+i);
}
void deleteDaughters(){
for(unsigned int i=0;ideleteDaughters();
delete daughters_[i];
}
daughters_.clear();
}
unsigned int DetId(){return detId_;}
Base* getDaughterWithDetId(unsigned int detid) const {
for(unsigned int i=0;iDetId()==detid)return daughters_[i];
}
return NULL;
}
};
}//FROG Namespace
#endif