Back to index

lightning-sunbird  0.9+nobinonly
morkEnv.h
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 _MORKENV_
00039 #define _MORKENV_ 1
00040 
00041 #ifndef _MORK_
00042 #include "mork.h"
00043 #endif
00044 
00045 #ifndef _MORKOBJECT_
00046 #include "morkObject.h"
00047 #endif
00048 
00049 #ifndef _MORKPOOL_
00050 #include "morkPool.h"
00051 #endif
00052 
00053 // sean was here
00054 #include "nsError.h"
00055 
00056 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00057 
00058 #define morkDerived_kEnv     /*i*/ 0x4576 /* ascii 'Ev' */
00059 
00060 // use NS error codes to make Mork easier to use with the rest of mozilla 
00061 #define morkEnv_kNoError         NS_SUCCEEDED /* no error has happened */
00062 #define morkEnv_kGenericError    NS_ERROR_FAILURE /* non-specific error code */
00063 #define morkEnv_kNonEnvTypeError NS_ERROR_FAILURE /* morkEnv::IsEnv() is false */
00064 
00065 #define morkEnv_kStubMethodOnlyError NS_ERROR_NO_INTERFACE
00066 #define morkEnv_kOutOfMemoryError    NS_ERROR_OUT_OF_MEMORY
00067 #define morkEnv_kNilPointerError     NS_ERROR_NULL_POINTER
00068 #define morkEnv_kNewNonEnvError      NS_ERROR_FAILURE 
00069 #define morkEnv_kNilEnvSlotError     NS_ERROR_FAILURE
00070 
00071 #define morkEnv_kBadFactoryError     NS_ERROR_FACTORY_NOT_LOADED
00072 #define morkEnv_kBadFactoryEnvError  NS_ERROR_FACTORY_NOT_LOADED
00073 #define morkEnv_kBadEnvError         NS_ERROR_FAILURE
00074 
00075 #define morkEnv_kNonHandleTypeError  NS_ERROR_FAILURE
00076 #define morkEnv_kNonOpenNodeError    NS_ERROR_FAILURE 
00077 
00078 
00079 #define morkEnv_kWeakRefCountEnvBonus 0 /* try NOT to leak all env instances */
00080 
00081 /*| morkEnv:
00082 |*/
00083 class morkEnv : public morkObject, public nsIMdbEnv {
00084   NS_DECL_ISUPPORTS_INHERITED
00085 
00086 // public: // slots inherited from morkObject (meant to inform only)
00087   // nsIMdbHeap*       mNode_Heap;
00088 
00089   // mork_base      mNode_Base;     // must equal morkBase_kNode
00090   // mork_derived   mNode_Derived;  // depends on specific node subclass
00091   
00092   // mork_access    mNode_Access;   // kOpen, kClosing, kShut, or kDead
00093   // mork_usage     mNode_Usage;    // kHeap, kStack, kMember, kGlobal, kNone
00094   // mork_able      mNode_Mutable;  // can this node be modified?
00095   // mork_load      mNode_Load;     // is this node clean or dirty?
00096   
00097   // mork_uses      mNode_Uses;     // refcount for strong refs
00098   // mork_refs      mNode_Refs;     // refcount for strong refs + weak refs
00099 
00100   // mork_color   mBead_Color;   // ID for this bead
00101   // morkHandle*  mObject_Handle;  // weak ref to handle for this object
00102 
00103 public: // state is public because the entire Mork system is private
00104   
00105   morkFactory*      mEnv_Factory;  // NON-refcounted factory
00106   nsIMdbHeap*       mEnv_Heap;     // NON-refcounted heap
00107 
00108   nsIMdbEnv*        mEnv_SelfAsMdbEnv;
00109   nsIMdbErrorHook*  mEnv_ErrorHook;
00110   
00111   morkPool*         mEnv_HandlePool; // pool for re-using handles
00112     
00113   mork_u2           mEnv_ErrorCount; 
00114   mork_u2           mEnv_WarningCount; 
00115   
00116   mork_u4           mEnv_ErrorCode; // simple basis for mdb_err style errors
00117   
00118   mork_bool         mEnv_DoTrace;
00119   mork_able         mEnv_AutoClear;
00120   mork_bool         mEnv_ShouldAbort;
00121   mork_bool         mEnv_BeVerbose;
00122   mork_bool         mEnv_OwnsHeap;
00123   
00124 // { ===== begin morkNode interface =====
00125 public: // morkNode virtual methods
00126   virtual void CloseMorkNode(morkEnv* ev); // CloseEnv() only if open
00127   virtual ~morkEnv(); // assert that CloseEnv() executed earlier
00128   
00129   // { ----- begin attribute methods -----
00130   NS_IMETHOD GetErrorCount(mdb_count* outCount,
00131     mdb_bool* outShouldAbort);
00132   NS_IMETHOD GetWarningCount(mdb_count* outCount,
00133     mdb_bool* outShouldAbort);
00134   
00135   NS_IMETHOD GetEnvBeVerbose(mdb_bool* outBeVerbose);
00136   NS_IMETHOD SetEnvBeVerbose(mdb_bool inBeVerbose);
00137   
00138   NS_IMETHOD GetDoTrace(mdb_bool* outDoTrace);
00139   NS_IMETHOD SetDoTrace(mdb_bool inDoTrace);
00140   
00141   NS_IMETHOD GetAutoClear(mdb_bool* outAutoClear);
00142   NS_IMETHOD SetAutoClear(mdb_bool inAutoClear);
00143   
00144   NS_IMETHOD GetErrorHook(nsIMdbErrorHook** acqErrorHook);
00145   NS_IMETHOD SetErrorHook(
00146     nsIMdbErrorHook* ioErrorHook); // becomes referenced
00147   
00148   NS_IMETHOD GetHeap(nsIMdbHeap** acqHeap);
00149   NS_IMETHOD SetHeap(
00150     nsIMdbHeap* ioHeap); // becomes referenced
00151   // } ----- end attribute methods -----
00152   
00153   NS_IMETHOD ClearErrors(); // clear errors beore re-entering db API
00154   NS_IMETHOD ClearWarnings(); // clear warnings
00155   NS_IMETHOD ClearErrorsAndWarnings(); // clear both errors & warnings
00156 // } ===== end nsIMdbEnv methods =====
00157 public: // morkEnv construction & destruction
00158   morkEnv(const morkUsage& inUsage, nsIMdbHeap* ioHeap,
00159     morkFactory* ioFactory, nsIMdbHeap* ioSlotHeap);
00160   morkEnv(morkEnv* ev, const morkUsage& inUsage, nsIMdbHeap* ioHeap, 
00161      nsIMdbEnv* inSelfAsMdbEnv, morkFactory* ioFactory,
00162      nsIMdbHeap* ioSlotHeap);
00163   void CloseEnv(morkEnv* ev); // called by CloseMorkNode();
00164 
00165 private: // copying is not allowed
00166   morkEnv(const morkEnv& other);
00167   morkEnv& operator=(const morkEnv& other);
00168 
00169 public: // dynamic type identification
00170   mork_bool IsEnv() const
00171   { return IsNode() && mNode_Derived == morkDerived_kEnv; }
00172 // } ===== end morkNode methods =====
00173 
00174 public: // utility env methods
00175 
00176   mork_u1 HexToByte(mork_ch inFirstHex, mork_ch inSecondHex);
00177 
00178   mork_size TokenAsHex(void* outBuf, mork_token inToken);
00179   // TokenAsHex() is the same as sprintf(outBuf, "%lX", (long) inToken);
00180  
00181   mork_size OidAsHex(void* outBuf, const mdbOid& inOid);
00182   // sprintf(buf, "%lX:^%lX", (long) inOid.mOid_Id, (long) inOid.mOid_Scope);
00183  
00184   char* CopyString(nsIMdbHeap* ioHeap, const char* inString);
00185   void  FreeString(nsIMdbHeap* ioHeap, char* ioString);
00186   void  StringToYarn(const char* inString, mdbYarn* outYarn);
00187 
00188 public: // other env methods
00189 
00190   morkHandleFace*  NewHandle(mork_size inSize)
00191   { return mEnv_HandlePool->NewHandle(this, inSize, (morkZone*) 0); }
00192   
00193   void ZapHandle(morkHandleFace* ioHandle)
00194   { mEnv_HandlePool->ZapHandle(this, ioHandle); }
00195 
00196   void EnableAutoClear() { mEnv_AutoClear = morkAble_kEnabled; }
00197   void DisableAutoClear() { mEnv_AutoClear = morkAble_kDisabled; }
00198   
00199   mork_bool DoAutoClear() const
00200   { return mEnv_AutoClear == morkAble_kEnabled; }
00201 
00202   void NewErrorAndCode(const char* inString, mork_u2 inCode);
00203   void NewError(const char* inString);
00204   void NewWarning(const char* inString);
00205 
00206   void ClearMorkErrorsAndWarnings(); // clear both errors & warnings
00207   void AutoClearMorkErrorsAndWarnings(); // clear if auto is enabled
00208   
00209   void StubMethodOnlyError();
00210   void OutOfMemoryError();
00211   void NilPointerError();
00212   void NilPointerWarning();
00213   void CantMakeWhenBadError();
00214   void NewNonEnvError();
00215   void NilEnvSlotError();
00216     
00217   void NonEnvTypeError(morkEnv* ev);
00218   
00219   // canonical env convenience methods to check for presence of errors:
00220   mork_bool Good() const { return ( mEnv_ErrorCount == 0 ); }
00221   mork_bool Bad() const { return ( mEnv_ErrorCount != 0 ); }
00222   
00223   nsIMdbEnv* AsMdbEnv() { return (nsIMdbEnv *) this; }
00224   static morkEnv* FromMdbEnv(nsIMdbEnv* ioEnv); // dynamic type checking
00225   
00226   mork_u4 ErrorCode() const { return mEnv_ErrorCode; }
00227   
00228   mdb_err AsErr() const { return (mdb_err) mEnv_ErrorCode; }
00229   //mdb_err AsErr() const { return (mdb_err) ( mEnv_ErrorCount != 0 ); }
00230 
00231 public: // typesafe refcounting inlines calling inherited morkNode methods
00232   static void SlotWeakEnv(morkEnv* me,
00233     morkEnv* ev, morkEnv** ioSlot)
00234   { morkNode::SlotWeakNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00235   
00236   static void SlotStrongEnv(morkEnv* me,
00237     morkEnv* ev, morkEnv** ioSlot)
00238   { morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00239 };
00240 
00241 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00242 
00243 #endif /* _MORKENV_ */