home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Core / ObjectNS.cpp < prev    next >
Encoding:
Text File  |  1996-08-28  |  11.9 KB  |  395 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ObjectNS.cpp
  3.  
  4.     Contains:    Implementation of ODObjectNameSpace class
  5.  
  6.     Owned by:    David McCusker
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <13>     10/8/95    TJ        Fixes Recomended by Refball
  13.         <12>     8/29/95    DM        1279507: make GetEntry() consistently
  14.                                     return null object when it returns
  15.                                     false.1279569: throw if null after newing
  16.                                     SOM instances
  17.         <11>      8/3/95    RR        #1257260: Collapse B classes. Remove
  18.                                     somInit methods. Don't call IsInitialized
  19.                                     or SubclassResponsibility
  20.         <10>     7/24/95    CG        #1270320: Memory leak fixes.
  21.          <9>     7/14/95    TJ        Jens added a test to check for non object
  22.                                     deletions.
  23.          <8>     5/11/95    DM        #1181622: Updated file ownership.
  24.          <7>      5/1/95    CG        1241627    BB: SOM_TRY blocks needed in Core
  25.                                     SOM methods
  26.          <6>     4/26/95    CG        1211082 BB: 5$ Bugs need to be evaluated
  27.                                     and removed from Core
  28.          <5>    12/13/94    CG        #1203295:  somUninit disposes of all
  29.                                     objects.
  30.          <4>     8/31/94    TÇ        #1183129, #1183116, #1183119, #1183111:
  31.                                     Lots of ErrorCode cleanup.
  32.          <3>     8/24/94    CG        #1181622: Updated file ownership.
  33.          <2>     8/18/94    jpa        Deleted obsolete I/O methods [1181839]
  34.          <1>     7/29/94    CG        first checked in
  35.          <2>      7/1/94    CG        Moved ReadSize and ReadBytes to
  36.                                     BndNSUtl.cpp.
  37.          <1>     6/24/94    CG        first checked in
  38.          <0>     6/24/94    SV        SOMverted
  39.          <8>     5/27/94    jpa        New exception support [1165267]
  40.          <7>      5/9/94    MB        #1162181: Changes necessary to install MMM.
  41.          <6>      4/1/94    TÇ        #1154941: Get OpenDoc source to actually
  42.                                     compile for PowerPC
  43.          <5>     3/28/94    CG        1152929: Created new error
  44.                                     kInvalidFileContents to throw when
  45.                                     Read fails.
  46.          <4>     3/25/94    TÇ        removed erroneous comment: // $$$$ POWRPC
  47.                                     NATVE
  48.          <3>     3/15/94    MB        Changes to support SCpp/ASLM builds,
  49.                                     #1150864.
  50.          <2>     2/15/94    CG        #1143680 - NameSpace now being allocated on
  51.                                     specified heap.
  52.          <6>      2/8/94    TÇ        Throw -> THROW & some code clean up
  53.          <5>      2/4/94    TÇ        SysEqu -> LowMem
  54.          <4>      2/3/94    CG        Added _fHeap field to store current heap.
  55.          <3>     1/29/94    NP        Init changes.
  56.          <2>     1/18/94    CG        Changed GetValue to take valueLen parameter.
  57.          <1>     1/14/94    CG        first checked in
  58.         <40>     1/12/94    CG        Fixed bug in LoadNameSpaceTable().
  59.         <39>     1/11/94    TÇ        Init... changes
  60.         <38>    12/17/93    eeh        implement GetRegLibFileFolder
  61.         <37>    12/15/93    TÇ        more InitObject changes, remove
  62.                                     Initialize() method
  63.         <36>    12/15/93    TÇ        InitObject Changes
  64.         <35>    12/10/93    CG        Use HasNameSpace where appropriate.
  65.         <34>     12/6/93    CG        fixed NMAP reading bug.
  66.         <33>     12/3/93    TÇ        Stop including ODError.h, it is included
  67.                                     as ErrorDef.h inside Except.h
  68.         <32>    11/18/93    CG        Changed prefs file format, mapping rez
  69.                                     format.  Fixed bugs.
  70.         <31>    11/15/93    NP        Changed XMPSymbols to ODNameSpaceManager.
  71.                                     Fixed bugs.
  72.         <30>    11/12/93    CG        The Symbols object  now uses ReadFromFile
  73.                                     and WriteToFile for reading and writing to
  74.                                     prefs file.
  75.         <29>     11/8/93    NP        Added WriteToStorage and ReadFromStorage.
  76.         <28>     11/8/93    NP        Tweak ReadFromFile and WriteToFile to work
  77.                                     with refnum.
  78.         <27>     11/5/93    NP        Adding streaming functionality.
  79.         <26>     11/2/93    NP        Cast for CFront.
  80.         <25>     11/2/93    NP        Bug fixes.
  81.         <24>     11/2/93    CG        Fix for hash table changes.
  82.         <23>    10/28/93    NP        Comment out obsoleted parts of API. Will
  83.                                     remove later.
  84.         <22>    10/22/93    NP        Mods for new XMPAEHashTable and
  85.                                     StringHashTable protocols.
  86.         <21>    10/18/93    CG        Inability to creat prefs file no longer
  87.                                     prevents launch.
  88.         <20>    10/14/93    JA        Changed prefs file name to a fixed "OpenDoc
  89.                                     Prefs". (Using CurApName will not work
  90.                                     later on when every doc is a process!)
  91.         <18>     10/7/93    CG        Symbols now get written to prefs file.
  92.         <17>     10/5/93    CG        Added Exists method to ODNameSpace.
  93.         <16>     9/22/93    JA        Updated ASSERTs for new macro.
  94.         <15>     9/21/93    CG        Added namespace table loading.
  95.         <14>     9/20/93    CG        Fixed ASSERT.
  96.         <13>     7/21/93    NP        Changed ODValueIterator::FirstNextAux.
  97.         <12>      7/2/93    NP        Abstract/Concrete breakout.
  98.         <11>     6/22/93    NP        Fixed some bugs. Synced with header.
  99.         <10>     6/15/93    NP        Mods to allow compilation with
  100.                                     LibraryManager.h and a few bug fixes.
  101.          <9>     6/14/93    NP        Implemented inheritance.
  102.          <8>      6/10/93    RCR        Change to new Exception macros
  103.          <7>      6/7/93    NP        Added missing cast in HasNameSpace. CFront
  104.                                     complained, but Scpp did not.
  105.          <6>      6/4/93    NP        Implemented everything except inheritance
  106.                                     of name spaces.
  107.          <5>      6/2/93    NP        Basic functionality working. Missing:
  108.                                     Finding name space by name and iterator.
  109.          <4>     5/18/93    NP        Updated HasNameSpace.
  110.          <3>     4/28/93    NP        File name changes.
  111.          <2>     4/27/93    NP        Added Purge.
  112.          <1>     4/23/93    NP        first checked in
  113.  
  114.     To Do:
  115.         Get prefs file name from a string resource.
  116.     In Progress:
  117.     
  118.         Work around presence of non-objects in name space in somUninit.
  119. */
  120.  
  121. #ifndef _PLFMDEF_
  122. #include "PlfmDef.h"
  123. #endif
  124.  
  125.  
  126. #define ODObjectNameSpace_Class_Source
  127. #include <ObjectNS.xih>
  128.  
  129. #ifndef _STRHSHTB_
  130. #include "StrHshTb.h"
  131. #endif
  132.  
  133. #ifndef _EXCEPT_
  134. #include "Except.h"
  135. #endif
  136.  
  137. #ifndef _PASCLSTR_
  138. #include "PasclStr.h"
  139. #endif
  140.  
  141. #ifndef __STRING__
  142. #include <string.h>
  143. #endif
  144.  
  145. #ifndef __LIMITS__
  146. #include <limits.h>
  147. #endif
  148.  
  149. #ifndef __LOWMEM__
  150. #include <LowMem.h>
  151. #endif
  152.  
  153. #ifndef _ODTYPES_
  154. #include <ODTypes.h>
  155. #endif
  156.  
  157. #ifndef SOM_ODStorageUnitView_xh
  158. #include <SUView.xh>
  159. #endif
  160.  
  161. #ifndef _ODMEMORY_
  162. #include "ODMemory.h"
  163. #endif
  164.  
  165. #ifndef _ISOSTR_
  166. #include "ISOStr.h"
  167. #endif
  168.  
  169. #ifndef SOM_ODObjectIterator_xh
  170. #include <ObjctItr.xh>
  171. #endif
  172.  
  173. #ifndef _BNDNSUTL_
  174. #include "BndNSUtl.h"
  175. #endif
  176.  
  177. #ifndef _PLFMFILE_
  178. #include "PLFMFile.h"
  179. #endif
  180.  
  181. #ifndef _ODUTILS_
  182. #include <ODUtils.h>
  183. #endif
  184.  
  185. #ifndef _MEMMGR_
  186. #include <MemMgr.h>
  187. #endif
  188.  
  189. #pragma segment ODObjectNameSpace
  190.  
  191. //==============================================================================
  192. // Implementation notes
  193. //==============================================================================
  194.  
  195. /*
  196. Hash tables used for the implementation are lazily instantiated. They will be
  197. created at the first request to add an entry.
  198. */
  199.  
  200. //==============================================================================
  201. // Constants
  202. //==============================================================================
  203.  
  204. // for hash tables
  205.  
  206. const ODUShort        kOSTypeKeySize = sizeof(ODOSType);
  207. const ODUShort        kSLongKeySize = sizeof(ODSLong);
  208. const ODUShort        kODPtrSize = sizeof(ODPtr);
  209. const ODBoolean    kNotInSystemHeap = false;
  210.  
  211. //==============================================================================
  212. // Function Prototype
  213. //==============================================================================
  214.  
  215. //==============================================================================
  216. // Local Functions
  217. //==============================================================================
  218.  
  219. //==============================================================================
  220. // ODObjectNameSpace
  221. //==============================================================================
  222.  
  223. //------------------------------------------------------------------------------
  224. // ODObjectNameSpace: InitObjectNameSpace
  225. //------------------------------------------------------------------------------
  226.  
  227. SOM_Scope void  SOMLINK ODObjectNameSpaceInitObjectNameSpace(ODObjectNameSpace *somSelf, Environment *ev,
  228.                                         ODNameSpaceManager*    mgr,
  229.                                         ODNameSpace*        parent,
  230.                                         ODULong                numExpectedEntries,
  231.                                         ODISOStr            name)
  232. {
  233.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  234.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","InitObjectNameSpace");
  235.     
  236.     SOM_TRY
  237.  
  238.     somSelf->SetType( ev, kODNSDataTypeODObject );
  239.     somSelf->InitNameSpace( ev, mgr, parent,numExpectedEntries, name);
  240.     
  241.     SOM_CATCH_ALL
  242.     SOM_ENDTRY
  243. }
  244.  
  245. //------------------------------------------------------------------------------
  246. // ODObjectNameSpace: ~ODObjectNameSpace
  247. //------------------------------------------------------------------------------
  248.  
  249. SOM_Scope void  SOMLINK ODObjectNameSpacesomUninit(ODObjectNameSpace *somSelf)
  250. {
  251.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  252.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","somUninit");
  253.  
  254.     ODISOStr                key;
  255.     ODObject*                object;
  256.     ODULong                    length;
  257.     
  258.     TRY{
  259.  
  260.         Environment* ev = somGetGlobalEnvironment();
  261.         
  262.         ODObjectIterator*    iter = somSelf->CreateIterator(ev);
  263.         for (iter->First(ev, &key, &object, &length);
  264.                 iter->IsNotComplete(ev);
  265.                 iter->Next(ev, &key, &object, &length))
  266.         {
  267.             // This test is a workaround only; non-objects should not be stored here! $$$$$
  268.             if( MMIsObject(object) && somIsObj(object) )
  269.                 delete object;
  270.             else
  271.                 ODDisposePtr(object);
  272.             ODDisposePtr( key );
  273.         }
  274.     
  275.         ODDeleteObject( iter );
  276.     }CATCH_ALL{
  277.         // ignore exception
  278.     }ENDTRY
  279. }
  280.  
  281. //------------------------------------------------------------------------------
  282. // ODObjectNameSpace: Purge
  283. //------------------------------------------------------------------------------
  284.  
  285. SOM_Scope ODSize  SOMLINK ODObjectNameSpacePurge(ODObjectNameSpace *somSelf, Environment *ev,
  286.         ODSize size)
  287. {
  288.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  289.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","Purge");
  290.  
  291.     ODUnused(size);
  292.     return 0;
  293. }
  294.  
  295. //------------------------------------------------------------------------------
  296. // ODObjectNameSpace: Register
  297. //------------------------------------------------------------------------------
  298.  
  299. SOM_Scope void  SOMLINK ODObjectNameSpaceRegister(ODObjectNameSpace *somSelf, Environment *ev,
  300.         ODISOStr key, ODObject* object)
  301. {
  302.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  303.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","Register");
  304.     
  305.     //1242543
  306.     //if (somSelf->GetType(ev) != kODNSDataTypeODObject)
  307.     //    THROW(kODErrWrongNameSpaceType);
  308.  
  309.     SOM_TRY
  310.  
  311.     ODULong len = ODISOStrLength(key);
  312.     ODULong valueLength = sizeof(ODObject);
  313.     somSelf->GetHashTable(ev)->Insert((ODUByte*)key, len, (ODPtr)object, valueLength);
  314.     
  315.     SOM_CATCH_ALL
  316.     SOM_ENDTRY
  317. }
  318.  
  319.  
  320. //------------------------------------------------------------------------------
  321. // ODObjectNameSpace: GetEntry
  322. //------------------------------------------------------------------------------
  323.  
  324. SOM_Scope ODBoolean  SOMLINK ODObjectNameSpaceGetEntry(ODObjectNameSpace *somSelf, Environment *ev,
  325.         ODISOStr key, ODObject** object )
  326. {
  327.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  328.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","GetEntry");
  329.  
  330.     ODBoolean    result = kODFalse;
  331.     *object = kODNULL;
  332.     
  333.     SOM_TRY
  334.  
  335.     if (somSelf->GetType(ev) == kODNSDataTypeODObject)
  336.     {
  337.         StringHashTable*    strHashTable = somSelf->GetHashTable(ev);
  338.         if (strHashTable)
  339.         {
  340.             ODULong        objLen;
  341.             size_t         len = strlen(key);
  342.             result = strHashTable->Find((ODUByte*)key, len, (ODPtr*)object, &objLen);
  343.             if ( result == kODFalse )
  344.             {
  345.                 ODObjectNameSpace *parent = (ODObjectNameSpace*)somSelf->GetParent(ev);
  346.                 if (parent)
  347.                 {
  348.                     if (parent->GetType(ev) == kODNSDataTypeODObject)
  349.                         result = parent->GetEntry(ev, key, object);
  350.                 }
  351.             }
  352.         }
  353.         else
  354.         {
  355.             ODObjectNameSpace *parent = (ODObjectNameSpace*)somSelf->GetParent(ev);
  356.             if (parent)
  357.             {
  358.                 if (parent->GetType(ev) == kODNSDataTypeODObject)
  359.                     result = parent->GetEntry(ev, key, object);
  360.             }
  361.         }
  362.     }
  363.     
  364.     SOM_CATCH_ALL
  365.     SOM_ENDTRY
  366.  
  367.     if ( !result )
  368.         *object = kODNULL;
  369.  
  370.     return result;
  371. }
  372.  
  373. //------------------------------------------------------------------------------
  374. // ODObjectNameSpace: CreateIterator
  375. //------------------------------------------------------------------------------
  376.  
  377. SOM_Scope ODObjectIterator*  SOMLINK ODObjectNameSpaceCreateIterator(ODObjectNameSpace *somSelf, Environment *ev)
  378. {
  379.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  380.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","CreateIterator");
  381.     
  382.     ODObjectIterator* theObjectIterator = kODNULL;
  383.  
  384.     SOM_TRY
  385.     
  386.     theObjectIterator = new ODObjectIterator;
  387.     THROW_IF_NULL(theObjectIterator);
  388.     theObjectIterator->InitObjectIterator(ev, somSelf);
  389.     
  390.     SOM_CATCH_ALL
  391.     SOM_ENDTRY
  392.  
  393.     return theObjectIterator;
  394. }
  395.