inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_rroot_named 00005 #define inlib_rroot_named 00006 00007 #include "buffer" 00008 #include "../vdata" 00009 #include "../vmanip" 00010 #include "../scast" //for ObjArray 00011 00012 namespace inlib { 00013 namespace rroot { 00014 00015 inline bool Object_stream(buffer& a_buffer,uint32& a_id,uint32& a_bits) { 00016 short v; 00017 if(!a_buffer.read_version(v)) return false; 00018 if(!a_buffer.read(a_id)) return false; 00019 if(!a_buffer.read(a_bits)) return false; 00020 return true; 00021 } 00022 00023 inline bool Named_stream(buffer& a_buffer, 00024 std::string& a_name, 00025 std::string& a_title) { 00026 short v; 00027 unsigned int s, c; 00028 if(!a_buffer.read_version(v,s,c)) return false; 00029 {uint32 id,bits; 00030 if(!Object_stream(a_buffer,id,bits)) return false;} 00031 if(!a_buffer.read(a_name)) return false; 00032 if(!a_buffer.read(a_title)) return false; 00033 if(!a_buffer.check_byte_count(s,c,"TNamed")) return false; 00034 return true; 00035 } 00036 00037 inline bool AttLine_stream(buffer& a_buffer){ 00038 short fLineColor; 00039 short fLineStyle; 00040 short fLineWidth; 00041 short v; 00042 unsigned int s, c; 00043 if(!a_buffer.read_version(v,s,c)) return false; 00044 if(!a_buffer.read(fLineColor)) return false; 00045 if(!a_buffer.read(fLineStyle)) return false; 00046 if(!a_buffer.read(fLineWidth)) return false; 00047 if(!a_buffer.check_byte_count(s,c,"TAttLine")) return false; 00048 return true; 00049 } 00050 00051 inline bool AttFill_stream(buffer& a_buffer){ 00052 short fFillColor; 00053 short fFillStyle; 00054 short v; 00055 unsigned int s, c; 00056 if(!a_buffer.read_version(v,s,c)) return false; 00057 if(!a_buffer.read(fFillColor)) return false; 00058 if(!a_buffer.read(fFillStyle)) return false; 00059 if(!a_buffer.check_byte_count(s,c,"TAttFill")) return false; 00060 return true; 00061 } 00062 00063 inline bool AttMarker_stream(buffer& a_buffer) { 00064 short fMarkerColor; 00065 short fMarkerStyle; 00066 float fMarkerWidth; 00067 short v; 00068 unsigned int s, c; 00069 if(!a_buffer.read_version(v,s,c)) return false; 00070 if(!a_buffer.read(fMarkerColor)) return false; 00071 if(!a_buffer.read(fMarkerStyle)) return false; 00072 if(!a_buffer.read(fMarkerWidth)) return false; 00073 if(!a_buffer.check_byte_count(s,c,"TAttMarker")) return false; 00074 return true; 00075 } 00076 00077 template <class T> 00078 inline bool Array_stream(buffer& a_buffer,std::vector<T>& a_v) { 00079 a_v.clear(); 00080 int sz; 00081 if(!a_buffer.read(sz)) return false; 00082 //check sz is not crazy : 00083 if(!a_buffer.check_eob(sz)) return false; 00084 a_v.resize(sz); 00085 if(!a_buffer.read_fast_array<T>(vec_data(a_v),sz)) return false; 00086 return true; 00087 } 00088 00089 template <class T> 00090 class ObjArray : public std::vector<T*> { 00091 public: 00092 ObjArray(bool a_owner):m_owner(a_owner){} 00093 virtual ~ObjArray(){_clear();} 00094 protected: 00095 ObjArray(const ObjArray&): std::vector<T*>(){} 00096 ObjArray& operator=(const ObjArray&){return *this;} 00097 public: 00098 void cleanup() {_clear();} 00099 public: 00100 bool stream(buffer& a_buffer,ifac& a_fac,const ifac::args& a_args,bool a_accept_null = false) { 00101 _clear(); 00102 00103 short v; 00104 unsigned int s, c; 00105 if(!a_buffer.read_version(v,s,c)) return false; 00106 00107 //::printf("debug : ObjArray::stream : version %d count %d\n",v,c); 00108 00109 {uint32 id,bits; 00110 if(!Object_stream(a_buffer,id,bits)) return false;} 00111 std::string name; 00112 if(!a_buffer.read(name)) return false; 00113 int nobjects; 00114 if(!a_buffer.read(nobjects)) return false; 00115 int lowerBound; 00116 if(!a_buffer.read(lowerBound)) return false; 00117 00118 //::printf("debug : ObjArray : nobject \"%s\" %d %d\n", 00119 // name.c_str(),nobjects,lowerBound); 00120 00121 for (int i=0;i<nobjects;i++) { 00122 //::printf("debug : ObjArray : n=%d i=%d ...\n",nobjects,i); 00123 iro* obj; 00124 if(!a_buffer.read_object(a_fac,a_args,obj)){ 00125 a_buffer.out() << "inlib::rroot::ObjArray::stream :" 00126 << " can't read object." 00127 << std::endl; 00128 return false; 00129 } 00130 //::printf("debug : ObjArray : n=%d i=%d : ok\n",nobjects,i); 00131 if(obj) { 00132 T* to = inlib::cast<iro,T>(*obj); 00133 if(!to) { 00134 a_buffer.out() << "inlib::rroot::ObjArray::stream :" 00135 << " inlib::cast failed." 00136 << std::endl; 00137 } else { 00138 push_back(to); 00139 } 00140 } else { 00141 //a_accept_null for branch::stream m_baskets. 00142 if(a_accept_null) this->push_back(0); 00143 } 00144 } 00145 00146 return a_buffer.check_byte_count(s,c,"TObjArray"); 00147 } 00148 protected: 00149 void _clear() { 00150 if(m_owner) { 00151 inlib::clear<T>(*this); 00152 } else { 00153 std::vector<T*>::clear(); 00154 } 00155 } 00156 protected: 00157 bool m_owner; 00158 }; 00159 00160 }} 00161 00162 #endif