Warning, /include/Geant4/tools/rroot/obj_list 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_list
0005 #define tools_rroot_obj_list
0006
0007 //#include "../vmanip"
0008
0009 #include "iro"
0010
0011 #include "object"
0012 #include "cids"
0013
0014 #include "../forit"
0015 #include "../scast"
0016
0017 namespace tools {
0018 namespace rroot {
0019
0020 class obj_list : public virtual iro, protected std::vector<iro*> { //proteced to avoid using std::vector::clear().
0021 typedef std::vector<iro*> parent;
0022 public:
0023 static const std::string& s_store_class() {
0024 static const std::string s_v("TList");
0025 return s_v;
0026 }
0027 public:
0028 static const std::string& s_class() {
0029 static const std::string s_v("tools::rroot::obj_list");
0030 return s_v;
0031 }
0032 public: //iro
0033 virtual void* cast(const std::string& a_class) const {
0034 if(void* p = cmp_cast<obj_list>(this,a_class)) return p;
0035 return 0;
0036 }
0037 virtual const std::string& s_cls() const {return s_class();}
0038 public:
0039 static cid id_class() {return obj_list_cid();}
0040 virtual void* cast(cid a_class) const {
0041 if(void* p = cmp_cast<obj_list>(this,a_class)) {return p;}
0042 else return 0;
0043 }
0044 //virtual void* cast(cid) const {return obj_list_cid();}
0045 public:
0046 virtual iro* copy() const {return new obj_list(*this);}
0047 virtual bool stream(buffer& a_buffer) {
0048 safe_clear();
0049
0050 short v;
0051 unsigned int _s,_c;
0052 if(!a_buffer.read_version(v,_s,_c)) return false;
0053
0054 //::printf("debug : obj_list::stream : version %d, byte count %d\n",v,c);
0055
0056 {uint32 id,bits;
0057 if(!Object_stream(a_buffer,id,bits)) return false;}
0058
0059 std::string name;
0060 if(!a_buffer.read(name)) return false;
0061 int nobjects;
0062 if(!a_buffer.read(nobjects)) return false;
0063
0064 //::printf("debug : obj_list : name \"%s\", nobject %d\n",
0065 // name.c_str(),nobjects);
0066
0067 ifac::args args;
0068 for (int i=0;i<nobjects;i++) {
0069 //::printf("debug : obj_list : n=%d i=%d ...\n",nobjects,i);
0070
0071 iro* obj;
0072 bool created;
0073 if(!a_buffer.read_object(m_fac,args,obj,created)){
0074 a_buffer.out() << "tools::rroot::obj_list::stream : can't read object." << std::endl;
0075 return false;
0076 }
0077
0078 unsigned char nch;
0079 if(!a_buffer.read(nch)) return false;
0080 if(nch) {
0081 char readOption[256];
0082 if(!a_buffer.read_fast_array(readOption,nch)) return false;
0083 readOption[nch] = 0;
0084 }
0085 if(obj) {
0086 if(created) {
0087 parent::push_back(obj);
0088 m_owns.push_back(true);
0089 } else { //someone else may manage this object.
0090 parent::push_back(obj);
0091 m_owns.push_back(false);
0092 }
0093 }
0094 }
0095
0096 return a_buffer.check_byte_count(_s,_c,s_store_class());
0097 }
0098 public:
0099 obj_list(ifac& a_fac)
0100 :m_fac(a_fac)
0101 {
0102 #ifdef TOOLS_MEM
0103 mem::increment(s_class().c_str());
0104 #endif
0105 }
0106 virtual ~obj_list(){
0107 safe_clear();
0108 #ifdef TOOLS_MEM
0109 mem::decrement(s_class().c_str());
0110 #endif
0111 }
0112 public:
0113 obj_list(const obj_list& a_from)
0114 :iro(a_from)
0115 ,parent()
0116 ,m_fac(a_from.m_fac)
0117 {
0118 #ifdef TOOLS_MEM
0119 mem::increment(s_class().c_str());
0120 #endif
0121 tools_vforcit(iro*,a_from,it) {
0122 parent::push_back((*it)->copy());
0123 m_owns.push_back(true);
0124 }
0125 }
0126 obj_list& operator=(const obj_list& a_from) {
0127 if(&a_from==this) return *this;
0128
0129 safe_clear();
0130
0131 tools_vforcit(iro*,a_from,it) {
0132 parent::push_back((*it)->copy());
0133 m_owns.push_back(true);
0134 }
0135
0136 return *this;
0137 }
0138 public:
0139 parent::const_iterator begin() const {return parent::begin();}
0140 parent::iterator begin() {return parent::begin();}
0141 parent::const_iterator end() const {return parent::end();}
0142 parent::iterator end() {return parent::end();}
0143 parent::size_type size() const {return parent::size();}
0144 bool empty() const {return parent::empty();}
0145 public:
0146 void add_object(iro* a_obj) { //take ownership.
0147 parent::push_back(a_obj);
0148 m_owns.push_back(true);
0149 }
0150
0151 template <class T>
0152 T* get_entry(std::ostream& a_out,size_t a_index) const {
0153 if(a_index>=size()) return 0;
0154 iro* _obj = parent::operator[](a_index);
0155 if(!_obj) return 0;
0156 T* od = id_cast<iro,T>(*_obj);
0157 if(!od) {
0158 a_out << "tools::rroot::obj_list::get_entry :"
0159 << " object of class " << sout(_obj->s_cls()) << " not a " << T::s_class() << std::endl;
0160 return 0;
0161 }
0162 return od;
0163 }
0164
0165 void safe_clear() {
0166 typedef parent::iterator it_t;
0167 typedef std::vector<bool>::iterator itb_t;
0168 while(!parent::empty()) {
0169 it_t it = parent::begin();
0170 itb_t itb = m_owns.begin();
0171 iro* entry = (*it);
0172 bool own = (*itb);
0173 parent::erase(it);
0174 m_owns.erase(itb);
0175 if(own) delete entry;
0176 }
0177 }
0178
0179 void cleanup() {safe_clear();}
0180 protected:
0181 ifac& m_fac;
0182 std::vector<bool> m_owns;
0183 };
0184
0185 }}
0186
0187 #endif