Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef tools_rroot_iros 4 #ifndef tools_rroot_iros 5 #define tools_rroot_iros 5 #define tools_rroot_iros 6 6 7 #include "object" 7 #include "object" 8 #include "../vmanip" 8 #include "../vmanip" 9 #include "../forit" 9 #include "../forit" 10 #include "../scast" 10 #include "../scast" 11 11 12 #include "cids" 12 #include "cids" 13 13 14 namespace tools { 14 namespace tools { 15 namespace rroot { 15 namespace rroot { 16 16 17 class iros : public virtual iro,protected std: 17 class iros : public virtual iro,protected std::vector<iro*> { //proteced to avoid using std::vector::clear(). 18 typedef std::vector<iro*> parent; 18 typedef std::vector<iro*> parent; 19 public: 19 public: 20 static const std::string& s_store_class() { 20 static const std::string& s_store_class() { 21 static const std::string s_v("TObjArray"); 21 static const std::string s_v("TObjArray"); 22 return s_v; 22 return s_v; 23 } 23 } 24 public: 24 public: 25 static const std::string& s_class() { 25 static const std::string& s_class() { 26 static const std::string s_v("tools::rroot 26 static const std::string s_v("tools::rroot::iros"); 27 return s_v; 27 return s_v; 28 } 28 } 29 public: //iro 29 public: //iro 30 virtual void* cast(const std::string& a_clas 30 virtual void* cast(const std::string& a_class) const { 31 if(void* p = cmp_cast<iros>(this,a_class)) 31 if(void* p = cmp_cast<iros>(this,a_class)) return p; 32 return 0; 32 return 0; 33 } 33 } 34 virtual const std::string& s_cls() const {re 34 virtual const std::string& s_cls() const {return s_class();} 35 public: 35 public: 36 static cid id_class() {return obj_list_cid() 36 static cid id_class() {return obj_list_cid();} 37 virtual void* cast(cid a_class) const { 37 virtual void* cast(cid a_class) const { 38 if(void* p = cmp_cast<iros>(this,a_class)) 38 if(void* p = cmp_cast<iros>(this,a_class)) {return p;} 39 else return 0; 39 else return 0; 40 } 40 } 41 public: 41 public: 42 virtual iro* copy() const {return new iros(* 42 virtual iro* copy() const {return new iros(*this);} 43 virtual bool stream(buffer& a_buffer) { 43 virtual bool stream(buffer& a_buffer) { 44 ifac::args args; 44 ifac::args args; 45 bool accept_null = false; 45 bool accept_null = false; 46 return stream(a_buffer,args,accept_null); 46 return stream(a_buffer,args,accept_null); 47 } 47 } 48 public: 48 public: 49 iros(ifac& a_fac) 49 iros(ifac& a_fac) 50 :m_fac(a_fac) 50 :m_fac(a_fac) 51 { 51 { 52 #ifdef TOOLS_MEM 52 #ifdef TOOLS_MEM 53 mem::increment(s_class().c_str()); 53 mem::increment(s_class().c_str()); 54 #endif 54 #endif 55 } 55 } 56 virtual ~iros(){ 56 virtual ~iros(){ 57 _clear(); 57 _clear(); 58 #ifdef TOOLS_MEM 58 #ifdef TOOLS_MEM 59 mem::decrement(s_class().c_str()); 59 mem::decrement(s_class().c_str()); 60 #endif 60 #endif 61 } 61 } 62 public: 62 public: 63 iros(const iros& a_from) 63 iros(const iros& a_from) 64 :iro(a_from) 64 :iro(a_from) 65 ,parent() 65 ,parent() 66 ,m_fac(a_from.m_fac) 66 ,m_fac(a_from.m_fac) 67 { 67 { 68 #ifdef TOOLS_MEM 68 #ifdef TOOLS_MEM 69 mem::increment(s_class().c_str()); 69 mem::increment(s_class().c_str()); 70 #endif 70 #endif 71 tools_vforcit(iro*,a_from,it) { 71 tools_vforcit(iro*,a_from,it) { 72 parent::push_back((*it)->copy()); 72 parent::push_back((*it)->copy()); 73 m_owns.push_back(true); 73 m_owns.push_back(true); 74 } 74 } 75 } 75 } 76 iros& operator=(const iros& a_from){ 76 iros& operator=(const iros& a_from){ 77 if(&a_from==this) return *this; 77 if(&a_from==this) return *this; 78 78 79 _clear(); 79 _clear(); 80 80 81 tools_vforcit(iro*,a_from,it) { 81 tools_vforcit(iro*,a_from,it) { 82 parent::push_back((*it)->copy()); 82 parent::push_back((*it)->copy()); 83 m_owns.push_back(true); 83 m_owns.push_back(true); 84 } 84 } 85 85 86 return *this; 86 return *this; 87 } 87 } 88 public: 88 public: 89 parent::const_iterator begin() const {return 89 parent::const_iterator begin() const {return parent::begin();} 90 parent::iterator begin() {return parent::beg 90 parent::iterator begin() {return parent::begin();} 91 parent::const_iterator end() const {return p 91 parent::const_iterator end() const {return parent::end();} 92 parent::iterator end() {return parent::end() 92 parent::iterator end() {return parent::end();} 93 parent::size_type size() const {return paren 93 parent::size_type size() const {return parent::size();} 94 public: 94 public: 95 void cleanup() {_clear();} //warning : clea 95 void cleanup() {_clear();} //warning : clear is a function of the parent std::vector. 96 void dump(std::ostream& a_out) { 96 void dump(std::ostream& a_out) { 97 a_out << " iros : size " << size() << std: 97 a_out << " iros : size " << size() << std::endl; 98 tools_vforcit(iro*,*this,it) { 98 tools_vforcit(iro*,*this,it) { 99 a_out << " class " << (*it)->s_cls() << 99 a_out << " class " << (*it)->s_cls() << std::endl; 100 } 100 } 101 } 101 } 102 public: 102 public: 103 bool stream(buffer& a_buffer,const ifac::arg 103 bool stream(buffer& a_buffer,const ifac::args& a_args,bool a_accept_null = false) { 104 _clear(); 104 _clear(); 105 105 106 short v; 106 short v; 107 unsigned int s, c; 107 unsigned int s, c; 108 if(!a_buffer.read_version(v,s,c)) return f 108 if(!a_buffer.read_version(v,s,c)) return false; 109 109 110 //::printf("debug : iros::stream : version 110 //::printf("debug : iros::stream : version %d, byte count %d\n",v,c); 111 111 112 {uint32 id,bits; 112 {uint32 id,bits; 113 if(!Object_stream(a_buffer,id,bits)) retur 113 if(!Object_stream(a_buffer,id,bits)) return false;} 114 std::string name; 114 std::string name; 115 if(!a_buffer.read(name)) return false; 115 if(!a_buffer.read(name)) return false; 116 int nobjects; 116 int nobjects; 117 if(!a_buffer.read(nobjects)) return false; 117 if(!a_buffer.read(nobjects)) return false; 118 int lowerBound; 118 int lowerBound; 119 if(!a_buffer.read(lowerBound)) return fals 119 if(!a_buffer.read(lowerBound)) return false; 120 120 121 //::printf("debug : iros : name \"%s\", no 121 //::printf("debug : iros : name \"%s\", nobject %d, lowerBound %d\n", 122 // name.c_str(),nobjects,lowerBound); 122 // name.c_str(),nobjects,lowerBound); 123 123 124 for (int i=0;i<nobjects;i++) { 124 for (int i=0;i<nobjects;i++) { 125 //::printf("debug : iros : n=%d i=%d 125 //::printf("debug : iros : n=%d i=%d ...\n",nobjects,i); 126 126 127 iro* obj; 127 iro* obj; 128 bool created; 128 bool created; 129 if(!a_buffer.read_object(m_fac,a_args,ob 129 if(!a_buffer.read_object(m_fac,a_args,obj,created)){ 130 a_buffer.out() << "tools::rroot::iros: 130 a_buffer.out() << "tools::rroot::iros::stream : can't read object." << std::endl; 131 return false; 131 return false; 132 } 132 } 133 //::printf("debug : iros : n=%d i=%d 133 //::printf("debug : iros : n=%d i=%d : ok\n",nobjects,i); 134 if(obj) { 134 if(obj) { 135 if(created) { 135 if(created) { 136 parent::push_back(obj); 136 parent::push_back(obj); 137 m_owns.push_back(true); 137 m_owns.push_back(true); 138 } else { //someone else manage this object. 138 } else { //someone else manage this object. 139 parent::push_back(obj); 139 parent::push_back(obj); 140 m_owns.push_back(false); 140 m_owns.push_back(false); 141 } 141 } 142 } else { 142 } else { 143 //a_accept_null for branch::stream m_b 143 //a_accept_null for branch::stream m_baskets. 144 if(a_accept_null) { 144 if(a_accept_null) { 145 parent::push_back(0); 145 parent::push_back(0); 146 m_owns.push_back(false); 146 m_owns.push_back(false); 147 } 147 } 148 } 148 } 149 } 149 } 150 150 151 return a_buffer.check_byte_count(s,c,s_sto 151 return a_buffer.check_byte_count(s,c,s_store_class()); 152 } 152 } 153 protected: 153 protected: 154 void _clear() { 154 void _clear() { 155 typedef parent::iterator it_t; 155 typedef parent::iterator it_t; 156 typedef std::vector<bool>::iterator itb_t; 156 typedef std::vector<bool>::iterator itb_t; 157 while(!parent::empty()) { 157 while(!parent::empty()) { 158 it_t it = parent::begin(); 158 it_t it = parent::begin(); 159 itb_t itb = m_owns.begin(); 159 itb_t itb = m_owns.begin(); 160 iro* entry = (*it); 160 iro* entry = (*it); 161 bool own = (*itb); 161 bool own = (*itb); 162 parent::erase(it); 162 parent::erase(it); 163 m_owns.erase(itb); 163 m_owns.erase(itb); 164 if(own) delete entry; 164 if(own) delete entry; 165 } 165 } 166 } 166 } 167 protected: 167 protected: 168 ifac& m_fac; 168 ifac& m_fac; 169 std::vector<bool> m_owns; 169 std::vector<bool> m_owns; 170 }; 170 }; 171 171 172 /* 172 /* 173 inline bool dummy_TObjArray_pointer_stream(buf 173 inline bool dummy_TObjArray_pointer_stream(buffer& a_buffer,ifac& a_fac,bool a_owner,bool a_warn) { 174 iros oa(a_fac,a_owner,a_warn); 174 iros oa(a_fac,a_owner,a_warn); 175 iros oa(a_fac,true,true); 175 iros oa(a_fac,true,true); 176 ifac::args args; 176 ifac::args args; 177 return oa.stream(a_buffer,args); 177 return oa.stream(a_buffer,args); 178 } 178 } 179 */ 179 */ 180 180 181 }} 181 }} 182 182 183 #endif 183 #endif