Oid.cc

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 #include "eyedb_p.h"
00026         
00027 using namespace std;
00028 
00029 namespace eyedb {
00030 
00031   const Oid Oid::nullOid;
00032 
00033   /*
00034     Oid::Oid()
00035     {
00036     mset(&oid, 0, sizeof(eyedbsm::Oid));
00037     }
00038   */
00039 
00040   Oid::Oid(const eyedbsm::Oid &_oid)
00041   {
00042     oid = _oid;
00043   }
00044 
00045   Oid::Oid(const Oid &_oid)
00046   {
00047     oid = _oid.oid;
00048   }
00049 
00050   Oid::Oid(const eyedbsm::Oid *_oid)
00051   {
00052     oid = *_oid;
00053   }
00054 
00055   Oid::Oid(const char *str)
00056   {
00057     oid = stringGetOid(str);
00058   }
00059 
00060   Oid::Oid(eyedbsm::Oid::NX nx, eyedbsm::Oid::DbID dbid,
00061            eyedbsm::Oid::Unique unique)
00062   {
00063     oid.setNX(nx);
00064     eyedbsm::dbidSet(&oid, dbid);
00065     oid.setUnique(unique);
00066   }
00067 
00068   const char *Oid::getString() const
00069   {
00070     if (isValid())
00071       return OidGetString(&oid);
00072     return NullString;
00073   }
00074 
00075   void Oid::setOid(const eyedbsm::Oid &_oid)
00076   {
00077     oid = _oid;
00078   }
00079 
00080   Oid &Oid::operator=(const Oid &_oid)
00081   {
00082     oid = _oid.oid;
00083     return *this;
00084   }
00085 
00086   ostream& operator<<(ostream& os, const Oid &oid)
00087   {
00088     os << oid.toString();
00089     return os;
00090   }
00091 
00092   ostream& operator<<(ostream& os, const Oid *oid)
00093   {
00094     if (!oid)
00095       return os << "NULL";
00096 
00097     os << oid->toString();
00098     return os;
00099   }
00100 
00101   OidArray::OidArray(const Oid *_oids, int _count)
00102   {
00103     oids = 0;
00104     count = 0;
00105     set(_oids, _count);
00106   }
00107 
00108   OidArray::OidArray(const Collection *coll)
00109   {
00110     oids = 0;
00111     count = 0;
00112     coll->getElements(*this);
00113   }
00114 
00115   OidArray::OidArray(const OidArray &oid_arr)
00116   {
00117     oids = 0;
00118     count = 0;
00119     *this = oid_arr;
00120   }
00121 
00122   OidArray::OidArray(const OidList &list)
00123   {
00124     count = 0;
00125     int cnt = list.getCount();
00126     if (!cnt)
00127       {
00128         oids = 0;
00129         return;
00130       }
00131 
00132     oids = (Oid *)malloc(sizeof(Oid) * cnt);
00133     memset(oids, 0, sizeof(Oid) * cnt);
00134 
00135     OidListCursor c(list);
00136     Oid oid;
00137 
00138     for (; c.getNext(oid); count++)
00139       oids[count] = oid;
00140   }
00141 
00142   OidArray& OidArray::operator=(const OidArray &oidarr)
00143   {
00144     set(oidarr.oids, oidarr.count);
00145     return *this;
00146   }
00147 
00148   void OidArray::set(const Oid *_oids, int _count)
00149   {
00150     free(oids);
00151     count = _count;
00152     if (count)
00153       {
00154         int size = count * sizeof(Oid);
00155         oids = (Oid *)malloc(size);
00156         if (_oids)
00157           memcpy(oids, _oids, size);
00158       }
00159     else
00160       oids = 0;
00161   }
00162 
00163   OidList *
00164   OidArray::toList() const
00165   {
00166     return new OidList(*this);
00167   }
00168 
00169   OidArray::~OidArray()
00170   {
00171     free(oids);
00172   }
00173 
00174   OidList::OidList()
00175   {
00176     list = new LinkedList();
00177   }
00178 
00179   OidList::OidList(const OidArray &oid_array)
00180   {
00181     list = new LinkedList();
00182     for (int i = 0; i < oid_array.getCount(); i++)
00183       insertOidLast(oid_array[i]);
00184   }
00185 
00186   int OidList::getCount(void) const
00187   {
00188     return list->getCount();
00189   }
00190 
00191   void
00192   OidList::insertOid(const Oid &oid)
00193   {
00194     list->insertObject(new Oid(oid));
00195   }
00196 
00197   void
00198   OidList::insertOidFirst(const Oid &oid)
00199   {
00200     list->insertObjectFirst(new Oid(oid));
00201   }
00202 
00203   void
00204   OidList::insertOidLast(const Oid &oid)
00205   {
00206     list->insertObjectLast(new Oid(oid));
00207   }
00208 
00209   Bool
00210   OidList::suppressOid(const Oid &oid)
00211   {
00212     LinkedListCursor c(list);
00213     Oid *xoid;
00214     while (c.getNext((void *&)xoid))
00215       if (xoid->compare(oid))
00216         {
00217           list->deleteObject(xoid);
00218           return True;
00219         }
00220 
00221     return False;
00222   }
00223 
00224   Bool
00225   OidList::exists(const Oid &oid) const
00226   {
00227     LinkedListCursor c(list);
00228     Oid *xoid;
00229     while (c.getNext((void *&)xoid))
00230       if (xoid->compare(oid))
00231         return True;
00232     return False;
00233   }
00234 
00235   void
00236   OidList::empty()
00237   {
00238     list->empty();
00239   }
00240 
00241   OidArray *
00242   OidList::toArray() const
00243   {
00244     int cnt = list->getCount();
00245     if (!cnt)
00246       return new OidArray();
00247     Oid *arr = (Oid *)malloc(sizeof(Oid) * cnt);
00248     memset(arr, 0, sizeof(Oid) * cnt);
00249 
00250     LinkedListCursor c(list);
00251     Oid *xoid;
00252     for (int i = 0; c.getNext((void *&)xoid); i++)
00253       arr[i] = *xoid;
00254   
00255     OidArray *oid_arr = new OidArray(arr, cnt);
00256     free(arr);
00257     return oid_arr;
00258   }
00259 
00260   OidList::~OidList()
00261   {
00262     LinkedListCursor c(list);
00263     Oid *xoid;
00264     while (c.getNext((void *&)xoid))
00265       delete xoid;
00266     delete list;
00267   }
00268 
00269   OidListCursor::OidListCursor(const OidList &oidlist)
00270   {
00271     c = new LinkedListCursor(oidlist.list);
00272   }
00273 
00274   OidListCursor::OidListCursor(const OidList *oidlist)
00275   {
00276     c = new LinkedListCursor(oidlist->list);
00277   }
00278 
00279   Bool
00280   OidListCursor::getNext(Oid &oid)
00281   {
00282     Oid *xoid;
00283     if (c->getNext((void *&)xoid))
00284       {
00285         oid = *xoid;
00286         return True;
00287       }
00288 
00289     return False;
00290   }
00291 
00292   OidListCursor::~OidListCursor()
00293   {
00294     delete c;
00295   }
00296 }

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