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