Fork me on GitHub

source: svn/trunk/Utilities/frog/Base.h@ 584

Last change on this file since 584 was 583, checked in by cp3-support, 13 years ago

FROG 3.0

File size: 3.9 KB
RevLine 
[583]1#ifndef _FROG_Base_H__
2#define _FROG_Base_H__
3
4#include "Utils.h"
5
6namespace FROG{
7
8class Base{
9protected :
10 std::vector<Base*> daughters_;
11 Base* mother_;
12 unsigned int detId_;
13 char* Name_;
14 unsigned short NameLength_;
15
16public :
17 virtual unsigned int chunkId(){ return 55556;}
18 virtual bool isCompactible() { return true; }
19
20 class cmp {
21 public:
22 bool operator () (Base* a, Base* b) {return (a->chunkId() < b->chunkId());}
23 };
24
25 Base(){
26 mother_ = NULL;
27 }
28
29 virtual ~Base(){
30 for(unsigned int i=0;i<daughters_.size();i++){if(daughters_[i]){
31 delete daughters_[i];
32 daughters_[i]=NULL;}
33 }
34 }
35
36 virtual ByteBuffer writeData(){
37 return ByteBuffer(0);
38 }
39
40 virtual ByteBuffer writeDaughtersData(){
41 if(daughters_.size()==0)return ByteBuffer(0);
42
43 ByteBuffer* daughterBuffer = new ByteBuffer[daughters_.size()];
44 int daughterBufferSize = 0;
45 for(unsigned int i=0;i<daughters_.size();i++){
46 daughterBuffer[i] = (daughters_[i])->write();
47 daughterBufferSize += daughterBuffer[i].position();
48 }
49
50 ByteBuffer toReturn = ByteBuffer(daughterBufferSize);
51 unsigned int PreviousChunkId = -1; int PreviousSizeBlock = 2;
52 int Size = 0;
53 for(unsigned int i=0;i<daughters_.size();i++){
54 bool Compacting = daughters_[i]->chunkId() == PreviousChunkId && daughters_[i]->isCompactible();
55 //printf("Compacting = %i\n",(int)Compacting);
56 //printf("pos = %i\n",(int)daughterBuffer[i].position());
57 daughterBuffer[i].flip();
58 //printf("pos = %i\n",(int)daughterBuffer[i].position());
59 if(Compacting){ daughterBuffer[i].position(6);
60 }else{ PreviousSizeBlock = toReturn.position()+2; }
61 Size += daughterBuffer[i].remaining();
62 //printf("ToReturnpos = %i (%i)\n",toReturn.position(), daughterBuffer[i].position());
63 toReturn.put(daughterBuffer[i]);
64 //printf("ToReturnpos = %i (%i)\n",toReturn.position(), daughterBuffer[i].position());
65 if(Compacting)toReturn.putInt(PreviousSizeBlock, Size );
66 PreviousChunkId = daughters_[i]->chunkId();
67 }
68 //printf("DaughtersData size = %i - position =%i\n",toReturn.capacity(), toReturn.position());
69 return toReturn;
70 }
71
72 ByteBuffer write(){
73 ByteBuffer data = writeData();
74 ByteBuffer daughterData = writeDaughtersData();
75 ByteBuffer toReturn = ByteBuffer(6 + data.position() + daughterData.position());
76 toReturn.putUShort(chunkId());
77 toReturn.putInt(0);
78 data.flip();
79 toReturn.put(data);
80 data.clear();
81 daughterData.flip();
82 toReturn.put(daughterData);
83 daughterData.clear();
84 toReturn.putInt(2,toReturn.position());
85 return toReturn;
86 }
87
88 void save(std::string fileName){
89 ByteBuffer data = write();
90 FILE* pFile = fopen(fileName.c_str(), "wb");
91 fwrite(data.buffer,data.position(),1,pFile);
92 fclose(pFile);
93 data.clear();
94 }
95
96
97 const std::vector<Base*>& daughters() const { return daughters_; }
98 void setMother(Base* mother){ mother_ = mother;}
99 Base* getMother(){
100 return mother_;
101 }
102
103 void addDaughter( Base* dau){
104 dau->setMother(this);
105 daughters_.push_back(dau);
106 }
107
108 Base* getDaughter(unsigned int i){
109 return daughters_[i];
110 }
111
112 unsigned int getDaughterSize(){
113 return daughters_.size();
114 }
115
116 void deleteDaughter(unsigned int i){
117 if(i>=daughters_.size())return;
118
119 daughters_[i]->deleteDaughters();
120 delete daughters_[i];
121 daughters_.erase(daughters_.begin()+i);
122 }
123
124 void deleteDaughters(){
125 for(unsigned int i=0;i<daughters_.size();i++){
126 daughters_[i]->deleteDaughters();
127 delete daughters_[i];
128 }
129 daughters_.clear();
130 }
131
132 unsigned int DetId(){return detId_;}
133 Base* getDaughterWithDetId(unsigned int detid) const {
134 for(unsigned int i=0;i<daughters_.size();i++){
135 if(daughters_[i]->DetId()==detid)return daughters_[i];
136 }
137 return NULL;
138 }
139
140};
141
142}//FROG Namespace
143#endif
Note: See TracBrowser for help on using the repository browser.