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