Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/rroot/obj_array is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_rroot_obj_array
0005 #define tools_rroot_obj_array
0006 
0007 #include "object"
0008 #include "../vmanip"
0009 #include "../scast"
0010 
0011 #include "cids"
0012 
0013 namespace tools {
0014 namespace rroot {
0015 
0016 template <class T>
0017 class obj_array : public virtual iro,public std::vector<T*> {
0018   typedef typename std::vector<T*> parent;
0019 private:
0020   static const std::string& s_store_class() {
0021     static const std::string s_v("TObjArray");
0022     return s_v;
0023   }
0024 public:
0025   static const std::string& s_class() {
0026     static const std::string s_v("tools::rroot::obj_array<"+T::s_class()+">");
0027     return s_v;
0028   }
0029 public: //iro
0030   virtual void* cast(const std::string& a_class) const {
0031     if(void* p = cmp_cast< obj_array<T> >(this,a_class)) return p;
0032     return 0;
0033   }
0034   virtual const std::string& s_cls() const {return s_class();}
0035 public:
0036   static cid id_class() {return obj_array_cid()+T::id_class();}
0037   virtual void* cast(cid a_class) const {
0038     if(void* p = cmp_cast<obj_array>(this,a_class)) {return p;}
0039     return 0;
0040   }
0041   virtual iro* copy() const {return new obj_array<T>(*this);}
0042   virtual bool stream(buffer& a_buffer) {
0043     ifac::args args;
0044     bool accept_null = false;
0045     return stream(a_buffer,args,accept_null);
0046   }
0047 public:
0048   obj_array(ifac& a_fac)
0049   :m_fac(a_fac)
0050   {
0051 #ifdef TOOLS_MEM
0052     mem::increment(s_class().c_str());
0053 #endif
0054   }
0055   virtual ~obj_array(){
0056     _clear();
0057 #ifdef TOOLS_MEM
0058     mem::decrement(s_class().c_str());
0059 #endif
0060   }
0061 public:
0062   obj_array(const obj_array& a_from)
0063   :iro(a_from)
0064   ,parent()
0065   ,m_fac(a_from.m_fac)
0066   {
0067 #ifdef TOOLS_MEM
0068     mem::increment(s_class().c_str());
0069 #endif
0070     typedef typename parent::const_iterator it_t;
0071     for(it_t it=a_from.begin();it!=a_from.end();++it) {
0072       if(!(*it)) {
0073         parent::push_back(0);
0074         m_owns.push_back(false);
0075       } else {
0076         iro* _obj = (*it)->copy();
0077         T* obj = safe_cast<iro,T>(*_obj);
0078         if(!obj) {
0079           m_fac.out() << "tools::rroot::obj_array::obj_array :"
0080                       << " tools::cast failed."
0081                       << std::endl;
0082           delete _obj;
0083           parent::push_back(0);
0084           m_owns.push_back(false);
0085         } else {
0086           parent::push_back(obj);
0087           m_owns.push_back(true);
0088         }
0089       }
0090     }
0091   }
0092   obj_array& operator=(const obj_array& a_from){
0093     if(&a_from==this) return *this;
0094 
0095     _clear();
0096 
0097     typedef typename parent::const_iterator it_t;
0098     for(it_t it=a_from.begin();it!=a_from.end();++it) {
0099       if(!(*it)) {
0100         parent::push_back(0);
0101         m_owns.push_back(false);
0102       } else {
0103         iro* _obj = (*it)->copy();
0104         T* obj = safe_cast<iro,T>(*_obj);
0105         if(!obj) {
0106           m_fac.out() << "tools::rroot::obj_array::operator= :"
0107                       << " tools::cast failed."
0108                       << std::endl;
0109           delete _obj;
0110           parent::push_back(0);
0111           m_owns.push_back(false);
0112         } else {
0113           parent::push_back(obj);
0114           m_owns.push_back(true);
0115         }
0116       }
0117     }
0118 
0119     return *this;
0120   }
0121 public:
0122   void cleanup() {_clear();}
0123 public:
0124   bool stream(buffer& a_buffer,const ifac::args& a_args,bool a_accept_null = false) {
0125     _clear();
0126 
0127     //::printf("debug : obj_array::stream : %lu : begin\n",(unsigned long)this);
0128 
0129     short v;
0130     unsigned int sp, bc;
0131     if(!a_buffer.read_version(v,sp,bc)) return false;
0132 
0133     //::printf("debug : obj_array::stream : version %d, byte count %d\n",v,bc);
0134 
0135    {uint32 id,bits;
0136     if(!Object_stream(a_buffer,id,bits)) return false;}
0137     std::string name;
0138     if(!a_buffer.read(name)) return false;
0139     int nobjects;
0140     if(!a_buffer.read(nobjects)) return false;
0141     int lowerBound;
0142     if(!a_buffer.read(lowerBound)) return false;
0143 
0144     //::printf("debug : obj_array : name \"%s\", nobject %d, lowerBound %d\n",name.c_str(),nobjects,lowerBound);
0145 
0146     for (int i=0;i<nobjects;i++) {
0147       //::printf("debug : obj_array::stream : %lu : n=%d i=%d ...\n",(unsigned long)this,nobjects,i);
0148 
0149       iro* obj;
0150       bool created;
0151       if(!a_buffer.read_object(m_fac,a_args,obj,created)){
0152         a_buffer.out() << "tools::rroot::obj_array::stream : can't read object"
0153                        << " in obj_array : name " << sout(name)
0154                        << ", nobjects " << nobjects << ", iobject " << i << std::endl;
0155         return false;
0156       }
0157       //::printf("debug : obj_array::stream : %lu : n=%d i=%d : ok, obj %lu\n",(unsigned long)this,
0158       //    nobjects,i,(unsigned long)obj);
0159       if(obj) {
0160         T* to = safe_cast<iro,T>(*obj);
0161         if(!to) {
0162           a_buffer.out() << "tools::rroot::obj_array::stream :"
0163                          << " tools::cast failed."
0164                          << " " << obj->s_cls() << " is not a " << T::s_class() << "."
0165                          << std::endl;
0166           if(created) {
0167             if(a_buffer.map_objs()) a_buffer.remove_in_map(obj);
0168             delete obj;
0169           }
0170         } else {
0171           if(created) {
0172             parent::push_back(to);
0173             m_owns.push_back(true);
0174           } else { //someone else manage this object.
0175             parent::push_back(to);
0176             m_owns.push_back(false);
0177           }
0178         }
0179       } else {
0180         //a_accept_null for branch::stream m_baskets.
0181         if(a_accept_null) {
0182           parent::push_back(0);
0183           m_owns.push_back(false);
0184         }
0185       }
0186     }
0187 
0188     return a_buffer.check_byte_count(sp,bc,s_store_class());
0189   }
0190 protected:
0191   void _clear() {
0192     typedef typename parent::iterator it_t;
0193     typedef std::vector<bool>::iterator itb_t;
0194     while(!parent::empty()) {
0195       it_t it = parent::begin();
0196       itb_t itb = m_owns.begin();
0197       T* entry  = (*it);
0198       bool own = (*itb);
0199       parent::erase(it);
0200       m_owns.erase(itb);
0201       if(own) delete entry;
0202     }
0203   }
0204 protected:
0205   ifac& m_fac;
0206   std::vector<bool> m_owns;
0207 };
0208 
0209 }}
0210 
0211 #endif