g4tools  5.4.0
iros
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_rroot_iros
5 #define tools_rroot_iros
6 
7 #include "object"
8 #include "../vmanip"
9 #include "../forit"
10 #include "../scast"
11 
12 #include "cids"
13 
14 namespace tools {
15 namespace rroot {
16 
17 class iros : public virtual iro,protected std::vector<iro*> { //proteced to avoid using std::vector::clear().
18  typedef std::vector<iro*> parent;
19 public:
20  static const std::string& s_store_class() {
21  static const std::string s_v("TObjArray");
22  return s_v;
23  }
24 public:
25  static const std::string& s_class() {
26  static const std::string s_v("tools::rroot::iros");
27  return s_v;
28  }
29 public: //iro
30  virtual void* cast(const std::string& a_class) const {
31  if(void* p = cmp_cast<iros>(this,a_class)) return p;
32  return 0;
33  }
34  virtual const std::string& s_cls() const {return s_class();}
35 public:
36  static cid id_class() {return obj_list_cid();}
37  virtual void* cast(cid a_class) const {
38  if(void* p = cmp_cast<iros>(this,a_class)) {return p;}
39  else return 0;
40  }
41 public:
42  virtual iro* copy() const {return new iros(*this);}
43  virtual bool stream(buffer& a_buffer) {
45  bool accept_null = false;
46  return stream(a_buffer,args,accept_null);
47  }
48 public:
49  iros(ifac& a_fac)
50  :m_fac(a_fac)
51  {
52 #ifdef TOOLS_MEM
53  mem::increment(s_class().c_str());
54 #endif
55  }
56  virtual ~iros(){
57  _clear();
58 #ifdef TOOLS_MEM
59  mem::decrement(s_class().c_str());
60 #endif
61  }
62 public:
63  iros(const iros& a_from)
64  :iro(a_from)
65  ,parent()
66  ,m_fac(a_from.m_fac)
67  {
68 #ifdef TOOLS_MEM
69  mem::increment(s_class().c_str());
70 #endif
71  tools_vforcit(iro*,a_from,it) {
72  parent::push_back((*it)->copy());
73  m_owns.push_back(true);
74  }
75  }
76  iros& operator=(const iros& a_from){
77  if(&a_from==this) return *this;
78 
79  _clear();
80 
81  tools_vforcit(iro*,a_from,it) {
82  parent::push_back((*it)->copy());
83  m_owns.push_back(true);
84  }
85 
86  return *this;
87  }
88 public:
89  parent::const_iterator begin() const {return parent::begin();}
90  parent::iterator begin() {return parent::begin();}
91  parent::const_iterator end() const {return parent::end();}
92  parent::iterator end() {return parent::end();}
93  parent::size_type size() const {return parent::size();}
94 public:
95  void cleanup() {_clear();} //warning : clear is a function of the parent std::vector.
96  void dump(std::ostream& a_out) {
97  a_out << " iros : size " << size() << std::endl;
98  tools_vforcit(iro*,*this,it) {
99  a_out << " class " << (*it)->s_cls() << std::endl;
100  }
101  }
102 public:
103  bool stream(buffer& a_buffer,const ifac::args& a_args,bool a_accept_null = false) {
104  _clear();
105 
106  short v;
107  unsigned int s, c;
108  if(!a_buffer.read_version(v,s,c)) return false;
109 
110  //::printf("debug : iros::stream : version %d, byte count %d\n",v,c);
111 
112  {uint32 id,bits;
113  if(!Object_stream(a_buffer,id,bits)) return false;}
114  std::string name;
115  if(!a_buffer.read(name)) return false;
116  int nobjects;
117  if(!a_buffer.read(nobjects)) return false;
118  int lowerBound;
119  if(!a_buffer.read(lowerBound)) return false;
120 
121  //::printf("debug : iros : name \"%s\", nobject %d, lowerBound %d\n",
122  // name.c_str(),nobjects,lowerBound);
123 
124  for (int i=0;i<nobjects;i++) {
125  //::printf("debug : iros : n=%d i=%d ...\n",nobjects,i);
126 
127  iro* obj;
128  bool created;
129  if(!a_buffer.read_object(m_fac,a_args,obj,created)){
130  a_buffer.out() << "tools::rroot::iros::stream : can't read object." << std::endl;
131  return false;
132  }
133  //::printf("debug : iros : n=%d i=%d : ok\n",nobjects,i);
134  if(obj) {
135  if(created) {
136  parent::push_back(obj);
137  m_owns.push_back(true);
138  } else { //someone else manage this object.
139  parent::push_back(obj);
140  m_owns.push_back(false);
141  }
142  } else {
143  //a_accept_null for branch::stream m_baskets.
144  if(a_accept_null) {
145  parent::push_back(0);
146  m_owns.push_back(false);
147  }
148  }
149  }
150 
151  return a_buffer.check_byte_count(s,c,s_store_class());
152  }
153 protected:
154  void _clear() {
155  typedef parent::iterator it_t;
156  typedef std::vector<bool>::iterator itb_t;
157  while(!parent::empty()) {
158  it_t it = parent::begin();
159  itb_t itb = m_owns.begin();
160  iro* entry = (*it);
161  bool own = (*itb);
162  parent::erase(it);
163  m_owns.erase(itb);
164  if(own) delete entry;
165  }
166  }
167 protected:
169  std::vector<bool> m_owns;
170 };
171 
172 /*
173 inline bool dummy_TObjArray_pointer_stream(buffer& a_buffer,ifac& a_fac,bool a_owner,bool a_warn) {
174  iros oa(a_fac,a_owner,a_warn);
175  iros oa(a_fac,true,true);
176  ifac::args args;
177  return oa.stream(a_buffer,args);
178 }
179 */
180 
181 }}
182 
183 #endif
tools::rroot::iros::end
parent::const_iterator end() const
Definition: iros:91
tools::rroot::iros::stream
bool stream(buffer &a_buffer, const ifac::args &a_args, bool a_accept_null=false)
Definition: iros:103
tools::rroot::Object_stream
bool Object_stream(buffer &a_buffer, uint32 &a_id, uint32 &a_bits)
Definition: object:12
tools::rroot::iros::cast
virtual void * cast(const std::string &a_class) const
Definition: iros:30
tools::rroot::buffer::read_object
bool read_object(ifac &a_fac, const ifac::args &a_args, iro *&a_obj, bool &a_created)
Definition: buffer:283
tools::rroot::iros::end
parent::iterator end()
Definition: iros:92
tools::rroot::buffer::check_byte_count
bool check_byte_count(uint32 a_start_pos, uint32 a_byte_count, const std::string &a_store_cls)
Definition: buffer:568
tools::rroot::iros::size
parent::size_type size() const
Definition: iros:93
tools::rroot::iros::s_cls
virtual const std::string & s_cls() const
Definition: iros:34
tools::waxml::begin
void begin(std::ostream &a_writer)
Definition: begend:15
tools::rroot::iros::m_fac
ifac & m_fac
Definition: iros:168
tools::rroot::iros::operator=
iros & operator=(const iros &a_from)
Definition: iros:76
tools::rroot::iros::m_owns
std::vector< bool > m_owns
Definition: iros:169
tools::rroot::iros::dump
void dump(std::ostream &a_out)
Definition: iros:96
tools::rroot::iros::id_class
static cid id_class()
Definition: iros:36
tools::rroot::iros::s_store_class
static const std::string & s_store_class()
Definition: iros:20
tools::rroot::rbuf::out
std::ostream & out() const
Definition: rbuf:125
tools::rroot::buffer::read_version
bool read_version(short &a_version)
Definition: buffer:503
tools::args
Definition: args:24
tools::file::size
bool size(const std::string &a_file, long &a_size)
Definition: fsize:13
tools::rroot::iros::cleanup
void cleanup()
Definition: iros:95
tools::rroot::iros::iros
iros(ifac &a_fac)
Definition: iros:49
tools::rroot::iros::begin
parent::iterator begin()
Definition: iros:90
tools::rroot::iros
Definition: iros:17
tools::rroot::iros::s_class
static const std::string & s_class()
Definition: iros:25
tools::rroot::iros::_clear
void _clear()
Definition: iros:154
tools::rroot::ifac
Definition: ifac:19
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::rroot::iros::~iros
virtual ~iros()
Definition: iros:56
tools::waxml::end
void end(std::ostream &a_writer)
Definition: begend:31
tools::rroot::buffer
Definition: buffer:43
tools::rroot::iros::begin
parent::const_iterator begin() const
Definition: iros:89
tools::rroot::iros::cast
virtual void * cast(cid a_class) const
Definition: iros:37
tools::rroot::rbuf::read
bool read(unsigned char &a_x)
Definition: rbuf:146
tools::rroot::iro
Definition: iro:19
tools::rroot::ifac::args
std::map< char, void * > args
Definition: ifac:21
cids
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::rroot::iros::copy
virtual iro * copy() const
Definition: iros:42
tools::rroot::iros::stream
virtual bool stream(buffer &a_buffer)
Definition: iros:43
tools::rroot::obj_list_cid
cid obj_list_cid()
Definition: cids:17
object
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::rroot::iros::iros
iros(const iros &a_from)
Definition: iros:63
tools::cid
unsigned short cid
Definition: cid:9