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 }
0053 virtual ~iros(){
0054 _clear();
0055 }
0056 public:
0057 iros(const iros& a_from)
0058 :iro(a_from)
0059 ,parent()
0060 ,m_fac(a_from.m_fac)
0061 {
0062 tools_vforcit(iro*,a_from,it) {
0063 parent::push_back((*it)->copy());
0064 m_owns.push_back(true);
0065 }
0066 }
0067 iros& operator=(const iros& a_from){
0068 if(&a_from==this) return *this;
0069
0070 _clear();
0071
0072 tools_vforcit(iro*,a_from,it) {
0073 parent::push_back((*it)->copy());
0074 m_owns.push_back(true);
0075 }
0076
0077 return *this;
0078 }
0079 public:
0080 parent::const_iterator begin() const {return parent::begin();}
0081 parent::iterator begin() {return parent::begin();}
0082 parent::const_iterator end() const {return parent::end();}
0083 parent::iterator end() {return parent::end();}
0084 parent::size_type size() const {return parent::size();}
0085 public:
0086 void cleanup() {_clear();} //warning : clear is a function of the parent std::vector.
0087 void dump(std::ostream& a_out) {
0088 a_out << " iros : size " << size() << std::endl;
0089 tools_vforcit(iro*,*this,it) {
0090 a_out << " class " << (*it)->s_cls() << std::endl;
0091 }
0092 }
0093 public:
0094 bool stream(buffer& a_buffer,const ifac::args& a_args,bool a_accept_null = false) {
0095 _clear();
0096
0097 short v;
0098 unsigned int s, c;
0099 if(!a_buffer.read_version(v,s,c)) return false;
0100
0101 {uint32 id,bits;
0102 if(!Object_stream(a_buffer,id,bits)) return false;}
0103 std::string name;
0104 if(!a_buffer.read(name)) return false;
0105 int nobjects;
0106 if(!a_buffer.read(nobjects)) return false;
0107 int lowerBound;
0108 if(!a_buffer.read(lowerBound)) return false;
0109
0110 for (int i=0;i<nobjects;i++) {
0111 iro* obj;
0112 bool created;
0113 if(!a_buffer.read_object(m_fac,a_args,obj,created)){
0114 a_buffer.out() << "tools::rroot::iros::stream : can't read object." << std::endl;
0115 return false;
0116 }
0117 if(obj) {
0118 if(created) {
0119 parent::push_back(obj);
0120 m_owns.push_back(true);
0121 } else { //someone else manage this object.
0122 parent::push_back(obj);
0123 m_owns.push_back(false);
0124 }
0125 } else {
0126 //a_accept_null for branch::stream m_baskets.
0127 if(a_accept_null) {
0128 parent::push_back(0);
0129 m_owns.push_back(false);
0130 }
0131 }
0132 }
0133
0134 return a_buffer.check_byte_count(s,c,s_store_class());
0135 }
0136 protected:
0137 void _clear() {
0138 typedef parent::iterator it_t;
0139 typedef std::vector<bool>::iterator itb_t;
0140 while(!parent::empty()) {
0141 it_t it = parent::begin();
0142 itb_t itb = m_owns.begin();
0143 iro* entry = (*it);
0144 bool own = (*itb);
0145 parent::erase(it);
0146 m_owns.erase(itb);
0147 if(own) delete entry;
0148 }
0149 }
0150 protected:
0151 ifac& m_fac;
0152 std::vector<bool> m_owns;
0153 };
0154
0155 }}
0156
0157 #endif