Value.h

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #ifndef _EYEDB_VALUE_H
00026 #define _EYEDB_VALUE_H
00027 
00028 namespace eyedb {
00029 
00035   class ValueArray;
00036   class ObjectArray;
00037 
00041   class Value {
00042 
00043     // ----------------------------------------------------------------------
00044     // Value Interface
00045     // ----------------------------------------------------------------------
00046 
00047   public:
00048 
00049     enum Type {
00050       tNil = 0,
00051       tNull,
00052       tBool,
00053       tByte,
00054       tChar,
00055       tShort,
00056       tInt,
00057       tLong,
00058       tDouble,
00059       tIdent,
00060       tString,
00061       tData,
00062       tOid,
00063       tObject,
00064       tObjectPtr,
00065       tPobj,
00066       tList,
00067       tSet,
00068       tArray,
00069       tBag,
00070       tStruct
00071     } type;
00072 
00073     struct Attr {
00074       char *name;
00075       Value *value;
00076 
00077       Attr() {
00078         name = 0;
00079         value = 0;
00080       }
00081 
00082       Attr(const char *_name, Value *_value) {
00083         name = strdup(_name);
00084         value = _value;
00085       }
00086 
00087       ~Attr() {
00088         free(name);
00089       }
00090     };
00091 
00092     struct Struct {
00093       int attr_cnt;
00094       Value::Attr **attrs;
00095 
00096       Struct() {attr_cnt = 0; attrs = 0;}
00097 
00098       Struct(int _attr_cnt) {
00099         attr_cnt = _attr_cnt;
00100         attrs = new Attr*[attr_cnt];
00101       }
00102 
00103       void print(FILE *fd) const;
00104       int operator==(const Struct &);
00105       std::string toString() const;
00106 
00107       ~Struct() {
00108         for (int ii = 0; ii < attr_cnt; ii++)
00109           delete attrs[ii];
00110 
00111         if (attr_cnt)
00112           delete [] attrs;
00113       }
00114 
00115     private:
00116       Struct(const Struct &);
00117       Struct& operator=(const Struct &);
00118     };
00119 
00120     struct V_Data {
00121       Data data;
00122       Size size;
00123     };
00124 
00125     union {
00126       Bool b;
00127       unsigned char by;
00128       char c;
00129       short s;
00130       eyedblib::int32 i;
00131       eyedblib::int64 l;
00132       double d;
00133       char *str;
00134       V_Data data;
00135       Oid *oid;
00136       Object *o;
00137       ObjectPtr *o_ptr;
00138       unsigned int idx;
00139       LinkedList *list;
00140       Value::Struct *stru;
00141     };
00142 
00146     Value() {
00147       bufstr = NULL;
00148       auto_obj_garb = false;
00149       set();
00150     }
00151 
00157     Value(Bool b1, Bool b2) {
00158       bufstr = NULL;
00159       auto_obj_garb = false;
00160       set(b1, b2);
00161     }
00162 
00167     Value(Bool _b) {
00168       bufstr = NULL;
00169       auto_obj_garb = false;
00170       set(_b);
00171     }
00172 
00177     Value(unsigned char _by) {
00178       bufstr = NULL;
00179       auto_obj_garb = false;
00180       set(_by);
00181     }
00182 
00187     Value(char _c) {
00188       bufstr = NULL;
00189       auto_obj_garb = false;
00190       set(_c);
00191     }
00192 
00197     Value(short _s) {
00198       bufstr = NULL;
00199       auto_obj_garb = false;
00200       set(_s);
00201     }
00202 
00207     Value(double _d) {
00208       bufstr = NULL;
00209       auto_obj_garb = false;
00210       set(_d);
00211     }
00212 
00217     Value(eyedblib::int32 _i) {
00218       bufstr = NULL;
00219       auto_obj_garb = false;
00220       set(_i);
00221     }
00222 
00227     Value(eyedblib::int64 _l) {
00228       bufstr = NULL;
00229       auto_obj_garb = false;
00230       set(_l);
00231     }
00232 
00237     Value(const char *_str) {
00238       bufstr = NULL;
00239       auto_obj_garb = false;
00240       set(_str);
00241     }
00242 
00248     Value(const char *_str, Bool isident) {
00249       bufstr = NULL;
00250       auto_obj_garb = false;
00251       set(_str, isident);
00252     }
00253 
00258     Value(Data _data, Size _size) {
00259       bufstr = NULL;
00260       auto_obj_garb = false;
00261       set(_data, _size);
00262     }
00263 
00268     Value(const Oid &_oid) {
00269       bufstr = NULL;
00270       auto_obj_garb = false;
00271       set(_oid);
00272     }
00273 
00278     Value(const Object *_o) {
00279       bufstr = NULL;
00280       auto_obj_garb = false;
00281       o = 0;
00282       set(_o);
00283     }
00284 
00290     Value(const Object *_o, unsigned int _idx) {
00291       bufstr = NULL;
00292       auto_obj_garb = false;
00293       o = 0;
00294       set(_o, _idx);
00295     }
00296 
00301     Value(Object *_o, bool _auto_obj_garb = false) {
00302       bufstr = NULL;
00303       auto_obj_garb = _auto_obj_garb;
00304       o = 0;
00305       set(_o);
00306     }
00307 
00312     Value(const ObjectPtr &_o_ptr) {
00313       bufstr = NULL;
00314       auto_obj_garb = false;
00315       set(_o_ptr);
00316     }
00317 
00323     Value(LinkedList *_list, Value::Type _type) {
00324       bufstr = NULL;
00325       auto_obj_garb = false;
00326       set(_list, _type);
00327     }
00328 
00333     Value(Value::Struct *_stru) {
00334       bufstr = NULL;
00335       auto_obj_garb = false;
00336       set(_stru);
00337     }
00338 
00342     void set() {
00343       type = tNil;
00344       unvalid();
00345     }
00346 
00352     void set(Bool b1, Bool b2) {
00353       type = tNull;
00354       unvalid();
00355     }
00356 
00361     void set(Bool _b) {
00362       type = tBool;
00363       b = _b;
00364       unvalid();
00365     }
00366 
00371     void set (unsigned char _by) {
00372       type = tByte;
00373       by = _by;
00374       unvalid();
00375     }
00376 
00381     void set(char _c) {
00382       type = tChar;
00383       c = _c;
00384       unvalid();
00385     }
00386 
00391     void set(short _s) {
00392       type = tShort;
00393       s = _s;
00394       unvalid();
00395     }
00396 
00401     void set(double _d) {
00402       type = tDouble;
00403       d = _d;
00404       unvalid();
00405     }
00406 
00411     void set(eyedblib::int32 _i) {
00412       type = tInt;
00413       i = _i;
00414       unvalid();
00415     }
00416 
00421     void set(eyedblib::int64 _l) {
00422       type = tLong;
00423       l = _l;
00424       unvalid();
00425     }
00426 
00431     void set(const char *_str) {
00432       type = tString;
00433       str = _str ? strdup(_str) : NULL;
00434       unvalid();
00435     }
00436 
00442     void set(const char *_str, Bool isident) {
00443       type = (isident ? tIdent : tString);
00444       str = _str ? strdup(_str) : NULL;
00445       unvalid();
00446     }
00447 
00452     void set(Data _data, Size _size) {
00453       type = tData;
00454       data.data = _data;
00455       data.size = _size;
00456       unvalid();
00457     }
00458 
00463     void set(const Oid &_oid) {
00464       type = tOid;
00465       oid = new Oid(_oid);
00466       unvalid();
00467     }
00468 
00473     void set(const Object *_o) {
00474       type = tObject;
00475       o = (Object *)_o;
00476       unvalid();
00477     }
00478 
00484     void set(const Object *_o, unsigned int _idx) {
00485       type = tPobj;
00486       idx = _idx;
00487       unvalid();
00488     }
00489 
00494     void set(const ObjectPtr &_o_ptr) {
00495       type = tObjectPtr;
00496       o_ptr = new ObjectPtr(_o_ptr);
00497       unvalid();
00498     }
00499 
00504     void set(Object *_o);
00505 
00511     void set(LinkedList *_list, Value::Type _type) {
00512       if (_type != tList && _type != tSet && _type != tArray && _type != tBag)
00513         {
00514           (void)Exception::make("setting collection value: type must be "
00515                                 "tList, tSet, tArray or tBag");
00516           return;
00517         }
00518 
00519       type = _type;
00520       list = _list;
00521       unvalid();
00522     }
00523 
00528     void set(Value::Struct *_stru) {
00529       type = tStruct;
00530       stru = _stru;
00531       unvalid();
00532     }
00533 
00534     // flatten methods
00535 
00543     Status toArray(Database *db, ObjectPtrVector &obj_vect,
00544                    const RecMode *recmode = RecMode::NoRecurs);
00545 
00553     Status toArray(Database *db, ObjectArray &obj_array,
00554                    const RecMode *recmode = RecMode::NoRecurs);
00555 
00561     Status toArray(ValueArray &valarr);
00562 
00568     Status toArray(OidArray &oidarr);
00569 
00574     Value(const Value &val);
00575 
00581     int operator ==(const Value &val) const;
00582 
00588     int operator <(const Value &val) const;
00589 
00595     int operator !=(const Value &val) const;
00596 
00602     Value &operator =(const Value &val);
00603 
00608     void trace(FILE *fd = stdout) const {
00609       print(fd);
00610     }
00611 
00616     void print(FILE *fd = stdout) const;
00617 
00622     Data getData(Size *psize = 0) const;
00623 
00628     const char *getString() const;
00629 
00634     const char *getStringType() const;
00635 
00640     Value::Type getType() const {return type;}
00641 
00647     static const char *getStringType(Value::Type type);
00648 
00655     static const char *getAttributeName(const Class *cl, Bool isIndirect);
00656 
00660     void init() {
00661       bufstr = NULL;
00662       type = tNil;
00663     }
00664 
00670     void decode(Data idr, Offset &offset);
00671 
00678     void code(Data &idr, Offset &offset, Size &alloc_size) const;
00679 
00685     void setAutoObjGarbage(bool _auto_obj_garb) {auto_obj_garb = _auto_obj_garb;}
00686 
00691     bool isAutoObjGarbage() const {return auto_obj_garb;}
00692 
00698     void setMustRelease(bool must_release);
00699 
00700     ~Value();
00701 
00702   private:
00703     Status toValueArray(LinkedList &);
00704     Status toOidObjectArray(Database *db, LinkedList &,
00705                             Bool isObj, const RecMode *rcm);
00706     void garbage();
00707     char *bufstr;
00708     bool auto_obj_garb;
00709     void unvalid() {
00710       free(bufstr);
00711       bufstr = NULL;
00712     }
00713   };
00714 
00715   std::ostream& operator<<(std::ostream&, const Value &);
00716   std::ostream& operator<<(std::ostream&, const Value *);
00717 
00718   class ValueList;
00719 
00720   class ValueArray {
00721 
00722     // ----------------------------------------------------------------------
00723     // ValueArray Interface
00724     // ----------------------------------------------------------------------
00725 
00726   public:
00730     ValueArray(bool _auto_obj_garb = false) {
00731       values = NULL;
00732       value_cnt = 0;
00733       auto_obj_garb = _auto_obj_garb;
00734     }
00735 
00742     ValueArray(Value *_values, unsigned int _value_cnt, Bool copy = True) {
00743       values = NULL;
00744       value_cnt = 0;
00745       auto_obj_garb = false;
00746       set(_values, _value_cnt, copy);
00747     }
00748 
00756     ValueArray(bool _auto_obj_garb, Value *_values, unsigned int _value_cnt,
00757                Bool copy = True) {
00758       values = NULL;
00759       value_cnt = 0;
00760       auto_obj_garb = _auto_obj_garb;
00761       set(_values, _value_cnt, copy);
00762     }
00763 
00768     ValueArray(const ValueArray &valarr);
00769 
00774     ValueArray(const Collection *coll);
00775 
00780     ValueArray(const ValueList &list);
00781 
00787     ValueArray &operator=(const ValueArray &valarr);
00788 
00793     unsigned int getCount() const {return value_cnt;}
00794 
00801     void set(Value *_values, unsigned int _value_cnt, Bool copy = True);
00802 
00809     Status setValueAt(unsigned int ind, const Value &value);
00810 
00814     Value *getValues() {return values;}
00815 
00821     const Value & operator[](unsigned int ind) const {
00822       return values[ind];
00823     }
00824 
00829     ValueList *toList() const;
00830 
00836     void setAutoObjGarbage(bool auto_obj_garb);
00837 
00842     bool isAutoObjGarbage() const {return auto_obj_garb;}
00843 
00849     void setMustRelease(bool must_release);
00850 
00851     ~ValueArray();
00852 
00853   private:
00854     Value *values;
00855     unsigned int value_cnt;
00856     bool auto_obj_garb;
00857   };
00858 
00859   class ValueListCursor;
00860 
00864   class ValueList {
00865 
00866   public:
00870     ValueList();
00871 
00876     ValueList(const ValueArray &value_array);
00877 
00882     int getCount() const;
00883 
00888     void insertValue(const Value &value);
00889 
00894     void insertValueLast(const Value &value);
00895 
00900     void insertValueFirst(const Value &value);
00901 
00907     Bool suppressValue(const Value &value);
00908 
00915     Bool suppressPairValues(const Value &value1, const Value &value2);
00916 
00922     Bool exists(const Value &value) const;
00923 
00928     void empty();
00929 
00934     ValueArray *toArray() const;
00935 
00936     ~ValueList();
00937 
00938   private:
00939     LinkedList *list;
00940     friend class ValueListCursor;
00941   };
00942 
00943   class ValueListCursor {
00944 
00945   public:
00950     ValueListCursor(const ValueList &valuelist);
00951 
00956     ValueListCursor(const ValueList *valuelist);
00957 
00963     Bool getNext(Value &value);
00964 
00965     ~ValueListCursor();
00966 
00967   private:
00968     LinkedListCursor *c;
00969   };
00970 
00975 }
00976 
00977 #endif

Generated on Mon Dec 22 18:16:14 2008 for eyedb by  doxygen 1.5.3