inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/ntu/base
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_ntu_base
00005 #define inlib_ntu_base
00006 
00007 #ifdef INLIB_MEM
00008 #include "../mem"
00009 #endif
00010 
00011 #include "../vmanip"
00012 #include "../vfind"
00013 #include "../typedefs"
00014 #include "../scast"
00015 #include "saida"
00016 
00017 #include <string>
00018 #include <vector>
00019 #include <ostream>
00020 
00021 namespace inlib {
00022 namespace ntu {
00023 
00024 class base_col {
00025 public:
00026   static const std::string& s_class() {
00027     static const std::string s_v("inlib::ntu::base_col");
00028     return s_v;
00029   }
00030   virtual void* cast(const std::string& a_class) const {
00031     if(void* p = cmp_cast<base_col>(this,a_class)) {return p;}
00032     else return 0;
00033   }
00034 public:
00035   virtual base_col* copy() const = 0;
00036   virtual uint64 num_elems() const = 0;
00037   virtual bool add() = 0;
00038   virtual bool reset() = 0;
00039 protected:
00040   base_col(std::ostream& a_out,const std::string& a_name)
00041   :m_out(a_out)
00042   ,m_name(a_name),m_index(0){
00043 #ifdef INLIB_MEM
00044     mem::increment(s_class().c_str());
00045 #endif
00046   }
00047 public:
00048   virtual ~base_col(){
00049 #ifdef INLIB_MEM
00050     mem::decrement(s_class().c_str());
00051 #endif
00052   }
00053 protected:
00054   base_col(const base_col& a_from)
00055   :m_out(a_from.m_out)
00056   ,m_name(a_from.m_name)
00057   ,m_index(a_from.m_index){
00058 #ifdef INLIB_MEM
00059     mem::increment(s_class().c_str());
00060 #endif
00061   }
00062   base_col& operator=(const base_col& a_from){
00063     m_name = a_from.m_name;
00064     m_index = a_from.m_index;
00065     return *this;
00066   }
00067 public:
00068   std::string name() {return m_name;}
00069   const std::string& name() const {return m_name;}
00070 
00071   void set_index(uint64 a_index){m_index = a_index;}
00072 protected:
00073   std::ostream& m_out;
00074   std::string m_name;
00075   uint64 m_index;
00076 };
00077 
00078 class base_ntu {
00079 public:
00080   static const std::string& s_class() {
00081     static const std::string s_v("inlib::ntu::base_ntu");
00082     return s_v;
00083   }
00084   virtual void* cast(const std::string& a_class) const {
00085     if(void* p = cmp_cast<base_ntu>(this,a_class)) {return p;}
00086     else return 0;
00087   }
00088 protected:
00089   base_ntu(std::ostream& a_out,const std::string& a_title)
00090   :m_out(a_out),m_title(a_title),m_index(-1){
00091 #ifdef INLIB_MEM
00092     mem::increment(s_class().c_str());
00093 #endif
00094   }
00095   virtual ~base_ntu() {
00096     clear();
00097 #ifdef INLIB_MEM
00098     mem::decrement(s_class().c_str());
00099 #endif
00100   }
00101 protected:
00102   base_ntu(const base_ntu& a_from)
00103   :m_out(a_from.m_out)
00104   ,m_title(a_from.m_title),m_index(a_from.m_index)
00105   {
00106 #ifdef INLIB_MEM
00107     mem::increment(s_class().c_str());
00108 #endif
00109     std::vector<base_col*>::const_iterator it;
00110     for(it=a_from.m_cols.begin();it!=a_from.m_cols.end();++it) {
00111       base_col* column = (*it)->copy();
00112       if(!column) {
00113         m_out << s_class() << "::cstor :"
00114               << " can't copy column."
00115               << std::endl;
00116         inlib::clear<base_col>(m_cols);
00117         m_index = -1;
00118         return; //throw
00119       }
00120       m_cols.push_back(column);
00121     }
00122   }
00123   base_ntu& operator=(const base_ntu& a_from){
00124     inlib::clear<base_col>(m_cols);
00125     m_index = a_from.m_index;
00126 
00127     m_title = a_from.m_title;
00128     std::vector<base_col*>::const_iterator it;
00129     for(it=a_from.m_cols.begin();it!=a_from.m_cols.end();++it) {
00130       base_col* column = (*it)->copy();
00131       if(!column) {
00132         m_out << s_class << "::operator=() :"
00133               << " can't copy column."
00134               << std::endl;
00135         inlib::clear<base_col>(m_cols);
00136         m_index = -1;
00137         return *this;
00138       }
00139       m_cols.push_back(column);
00140     }
00141 
00142     return *this;
00143   }
00144 public:
00145   std::ostream& out() {return m_out;}
00146   const std::vector<base_col*>& cols() const {return m_cols;}
00147 
00148   std::string title() {return m_title;}
00149   const std::string& title() const {return m_title;}
00150   void set_title(const std::string& a_title) {m_title = a_title;}
00151 
00152   uint64 rows() const {
00153     if(m_cols.empty()) return 0;
00154     return m_cols.front()->num_elems();
00155   }
00156 
00157   void clear() { //must not be confused with reset().
00158     inlib::clear<base_col>(m_cols);
00159     m_index = -1;
00160   }
00161 
00162   bool reset() { //clear data in columns (but not the column set)
00163     bool status = true;
00164     std::vector<base_col*>::iterator it;
00165     for(it=m_cols.begin();it!=m_cols.end();++it) {
00166       if(!(*it)->reset()) status = false;
00167     }
00168     m_index = -1;
00169     return status;
00170   }
00171 
00172   // reading :
00173   void start() {m_index = -1;set_columns_index(0);}
00174   bool next() { 
00175     // a tuple loop is of the form :
00176     //  tuple.start();
00177     //  while(tuple.next()) {
00178     //    ...
00179     //  }
00180     if((m_index+1)>=(int64)rows()) return false;
00181     m_index++;
00182     set_columns_index(m_index);
00183     return true;
00184   }
00185   int64 row_index() const {return m_index;}
00186 
00187   // filling :
00188   bool add_row() {
00189     bool status = true;
00190     std::vector<base_col*>::iterator it;
00191     for(it=m_cols.begin();it!=m_cols.end();++it) {
00192       if(!(*it)->add()) status = false;
00193     }
00194     return status;
00195   }
00196 
00197 public:
00198   base_col* find_column(const std::string& a_name){
00199     return find_named<base_col>(m_cols,a_name);
00200   }
00201 
00202   void add_column(base_col* a_col) { //we take ownership.
00203     m_cols.push_back(a_col);
00204   }
00205 protected:
00206   void set_columns_index(uint64 a_index) {
00207     std::vector<base_col*>::iterator it;
00208     for(it=m_cols.begin();it!=m_cols.end();++it) {
00209       (*it)->set_index(a_index);
00210     }
00211   }
00212 protected:
00213   std::ostream& m_out;
00214   std::string m_title;
00215   int64 m_index;
00216   std::vector<base_col*> m_cols;
00217 };
00218 
00219 }}
00220 
00221 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines