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_wcsv_ntuple 00005 #define inlib_wcsv_ntuple 00006 00007 // A simple ntuple class to write at the csv format. 00008 // (csv = comma separated value). 00009 // Each add_row() write a row at the csv format. 00010 00011 #include "vfind" 00012 #include "vmanip" 00013 #include <ostream> 00014 00015 namespace inlib { 00016 namespace wcsv { 00017 00018 class ntuple { 00019 protected: 00020 class icol { 00021 public: 00022 virtual ~icol(){} 00023 public: 00024 virtual void add() = 0; 00025 virtual const std::string& name() const = 0; 00026 }; 00027 00028 public: 00029 template <class T> 00030 class column : public virtual icol { 00031 public: //icol 00032 virtual void add() { 00033 m_writer << m_tmp; 00034 m_tmp = m_def; 00035 } 00036 virtual const std::string& name() const {return m_name;} 00037 public: 00038 inline column(std::ostream& a_writer, 00039 const std::string& a_name, 00040 const T& a_def) 00041 :m_writer(a_writer) 00042 ,m_name(a_name),m_def(a_def),m_tmp(a_def) 00043 {} 00044 inline virtual ~column(){} 00045 protected: 00046 inline column(const column& a_from) 00047 :icol(a_from) 00048 ,m_writer(a_from.m_writer) 00049 ,m_name(a_from.m_name) 00050 ,m_def(a_from.m_def) 00051 ,m_tmp(a_from.m_tmp) 00052 {} 00053 inline column& operator=(const column& a_from){ 00054 m_name = a_from.m_name; 00055 m_def = a_from.m_def; 00056 m_tmp = a_from.m_tmp; 00057 return *this; 00058 } 00059 public: 00060 inline bool fill(const T& a_value) {m_tmp = a_value;return true;} 00061 protected: 00062 std::ostream& m_writer; 00063 std::string m_name; 00064 T m_def; 00065 T m_tmp; 00066 }; 00067 00068 00069 public: 00070 inline ntuple(std::ostream& a_writer,char a_sep = ',') 00071 :m_writer(a_writer) 00072 ,m_sep(a_sep) 00073 {} 00074 inline virtual ~ntuple() { 00075 inlib::clear<icol>(m_cols); 00076 } 00077 protected: 00078 inline ntuple(const ntuple& a_from) 00079 :m_writer(a_from.m_writer) 00080 ,m_sep(a_from.m_sep) 00081 {} 00082 inline ntuple& operator=(const ntuple& a_from){ 00083 m_sep = a_from.m_sep; 00084 return *this; 00085 } 00086 public: 00087 template <class T> 00088 inline column<T>* create_column(const std::string& a_name, 00089 const T& a_def = T()) { 00090 if(find_named<icol>(m_cols,a_name)) return 0; 00091 column<T>* col = new column<T>(m_writer,a_name,a_def); 00092 if(!col) return 0; 00093 m_cols.push_back(col); 00094 return col; 00095 } 00096 00097 inline bool add_row() { 00098 if(m_cols.empty()) return false; 00099 std::vector<icol*>::iterator it; 00100 it=m_cols.begin(); 00101 (*it)->add(); 00102 it++; 00103 for(;it!=m_cols.end();++it) { 00104 m_writer << m_sep; 00105 (*it)->add(); 00106 } 00107 m_writer << std::endl; 00108 return true; 00109 } 00110 00111 inline const std::vector<icol*>& columns() const {return m_cols;} 00112 protected: 00113 std::ostream& m_writer; 00114 char m_sep; 00115 std::vector<icol*> m_cols; 00116 }; 00117 00118 }} 00119 00120 #endif