Back to index

lightning-sunbird  0.9+nobinonly
morkNodeMap.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 _MORKENV_
00051 #include "morkEnv.h"
00052 #endif
00053 
00054 #ifndef _MORKMAP_
00055 #include "morkMap.h"
00056 #endif
00057 
00058 #ifndef _MORKINTMAP_
00059 #include "morkIntMap.h"
00060 #endif
00061 
00062 #ifndef _MORKNODEMAP_
00063 #include "morkNodeMap.h"
00064 #endif
00065 
00066 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00067 
00068 // ````` ````` ````` ````` ````` 
00069 // { ===== begin morkNode interface =====
00070 
00071 /*public virtual*/ void
00072 morkNodeMap::CloseMorkNode(morkEnv* ev) // CloseNodeMap() only if open
00073 {
00074   if ( this->IsOpenNode() )
00075   {
00076     this->MarkClosing();
00077     this->CloseNodeMap(ev);
00078     this->MarkShut();
00079   }
00080 }
00081 
00082 /*public virtual*/
00083 morkNodeMap::~morkNodeMap() // assert CloseNodeMap() executed earlier
00084 {
00085   MORK_ASSERT(this->IsShutNode());
00086 }
00087 
00088 /*public non-poly*/
00089 morkNodeMap::morkNodeMap(morkEnv* ev,
00090   const morkUsage& inUsage, nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap)
00091 : morkIntMap(ev, inUsage, /*valsize*/ sizeof(morkNode*), ioHeap, ioSlotHeap,
00092   /*inHoldChanges*/ morkBool_kTrue)
00093 {
00094   if ( ev->Good() )
00095     mNode_Derived = morkDerived_kNodeMap;
00096 }
00097 
00098 /*public non-poly*/ void
00099 morkNodeMap::CloseNodeMap(morkEnv* ev) // called by CloseMorkNode();
00100 {
00101   if ( this )
00102   {
00103     if ( this->IsNode() )
00104     {
00105       this->CutAllNodes(ev);
00106       this->CloseMap(ev);
00107       this->MarkShut();
00108     }
00109     else
00110       this->NonNodeError(ev);
00111   }
00112   else
00113     ev->NilPointerError();
00114 }
00115 
00116 // } ===== end morkNode methods =====
00117 // ````` ````` ````` ````` ````` 
00118 
00119 mork_bool
00120 morkNodeMap::AddNode(morkEnv* ev, mork_token inToken, morkNode* ioNode)
00121   // the AddNode() method return value equals ev->Good().
00122 {
00123   if ( ioNode && ev->Good() )
00124   {
00125     morkNode* node = 0; // old val in the map
00126     
00127     mork_bool put = this->Put(ev, &inToken, &ioNode,
00128       /*key*/ (void*) 0, &node, (mork_change**) 0);
00129       
00130     if ( put ) // replaced an existing value for key inToken?
00131     {
00132       if ( node && node != ioNode ) // need to release old node?
00133         node->CutStrongRef(ev);
00134     }
00135     
00136     if ( ev->Bad() || !ioNode->AddStrongRef(ev) )
00137     {
00138       // problems adding node or increasing refcount?
00139       this->Cut(ev, &inToken,  // make sure not in map
00140         /*key*/ (void*) 0, /*val*/ (void*) 0, (mork_change**) 0);
00141     }
00142   }
00143   else if ( !ioNode )
00144     ev->NilPointerError();
00145     
00146   return ev->Good();
00147 }
00148 
00149 mork_bool
00150 morkNodeMap::CutNode(morkEnv* ev, mork_token inToken)
00151 {
00152   morkNode* node = 0; // old val in the map
00153   mork_bool outCutNode = this->Cut(ev, &inToken, 
00154     /*key*/ (void*) 0, &node, (mork_change**) 0);
00155   if ( node )
00156     node->CutStrongRef(ev);
00157   
00158   return outCutNode;
00159 }
00160 
00161 morkNode*
00162 morkNodeMap::GetNode(morkEnv* ev, mork_token inToken)
00163   // Note the returned node does NOT have an increase in refcount for this.
00164 {
00165   morkNode* node = 0; // old val in the map
00166   this->Get(ev, &inToken, /*key*/ (void*) 0, &node, (mork_change**) 0);
00167   
00168   return node;
00169 }
00170 
00171 mork_num
00172 morkNodeMap::CutAllNodes(morkEnv* ev)
00173   // CutAllNodes() releases all the reference node values.
00174 {
00175   mork_num outSlots = mMap_Slots;
00176   mork_token key = 0; // old key token in the map
00177   morkNode* val = 0; // old val node in the map
00178   
00179   mork_change* c = 0;
00180   morkNodeMapIter i(ev, this);
00181   for ( c = i.FirstNode(ev, &key, &val); c ; c = i.NextNode(ev, &key, &val) )
00182   {
00183     if ( val )
00184       val->CutStrongRef(ev);
00185     i.CutHereNode(ev, /*key*/ (mork_token*) 0, /*val*/ (morkNode**) 0);
00186   }
00187   
00188   return outSlots;
00189 }
00190 
00191 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00192