Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

vdbl_stview.h

Go to the documentation of this file.
00001 // Standard view implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003 Hermann Schichl
00004 //
00005 // This file is part of the Vienna Database Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the Library GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // Library GNU General Public License for more details.
00015 
00016 // As a special exception, you may use this file as part of a free software
00017 // library without restriction.  Specifically, if other files instantiate
00018 // templates or use macros or inline functions from this file, or you compile
00019 // this file and link it with other files to produce an executable, this
00020 // file does not by itself cause the resulting executable to be covered by
00021 // the Library GNU General Public License.  This exception does not however
00022 // invalidate any other reasons why the executable file might be covered by
00023 // the Library GNU General Public License.
00024 
00030 #ifndef __VDBL_STVIEW_H
00031 #define __VDBL_STVIEW_H
00032 
00033 #include <iostream>
00034 #include <vdbl_table.h>
00035 #include <vdbl_view.h>
00036 
00037 __VDBL_BEGIN_NAMESPACE
00038 
00039 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00040 #pragma set woff 1209
00041 #endif
00042 
00044 
00048 class _VDBL_standardview : public _VDBL_view
00049 {
00050 private:
00051   typedef _VDBL_view _Base;
00052   
00054   typedef _VDBL_colid _V_colref;
00056   typedef _VDBL_rowid _V_rowref;
00058   typedef std::map<_VDBL_rowid, _VDBL_col> _V_colentry;
00060   typedef std::map<_V_colref, _V_colentry> _V_colcache;
00062   typedef std::vector<_V_rowref> _V_rows;
00064   typedef std::vector<_V_colref> _V_cols;
00065   
00066 private:
00068   std::pair<_VDBL_tableid,_VDBL_table *> _V_t;
00070   const _VDBL_context* _V_ctx;            
00071 
00073   unsigned int _V_lastchg;
00074 
00076   _V_colcache _V_cc;
00077 
00078 protected:
00082   _V_rows _V_r;
00086   _V_cols _V_c;
00087 
00088 private:
00090   std::map<std::string,_V_colref> _V_colnames;
00091 
00092   friend class _VDBL_table;
00093 
00094 public:
00096   typedef _Base::default_const_iterator defaults_const_iterator;
00097 
00098 protected:
00099   triple<_VDBL_tableid,_VDBL_colid,void*>
00100         _next_def_col(const _VDBL_tableid& _t, const _VDBL_colid& _c,
00101                       void *_d) const
00102     {
00103       _V_cols::const_iterator& _cit(*(_V_cols::const_iterator*)_d);
00104       if(_t == 0 && _c == 0) // end
00105         return make_triple(0,0,_d);
00106       ++_cit;
00107       while(_cit != _V_c.end())
00108       {
00109         if(_V_t.second->has_def(*_cit))
00110           break;
00111         ++_cit;
00112       }
00113       if(_cit == _V_c.end())
00114         return make_triple(0,0,_d);
00115       else
00116         return make_triple(_t,*_cit,_d);
00117     }
00118 
00119   triple<_VDBL_tableid,_VDBL_colid,void*>
00120         _prev_def_col(const _VDBL_tableid& _t, const _VDBL_colid& _c,
00121                       void *_d) const
00122     {
00123       _V_cols::const_iterator& _cit(*(_V_cols::const_iterator*)_d);
00124       if(_t == 0 && _c == 0) // end
00125         _cit = _V_c.end();
00126       --_cit;
00127       while(_cit != _V_c.begin())
00128       {
00129         if(_V_t.second->has_def(*_cit))
00130           break;
00131         --_cit;
00132       }
00133       if(_cit == _V_c.begin() && !_V_t.second->has_def(*_cit))
00134         return make_triple(0,0,_d);
00135       if(_cit == _V_c.end())
00136         return make_triple(0,0,_d);
00137       else
00138         return make_triple(_t,*_cit,_d);
00139     }
00140 
00141   void _destroy_def_data(void *&_d) const
00142     { delete (_V_cols::const_iterator*)_d; }
00143   
00144   void* _copy_def_data(void *_d) const
00145     { return (void *)
00146         new _V_cols::const_iterator(*(_V_cols::const_iterator*)_d); }
00147   
00148   triple<_VDBL_tableid,_VDBL_colid,void*>
00149         _next_col(const _VDBL_tableid& _t, const _VDBL_rowid& _r,
00150                   const _VDBL_colid& _c, void *_d) const
00151     {
00152       _V_cols::const_iterator& _cit(*(_V_cols::const_iterator*)_d);
00153       if(_t == 0 && _c == 0) // end
00154         return make_triple(0,0,_d);
00155       ++_cit;
00156       while(_cit != _V_c.end())
00157       {
00158         if(_V_t.second->has_def(*_cit) || _V_t.second->has_col(_r,*_cit))
00159           break;
00160         ++_cit;
00161       }
00162       if(_cit == _V_c.end())
00163         return make_triple(0,0,_d);
00164       else
00165         return make_triple(_t,*_cit,_d);
00166     }
00167 
00168   triple<_VDBL_tableid,_VDBL_colid,void*>
00169         _prev_col(const _VDBL_tableid& _t, const _VDBL_rowid& _r,
00170                   const _VDBL_colid& _c, void *_d) const
00171     {
00172       _V_cols::const_iterator& _cit(*(_V_cols::const_iterator*)_d);
00173       if(_t == 0 && _c == 0) // end
00174         _cit = _V_c.end();
00175       --_cit;
00176       while(_cit != _V_c.begin())
00177       {
00178         if(_V_t.second->has_def(*_cit) || _V_t.second->has_col(_r,*_cit))
00179           break;
00180         --_cit;
00181       }
00182       if(_cit == _V_c.begin() && !_V_t.second->has_def(*_cit) &&
00183          !_V_t.second->has_col(_r,*_cit))
00184         return make_triple(0,0,_d);
00185       if(_cit == _V_c.end())
00186         return make_triple(0,0,_d);
00187       else
00188         return make_triple(_t,*_cit,_d);
00189     }
00190 
00191   void _destroy_col_data(void *&_d) const
00192     { delete (_V_cols::const_iterator*)_d; }
00193   
00194   void* _copy_col_data(void *_d) const
00195     { return (void *)
00196         new _V_cols::const_iterator(*(_V_cols::const_iterator*)_d); }
00197   
00198   triple<_VDBL_tableid,_VDBL_rowid,void*>
00199         _next_row(const _VDBL_tableid& _t, const _VDBL_rowid& _r,
00200                       void *_d) const
00201     {
00202       _V_rows::const_iterator& _rit(*(_V_rows::const_iterator*)_d);
00203       if(_t == 0 && _r == 0) // end
00204         return make_triple(0,0,_d);
00205       ++_rit;
00206       if(_rit == _V_r.end())
00207         return make_triple(0,0,_d);
00208       else
00209         return make_triple(_t,*_rit,_d);
00210     }
00211 
00212   triple<_VDBL_tableid,_VDBL_rowid,void*>
00213         _prev_row(const _VDBL_tableid& _t, const _VDBL_rowid& _r,
00214                       void *_d) const
00215     {
00216       _V_rows::const_iterator& _rit(*(_V_rows::const_iterator*)_d);
00217       if(_t == 0 && _r == 0) // end
00218         _rit = _V_r.end();
00219       if(_rit == _V_r.begin())
00220         return make_triple(0,0,_d);
00221       --_rit;
00222       if(_rit == _V_r.end())
00223         return make_triple(0,0,_d);
00224       return make_triple(_t,*_rit,_d);
00225     }
00226   
00227   void _destroy_row_data(void *&_d) const
00228     { delete (_V_rows::const_iterator*)_d; }
00229   
00230   void* _copy_row_data(void *_d) const
00231     { return (void *)
00232         new _V_rows::const_iterator(*(_V_rows::const_iterator*)_d); }
00233   
00234 private:
00236   bool _row_visible(const _VDBL_rowid& _r) const
00237   {
00238     return std::binary_search(_V_r.begin(), _V_r.end(), _r);
00239   }
00240 
00241 public:
00246   _VDBL_standardview(const _VDBL_tableid& __ti, _VDBL_table* __t,
00247                      const _VDBL_context& __c, _V_enum __e)
00248               : _Base(__e), _V_t(std::make_pair(__ti,__t)), _V_ctx(&__c),
00249                 _V_lastchg(__t->get_change_ctr()), _V_cc(), _V_r(), _V_c(),
00250                 _V_colnames()
00251       {
00252         _VDBL_table::col_const_iterator __x;
00253         for(__x = __t->col_begin(); __x != __t->col_end(); ++__x)
00254         {
00255           _V_colnames.insert(*__x);
00256           _V_c.push_back((*__x).second);
00257         }
00258         _VDBL_table::row_const_iterator __y;
00259         for(__y = __t->row_begin(); __y != __t->row_end(); ++__y)
00260           _V_r.push_back(*__y);
00261         std::sort(_V_r.begin(), _V_r.end());
00262       }
00263 
00269   _VDBL_standardview(const _VDBL_tableid& __ti, _VDBL_table* __t,
00270                      const _VDBL_context& __c, _V_enum __e,
00271                      const std::vector<_VDBL_rowid>& _rs)
00272               : _Base(__e), _V_t(std::make_pair(__ti,__t)), _V_ctx(&__c),
00273                 _V_lastchg(__t->get_change_ctr()), _V_cc(), _V_r(_rs), _V_c(),
00274                 _V_colnames()
00275       {
00276         _VDBL_table::col_const_iterator __x;
00277         for(__x = __t->col_begin(); __x != __t->col_end(); ++__x)
00278         {
00279           _V_colnames.insert(*__x);
00280           _V_c.push_back((*__x).second);
00281         }
00282         std::sort(_V_r.begin(), _V_r.end());
00283       }
00284 
00288   _VDBL_standardview(const _VDBL_standardview& __v)
00289                 : _Base(__v), _V_t(__v._V_t), _V_ctx(__v._V_ctx),
00290                   _V_cc(__v._V_cc), _V_r(__v._V_r), _V_c(__v._V_c),
00291                   _V_colnames(__v._V_colnames)
00292       {}
00293                 
00294 
00298   virtual ~_VDBL_standardview() {}
00299 
00300 public:
00301   const std::type_info& get_colinfo(const std::string& _C_n,
00302                 triple<bool,_VDBL_colid,_VDBL_colflags>& _r) const
00303   {
00304     std::map<std::string,_V_colref>::const_iterator __x(_V_colnames.find(_C_n));
00305     if(__x == _V_colnames.end())
00306     {
00307       _r = make_triple(false, _VDBL_colid(), _VDBL_colflags());
00308       return typeid(void);
00309     }
00310     return _V_t.second->get_colinfo(_C_n, _r);
00311   }
00312 
00313   bool insert(const std::vector<_T_colspec>& _row)
00314   {
00315     if(view_type() != V_window)
00316       return false;
00319     _VDBL_rowid _ri;
00320     std::vector<std::pair<const std::string*,const _VDBL_col*> > _v;
00321     _v.reserve(_row.size());
00322     for(std::vector<_T_colspec>::const_iterator _x = _row.begin();
00323         _x != _row.end(); ++_x)
00324       _v.push_back(std::make_pair(&(*_x).first,&(*_x).second));
00325     bool ret = _V_t.second->insert(_v, _ri);
00326     _V_r.insert(std::upper_bound(_V_r.begin(), _V_r.end(), _ri), _ri);
00327     return ret;
00328   }
00329 
00330   bool remove(std::pair<_VDBL_tableid,_VDBL_rowid> _r)
00331   {
00332     if(!_row_visible(_r.second))
00333       return false;
00334     if(view_type() != V_window)
00335       return false;
00336     bool ret = _V_t.second->remove(_r.second);
00337     if(ret)
00338       _V_r.erase(std::lower_bound(_V_r.begin(), _V_r.end(), _r.second));
00339     return ret;
00340   }
00341 
00342   bool has_col(const std::string& _C_n) const
00343   {
00344     std::map<std::string,_V_colref>::const_iterator __x(_V_colnames.find(_C_n));
00345     return __x != _V_colnames.end();
00346   }
00347 
00348   std::pair<_VDBL_tableid,_VDBL_colid> get_col_id(const std::string& _C_n) const
00349   {
00350     std::map<std::string,_V_colref>::const_iterator __x(_V_colnames.find(_C_n));
00351     if(__x == _V_colnames.end())
00352       return std::make_pair(_VDBL_tableid(),_VDBL_colid());
00353     return std::make_pair(_V_t.first,(*__x).second);
00354   }
00355 
00356   const _VDBL_row& get_row(const std::pair<_VDBL_tableid,_VDBL_rowid>& _ri,
00357                            bool& error) const
00358   {
00359     if(!_row_visible(_ri.second))
00360     {
00361       error = true;
00362       return ___empty_row_return;
00363     }
00364     return _V_t.second->get_row(_ri.second, error);
00365   }
00366  
00367   const _VDBL_col& get_raw_col(const std::pair<_VDBL_tableid,_VDBL_rowid>& _ri,
00368                            const _VDBL_colid& _ci, _VDBL_row const *& _rr,
00369                            bool& error) const
00370   {
00371     if(!_row_visible(_ri.second))
00372     {
00373       error = true;
00374       return ___empty_col_return;
00375     }
00376     const _VDBL_row& _r(_V_t.second->get_row(_ri.second, error));
00377     if(error)
00378       return ___empty_col_return;
00379     _rr = &_r;
00380     const _VDBL_col& _c(_r.get_col(_ci, error));
00381     if(error)
00382       return _V_t.second->get_def(_ci, error);
00383     else
00384       return _c;
00385   }                                             
00386 
00391   std::ostream& print_col(std::ostream& o,
00392                           const std::pair<_VDBL_tableid,_VDBL_rowid>& _ri,
00393                           const _VDBL_colid& _ci, bool& printed) const
00394   {
00395     bool error;
00396     const _VDBL_row* _rr;
00397     vdbl::col c(get_raw_col(_ri, _ci, _rr, error));
00398     if(error)
00399     {
00400       printed = false;
00401       return o;
00402     }
00403     printed = true;
00404     c.setcontext(_V_ctx, _rr);
00405     return o << c;
00406   }
00407  
00413   template <class _R>
00414   bool get(const std::pair<_VDBL_tableid,_VDBL_rowid>& _ri,
00415                                  const _VDBL_colid& _ci, _R& r) const
00416   {
00417     bool error;
00418     const _VDBL_row* _rr;
00419     vdbl::col c(get_raw_col(_ri, _ci, _rr, error));
00420     if(error)
00421       return false;
00422     c.setcontext(_V_ctx, _rr);
00423     c.get(r);
00424     return true;
00425   }
00426  
00435   template <class _R>
00436   bool get_raw_ptr(const std::pair<_VDBL_tableid,_VDBL_rowid>& _ri,
00437                    const _VDBL_colid& _ci, _R const *& r) const
00438   {
00439     bool error;
00440     const _VDBL_row* _rr;
00441     vdbl::col c(get_raw_col(_ri, _ci, _rr, error));
00442     if(error)
00443       return false;
00444     c.setcontext(_V_ctx, _rr);
00445     c.get_ptr(r);
00446     return true;
00447   }
00448  
00449   const _VDBL_col& get_def(const std::pair<_VDBL_tableid,_VDBL_colid>& _ri,
00450                            bool& error) const
00451   { return _V_t.second->get_def(_ri.second, error); }
00452 
00453   default_const_iterator defaults_begin() const
00454   {
00455     _V_cols::const_iterator* _rit = new _V_cols::const_iterator(_V_c.begin());
00456     while(*_rit != _V_c.end())
00457     {
00458       if(_V_t.second->has_def(**_rit))
00459         break;
00460       ++*_rit;
00461     }
00462     if(*_rit == _V_c.end())
00463       return default_const_iterator(this,0,0,(void*)_rit);
00464     return default_const_iterator(this,_V_t.first,**_rit,(void*)_rit);
00465   }
00466 
00467   default_const_iterator defaults_end() const
00468   {
00469     _V_cols::const_iterator* _rit = new _V_cols::const_iterator(_V_c.begin());
00470     return default_const_iterator(this,0,0,(void*)_rit);
00471   }
00472 
00473   col_const_iterator cols_begin(const _VDBL_rowid& _r) const
00474   {
00475     _V_cols::const_iterator* _rit = new _V_cols::const_iterator(_V_c.begin());
00476     while(*_rit != _V_c.end())
00477     {
00478       if(_V_t.second->has_col(_r,**_rit) || _V_t.second->has_def(**_rit))
00479         break;
00480       ++*_rit;
00481     }
00482     if(*_rit == _V_c.end())
00483       return col_const_iterator(this,0,_r,0,(void*)_rit);
00484     return col_const_iterator(this,_V_t.first,_r,**_rit,(void*)_rit);
00485   }
00486 
00487   col_const_iterator cols_end(const _VDBL_rowid& _r) const
00488   {
00489     _V_cols::const_iterator* _rit = new _V_cols::const_iterator(_V_c.begin());
00490     return col_const_iterator(this,0,_r,0,(void*)_rit);
00491   }
00492 
00493   row_const_iterator rows_begin() const
00494   {
00495     _V_rows::const_iterator* _rit = new _V_rows::const_iterator(_V_r.begin());
00496     if(*_rit == _V_r.end())
00497       return row_const_iterator(this,0,0,(void*)_rit);
00498     return row_const_iterator(this,_V_t.first,**_rit,(void*)_rit);
00499   }
00500 
00501   row_const_iterator rows_end() const
00502   {
00503     _V_rows::const_iterator* _rit = new _V_rows::const_iterator(_V_r.end());
00504     return row_const_iterator(this,0,0,(void*)_rit);
00505   }
00506 };
00507 
00509 
00514 class view : public _VDBL_standardview
00515 {
00516   typedef _VDBL_standardview _Base;
00517 
00518 public:
00519   typedef _V_enum view_enum;
00520 
00521 public:
00526   view(const _VDBL_tableid& __ti, _VDBL_table* __t,
00527        const _VDBL_context& __c, _V_enum __e = V_window)
00528                 : _VDBL_standardview(__ti,__t,__c,__e) {}
00529 
00533   view(const view& _v) : _VDBL_standardview(_v) {}
00534 
00539   template <class _R>
00540   bool get(const tableid& _ti, const rowid& _ri, const colid& _ci, _R& r) const
00541   { return _Base::get(std::make_pair(_ti, _ri), _ci, r); }
00542 
00549   template <class _R>
00550   bool get_raw_ptr(const tableid& _ti, const rowid& _ri, const colid& _ci,
00551                    _R const *& r) const
00552   { return _Base::get_raw_ptr(std::make_pair(_ti, _ri), _ci, r); }
00553 
00555 
00559   template <class _R>
00560   bool get(const rowid& _ri, const std::string& _c, _R& r) const
00561   { std::pair<tableid,colid> _tci(get_col_id(_c));
00562     return _Base::get(std::make_pair(_tci.first, _ri), _tci.second, r); }
00563 
00564   template <class _R>
00565   bool get(const rowid& _ri, const char* _c, _R& r) const
00566   { return get(_ri, std::string(_c), r); }
00568 
00574   const col& get_raw_col(const std::pair<tableid,rowid>& _ri, const colid& _ci,
00575                      row const *& _rr, bool& error) const
00576   { return _Base::get_raw_col(_ri, _ci, (_VDBL_row const *)_rr, error); }
00577 };
00578 
00579 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00580 #pragma reset woff 1209
00581 #endif
00582 
00583 __VDBL_END_NAMESPACE
00584 
00585 #endif /* __VDBL_STVIEW_H */
00586 
00587 // Local Variables:
00588 // mode:C++
00589 // End:

Generated on Tue Nov 4 01:29:11 2003 for Vienna Database Library by doxygen1.2.18