Back to index

lightning-sunbird  0.9+nobinonly
orkinEnv.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-  */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef _MDB_
00039 #include "mdb.h"
00040 #endif
00041 
00042 #ifndef _MORK_
00043 #include "mork.h"
00044 #endif
00045 
00046 #ifndef _MORKNODE_
00047 #include "morkNode.h"
00048 #endif
00049 
00050 #ifndef _MORKHANDLE_
00051 #include "morkHandle.h"
00052 #endif
00053 
00054 #ifndef _MORKENV_
00055 #include "morkEnv.h"
00056 #endif
00057 
00058 #ifndef _ORKINENV_
00059 #include "orkinEnv.h"
00060 #endif
00061 
00062 #ifndef _ORKINHEAP_
00063 #include "orkinHeap.h"
00064 #endif
00065 
00066 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00067 
00068 /* public virtual*/
00069 orkinEnv:: ~orkinEnv() // morkHandle destructor does everything
00070 {
00071 }
00072 
00073 void orkinEnv::CloseMorkNode(morkEnv* ev) // override to clean up mork env
00074 {
00075   morkEnv* mev = (morkEnv*) this->mHandle_Object;
00076 
00077   if ( mev->IsOpenNode() )
00078   {
00079     mev->MarkClosing();
00080     mev->CloseEnv(ev);
00081     mev->MarkShut();
00082   }
00083   morkHandle::CloseMorkNode(ev);
00084 }
00085 
00086 /*protected non-poly construction*/
00087 orkinEnv::orkinEnv(morkEnv* ev, // morkUsage is morkUsage_kPool
00088     morkHandleFace* ioFace,    // must not be nil, cookie for this handle
00089     morkEnv* ioObject)  // must not be nil, the object for this handle
00090 : morkHandle(ev, ioFace, ioObject, morkMagic_kEnv)
00091 {
00092   // do not modify mNode_Derived; leave it equal to morkDerived_kHandle
00093 }
00094 
00095 
00096 /*static */ orkinEnv*
00097 orkinEnv::MakeEnv(morkEnv* ev, morkEnv* ioObject)
00098 {
00099   mork_bool isEnv = ev->IsEnv();
00100   MORK_ASSERT(isEnv);
00101   if ( isEnv )
00102   {
00103     morkHandleFace* face = ev->NewHandle(sizeof(orkinEnv));
00104     if ( face )
00105       return new(face) orkinEnv(ev, face, ioObject);
00106     else
00107       ev->OutOfMemoryError();
00108   }
00109     
00110   return (orkinEnv*) 0;
00111 }
00112 
00113 morkEnv*
00114 orkinEnv::CanUseEnv(mork_bool inMutable, mdb_err* outErr) const
00115 {
00116   MORK_USED_1(inMutable);
00117   morkEnv* outEnv = 0;
00118   mdb_err err = morkEnv_kBadEnvError;
00119   if ( this->IsHandle() )
00120   {
00121     if ( this->IsOpenNode() )
00122     {
00123       morkEnv* ev = (morkEnv*) this->mHandle_Object;
00124       if ( ev && ev->IsEnv() )
00125       {
00126         outEnv = ev;
00127         err = 0;
00128       }
00129       else
00130       {
00131         err = morkEnv_kNonEnvTypeError;
00132         MORK_ASSERT(outEnv);
00133       }
00134     }
00135     else
00136     {
00137       err = morkEnv_kNonOpenNodeError;
00138       MORK_ASSERT(outEnv);
00139     }
00140   }
00141   else
00142   {
00143     err = morkEnv_kNonHandleTypeError;
00144     MORK_ASSERT(outEnv);
00145   }
00146   *outErr = err;
00147   return outEnv;
00148 }
00149 
00150 
00151 // { ===== begin nsIMdbISupports methods =====
00152 NS_IMPL_QUERY_INTERFACE0(orkinEnv)
00153 
00154 /*virtual*/ nsrefcnt
00155 orkinEnv::AddRef() // add strong ref with no
00156 {
00157   morkEnv* ev = mHandle_Env;
00158   if ( ev && ev->IsEnv() )
00159     return this->Handle_AddStrongRef(ev->AsMdbEnv());
00160   else
00161     return morkEnv_kNonEnvTypeError;
00162 }
00163 
00164 /*virtual*/ nsrefcnt
00165 orkinEnv::Release() // cut strong ref
00166 {
00167   morkEnv* ev = mHandle_Env;
00168   if ( ev && ev->IsEnv() )
00169     return this->Handle_CutStrongRef(ev->AsMdbEnv());
00170   else
00171     return morkEnv_kNonEnvTypeError;
00172 }
00173 // } ===== end nsIMdbObject methods =====
00174 
00175 // { ===== begin nsIMdbObject methods =====
00176 
00177 // { ----- begin attribute methods -----
00178 /*virtual*/ mdb_err
00179 orkinEnv::IsFrozenMdbObject(nsIMdbEnv* mev, mdb_bool* outIsReadonly)
00180 {
00181   return this->Handle_IsFrozenMdbObject(mev, outIsReadonly);
00182 }
00183 // same as nsIMdbPort::GetIsPortReadonly() when this object is inside a port.
00184 // } ----- end attribute methods -----
00185 
00186 // { ----- begin factory methods -----
00187 /*virtual*/ mdb_err
00188 orkinEnv::GetMdbFactory(nsIMdbEnv* mev, nsIMdbFactory** acqFactory)
00189 {
00190   return this->Handle_GetMdbFactory(mev, acqFactory);
00191 } 
00192 // } ----- end factory methods -----
00193 
00194 // { ----- begin ref counting for well-behaved cyclic graphs -----
00195 /*virtual*/ mdb_err
00196 orkinEnv::GetWeakRefCount(nsIMdbEnv* mev, // weak refs
00197   mdb_count* outCount)
00198 {
00199   return this->Handle_GetWeakRefCount(mev, outCount);
00200 }  
00201 /*virtual*/ mdb_err
00202 orkinEnv::GetStrongRefCount(nsIMdbEnv* mev, // strong refs
00203   mdb_count* outCount)
00204 {
00205   return this->Handle_GetStrongRefCount(mev, outCount);
00206 }
00207 
00208 /*virtual*/ mdb_err
00209 orkinEnv::AddWeakRef(nsIMdbEnv* mev)
00210 {
00211   return this->Handle_AddWeakRef(mev);
00212 }
00213 /*virtual*/ mdb_err
00214 orkinEnv::AddStrongRef(nsIMdbEnv* mev)
00215 {
00216   return this->Handle_AddStrongRef(mev);
00217 }
00218 
00219 /*virtual*/ mdb_err
00220 orkinEnv::CutWeakRef(nsIMdbEnv* mev)
00221 {
00222   return this->Handle_CutWeakRef(mev);
00223 }
00224 /*virtual*/ mdb_err
00225 orkinEnv::CutStrongRef(nsIMdbEnv* mev)
00226 {
00227   return this->Handle_CutStrongRef(mev);
00228 }
00229 
00230 /*virtual*/ mdb_err
00231 orkinEnv::CloseMdbObject(nsIMdbEnv* mev)
00232 {
00233   morkEnv* ev = (morkEnv*) this->mHandle_Object;
00234   mdb_err ret = this->Handle_CloseMdbObject(mev);
00235   if (ev && ev->mEnv_Heap)
00236   {
00237     mork_bool ownsHeap = ev->mEnv_OwnsHeap;
00238     nsIMdbHeap*saveHeap = ev->mEnv_Heap;
00239 
00240     ev->mEnv_Heap->Free(this, ev);
00241     if (ownsHeap)
00242     {
00243 #ifdef MORK_DEBUG_HEAP_STATS
00244       printf("%d blocks remaining \n", ((orkinHeap *) saveHeap)->HeapBlockCount());
00245 #endif // MORK_DEBUG_HEAP_STATS
00246       delete saveHeap;
00247     }
00248 
00249   }
00250   return ret;
00251 }
00252 
00253 /*virtual*/ mdb_err
00254 orkinEnv::IsOpenMdbObject(nsIMdbEnv* mev, mdb_bool* outOpen)
00255 {
00256   return this->Handle_IsOpenMdbObject(mev, outOpen);
00257 }
00258 // } ----- end ref counting -----
00259 
00260 // } ===== end nsIMdbObject methods =====
00261 
00262 // { ===== begin nsIMdbEnv methods =====
00263 
00264 // { ----- begin attribute methods -----
00265 /*virtual*/ mdb_err
00266 orkinEnv::GetErrorCount(mdb_count* outCount,
00267   mdb_bool* outShouldAbort)
00268 {
00269   mdb_err outErr = 0;
00270   mdb_count count = 1;
00271   mork_bool shouldAbort = morkBool_kFalse;
00272   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kFalse, &outErr);
00273   if ( ev )
00274   {
00275     count = (mdb_count) ev->mEnv_ErrorCount;
00276     shouldAbort = ev->mEnv_ShouldAbort;
00277   }
00278   if ( outCount )
00279     *outCount = count;
00280   if ( outShouldAbort )
00281     *outShouldAbort = shouldAbort;
00282   return outErr;
00283 }
00284 
00285 /*virtual*/ mdb_err
00286 orkinEnv::GetWarningCount(mdb_count* outCount,
00287   mdb_bool* outShouldAbort)
00288 {
00289   mdb_err outErr = 0;
00290   mdb_count count = 1;
00291   mork_bool shouldAbort = morkBool_kFalse;
00292   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kFalse, &outErr);
00293   if ( ev )
00294   {
00295     count = (mdb_count) ev->mEnv_WarningCount;
00296     shouldAbort = ev->mEnv_ShouldAbort;
00297   }
00298   if ( outCount )
00299     *outCount = count;
00300   if ( outShouldAbort )
00301     *outShouldAbort = shouldAbort;
00302   return outErr;
00303 }
00304 
00305 /*virtual*/ mdb_err
00306 orkinEnv::GetEnvBeVerbose(mdb_bool* outBeVerbose)
00307 {
00308   mdb_err outErr = 0;
00309   mork_bool beVerbose = morkBool_kFalse;
00310   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kFalse, &outErr);
00311   if ( ev )
00312   {
00313     beVerbose = ev->mEnv_BeVerbose;
00314   }
00315   if ( outBeVerbose )
00316     *outBeVerbose = beVerbose;
00317   return outErr;
00318 }
00319 
00320 /*virtual*/ mdb_err
00321 orkinEnv::SetEnvBeVerbose(mdb_bool inBeVerbose)
00322 {
00323   mdb_err outErr = 0;
00324   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kTrue, &outErr);
00325   if ( ev )
00326   {
00327     ev->mEnv_BeVerbose = inBeVerbose;
00328   }
00329   return outErr;
00330 }
00331 
00332 /*virtual*/ mdb_err
00333 orkinEnv::GetDoTrace(mdb_bool* outDoTrace)
00334 {
00335   mdb_err outErr = 0;
00336   mork_bool doTrace = morkBool_kFalse;
00337   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kFalse, &outErr);
00338   if ( ev )
00339   {
00340     doTrace = ev->mEnv_DoTrace;
00341   }
00342   if ( outDoTrace )
00343     *outDoTrace = doTrace;
00344   return outErr;
00345 }
00346 
00347 /*virtual*/ mdb_err
00348 orkinEnv::SetDoTrace(mdb_bool inDoTrace)
00349 {
00350   mdb_err outErr = 0;
00351   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kTrue, &outErr);
00352   if ( ev )
00353   {
00354     ev->mEnv_DoTrace = inDoTrace;
00355   }
00356   return outErr;
00357 }
00358 
00359 /*virtual*/ mdb_err
00360 orkinEnv::GetAutoClear(mdb_bool* outAutoClear)
00361 {
00362   mdb_err outErr = 0;
00363   mork_bool autoClear = morkBool_kFalse;
00364   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kFalse, &outErr);
00365   if ( ev )
00366   {
00367     autoClear = ev->DoAutoClear();
00368   }
00369   if ( outAutoClear )
00370     *outAutoClear = autoClear;
00371   return outErr;
00372 }
00373 
00374 /*virtual*/ mdb_err
00375 orkinEnv::SetAutoClear(mdb_bool inAutoClear)
00376 {
00377   mdb_err outErr = 0;
00378   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kTrue, &outErr);
00379   if ( ev )
00380   {
00381     if ( inAutoClear )
00382       ev->EnableAutoClear();
00383     else
00384       ev->DisableAutoClear();
00385   }
00386   return outErr;
00387 }
00388 
00389 /*virtual*/ mdb_err
00390 orkinEnv::GetErrorHook(nsIMdbErrorHook** acqErrorHook)
00391 {
00392   mdb_err outErr = 0;
00393   nsIMdbErrorHook* outErrorHook = 0;
00394   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kFalse, &outErr);
00395   if ( ev )
00396   {
00397     outErrorHook = ev->mEnv_ErrorHook;
00398   }
00399   if ( acqErrorHook )
00400     *acqErrorHook = outErrorHook;
00401   return outErr;
00402 }
00403 
00404 /*virtual*/ mdb_err
00405 orkinEnv::SetErrorHook(
00406   nsIMdbErrorHook* ioErrorHook) // becomes referenced
00407 {
00408   mdb_err outErr = 0;
00409   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kTrue, &outErr);
00410   if ( ev )
00411   {
00412     ev->mEnv_ErrorHook = ioErrorHook;
00413   }
00414   return outErr;
00415 }
00416 
00417 /*virtual*/ mdb_err
00418 orkinEnv::GetHeap(nsIMdbHeap** acqHeap)
00419 {
00420   mdb_err outErr = 0;
00421   nsIMdbHeap* outHeap = 0;
00422   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kFalse, &outErr);
00423   if ( ev )
00424   {
00425     nsIMdbHeap* heap = ev->mEnv_Heap;
00426     if ( heap && heap->HeapAddStrongRef(this) == 0 )
00427       outHeap = heap;
00428   }
00429   if ( acqHeap )
00430     *acqHeap = outHeap;
00431   return outErr;
00432 }
00433 
00434 /*virtual*/ mdb_err
00435 orkinEnv::SetHeap(
00436   nsIMdbHeap* ioHeap) // becomes referenced
00437 {
00438   mdb_err outErr = 0;
00439   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kTrue, &outErr);
00440   if ( ev )
00441   {
00442     nsIMdbHeap_SlotStrongHeap(ioHeap, ev, &ev->mEnv_Heap);
00443   }
00444   return outErr;
00445 }
00446 // } ----- end attribute methods -----
00447 
00448 /*virtual*/ mdb_err
00449 orkinEnv::ClearErrors() // clear errors beore re-entering db API
00450 {
00451   mdb_err outErr = 0;
00452   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kTrue, &outErr);
00453   if ( ev )
00454   {
00455     ev->mEnv_ErrorCount = 0;
00456     ev->mEnv_ErrorCode = 0;
00457     ev->mEnv_ShouldAbort = morkBool_kFalse;
00458   }
00459   return outErr;
00460 }
00461 
00462 /*virtual*/ mdb_err
00463 orkinEnv::ClearWarnings() // clear warning
00464 {
00465   mdb_err outErr = 0;
00466   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kTrue, &outErr);
00467   if ( ev )
00468   {
00469     ev->mEnv_WarningCount = 0;
00470   }
00471   return outErr;
00472 }
00473 
00474 /*virtual*/ mdb_err
00475 orkinEnv::ClearErrorsAndWarnings() // clear both errors & warnings
00476 {
00477   mdb_err outErr = 0;
00478   morkEnv* ev = this->CanUseEnv(/*inMutable*/ morkBool_kTrue, &outErr);
00479   if ( ev )
00480   {
00481     ev->ClearMorkErrorsAndWarnings();
00482   }
00483   return outErr;
00484 }
00485 // } ===== end nsIMdbEnv methods =====
00486 
00487 
00488 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789