Back to index

lightning-sunbird  0.9+nobinonly
morkFactory.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 _MORKFACTORY_
00039 #define _MORKFACTORY_ 1
00040 
00041 #ifndef _MORK_
00042 #include "mork.h"
00043 #endif
00044 
00045 #ifndef _MORKENV_
00046 #include "morkEnv.h"
00047 #endif
00048 
00049 #ifndef _MORKOBJECT_
00050 #include "morkObject.h"
00051 #endif
00052 
00053 #ifndef _ORKINHEAP_
00054 #include "orkinHeap.h"
00055 #endif
00056 
00057 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00058 
00059 class nsIMdbFactory;
00060 
00061 #define morkDerived_kFactory  /*i*/ 0x4663 /* ascii 'Fc' */
00062 #define morkFactory_kWeakRefCountBonus 0 /* try NOT to leak all factories */
00063 
00064 /*| morkFactory: 
00065 |*/
00066 class morkFactory : public morkObject, public nsIMdbFactory { // nsIMdbObject
00067 
00068 // public: // slots inherited from morkObject (meant to inform only)
00069   // nsIMdbHeap*     mNode_Heap;
00070   // mork_able    mNode_Mutable; // can this node be modified?
00071   // mork_load    mNode_Load;    // is this node clean or dirty?
00072   // mork_base    mNode_Base;    // must equal morkBase_kNode
00073   // mork_derived mNode_Derived; // depends on specific node subclass
00074   // mork_access  mNode_Access;  // kOpen, kClosing, kShut, or kDead
00075   // mork_usage   mNode_Usage;   // kHeap, kStack, kMember, kGlobal, kNone
00076   // mork_uses    mNode_Uses;    // refcount for strong refs
00077   // mork_refs    mNode_Refs;    // refcount for strong refs + weak refs
00078 
00079   // mork_color   mBead_Color;   // ID for this bead
00080   // morkHandle*  mObject_Handle;  // weak ref to handle for this object
00081 
00082 public: // state is public because the entire Mork system is private
00083 
00084   morkEnv        mFactory_Env; // private env instance used internally
00085   orkinHeap      mFactory_Heap;
00086 
00087   NS_DECL_ISUPPORTS_INHERITED
00088 // { ===== begin morkNode interface =====
00089 public: // morkFactory virtual methods
00090   virtual void CloseMorkNode(morkEnv* ev); // CloseFactory() only if open
00091   virtual ~morkFactory(); // assert that CloseFactory() executed earlier
00092 
00093 
00094 // { ===== begin nsIMdbFactory methods =====
00095 
00096   // { ----- begin file methods -----
00097   NS_IMETHOD OpenOldFile(nsIMdbEnv* ev, nsIMdbHeap* ioHeap,
00098     const char* inFilePath,
00099     mdb_bool inFrozen, nsIMdbFile** acqFile);
00100   // Choose some subclass of nsIMdbFile to instantiate, in order to read
00101   // (and write if not frozen) the file known by inFilePath.  The file
00102   // returned should be open and ready for use, and presumably positioned
00103   // at the first byte position of the file.  The exact manner in which
00104   // files must be opened is considered a subclass specific detail, and
00105   // other portions or Mork source code don't want to know how it's done.
00106 
00107   NS_IMETHOD CreateNewFile(nsIMdbEnv* ev, nsIMdbHeap* ioHeap,
00108     const char* inFilePath,
00109     nsIMdbFile** acqFile);
00110   // Choose some subclass of nsIMdbFile to instantiate, in order to read
00111   // (and write if not frozen) the file known by inFilePath.  The file
00112   // returned should be created and ready for use, and presumably positioned
00113   // at the first byte position of the file.  The exact manner in which
00114   // files must be opened is considered a subclass specific detail, and
00115   // other portions or Mork source code don't want to know how it's done.
00116   // } ----- end file methods -----
00117 
00118   // { ----- begin env methods -----
00119   NS_IMETHOD MakeEnv(nsIMdbHeap* ioHeap, nsIMdbEnv** acqEnv); // new env
00120   // ioHeap can be nil, causing a MakeHeap() style heap instance to be used
00121   // } ----- end env methods -----
00122 
00123   // { ----- begin heap methods -----
00124   NS_IMETHOD MakeHeap(nsIMdbEnv* ev, nsIMdbHeap** acqHeap); // new heap
00125   // } ----- end heap methods -----
00126 
00127   // { ----- begin compare methods -----
00128   NS_IMETHOD MakeCompare(nsIMdbEnv* ev, nsIMdbCompare** acqCompare); // ASCII
00129   // } ----- end compare methods -----
00130 
00131   // { ----- begin row methods -----
00132   NS_IMETHOD MakeRow(nsIMdbEnv* ev, nsIMdbHeap* ioHeap, nsIMdbRow** acqRow); // new row
00133   // ioHeap can be nil, causing the heap associated with ev to be used
00134   // } ----- end row methods -----
00135   
00136   // { ----- begin port methods -----
00137   NS_IMETHOD CanOpenFilePort(
00138     nsIMdbEnv* ev, // context
00139     // const char* inFilePath, // the file to investigate
00140     // const mdbYarn* inFirst512Bytes,
00141     nsIMdbFile* ioFile, // db abstract file interface
00142     mdb_bool* outCanOpen, // whether OpenFilePort() might succeed
00143     mdbYarn* outFormatVersion); // informal file format description
00144     
00145   NS_IMETHOD OpenFilePort(
00146     nsIMdbEnv* ev, // context
00147     nsIMdbHeap* ioHeap, // can be nil to cause ev's heap attribute to be used
00148     // const char* inFilePath, // the file to open for readonly import
00149     nsIMdbFile* ioFile, // db abstract file interface
00150     const mdbOpenPolicy* inOpenPolicy, // runtime policies for using db
00151     nsIMdbThumb** acqThumb); // acquire thumb for incremental port open
00152   // Call nsIMdbThumb::DoMore() until done, or until the thumb is broken, and
00153   // then call nsIMdbFactory::ThumbToOpenPort() to get the port instance.
00154 
00155   NS_IMETHOD ThumbToOpenPort( // redeeming a completed thumb from OpenFilePort()
00156     nsIMdbEnv* ev, // context
00157     nsIMdbThumb* ioThumb, // thumb from OpenFilePort() with done status
00158     nsIMdbPort** acqPort); // acquire new port object
00159   // } ----- end port methods -----
00160   
00161   // { ----- begin store methods -----
00162   NS_IMETHOD CanOpenFileStore(
00163     nsIMdbEnv* ev, // context
00164     // const char* inFilePath, // the file to investigate
00165     // const mdbYarn* inFirst512Bytes,
00166     nsIMdbFile* ioFile, // db abstract file interface
00167     mdb_bool* outCanOpenAsStore, // whether OpenFileStore() might succeed
00168     mdb_bool* outCanOpenAsPort, // whether OpenFilePort() might succeed
00169     mdbYarn* outFormatVersion); // informal file format description
00170     
00171   NS_IMETHOD OpenFileStore( // open an existing database
00172     nsIMdbEnv* ev, // context
00173     nsIMdbHeap* ioHeap, // can be nil to cause ev's heap attribute to be used
00174     // const char* inFilePath, // the file to open for general db usage
00175     nsIMdbFile* ioFile, // db abstract file interface
00176     const mdbOpenPolicy* inOpenPolicy, // runtime policies for using db
00177     nsIMdbThumb** acqThumb); // acquire thumb for incremental store open
00178   // Call nsIMdbThumb::DoMore() until done, or until the thumb is broken, and
00179   // then call nsIMdbFactory::ThumbToOpenStore() to get the store instance.
00180     
00181   NS_IMETHOD
00182   ThumbToOpenStore( // redeem completed thumb from OpenFileStore()
00183     nsIMdbEnv* ev, // context
00184     nsIMdbThumb* ioThumb, // thumb from OpenFileStore() with done status
00185     nsIMdbStore** acqStore); // acquire new db store object
00186   
00187   NS_IMETHOD CreateNewFileStore( // create a new db with minimal content
00188     nsIMdbEnv* ev, // context
00189     nsIMdbHeap* ioHeap, // can be nil to cause ev's heap attribute to be used
00190     // const char* inFilePath, // name of file which should not yet exist
00191     nsIMdbFile* ioFile, // db abstract file interface
00192     const mdbOpenPolicy* inOpenPolicy, // runtime policies for using db
00193     nsIMdbStore** acqStore); // acquire new db store object
00194   // } ----- end store methods -----
00195 
00196 // } ===== end nsIMdbFactory methods =====
00197   
00198 public: // morkYarn construction & destruction
00199   morkFactory(); // uses orkinHeap
00200   morkFactory(nsIMdbHeap* ioHeap); // caller supplied heap
00201   morkFactory(morkEnv* ev, const morkUsage& inUsage, nsIMdbHeap* ioHeap);
00202   void CloseFactory(morkEnv* ev); // called by CloseMorkNode();
00203   
00204   
00205 public: // morkNode memory management operators
00206   void* operator new(size_t inSize) CPP_THROW_NEW
00207   { return ::operator new(inSize); }
00208   
00209   void* operator new(size_t inSize, nsIMdbHeap& ioHeap, morkEnv* ev) CPP_THROW_NEW
00210   { return morkNode::MakeNew(inSize, ioHeap, ev); }
00211   
00212 private: // copying is not allowed
00213   morkFactory(const morkFactory& other);
00214   morkFactory& operator=(const morkFactory& other);
00215 
00216 public: // dynamic type identification
00217   mork_bool IsFactory() const
00218   { return IsNode() && mNode_Derived == morkDerived_kFactory; }
00219 // } ===== end morkNode methods =====
00220 
00221 public: // other factory methods
00222 
00223   void NonFactoryTypeError(morkEnv* ev);
00224   morkEnv* GetInternalFactoryEnv(mdb_err* outErr);
00225   mork_bool CanOpenMorkTextFile(morkEnv* ev, nsIMdbFile* ioFile);
00226   
00227 public: // typesafe refcounting inlines calling inherited morkNode methods
00228   static void SlotWeakFactory(morkFactory* me,
00229     morkEnv* ev, morkFactory** ioSlot)
00230   { morkNode::SlotWeakNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00231   
00232   static void SlotStrongFactory(morkFactory* me,
00233     morkEnv* ev, morkFactory** ioSlot)
00234   { morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00235 };
00236 
00237 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00238 
00239 #endif /* _MORKFACTORY_ */