Back to index

lightning-sunbird  0.9+nobinonly
morkHandle.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 _MORKHANDLE_
00039 #define _MORKHANDLE_ 1
00040 
00041 #ifndef _MORK_
00042 #include "mork.h"
00043 #endif
00044 
00045 #ifndef _MORKNODE_
00046 #include "morkNode.h"
00047 #endif
00048 
00049 #ifndef _MORKDEQUE_
00050 #include "morkDeque.h"
00051 #endif
00052 
00053 #ifndef _MORKPOOL_
00054 #include "morkPool.h"
00055 #endif
00056 
00057 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00058 
00059 class morkPool;
00060 class morkObject;
00061 class morkFactory;
00062 
00063 #define morkDerived_kHandle     /*i*/ 0x486E /* ascii 'Hn' */
00064 #define morkHandle_kTag   0x68416E44 /* ascii 'hAnD' */
00065 
00066 /*| morkHandle: 
00067 |*/
00068 class morkHandle : public morkNode {
00069   
00070 // public: // slots inherited from morkNode (meant to inform only)
00071   // nsIMdbHeap*    mNode_Heap;
00072 
00073   // mork_base      mNode_Base;     // must equal morkBase_kNode
00074   // mork_derived   mNode_Derived;  // depends on specific node subclass
00075   
00076   // mork_access    mNode_Access;   // kOpen, kClosing, kShut, or kDead
00077   // mork_usage     mNode_Usage;    // kHeap, kStack, kMember, kGlobal, kNone
00078   // mork_able      mNode_Mutable;  // can this node be modified?
00079   // mork_load      mNode_Load;     // is this node clean or dirty?
00080   
00081   // mork_uses      mNode_Uses;     // refcount for strong refs
00082   // mork_refs      mNode_Refs;     // refcount for strong refs + weak refs
00083 
00084 public: // state is public because the entire Mork system is private
00085 
00086   mork_u4         mHandle_Tag;     // must equal morkHandle_kTag
00087   morkEnv*        mHandle_Env;     // pool that allocated this handle
00088   morkHandleFace* mHandle_Face;    // cookie from pool containing this
00089   morkObject*     mHandle_Object;  // object this handle wraps for MDB API
00090   mork_magic      mHandle_Magic;   // magic sig different in each subclass
00091   
00092 // { ===== begin morkNode interface =====
00093 public: // morkNode virtual methods
00094   virtual void CloseMorkNode(morkEnv* ev); // CloseHandle() only if open
00095   virtual ~morkHandle(); // assert that CloseHandle() executed earlier
00096   
00097 public: // morkHandle construction & destruction
00098   morkHandle(morkEnv* ev, // note morkUsage is always morkUsage_kPool
00099     morkHandleFace* ioFace,  // must not be nil, cookie for this handle
00100     morkObject* ioObject,    // must not be nil, the object for this handle
00101     mork_magic inMagic);     // magic sig to denote specific subclass
00102   void CloseHandle(morkEnv* ev); // called by CloseMorkNode();
00103 
00104 private: // copying is not allowed
00105   morkHandle(const morkHandle& other);
00106   morkHandle& operator=(const morkHandle& other);
00107   
00108 protected: // special case empty construction for morkHandleFrame
00109   friend class morkHandleFrame;
00110   morkHandle() { }
00111 
00112 public: // dynamic type identification
00113   mork_bool IsHandle() const
00114   { return IsNode() && mNode_Derived == morkDerived_kHandle; }
00115 // } ===== end morkNode methods =====
00116 
00117 public: // morkHandle memory management operators
00118   void* operator new(size_t inSize, morkPool& ioPool, morkZone& ioZone, morkEnv* ev) CPP_THROW_NEW
00119   { return ioPool.NewHandle(ev, inSize, &ioZone); }
00120   
00121   void* operator new(size_t inSize, morkPool& ioPool, morkEnv* ev) CPP_THROW_NEW
00122   { return ioPool.NewHandle(ev, inSize, (morkZone*) 0); }
00123   
00124   void* operator new(size_t inSize, morkHandleFace* ioFace) CPP_THROW_NEW
00125   { MORK_USED_1(inSize); return ioFace; }
00126   
00127 
00128 public: // other handle methods
00129   mork_bool GoodHandleTag() const
00130   { return mHandle_Tag == morkHandle_kTag; }
00131   
00132   void NewBadMagicHandleError(morkEnv* ev, mork_magic inMagic) const;
00133   void NewDownHandleError(morkEnv* ev) const;
00134   void NilFactoryError(morkEnv* ev) const;
00135   void NilHandleObjectError(morkEnv* ev) const;
00136   void NonNodeObjectError(morkEnv* ev) const;
00137   void NonOpenObjectError(morkEnv* ev) const;
00138   
00139   morkObject* GetGoodHandleObject(morkEnv* ev,
00140     mork_bool inMutable, mork_magic inMagicType, mork_bool inClosedOkay) const;
00141 
00142 public: // interface supporting mdbObject methods
00143 
00144   morkEnv* CanUseHandle(nsIMdbEnv* mev, mork_bool inMutable,
00145     mork_bool inClosedOkay, mdb_err* outErr) const;
00146     
00147   // { ----- begin mdbObject style methods -----
00148   mdb_err Handle_IsFrozenMdbObject(nsIMdbEnv* ev, mdb_bool* outIsReadonly);
00149 
00150   mdb_err Handle_GetMdbFactory(nsIMdbEnv* ev, nsIMdbFactory** acqFactory); 
00151   mdb_err Handle_GetWeakRefCount(nsIMdbEnv* ev,  mdb_count* outCount);  
00152   mdb_err Handle_GetStrongRefCount(nsIMdbEnv* ev, mdb_count* outCount);
00153 
00154   mdb_err Handle_AddWeakRef(nsIMdbEnv* ev);
00155   mdb_err Handle_AddStrongRef(nsIMdbEnv* ev);
00156 
00157   mdb_err Handle_CutWeakRef(nsIMdbEnv* ev);
00158   mdb_err Handle_CutStrongRef(nsIMdbEnv* ev);
00159   
00160   mdb_err Handle_CloseMdbObject(nsIMdbEnv* ev);
00161   mdb_err Handle_IsOpenMdbObject(nsIMdbEnv* ev, mdb_bool* outOpen);
00162   // } ----- end mdbObject style methods -----
00163 
00164 public: // typesafe refcounting inlines calling inherited morkNode methods
00165   static void SlotWeakHandle(morkHandle* me,
00166     morkEnv* ev, morkHandle** ioSlot)
00167   { morkNode::SlotWeakNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00168   
00169   static void SlotStrongHandle(morkHandle* me,
00170     morkEnv* ev, morkHandle** ioSlot)
00171   { morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00172 };
00173 
00174 #define morkHandleFrame_kPadSlotCount 4
00175 
00176 /*| morkHandleFrame: an object format used for allocating and maintaining
00177 **| instances of morkHandle, with leading slots used to maintain this in a
00178 **| linked list, and following slots to provide extra footprint that might
00179 **| be needed by any morkHandle subclasses that include very little extra
00180 **| space (by virtue of the fact that each morkHandle subclass is expected
00181 **| to multiply inherit from another base class that has only abstact methods
00182 **| for space overhead related only to some vtable representation).
00183 |*/
00184 class morkHandleFrame {
00185 public:
00186   morkLink    mHandleFrame_Link;    // list storage without trampling Handle
00187   morkHandle  mHandleFrame_Handle;
00188   mork_ip     mHandleFrame_Padding[ morkHandleFrame_kPadSlotCount ];
00189   
00190 public:
00191   morkHandle*  AsHandle() { return &mHandleFrame_Handle; }
00192   
00193   morkHandleFrame() {}  // actually, morkHandleFrame never gets constructed
00194 
00195 private: // copying is not allowed
00196   morkHandleFrame(const morkHandleFrame& other);
00197   morkHandleFrame& operator=(const morkHandleFrame& other);
00198 };
00199 
00200 #define morkHandleFrame_kHandleOffset \
00201   mork_OffsetOf(morkHandleFrame,mHandleFrame_Handle)
00202   
00203 #define morkHandle_AsHandleFrame(h) ((h)->mHandle_Block , \
00204  ((morkHandleFrame*) (((mork_u1*)(h)) - morkHandleFrame_kHandleOffset)))
00205 
00206 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00207 
00208 #endif /* _MORKHANDLE_ */