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   }
0052   virtual ~obj_array(){
0053     _clear();
0054   }
0055 public:
0056   obj_array(const obj_array& a_from)
0057   :iro(a_from)
0058   ,parent()
0059   ,m_fac(a_from.m_fac)
0060   {
0061     typedef typename parent::const_iterator it_t;
0062     for(it_t it=a_from.begin();it!=a_from.end();++it) {
0063       if(!(*it)) {
0064         parent::push_back(0);
0065         m_owns.push_back(false);
0066       } else {
0067         iro* _obj = (*it)->copy();
0068         T* obj = safe_cast<iro,T>(*_obj);
0069         if(!obj) {
0070           m_fac.out() << "tools::rroot::obj_array::obj_array :"
0071                       << " tools::cast failed."
0072                       << std::endl;
0073           delete _obj;
0074           parent::push_back(0);
0075           m_owns.push_back(false);
0076         } else {
0077           parent::push_back(obj);
0078           m_owns.push_back(true);
0079         }
0080       }
0081     }
0082   }
0083   obj_array& operator=(const obj_array& a_from){
0084     if(&a_from==this) return *this;
0085 
0086     _clear();
0087 
0088     typedef typename parent::const_iterator it_t;
0089     for(it_t it=a_from.begin();it!=a_from.end();++it) {
0090       if(!(*it)) {
0091         parent::push_back(0);
0092         m_owns.push_back(false);
0093       } else {
0094         iro* _obj = (*it)->copy();
0095         T* obj = safe_cast<iro,T>(*_obj);
0096         if(!obj) {
0097           m_fac.out() << "tools::rroot::obj_array::operator= :"
0098                       << " tools::cast failed."
0099                       << std::endl;
0100           delete _obj;
0101           parent::push_back(0);
0102           m_owns.push_back(false);
0103         } else {
0104           parent::push_back(obj);
0105           m_owns.push_back(true);
0106         }
0107       }
0108     }
0109 
0110     return *this;
0111   }
0112 public:
0113   void cleanup() {_clear();}
0114 public:
0115   bool stream(buffer& a_buffer,const ifac::args& a_args,bool a_accept_null = false) {
0116     _clear();
0117 
0118     short v;
0119     unsigned int sp, bc;
0120     if(!a_buffer.read_version(v,sp,bc)) return false;
0121 
0122    {uint32 id,bits;
0123     if(!Object_stream(a_buffer,id,bits)) return false;}
0124     std::string name;
0125     if(!a_buffer.read(name)) return false;
0126     int nobjects;
0127     if(!a_buffer.read(nobjects)) return false;
0128     int lowerBound;
0129     if(!a_buffer.read(lowerBound)) return false;
0130 
0131     for (int i=0;i<nobjects;i++) {
0132       iro* obj;
0133       bool created;
0134       if(!a_buffer.read_object(m_fac,a_args,obj,created)){
0135         a_buffer.out() << "tools::rroot::obj_array::stream : can't read object"
0136                        << " in obj_array : name " << sout(name)
0137                        << ", nobjects " << nobjects << ", iobject " << i << std::endl;
0138         return false;
0139       }
0140       if(obj) {
0141         T* to = safe_cast<iro,T>(*obj);
0142         if(!to) {
0143           a_buffer.out() << "tools::rroot::obj_array::stream :"
0144                          << " tools::cast failed."
0145                          << " " << obj->s_cls() << " is not a " << T::s_class() << "."
0146                          << std::endl;
0147           if(created) {
0148             if(a_buffer.map_objs()) a_buffer.remove_in_map(obj);
0149             delete obj;
0150           }
0151         } else {
0152           if(created) {
0153             parent::push_back(to);
0154             m_owns.push_back(true);
0155           } else { //someone else manage this object.
0156             parent::push_back(to);
0157             m_owns.push_back(false);
0158           }
0159         }
0160       } else {
0161         //a_accept_null for branch::stream m_baskets.
0162         if(a_accept_null) {
0163           parent::push_back(0);
0164           m_owns.push_back(false);
0165         }
0166       }
0167     }
0168 
0169     return a_buffer.check_byte_count(sp,bc,s_store_class());
0170   }
0171 protected:
0172   void _clear() {
0173     typedef typename parent::iterator it_t;
0174     typedef std::vector<bool>::iterator itb_t;
0175     while(!parent::empty()) {
0176       it_t it = parent::begin();
0177       itb_t itb = m_owns.begin();
0178       T* entry  = (*it);
0179       bool own = (*itb);
0180       parent::erase(it);
0181       m_owns.erase(itb);
0182       if(own) delete entry;
0183     }
0184   }
0185 protected:
0186   ifac& m_fac;
0187   std::vector<bool> m_owns;
0188 };
0189 
0190 }}
0191 
0192 #endif