src_other/tuplebase/ZTSoup.h

00001 /*  @(#) $Id: ZTSoup.h,v 1.19 2007/07/15 20:42:29 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2005 Andrew Green and Learning in Motion, Inc.
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 ------------------------------------------------------------ */
00024 
00025 #ifndef __ZTSoup__
00026 #define __ZTSoup__ 1
00027 
00028 #include "ZTSWatcher.h"
00029 #include "ZDList.h"
00030 
00031 class ZTCrouton;
00032 class ZTSieve;
00033 
00034 #ifndef ZCONFIG_Debug_TSoup_Debug
00035 #       define ZCONFIG_Debug_TSoup_Debug 1
00036 #endif
00037 
00038 // =================================================================================================
00039 #pragma mark -
00040 #pragma mark * ZTSoup
00041 
00042 class ZTSoup : public ZRefCounted
00043         {
00044 public:
00045         enum { kDebug = ZCONFIG_Debug_TSoup_Debug };
00046 
00047         enum EChange { eChange_Local, eChange_Remote };
00048 
00049         typedef void (*Callback_UpdateNeeded_t)(void* iRefcon);
00050         typedef void (*Callback_SyncNeeded_t)(void* iRefcon);
00051 
00052         ZTSoup(ZRef<ZTSWatcher> iTSWatcher);
00053         virtual ~ZTSoup();
00054 
00055         virtual void ChangedRemote(const set<ZRef<ZTSieve> >& iTSieves,
00056                 const set<ZRef<ZTCrouton> >& iTCroutons);
00057 
00058         virtual void Changed(const set<ZRef<ZTSieve> >& iTSieves);
00059 
00060         virtual void Changed(const set<ZRef<ZTCrouton> >& iTCroutons, EChange iChange);
00061 
00062         virtual void Changed(const ZRef<ZTSieve>& iTSieve);
00063 
00064         virtual void Changed(const ZRef<ZTCrouton>& iTCrouton, EChange iChange);
00065 
00066         void SetCallback_Update(
00067                 Callback_UpdateNeeded_t iCallback_UpdateNeeded, void* iRefcon_UpdateNeeded);
00068 
00069         void SetCallback_Sync(
00070                 Callback_SyncNeeded_t iCallback_SyncNeeded, void* iRefcon_SyncNeeded);
00071 
00072         bool AllocateIDs(size_t iCount, uint64& oBaseID, size_t& oCountIssued);
00073 
00074         uint64 AllocateID();
00075 
00076         void Set(uint64 iID, const ZTuple& iTuple);
00077 
00078         uint64 Add(const ZTuple& iTuple);
00079 
00080         void Register(ZRef<ZTSieve> iTSieve, const ZTBQuery& iTBQuery);
00081         void Register(ZRef<ZTSieve> iTSieve, const ZTBQuery& iTBQuery, bool iPrefetch);
00082         void Register(ZRef<ZTCrouton> iTCrouton, uint64 iID);
00083         void Register(ZRef<ZTCrouton> iTCrouton);
00084 
00085         bool Sync();
00086         void Update();
00087         void Purge();
00088 
00089 private:
00090         static void pCallback_TSWatcher(void* iRefcon);
00091 
00092         friend class ZTSieve;
00093         friend class ZTCrouton;
00094 
00095         class DLink_PSieve_Update;
00096         class DLink_PSieve_Sync;
00097         class DLink_PSieve_Changed;
00098         class PSieve;
00099 
00100         class DLink_PCrouton_Update;
00101         class DLink_PCrouton_Sync;
00102         class DLink_PCrouton_Changed;
00103         class DLink_PCrouton_Syncing;
00104         class DLink_PCrouton_Pending;
00105         class PCrouton;
00106 
00107         void pDisposingTSieve(ZTSieve* iTSieve);
00108         void pDisposingTCrouton(ZTCrouton* iTCrouton);
00109 
00110         bool pHasCurrent(PSieve* iPSieve);
00111         bool pHasPrior(PSieve* iPSieve);
00112 
00113         const std::vector<uint64>& pSieveGetCurrent(PSieve* iPSieve);
00114         bool pSieveGetCurrent(PSieve* iPSieve, std::vector<uint64>& oCurrent);
00115         bool pSieveGetCurrent(PSieve* iPSieve, std::set<uint64>& oCurrent);
00116 
00117         const std::vector<uint64>& pSieveGetPrior(PSieve* iPSieve);
00118         bool pSieveGetPrior(PSieve* iPSieve, std::vector<uint64>& oPrior);
00119         bool pSieveGetPrior(PSieve* iPSieve, std::set<uint64>& oPrior);
00120 
00121         void pSieveGetAdded(PSieve* iPSieve, std::set<uint64>& oAdded);
00122         void pSieveGetRemoved(PSieve* iPSieve, std::set<uint64>& oRemoved);
00123 
00124         void pCheckSieveDiffs(PSieve* iPSieve);
00125 
00126         bool pHasCurrent(PCrouton* iPCrouton);
00127         bool pHasPrior(PCrouton* iPCrouton);
00128 
00129         bool pCroutonGetCurrent(PCrouton* iPCrouton, ZTuple& oTuple);
00130         bool pCroutonGetPrior(PCrouton* iPCrouton, ZTuple& oTuple);
00131 
00132         PCrouton* pGetPCrouton(uint64 iID);
00133 
00134         void pSetCroutonFromTCrouton(PCrouton* iPCrouton, const ZTuple& iTuple);
00135         void pSet(ZMutexLocker& iLocker_Structure, PCrouton* iPCrouton, const ZTuple& iTuple);  
00136 
00137         void pTriggerUpdate();
00138         void pTriggerSync();
00139 
00140         ZRef<ZTSWatcher> fTSWatcher;
00141 
00142         ZMutex fMutex_CallSync;
00143         ZMutex fMutex_CallUpdate;
00144         ZMutex fMutex_Structure;
00145         ZMutex fMutex_TSWatcher;
00146 
00147         bool fCalled_UpdateNeeded;
00148         Callback_UpdateNeeded_t fCallback_UpdateNeeded;
00149         void* fRefcon_UpdateNeeded;
00150         
00151         bool fCalled_SyncNeeded;
00152         Callback_SyncNeeded_t fCallback_SyncNeeded;
00153         void* fRefcon_SyncNeeded;
00154 
00155         std::map<ZTBQuery, PSieve> fTBQuery_To_PSieve;
00156 
00157         ZooLib::DListHead<DLink_PSieve_Update> fPSieves_Update;
00158         ZooLib::DListHead<DLink_PSieve_Sync> fPSieves_Sync;
00159         ZooLib::DListHead<DLink_PSieve_Changed> fPSieves_Changed;
00160 
00161         std::map<uint64, PCrouton> fID_To_PCrouton;
00162 
00163         ZooLib::DListHead<DLink_PCrouton_Update> fPCroutons_Update;
00164         ZooLib::DListHead<DLink_PCrouton_Sync> fPCroutons_Sync;
00165         ZooLib::DListHead<DLink_PCrouton_Changed> fPCroutons_Changed;
00166         ZooLib::DListHead<DLink_PCrouton_Syncing> fPCroutons_Syncing;
00167         ZooLib::DListHead<DLink_PCrouton_Pending> fPCroutons_Pending;
00168         };
00169 
00170 // =================================================================================================
00171 #pragma mark -
00172 #pragma mark * ZTSieve
00173 
00174 class DLink_ZTSieve_Using : public ZooLib::DListLink<ZTSieve, DLink_ZTSieve_Using>
00175         {};
00176 
00177 class ZTSieve : public ZRefCounted, public DLink_ZTSieve_Using
00178         {
00179 public:
00180         ZTSieve();
00181         virtual ~ZTSieve();
00182 
00183         virtual void Changed();
00184 
00185         bool HasCurrent();
00186         bool HasPrior();
00187 
00188         const std::vector<uint64>& GetCurrent();
00189         bool GetCurrent(std::vector<uint64>& oCurrent);
00190         bool GetCurrent(std::set<uint64>& oCurrent);
00191 
00192         const std::vector<uint64>& GetPrior();
00193         bool GetPrior(std::vector<uint64>& oPrior);
00194         bool GetPrior(std::set<uint64>& oPrior);
00195 
00196         void GetAdded(std::set<uint64>& oAdded);
00197         void GetRemoved(std::set<uint64>& oRemoved);
00198 
00199         ZRef<ZTSoup> GetTSoup();
00200 
00201         ZTBQuery GetTBQuery();
00202 
00203 private:
00204         friend class ZTSoup;
00205         friend class ZTSoup::PSieve;
00206 
00207         ZTSoup::PSieve* fPSieve;
00208         };
00209 
00210 // =================================================================================================
00211 #pragma mark -
00212 #pragma mark * ZTCrouton
00213 
00214 class DLink_ZTCrouton_Using : public ZooLib::DListLink<ZTCrouton, DLink_ZTCrouton_Using>
00215         {};
00216 
00217 class ZTCrouton : public ZRefCounted, public DLink_ZTCrouton_Using
00218         {
00219 public:
00220         typedef ZTSoup::EChange EChange;
00221         static const EChange eChange_Local = ZTSoup::eChange_Local;
00222         static const EChange eChange_Remote = ZTSoup::eChange_Remote;
00223 
00224         ZTCrouton();
00225         virtual ~ZTCrouton();
00226 
00227         virtual void Changed(EChange iChange);
00228 
00229         uint64 GetID();
00230 
00231         bool HasCurrent();
00232         bool HasPrior();
00233         
00234         bool GetCurrent(ZTuple& oTuple);
00235         bool GetPrior(ZTuple& oTuple);
00236 
00237         ZTuple GetCurrent();
00238         ZTuple GetPrior();
00239 
00240         void Set(const ZTuple& iTuple);
00241 
00242         ZRef<ZTSoup> GetTSoup();
00243 
00244 private:
00245         friend class ZTSoup;
00246         friend class ZTSoup::PCrouton;
00247 
00248         ZTSoup::PCrouton* fPCrouton;
00249         };
00250 
00251 // =================================================================================================
00252 #pragma mark -
00253 #pragma mark * ZTCrouton_Bowl
00254 
00255 class ZTBowl;
00256 
00257 class ZTCrouton_Bowl : public ZTCrouton
00258         {
00259 public:
00260         ZTCrouton_Bowl();
00261         virtual void Changed(EChange iChange);
00262 
00263         ZRef<ZTBowl> GetBowl() const;
00264 
00265 private:
00266         friend class ZTBowl;
00267         ZTBowl* fTBowl;
00268         };
00269 
00270 // =================================================================================================
00271 #pragma mark -
00272 #pragma mark * ZTBowl
00273 
00274 class ZTBowl : public ZTSieve
00275         {
00276 public:
00277         ZTBowl();
00278         virtual ~ZTBowl();
00279 
00280 // From ZTSieve
00281         virtual void Changed();
00282 
00283 // Our protocol
00284         virtual ZRef<ZTCrouton_Bowl> MakeCrouton();
00285         virtual void CroutonChanged(ZRef<ZTCrouton> iCrouton, ZTCrouton::EChange iChange);
00286 
00287         const std::vector<ZRef<ZTCrouton> >& GetCroutons();
00288 
00289 private:
00290         std::vector<ZRef<ZTCrouton> > fTCroutons;
00291         };
00292 
00293 // =================================================================================================
00294 #pragma mark -
00295 #pragma mark * ZTBowl_T
00296 
00297 struct ZTBowlBogusInitializer_t;
00298 
00299 template <class Crouton_t, typename Initalizer_t = ZTBowlBogusInitializer_t>
00300 class ZTBowl_T : public ZTBowl
00301         {
00302 public:
00303         ZTBowl_T(Initalizer_t iInitializer)
00304         :       fInitializer(iInitializer)
00305                 {}
00306 
00307 // From ZTBowl
00308         virtual ZRef<ZTCrouton_Bowl> MakeCrouton()
00309                 { return new Crouton_t(fInitializer); }
00310 
00311 private:
00312         Initalizer_t fInitializer;
00313         };
00314 
00315 // For use with croutons that do not take an initializer.
00316 template <class Crouton_t>
00317 class ZTBowl_T<Crouton_t, ZTBowlBogusInitializer_t> : public ZTBowl
00318         {
00319 public:
00320         virtual ZRef<ZTCrouton_Bowl> MakeCrouton()
00321                 { return new Crouton_t; }
00322         };
00323 
00324 #endif // __ZSoup__

Generated on Thu Jul 26 11:21:59 2007 for ZooLib by  doxygen 1.4.7