COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/include/coopy/DataSheet.h
Go to the documentation of this file.
00001 #ifndef COOPY_DATASHEET
00002 #define COOPY_DATASHEET
00003 
00004 #include <coopy/Dbg.h>
00005 #include <coopy/SheetStyle.h>
00006 #include <coopy/SheetSchema.h>
00007 #include <coopy/RowRef.h>
00008 #include <coopy/ColumnRef.h>
00009 #include <coopy/RefCount.h>
00010 #include <coopy/SheetCell.h>
00011 #include <coopy/Appearance.h>
00012 
00013 #include <string>
00014 #include <vector>
00015 
00016 namespace coopy {
00017   namespace store {
00018     class RowCache;
00019     class DataSheet;
00020     class SheetRow;
00021     class OrderedSheetRow;
00022     class CacheSheetRow;
00023     class Pool;
00024     class SheetSchema;
00025   }
00026 }
00027 
00028 namespace coopy {
00029   namespace cmp {
00030     class Compare;
00031   }
00032 }
00033 
00034 class coopy::store::RowCache {
00035 public:
00036   std::vector<bool> flags;
00037   std::vector<bool> invent;
00038   std::vector<SheetCell> cells;
00039 
00040   RowCache(int len) {
00041     flags.resize(len,false);
00042     invent.resize(len,false);
00043     cells.resize(len);
00044   }
00045 };
00046 
00047 
00053 class coopy::store::DataSheet : public RefCount {
00054 public:
00055   DataSheet() {
00056     pool = 0 /*NULL*/;
00057     meta_hint = 0 /*NULL*/;
00058   }
00059 
00060   virtual ~DataSheet();
00061 
00067   virtual int width() const = 0;
00068 
00074   virtual int height() const = 0;
00075 
00082   virtual std::string cellString(int x, int y) const = 0;
00083 
00090   virtual std::string cellString(int x, int y, bool& escaped) const {
00091     escaped = false;
00092     return cellString(x,y);
00093   }
00094 
00100   virtual SheetCell cellSummary(int x, int y) const {
00101     SheetCell c;
00102     c.text = cellString(x,y,c.escaped);
00103     return c;
00104   }
00105 
00111   virtual bool cellString(int x, int y, const std::string& str) {
00112     return false;
00113   }
00114 
00121   virtual bool cellString(int x, int y, const std::string& str, bool escaped) {
00122     return cellString(x,y,str);
00123   }
00124 
00130   virtual bool cellSummary(int x, int y, const SheetCell& c) {
00131     return cellString(x,y,c.text,c.escaped);
00132   }
00133 
00139   virtual bool canEscape() const {
00140     return false;
00141   }
00142 
00148   virtual SheetCell getCell(int x, int y) const {
00149     return cellSummary(x,y);
00150   }
00151 
00157   virtual bool setCell(int x, int  y, const SheetCell& c) {
00158     return cellSummary(x,y,c);
00159   }
00160 
00166   std::string encode(const SheetStyle& style) const;
00167 
00173   std::string toString() const {
00174     SheetStyle style;
00175     return encode(style);
00176   }
00177 
00183   static std::string encodeCell(const SheetCell& str, 
00184                                 const SheetStyle& style);
00185 
00191   virtual bool clearCache() { return true; }
00192 
00193   virtual SheetSchema *getSchema() const {
00194     return 0 /*NULL*/;
00195   }
00196 
00197   virtual bool deleteColumn(const ColumnRef& column) {
00198     return false;
00199   }
00200 
00201   // insert a column before base; if base is invalid insert after all columns
00202   virtual ColumnRef insertColumn(const ColumnRef& base) {
00203     return ColumnRef(); // invalid column
00204   }
00205 
00206   virtual ColumnRef insertColumn(const ColumnRef& base, 
00207                                  const ColumnInfo& info) = 0;
00208 
00209   virtual bool modifyColumn(const ColumnRef& base, 
00210                             const ColumnInfo& info) = 0;
00211 
00212   // move a column before base; if base is invalid move after all columns
00213   virtual ColumnRef moveColumn(const ColumnRef& src, const ColumnRef& base) {
00214     return ColumnRef(); // invalid column
00215   }
00216 
00217   virtual bool deleteRow(const RowRef& src) {
00218     return false;
00219   }
00220 
00221   virtual bool deleteRows(const RowRef& first, const RowRef& last) {
00222     int start = first.getIndex();
00223     for (int i=first.getIndex(); i<=last.getIndex(); i++) {
00224       bool ok = deleteRow(RowRef(start));
00225       if (!ok) return false;
00226     }
00227     return true;
00228   }
00229 
00230   virtual bool deleteData(int offset = 0) {
00231     int h = height();
00232     for (int i=offset; i<h; i++) {
00233       deleteRow(RowRef(offset));
00234     }
00235     return true;
00236   }
00237 
00238   virtual bool hasDimension() const {
00239     return true;
00240   }
00241 
00242   virtual bool forceWidth(int width) {
00243     return false;
00244   }
00245 
00246   virtual bool forceHeight(int height) {
00247     return false;
00248   }
00249 
00250   // insert a row before base; if base is invalid insert after all rows
00251   virtual RowRef insertRow(const RowRef& base) {
00252     return RowRef(); // invalid row
00253   }
00254 
00255   // move a row before base; if base is invalid move after all rows
00256   virtual RowRef moveRow(const RowRef& src, const RowRef& base) {
00257     return RowRef(); // invalid column
00258   }
00259 
00260   virtual bool copyData(const DataSheet& src);
00261 
00262   virtual bool canWrite() { return true; }
00263 
00264   virtual bool canResize() { return false; }
00265 
00266   virtual bool hasExternalColumnNames() const {
00267     return false;
00268   }
00269 
00270   virtual bool resize(int w, int h) {
00271     return false;
00272   }
00273 
00274   virtual Poly<SheetRow> insertRow();
00275 
00276   virtual Poly<SheetRow> insertRowOrdered(const RowRef &base);
00277 
00278   virtual bool applyRowCache(const RowCache& cache, int row,
00279                              SheetCell *result);
00280 
00281   virtual bool applySchema(const SheetSchema& ss) {
00282     return false;
00283   }
00284 
00285   virtual bool addedHeader() {
00286     if (getSchema()) {
00287       getSchema()->addedHeader();
00288     }
00289     return true;
00290   }
00291 
00292   virtual std::string getDescription() const {
00293     return "data";
00294   }
00295 
00296   virtual std::vector<std::string> getNestedDescription() const {
00297     std::string name = getDescription();
00298     std::vector<std::string> v;
00299     v.push_back(name);
00300     return v;
00301   }
00302 
00303   virtual std::string desc() const {
00304     std::vector<std::string> v = getNestedDescription();
00305     std::string output;
00306     for (int i=0; i<(int)v.size(); i++) {
00307       if (output!="") {
00308         output += ".";
00309       }
00310       output += v[i];
00311     }
00312     return output;
00313   }
00314 
00315   virtual std::string getHash(bool cache=false) const;
00316 
00317   virtual std::string getRawHash() const {
00318     return "";
00319   }
00320 
00321   virtual DataSheet& tail() {
00322     return *this;
00323   }
00324 
00325   virtual const DataSheet& tail_const() const {
00326     return *this;
00327   }
00328 
00329   virtual const DataSheet& dataTail() const {
00330     return *this;
00331   }
00332 
00333   // has spreadsheet-like ordering, inserts happen in a "place"
00334   virtual bool isSequential() const {
00335     return true;
00336   }
00337 
00338   virtual DataSheet *getNestedSheet(int x, int y) {
00339     return 0/*NULL*/; 
00340   }
00341 
00342   virtual Poly<Appearance> getCellAppearance(int x, int y) {
00343     return Poly<Appearance>();
00344   }
00345 
00346   virtual Poly<Appearance> getRowAppearance(int y) {
00347     return Poly<Appearance>();
00348   }
00349 
00350   virtual Poly<Appearance> getColAppearance(int x) {
00351     return Poly<Appearance>();
00352   }
00353 
00354   virtual bool hasSheetName() const {
00355     return true;
00356   }
00357 
00358   virtual bool hasRowOffset() const {
00359     return false;
00360   }
00361 
00362   virtual bool setPool(Pool *pool) {
00363     this->pool = pool;
00364     return true;
00365   }
00366 
00367   virtual Pool *getPool() const {
00368     return pool;
00369   }
00370 
00371   virtual void setMeta(SheetSchema *hint);
00372 
00373   virtual SheetSchema *getMeta() const;
00374 
00375   virtual bool beginTransaction() {
00376     return false;
00377   }
00378   
00379   virtual bool rollbackTransaction() {
00380     return false;
00381   }
00382   
00383   virtual bool endTransaction() {
00384     return false;
00385   }
00386 
00387 
00388   virtual void *getDatabase() const {
00389     return 0/*NULL*/;
00390   }
00391 
00392   virtual coopy::cmp::Compare *getComparisonMethod() {
00393     return 0/*NULL*/;
00394   }
00395 
00396 private:
00397   std::string hash_cache;
00398   SheetSchema *meta_hint;
00399   Pool *pool;
00400 };
00401 
00402 class coopy::store::SheetRow : public RefCount {
00403 public:
00404   int delta;
00405 
00406   SheetRow() {
00407     delta = 0;
00408   }
00409 
00410   virtual SheetCell getCell(int x) const = 0;
00411   
00412   virtual bool setCell(int x, const SheetCell& c) = 0;
00413 
00414   virtual bool flush() = 0;
00415 
00416   virtual bool invent(int x) = 0;
00417 
00418   virtual bool setDelta(int dh) { delta = dh; return true; }
00419 
00420   // only valid AFTER flush
00421   virtual RowRef getRowAfterFlush() = 0;
00422 
00423   virtual bool undo() = 0;
00424 };
00425 
00426 
00427 
00428 class coopy::store::OrderedSheetRow : public SheetRow {
00429 protected:
00430   int y;
00431   DataSheet *sheet;
00432 public:
00433   OrderedSheetRow() {
00434     y = -1;
00435     sheet = 0 /*NULL*/;
00436   }
00437 
00438   OrderedSheetRow(DataSheet *sheet, int y) : sheet(sheet), y(y) {
00439   }
00440 
00441   virtual SheetCell getCell(int x) const {
00442     return sheet->cellSummary(x,y);
00443   }
00444   
00445   virtual bool setCell(int x, const SheetCell& c) {
00446     return sheet->cellSummary(x,y,c);
00447   }
00448 
00449   virtual bool invent(int x);
00450 
00451   virtual bool flush() {
00452     return true;
00453   }
00454 
00455   virtual RowRef getRowAfterFlush() {
00456     return RowRef((y!=-1)?(y+delta):y);
00457   }
00458 
00459   virtual bool undo() {
00460     return sheet->deleteRow(y);
00461   }
00462 };
00463 
00464 
00468 class coopy::store::CacheSheetRow : public SheetRow {
00469 protected:
00470   RowCache cache;
00471   SheetCell result;
00472   DataSheet *sheet;
00473 public:
00474   CacheSheetRow(DataSheet *sheet) : SheetRow(), 
00475     cache(sheet->width()) {
00476     this->sheet = sheet;
00477   }
00478 
00479   virtual SheetCell getCell(int x) const {
00480     return SheetCell();
00481   }
00482   
00483   virtual bool setCell(int x, const SheetCell& c) {
00484     cache.cells[x] = c;
00485     cache.flags[x] = true;
00486     return true;
00487   }
00488 
00489   virtual bool invent(int x) {
00490     cache.invent[x] = true;
00491     return true;
00492   }
00493 
00494   virtual bool flush() {
00495     return sheet->applyRowCache(cache,-1,&result);
00496   }
00497 
00498   SheetCell getResult() {
00499     return result;
00500   }
00501 
00502   virtual RowRef getRowAfterFlush() {
00503     if (result.escaped) return -1;
00504     int y = result.asInt();
00505     return RowRef((y!=-1)?(y+delta):y);
00506   }
00507 
00508   virtual bool undo() {
00509     return true;
00510   }
00511 };
00512 
00513 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines