Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef tools_wcsv_ntuple 4 #ifndef tools_wcsv_ntuple 5 #define tools_wcsv_ntuple 5 #define tools_wcsv_ntuple 6 6 7 // A simple ntuple class to write at the csv f 7 // A simple ntuple class to write at the csv format. 8 // (csv = comma separated value). 8 // (csv = comma separated value). 9 // Each add_row() write a row at the csv forma 9 // Each add_row() write a row at the csv format. 10 10 11 #include "vfind" 11 #include "vfind" 12 #include "vmanip" 12 #include "vmanip" 13 #include <ostream> 13 #include <ostream> 14 14 15 #include "scast" 15 #include "scast" 16 #include "ntuple_booking" 16 #include "ntuple_booking" 17 #include "sout" 17 #include "sout" 18 //#include "srep" 18 //#include "srep" 19 19 20 namespace tools { 20 namespace tools { 21 namespace wcsv { 21 namespace wcsv { 22 22 23 class ntuple { 23 class ntuple { 24 public: 24 public: 25 static const std::string& s_class() { 25 static const std::string& s_class() { 26 static const std::string s_v("tools::wcsv: 26 static const std::string s_v("tools::wcsv::ntuple"); 27 return s_v; 27 return s_v; 28 } 28 } 29 public: 29 public: 30 class icol { 30 class icol { 31 public: 31 public: 32 virtual ~icol(){} 32 virtual ~icol(){} 33 public: 33 public: 34 virtual void* cast(cid) const = 0; 34 virtual void* cast(cid) const = 0; 35 virtual cid id_cls() const = 0; 35 virtual cid id_cls() const = 0; 36 public: 36 public: 37 virtual void add() = 0; 37 virtual void add() = 0; 38 virtual const std::string& name() const = 38 virtual const std::string& name() const = 0; 39 }; 39 }; 40 40 41 public: 41 public: 42 template <class T> 42 template <class T> 43 class column_ref : public virtual icol { 43 class column_ref : public virtual icol { 44 public: 44 public: 45 static cid id_class() { 45 static cid id_class() { 46 static const T s_v = T(); //do that for 46 static const T s_v = T(); //do that for T = std::string. 47 return _cid(s_v)+10000; 47 return _cid(s_v)+10000; 48 } 48 } 49 virtual void* cast(cid a_class) const { 49 virtual void* cast(cid a_class) const { 50 if(void* p = cmp_cast<column_ref>(this,a 50 if(void* p = cmp_cast<column_ref>(this,a_class)) {return p;} 51 else return 0; 51 else return 0; 52 } 52 } 53 virtual cid id_cls() const {return id_clas 53 virtual cid id_cls() const {return id_class();} 54 public: //icol 54 public: //icol 55 virtual void add() {m_writer << m_ref;} 55 virtual void add() {m_writer << m_ref;} 56 virtual const std::string& name() const {r 56 virtual const std::string& name() const {return m_name;} 57 public: 57 public: 58 column_ref(std::ostream& a_writer,const st 58 column_ref(std::ostream& a_writer,const std::string& a_name,const T& a_ref) 59 :m_writer(a_writer) 59 :m_writer(a_writer) 60 ,m_name(a_name) 60 ,m_name(a_name) 61 ,m_ref(a_ref) 61 ,m_ref(a_ref) 62 {} 62 {} 63 virtual ~column_ref(){} 63 virtual ~column_ref(){} 64 protected: 64 protected: 65 column_ref(const column_ref& a_from) 65 column_ref(const column_ref& a_from) 66 :icol(a_from) 66 :icol(a_from) 67 ,m_writer(a_from.m_writer) 67 ,m_writer(a_from.m_writer) 68 ,m_name(a_from.m_name) 68 ,m_name(a_from.m_name) 69 ,m_ref(a_from.m_ref) 69 ,m_ref(a_from.m_ref) 70 {} 70 {} 71 column_ref& operator=(const column_ref& a_ 71 column_ref& operator=(const column_ref& a_from){ 72 m_name = a_from.m_name; 72 m_name = a_from.m_name; 73 return *this; 73 return *this; 74 } 74 } 75 protected: 75 protected: 76 std::ostream& m_writer; 76 std::ostream& m_writer; 77 std::string m_name; 77 std::string m_name; 78 const T& m_ref; 78 const T& m_ref; 79 }; 79 }; 80 80 81 template <class T> 81 template <class T> 82 class column : public column_ref<T> { 82 class column : public column_ref<T> { 83 typedef column_ref<T> parent; 83 typedef column_ref<T> parent; 84 public: 84 public: 85 static cid id_class() { 85 static cid id_class() { 86 static const T s_v = T(); //do that for 86 static const T s_v = T(); //do that for T = std::string. 87 return _cid(s_v); 87 return _cid(s_v); 88 } 88 } 89 virtual void* cast(cid a_class) const { 89 virtual void* cast(cid a_class) const { 90 if(void* p = cmp_cast<column>(this,a_cla 90 if(void* p = cmp_cast<column>(this,a_class)) {return p;} 91 else return 0; 91 else return 0; 92 } 92 } 93 virtual cid id_cls() const {return id_clas 93 virtual cid id_cls() const {return id_class();} 94 public: //icol 94 public: //icol 95 virtual void add() {parent::add();m_tmp = 95 virtual void add() {parent::add();m_tmp = m_def;} 96 public: 96 public: 97 column(std::ostream& a_writer,const std::s 97 column(std::ostream& a_writer,const std::string& a_name,const T& a_def) 98 :parent(a_writer,a_name,m_tmp) 98 :parent(a_writer,a_name,m_tmp) 99 ,m_def(a_def) 99 ,m_def(a_def) 100 ,m_tmp(a_def) 100 ,m_tmp(a_def) 101 {} 101 {} 102 virtual ~column(){} 102 virtual ~column(){} 103 protected: 103 protected: 104 column(const column& a_from) 104 column(const column& a_from) 105 :icol(a_from) 105 :icol(a_from) 106 ,parent(a_from) 106 ,parent(a_from) 107 ,m_def(a_from.m_def) 107 ,m_def(a_from.m_def) 108 ,m_tmp(a_from.m_tmp) 108 ,m_tmp(a_from.m_tmp) 109 {} 109 {} 110 column& operator=(const column& a_from){ 110 column& operator=(const column& a_from){ 111 parent::operator=(a_from); 111 parent::operator=(a_from); 112 m_def = a_from.m_def; 112 m_def = a_from.m_def; 113 m_tmp = a_from.m_tmp; 113 m_tmp = a_from.m_tmp; 114 return *this; 114 return *this; 115 } 115 } 116 public: 116 public: 117 bool fill(const T& a_value) {m_tmp = a_val 117 bool fill(const T& a_value) {m_tmp = a_value;return true;} 118 protected: 118 protected: 119 T m_def; 119 T m_def; 120 T m_tmp; 120 T m_tmp; 121 }; 121 }; 122 122 123 /* 123 /* 124 template <class T> 124 template <class T> 125 static void escape(T&,const std::string&){} 125 static void escape(T&,const std::string&){} 126 static void escape(std::string& a_s,const st 126 static void escape(std::string& a_s,const std::string& a_sep){ 127 replace(a_s,a_sep,"\\"+a_sep); //if changi 127 replace(a_s,a_sep,"\\"+a_sep); //if changing here, change rcsv_ntuple/_vec_read() too. 128 } 128 } 129 */ 129 */ 130 130 131 template <class T> 131 template <class T> 132 class std_vector_column : public virtual ico 132 class std_vector_column : public virtual icol { 133 public: 133 public: 134 static cid id_class() {return _cid_std_vec 134 static cid id_class() {return _cid_std_vector<T>();} 135 virtual void* cast(cid a_class) const { 135 virtual void* cast(cid a_class) const { 136 if(void* p = cmp_cast<std_vector_column> 136 if(void* p = cmp_cast<std_vector_column>(this,a_class)) {return p;} 137 else return 0; 137 else return 0; 138 } 138 } 139 virtual cid id_cls() const {return id_clas 139 virtual cid id_cls() const {return id_class();} 140 public: //icol 140 public: //icol 141 virtual void add() { 141 virtual void add() { 142 if(m_ref.empty()) { 142 if(m_ref.empty()) { 143 //m_writer << "none"; 143 //m_writer << "none"; 144 } else { 144 } else { 145 //std::string sep;sep+=m_vec_sep; 145 //std::string sep;sep+=m_vec_sep; 146 //T value; 146 //T value; 147 typedef typename std::vector<T>::const 147 typedef typename std::vector<T>::const_iterator it_t; 148 for(it_t it=m_ref.begin();it!=m_ref.en 148 for(it_t it=m_ref.begin();it!=m_ref.end();++it) { 149 if(it!=m_ref.begin()) m_writer << m_ 149 if(it!=m_ref.begin()) m_writer << m_vec_sep; 150 m_writer << *it; 150 m_writer << *it; 151 //value = *it;escape(value,sep); 151 //value = *it;escape(value,sep); 152 //m_writer << value; 152 //m_writer << value; 153 } 153 } 154 } 154 } 155 } 155 } 156 virtual const std::string& name() const {r 156 virtual const std::string& name() const {return m_name;} 157 public: 157 public: 158 std_vector_column(std::ostream& a_writer,c 158 std_vector_column(std::ostream& a_writer,const std::string& a_name,const std::vector<T>& a_ref,char a_vec_sep) 159 :m_writer(a_writer) 159 :m_writer(a_writer) 160 ,m_name(a_name) 160 ,m_name(a_name) 161 ,m_ref(a_ref) 161 ,m_ref(a_ref) 162 ,m_vec_sep(a_vec_sep) 162 ,m_vec_sep(a_vec_sep) 163 {} 163 {} 164 virtual ~std_vector_column(){} 164 virtual ~std_vector_column(){} 165 protected: 165 protected: 166 std_vector_column(const std_vector_column& 166 std_vector_column(const std_vector_column& a_from) 167 :icol(a_from) 167 :icol(a_from) 168 ,m_writer(a_from.m_writer) 168 ,m_writer(a_from.m_writer) 169 ,m_name(a_from.m_name) 169 ,m_name(a_from.m_name) 170 ,m_ref(a_from.m_ref) 170 ,m_ref(a_from.m_ref) 171 ,m_vec_sep(a_from.m_vec_sep) 171 ,m_vec_sep(a_from.m_vec_sep) 172 {} 172 {} 173 std_vector_column& operator=(const std_vec 173 std_vector_column& operator=(const std_vector_column& a_from){ 174 m_name = a_from.m_name; 174 m_name = a_from.m_name; 175 m_vec_sep = a_from.m_vec_sep; 175 m_vec_sep = a_from.m_vec_sep; 176 return *this; 176 return *this; 177 } 177 } 178 protected: 178 protected: 179 std::ostream& m_writer; 179 std::ostream& m_writer; 180 std::string m_name; 180 std::string m_name; 181 const std::vector<T>& m_ref; 181 const std::vector<T>& m_ref; 182 char m_vec_sep; 182 char m_vec_sep; 183 }; 183 }; 184 184 185 public: 185 public: 186 ntuple(std::ostream& a_writer,char a_sep = ' 186 ntuple(std::ostream& a_writer,char a_sep = ',',char a_vec_sep = ';') 187 :m_writer(a_writer) 187 :m_writer(a_writer) 188 ,m_sep(a_sep) 188 ,m_sep(a_sep) 189 ,m_vec_sep(a_vec_sep) 189 ,m_vec_sep(a_vec_sep) 190 {} 190 {} 191 191 192 ntuple(std::ostream& a_writer, 192 ntuple(std::ostream& a_writer, 193 std::ostream& a_out, //for errors. 193 std::ostream& a_out, //for errors. 194 const ntuple_booking& a_bkg, 194 const ntuple_booking& a_bkg, 195 char a_sep = ',',char a_vec_sep = ';' 195 char a_sep = ',',char a_vec_sep = ';') 196 :m_writer(a_writer) 196 :m_writer(a_writer) 197 ,m_sep(a_sep) 197 ,m_sep(a_sep) 198 ,m_vec_sep(a_vec_sep) 198 ,m_vec_sep(a_vec_sep) 199 ,m_title(a_bkg.title()) 199 ,m_title(a_bkg.title()) 200 { 200 { 201 const std::vector<column_booking>& cols = 201 const std::vector<column_booking>& cols = a_bkg.columns(); 202 std::vector<column_booking>::const_iterato 202 std::vector<column_booking>::const_iterator it; 203 for(it=cols.begin();it!=cols.end();++it){ 203 for(it=cols.begin();it!=cols.end();++it){ 204 204 205 #define TOOLS_WCSV_NTUPLE_CREATE_COL(a__type) 205 #define TOOLS_WCSV_NTUPLE_CREATE_COL(a__type) \ 206 if((*it).cls_id()==_cid(a__type())) {\ 206 if((*it).cls_id()==_cid(a__type())) {\ 207 a__type* user = (a__type*)(*it).user_o 207 a__type* user = (a__type*)(*it).user_obj();\ 208 if(user) {\ 208 if(user) {\ 209 if(!create_column_ref<a__type>((*it) 209 if(!create_column_ref<a__type>((*it).name(),*user)) {\ 210 a_out << "tools::wcsv_ntuple::wcsv_ntupl 210 a_out << "tools::wcsv_ntuple::wcsv_ntuple : create_column_ref(" << (*it).name() << ") failed." << std::endl;\ 211 safe_clear<icol>(m_cols);\ 211 safe_clear<icol>(m_cols);\ 212 return;\ 212 return;\ 213 }\ 213 }\ 214 } else {\ 214 } else {\ 215 if(!create_column<a__type>((*it).nam 215 if(!create_column<a__type>((*it).name())) {\ 216 a_out << "tools::wcsv_ntuple::wcsv_ntupl 216 a_out << "tools::wcsv_ntuple::wcsv_ntuple : create_column(" << (*it).name() << ") failed." << std::endl;\ 217 safe_clear<icol>(m_cols);\ 217 safe_clear<icol>(m_cols);\ 218 return;\ 218 return;\ 219 }\ 219 }\ 220 }\ 220 }\ 221 } 221 } 222 222 223 #define TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(a__ty 223 #define TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(a__type) \ 224 if((*it).cls_id()==_cid_std_vector<a__ty 224 if((*it).cls_id()==_cid_std_vector<a__type>()) {\ 225 std::vector<a__type>* vec = (std::vect 225 std::vector<a__type>* vec = (std::vector<a__type>*)(*it).user_obj();\ 226 if(vec) {\ 226 if(vec) {\ 227 if(!create_column<a__type>((*it).nam 227 if(!create_column<a__type>((*it).name(),*vec)) {\ 228 a_out << "tools::wcsv_ntuple::wcsv_ntupl 228 a_out << "tools::wcsv_ntuple::wcsv_ntuple : create_column(" << (*it).name() << ") failed." << std::endl;\ 229 safe_clear<icol>(m_cols);\ 229 safe_clear<icol>(m_cols);\ 230 return;\ 230 return;\ 231 }\ 231 }\ 232 } else {\ 232 } else {\ 233 a_out << "tools::wcsv_ntuple :"\ 233 a_out << "tools::wcsv_ntuple :"\ 234 << " for std::vector column " 234 << " for std::vector column " << sout((*it).name())\ 235 << ", the user vector pointer 235 << ", the user vector pointer is null."\ 236 << std::endl;\ 236 << std::endl;\ 237 safe_clear<icol>(m_cols);\ 237 safe_clear<icol>(m_cols);\ 238 return;\ 238 return;\ 239 }\ 239 }\ 240 } 240 } 241 241 242 TOOLS_WCSV_NTUPLE_CREATE_COL(char) 242 TOOLS_WCSV_NTUPLE_CREATE_COL(char) 243 else TOOLS_WCSV_NTUPLE_CREATE_COL(short) 243 else TOOLS_WCSV_NTUPLE_CREATE_COL(short) 244 else TOOLS_WCSV_NTUPLE_CREATE_COL(int) 244 else TOOLS_WCSV_NTUPLE_CREATE_COL(int) 245 else TOOLS_WCSV_NTUPLE_CREATE_COL(int64) 245 else TOOLS_WCSV_NTUPLE_CREATE_COL(int64) 246 246 247 else TOOLS_WCSV_NTUPLE_CREATE_COL(float) 247 else TOOLS_WCSV_NTUPLE_CREATE_COL(float) 248 else TOOLS_WCSV_NTUPLE_CREATE_COL(double 248 else TOOLS_WCSV_NTUPLE_CREATE_COL(double) 249 249 250 else TOOLS_WCSV_NTUPLE_CREATE_COL(byte) 250 else TOOLS_WCSV_NTUPLE_CREATE_COL(byte) 251 else TOOLS_WCSV_NTUPLE_CREATE_COL(ushort 251 else TOOLS_WCSV_NTUPLE_CREATE_COL(ushort) 252 else TOOLS_WCSV_NTUPLE_CREATE_COL(uint32 252 else TOOLS_WCSV_NTUPLE_CREATE_COL(uint32) 253 else TOOLS_WCSV_NTUPLE_CREATE_COL(uint64 253 else TOOLS_WCSV_NTUPLE_CREATE_COL(uint64) 254 254 255 else TOOLS_WCSV_NTUPLE_CREATE_COL(bool) 255 else TOOLS_WCSV_NTUPLE_CREATE_COL(bool) 256 else TOOLS_WCSV_NTUPLE_CREATE_COL(std::s 256 else TOOLS_WCSV_NTUPLE_CREATE_COL(std::string) 257 257 258 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(ch 258 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(char) 259 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(sh 259 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(short) 260 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(in 260 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(int) 261 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(in 261 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(int64) 262 262 263 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(fl 263 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(float) 264 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(do 264 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(double) 265 265 266 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(uc 266 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(uchar) 267 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(us 267 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(ushort) 268 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(ui 268 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(uint32) 269 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(ui 269 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(uint64) 270 270 271 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(st 271 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(std::string) 272 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(bo 272 else TOOLS_WCSV_NTUPLE_CREATE_VEC_COL(bool) 273 273 274 #undef TOOLS_WCSV_NTUPLE_CREATE_VEC_COL 274 #undef TOOLS_WCSV_NTUPLE_CREATE_VEC_COL 275 #undef TOOLS_WCSV_NTUPLE_CREATE_COL 275 #undef TOOLS_WCSV_NTUPLE_CREATE_COL 276 276 277 else { 277 else { 278 a_out << "tools::wcsv::ntuple :" 278 a_out << "tools::wcsv::ntuple :" 279 << " for column " << sout((*it). 279 << " for column " << sout((*it).name()) 280 << ", type with cid " << (*it).c 280 << ", type with cid " << (*it).cls_id() << " not yet handled." 281 << std::endl; 281 << std::endl; 282 //throw 282 //throw 283 safe_clear<icol>(m_cols); 283 safe_clear<icol>(m_cols); 284 return; 284 return; 285 } 285 } 286 } 286 } 287 } 287 } 288 288 289 virtual ~ntuple() { 289 virtual ~ntuple() { 290 safe_clear<icol>(m_cols); 290 safe_clear<icol>(m_cols); 291 } 291 } 292 protected: 292 protected: 293 ntuple(const ntuple& a_from) 293 ntuple(const ntuple& a_from) 294 :m_writer(a_from.m_writer) 294 :m_writer(a_from.m_writer) 295 ,m_sep(a_from.m_sep) 295 ,m_sep(a_from.m_sep) 296 ,m_vec_sep(a_from.m_vec_sep) 296 ,m_vec_sep(a_from.m_vec_sep) 297 ,m_title(a_from.m_title) 297 ,m_title(a_from.m_title) 298 {} 298 {} 299 ntuple& operator=(const ntuple& a_from){ 299 ntuple& operator=(const ntuple& a_from){ 300 m_sep = a_from.m_sep; 300 m_sep = a_from.m_sep; 301 m_vec_sep = a_from.m_vec_sep; 301 m_vec_sep = a_from.m_vec_sep; 302 m_title = a_from.m_title; 302 m_title = a_from.m_title; 303 return *this; 303 return *this; 304 } 304 } 305 public: 305 public: 306 void write_hippo_header() { 306 void write_hippo_header() { 307 m_writer << m_title << std::endl; 307 m_writer << m_title << std::endl; 308 std::vector<icol*>::const_iterator it; 308 std::vector<icol*>::const_iterator it; 309 for(it=m_cols.begin();it!=m_cols.end();++i 309 for(it=m_cols.begin();it!=m_cols.end();++it){ 310 if(it!=m_cols.begin()) m_writer << '\t'; 310 if(it!=m_cols.begin()) m_writer << '\t'; 311 m_writer << (*it)->name(); 311 m_writer << (*it)->name(); 312 } 312 } 313 m_writer << std::endl; 313 m_writer << std::endl; 314 } 314 } 315 315 316 bool write_commented_header(std::ostream& a_ 316 bool write_commented_header(std::ostream& a_out) { 317 // commented header similar to the histo c 317 // commented header similar to the histo case. 318 m_writer << "#class " << s_class() << std: 318 m_writer << "#class " << s_class() << std::endl; 319 m_writer << "#title " << m_title << std::e 319 m_writer << "#title " << m_title << std::endl; 320 m_writer << "#separator " << (unsigned int 320 m_writer << "#separator " << (unsigned int)m_sep << std::endl; 321 m_writer << "#vector_separator " << (unsig 321 m_writer << "#vector_separator " << (unsigned int)m_vec_sep << std::endl; 322 bool status = true; 322 bool status = true; 323 {for(unsigned int count=0;count<m_cols.size 323 {for(unsigned int count=0;count<m_cols.size();count++) { 324 icol* _col = m_cols[count]; 324 icol* _col = m_cols[count]; 325 std::string sid; 325 std::string sid; 326 if(!cid2s(_col->id_cls(),sid)) { 326 if(!cid2s(_col->id_cls(),sid)) { 327 a_out << "tools::wcsv::ntuple::write_ 327 a_out << "tools::wcsv::ntuple::write_commented_header :" 328 << " unknown column type id " < 328 << " unknown column type id " << _col->id_cls() << std::endl; 329 status = false; //but we continue. 329 status = false; //but we continue. 330 } else { 330 } else { 331 m_writer << "#column " << sid << " " 331 m_writer << "#column " << sid << " " << _col->name() << std::endl; 332 } 332 } 333 }} 333 }} 334 return status; 334 return status; 335 } 335 } 336 336 337 template <class T> 337 template <class T> 338 column_ref<T>* create_column_ref(const std:: 338 column_ref<T>* create_column_ref(const std::string& a_name,const T& a_ref) { 339 if(find_named<icol>(m_cols,a_name)) return 339 if(find_named<icol>(m_cols,a_name)) return 0; 340 column_ref<T>* col = new column_ref<T>(m_w 340 column_ref<T>* col = new column_ref<T>(m_writer,a_name,a_ref); 341 if(!col) return 0; 341 if(!col) return 0; 342 m_cols.push_back(col); 342 m_cols.push_back(col); 343 return col; 343 return col; 344 } 344 } 345 345 346 template <class T> 346 template <class T> 347 column<T>* create_column(const std::string& 347 column<T>* create_column(const std::string& a_name,const T& a_def = T()) { 348 if(find_named<icol>(m_cols,a_name)) return 348 if(find_named<icol>(m_cols,a_name)) return 0; 349 column<T>* col = new column<T>(m_writer,a_ 349 column<T>* col = new column<T>(m_writer,a_name,a_def); 350 if(!col) return 0; 350 if(!col) return 0; 351 m_cols.push_back(col); 351 m_cols.push_back(col); 352 return col; 352 return col; 353 } 353 } 354 354 355 template <class T> 355 template <class T> 356 std_vector_column<T>* create_column(const st 356 std_vector_column<T>* create_column(const std::string& a_name,const std::vector<T>& a_ref) { 357 //NOTE : to optimize, we do not handle a d 357 //NOTE : to optimize, we do not handle a default std::vector value logic. 358 if(find_named<icol>(m_cols,a_name)) return 358 if(find_named<icol>(m_cols,a_name)) return 0; 359 std_vector_column<T>* col = new std_vector 359 std_vector_column<T>* col = new std_vector_column<T>(m_writer,a_name,a_ref,m_vec_sep); 360 if(!col) return 0; 360 if(!col) return 0; 361 m_cols.push_back(col); 361 m_cols.push_back(col); 362 return col; 362 return col; 363 } 363 } 364 364 365 template <class T> 365 template <class T> 366 column_ref<T>* find_column_ref(const std::st 366 column_ref<T>* find_column_ref(const std::string& a_name) { 367 icol* col = find_named<icol>(m_cols,a_name 367 icol* col = find_named<icol>(m_cols,a_name); 368 if(!col) return 0; 368 if(!col) return 0; 369 return id_cast<icol, column_ref<T> >(*col) 369 return id_cast<icol, column_ref<T> >(*col); 370 } 370 } 371 371 372 template <class T> 372 template <class T> 373 column<T>* find_column(const std::string& a_ 373 column<T>* find_column(const std::string& a_name) { 374 icol* col = find_named<icol>(m_cols,a_name 374 icol* col = find_named<icol>(m_cols,a_name); 375 if(!col) return 0; 375 if(!col) return 0; 376 return id_cast<icol, column<T> >(*col); 376 return id_cast<icol, column<T> >(*col); 377 } 377 } 378 378 379 bool add_row() { 379 bool add_row() { 380 if(m_cols.empty()) return false; 380 if(m_cols.empty()) return false; 381 std::vector<icol*>::iterator it; 381 std::vector<icol*>::iterator it; 382 it=m_cols.begin(); 382 it=m_cols.begin(); 383 (*it)->add(); 383 (*it)->add(); 384 it++; 384 it++; 385 for(;it!=m_cols.end();++it) { 385 for(;it!=m_cols.end();++it) { 386 m_writer << m_sep; 386 m_writer << m_sep; 387 (*it)->add(); 387 (*it)->add(); 388 } 388 } 389 m_writer << std::endl; 389 m_writer << std::endl; 390 return true; 390 return true; 391 } 391 } 392 392 393 const std::vector<icol*>& columns() const {r 393 const std::vector<icol*>& columns() const {return m_cols;} 394 394 395 void set_title(const std::string& a_value) { 395 void set_title(const std::string& a_value) {m_title = a_value;} 396 const std::string& title() const {return m_t 396 const std::string& title() const {return m_title;} 397 protected: 397 protected: 398 std::ostream& m_writer; 398 std::ostream& m_writer; 399 char m_sep; 399 char m_sep; 400 char m_vec_sep; 400 char m_vec_sep; 401 std::string m_title; 401 std::string m_title; 402 std::vector<icol*> m_cols; 402 std::vector<icol*> m_cols; 403 }; 403 }; 404 404 405 }} 405 }} 406 406 407 #endif 407 #endif