home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osr1.exe / src / CMSU.cpp < prev    next >
C/C++ Source or Header  |  1997-03-21  |  103KB  |  3,317 lines

  1. /* @(#)Z 1.21 com/src/bento/CMSU.cpp, odstorage, od96os2, odos29712d 97/03/21 17:19:11 (96/10/29 09:11:34) */
  2. //====START_GENERATED_PROLOG======================================
  3. //
  4. //
  5. //   COMPONENT_NAME: odstorage
  6. //
  7. //   CLASSES: none
  8. //
  9. //   ORIGINS: 82,27
  10. //
  11. //
  12. //   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  13. //   All Rights Reserved
  14. //   Licensed Materials - Property of IBM
  15. //   US Government Users Restricted Rights - Use, duplication or
  16. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  17. //       
  18. //   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. //   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. //   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  22. //   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  23. //   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  24. //   OR PERFORMANCE OF THIS SOFTWARE.
  25. //
  26. //====END_GENERATED_PROLOG========================================
  27. //
  28.  
  29. /*
  30.     File:        CMSU.cpp
  31.  
  32.     Contains:    Implementation CMStorageUnit class.
  33.  
  34.     Owned by:    Vincent Lo
  35.  
  36.     Copyright:    ⌐ 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  37.  
  38.     Change History (most recent first):
  39.  
  40.         <53>    10/24/95    jpa        1293441: DM/VL: Bento memory reserve &
  41.                                     fatal container err & don't throw in
  42.                                     Release.
  43.         <52>    10/19/95    EL        1292685: After purging we may get a
  44.                                     different storage unit for the same
  45.                                     CMObject.
  46.         <51>     10/8/95    TJ        Fixes Recomended by Refball
  47.         <50>     10/3/95    TJ        Changes done by RefBall Team
  48.         <49>     9/29/95    TJ        Made Changes for MAC SOM
  49.         <48>     8/22/95    VL        1276990: Removed bogus THROW in
  50.                                     SetPromiseValue.
  51.         <47>     8/21/95    VL        1278330, 1278315: Error code cleanup.
  52.         <46>     8/17/95    VL        1266572: Added SOM_TRY and CATCH in
  53.                                     CountValues and fixed THROW in GetDraft.
  54.         <45>     8/16/95    VL        1266572: Added SOM_TRY and CATCH in
  55.                                     SetOffset and GetOffset.
  56.         <44>     8/12/95    Té        1276812 Need to use TempObjs and TempRefs
  57.                                     for exception safety and to avoid TRY
  58.                                     blocks, 1276807 Opt./Bug: use StdTypIO
  59.                                     routines for portable streaming & smaller
  60.                                     footprint, 1276806 Optimization: use
  61.                                     kODFalse instead of kODTrue in comparisons
  62.         <43>      8/8/95    EL        #1265282: GetStorageUnitRef does not check
  63.                                     for null. #1266000: Focus cannot be called
  64.                                     twice on the same value.
  65.         <42>      8/3/95    RR        #1257260: Collapse B classes. Remove
  66.                                     somInit methods. Don't call IsInitialized
  67.                                     or SubclassResponsibility
  68.         <41>     6/20/95    VL        Turned off debug code (accidentally left on
  69.                                     in the previous checkin.
  70.         <40>     6/16/95    VL        1259613: Changed CreateCursorXXX to allow
  71.                                     creation of cursor even when the su is
  72.                                     unfocused. Used PreserveFocus.
  73.         <39>     6/13/95    VL        1241352: Added modification date support
  74.                                     (mainly in SetChangedFromPrevFlag and
  75.                                     Externalize).
  76.         <38>     5/26/95    VL        1251403: Multithreading naming support.
  77.         <37>     5/25/95    jpa        Fixed usage of ODDebug. [1253321]
  78.         <36>     5/22/95    VL        1246940: Added more debug code.
  79.         <35>     5/19/95    VL        1249626: in length and out byteArray for
  80.                                     GetValue and GetPromiseValue.
  81.         <34>     5/11/95    EL        1242515: value not released in
  82.                                     GetObjectSize.
  83.         <33>      5/2/95    VL        1245113: RefCount check should not be used
  84.                                     for invariants.
  85.         <32>     4/25/95    VL        1210982: Removed 5$.
  86.         <31>     4/24/95    VL        1241729: Fixed backward iteration of SU
  87.                                     values.
  88.         <30>     4/11/95    VL        1236483: Fixed infinite cloning.
  89.         <29>     4/10/95    VL        1235948: Fixed crash for NULL input for
  90.                                     GetStorageUnitRef. 1236490: Made
  91.                                     GetPromiseValue work during FulfillPromise.
  92.         <28>     3/31/95    VL        1232945: Fixed leaking in CloneInto.
  93.         <27>     3/27/95    VL        1231412: Removed CopyTo.
  94.         <26>     3/19/95    VL        1228275: ASSERT if ODByteArray  is kODNULL.
  95.         <25>     3/14/95    VL        1225208,1225201: Added IsSettingPromise to
  96.                                     ensure that we don't call ResolvePromise
  97.                                     while setting a promise.
  98.         <24>     3/10/95    VL        1226116: No need to typecast for
  99.                                     SetStorageUnitRef.
  100.         <23>      3/9/95    VL        1225493: CloneInto should preserve the
  101.                                     focus. 1226082: Removed WASSERTM.
  102.         <22>     2/28/95    VL        1194656: Added checks for existing property
  103.                                     and value for AddProperty and AddValue.
  104.         <21>     2/21/95    EL        1182275: Do not throw if object cannot be
  105.                                     internalized (it probably was disposed in
  106.                                     garbage collection).
  107.         <20>     1/31/95    EL        1213321: Speed up focus and exist by using
  108.                                     CMUseValue.
  109.         <19>     1/26/95    VL        #???: SURef is now a 4-byte array.
  110.         <18>      1/9/95    VL        1183661: Use new SUCursor getters.
  111.         <17>    12/20/94    VL        1195012: Make Storage calls be
  112.                                     marshallable.
  113.         <16>     11/1/94    CC        1190911 - CloneInto(): Updated comment.
  114.         <15>     11/1/94    VL        1179951: Implemented Remove for all
  115.                                     Properties. 1194188: Implemented
  116.                                     unimplemented functions.
  117.         <14>     10/6/94    VL        1185112: Lock should not throw if the
  118.                                     storage unit is already locked.
  119.         <13>     9/29/94    RA        1189812: Mods for 68K build.
  120.         <12>     9/15/94    VL        1184871: In order to get cross-doc dad to
  121.                                     work, I need to fix Focus to correctly
  122.                                     focus to the value type first.
  123.         <11>      9/5/94    VL        1184871: Used Renew to remove dependency on
  124.                                     default heap.
  125.         <10>      9/1/94    VL        1183174: Removed Clone and GetClonedIDs.
  126.                                     1183054: Changed kODErrInvalidName to
  127.                                     kODErrInvalidProperty.
  128.          <9>     8/31/94    Té        #1183129, #1183116, #1183119, #1183111:
  129.                                     Lots of ErrorCode cleanup.
  130.          <8>     8/31/94    VL        1183174, 1106013: #ifdef ODDebug somPrintf
  131.                                     statements. Also, fixed GetName to return
  132.                                     kODNULL if there is no name.
  133.          <7>     8/26/94    VL        1183174: Implemented CloneInto and removed
  134.                                     CloneTo.
  135.          <6>     8/11/94    VL        1180299: Added CreateCursor and modified
  136.                                     CreateView.
  137.          <5>      8/3/94    VL        1153123: Storage to ODStor.
  138.          <4>     7/26/94    VL        Got rid of helper methods for promise
  139.                                     resolution. Instead, use PromiseResolver.
  140.          <3>     7/21/94    VL        Added WASSERTM at various places to check
  141.                                     for illegal sizes.
  142.          <2>      7/7/94    VL        Fixed up local variables in Focus.
  143.          <1>      7/5/94    VL        first checked in
  144.  
  145.     To Do:
  146.     In Progress:
  147.         
  148. */
  149.  
  150. #define CMStorageUnit_Class_Source
  151. #define VARIABLE_MACROS
  152. #include <CMSU.xih>
  153.  
  154. #ifndef SOM_CMStorageUnitRefIterator_xh
  155. #include <CMSURefI.xh>
  156. #endif
  157.  
  158. #ifndef _BENTODEF_
  159. #include "BentoDef.h"
  160. #endif
  161.  
  162. #ifndef SOM_ODStorageUnitCursor_xh
  163. #include <SUCursor.xh>
  164. #endif
  165.  
  166. #ifndef SOM_ODStorageUnitView_xh
  167. #include <SUView.xh>
  168. #endif
  169.  
  170. #ifndef SOM_CMDraft_xh
  171. #include <CMDraft.xh>
  172. #endif
  173.  
  174. #ifndef SOM_CMDocument_xh
  175. #include <CMDoc.xh>
  176. #endif
  177.  
  178. #ifndef SOM_CMCtr_xh
  179. #include <CMCtr.xh>
  180. #endif
  181.  
  182. #ifndef _DRAFPRIV_
  183. #include "DrafPriv.h"
  184. #endif
  185.  
  186. #ifndef __CM_API__
  187. #include "CMAPI.h"
  188. #endif
  189.  
  190. #ifndef _EXCEPT_
  191. #include "Except.h"
  192. #endif
  193.  
  194. #ifdef _PLATFORM_MACINTOSH_
  195. #ifndef _AEHSHTBL_
  196. #include "AEHshTbl.h"
  197. #endif
  198. #endif
  199.  
  200. #ifndef _ISOSTR_
  201. #include "ISOStr.h"
  202. #endif
  203.  
  204. #ifndef _TEMPOBJ_
  205. #include "TempObj.h"
  206. #endif
  207.  
  208. #ifndef _ODMEMORY_
  209. #include "ODMemory.h"
  210. #endif
  211.  
  212. #ifndef SOM_Module_OpenDoc_StdProps_defined
  213. #include <StdProps.xh>
  214. #endif
  215.  
  216. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  217. #include <StdTypes.xh>
  218. #endif
  219.  
  220. #ifndef SOM_ODStorageSystem_xh
  221. #include <ODStor.xh>
  222. #endif
  223.  
  224. #ifndef SOM_ODSession_xh
  225. #include <ODSessn.xh>
  226. #endif
  227.  
  228. #ifndef _PRMRSLVR_
  229. #include "PrmRslvr.h"
  230. #endif
  231.  
  232. #ifndef _SUREF_
  233. #include "SURef.h"
  234. #endif
  235.  
  236. #ifndef _SUCLONE_
  237. #include "SUClone.h"
  238. #endif
  239.  
  240. #ifndef _STORUTIL_
  241. #include <StorUtil.h>
  242. #endif
  243.  
  244. #ifndef __STDIO__
  245. #include <stdio.h>            // For sprintf
  246. #endif
  247.  
  248. #ifndef _ODDEBUG_
  249. #include "ODDebug.h"
  250. #endif
  251.  
  252. #ifndef _ODNEW_
  253. #include "ODNew.h"
  254. #endif
  255.  
  256. #ifndef _TIME
  257. #include <time.h>
  258. #endif
  259.  
  260. #ifndef _STDTYPIO_
  261. #include <StdTypIO.h>
  262. #endif
  263.  
  264. #ifndef _SESSHDR_
  265. #include "SessHdr.h"
  266. #endif
  267.  
  268. #if defined(_PLATFORM_WIN32_) || defined(_PLATFORM_OS2_) || defined(_PLATFORM_AIX_)
  269. #ifndef _ODUTILS_
  270. #include "ODUtils.h"
  271. #endif
  272. #endif
  273.  
  274. #pragma segment CMStorageUnit
  275.  
  276. #define USE_CLONEHELPER 1
  277.  
  278. //==============================================================================
  279. // Constants
  280. //==============================================================================
  281. #define kInitialHashTableEntries 8
  282. #define kHighBitMask    0x80000000
  283. #define kLowBitsMask    0x7FFFFFFF
  284.  
  285. #if ODDebug
  286. // #define ODDebug_ODStorageUnit 1
  287. #define ODDebug_AddProperty    1
  288. #define ODDebug_AddValue    1
  289. // #define ODDebug_SUCloneHelper    1
  290. #endif
  291.  
  292. //==============================================================================
  293. // Function Prototype
  294. //==============================================================================
  295.  
  296. static ODStorageUnitView* NewODStorageUnitView(ODMemoryHeapID heapID);
  297. static ODStorageUnitCursor* NewODStorageUnitCursor(ODMemoryHeapID heapID);
  298. static CMStorageUnitRefIterator*    NewCMStorageUnitRefIterator(ODMemoryHeapID heapID);
  299.  
  300. #define FailIfInvalidRefCount() ASSERTMSG(somSelf->GetRefCount(ev)!=0, kODErrZeroRefCount, "Invalid refCount", 0)
  301. #define FailIfIllegalByteArray(value) ASSERT(value != kODNULL, kODErrIllegalNullInput)
  302.  
  303. inline void BREAK_AND_THROW(ODError err)
  304. {
  305. //    Debugger();
  306.     THROW(err);
  307. }
  308.  
  309. #define ODEnterCriticalSection()
  310. #define ODExitCriticalSection()
  311.  
  312. // Callers must call ODSessionMustHaveCMAllocReserve(container);
  313. static ODULong GetPropertySize(CMObject object, CMProperty property);
  314. static ODULong GetObjectSize(CMObject object);
  315.  
  316. //==============================================================================
  317. // CMStorageUnit
  318. //==============================================================================
  319.  
  320. //------------------------------------------------------------------------------
  321. // CMStorageUnit: AcquireDraft
  322. //------------------------------------------------------------------------------
  323.  
  324. SOM_Scope ODDraft*  SOMLINK CMStorageUnitGetDraft(CMStorageUnit *somSelf, Environment *ev)
  325. {
  326.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  327.     CMStorageUnitMethodDebug("CMStorageUnit","GetDraft");
  328.     
  329.     return _fDraft;
  330. }
  331.  
  332. //------------------------------------------------------------------------------
  333. // CMStorageUnit: Acquire
  334. //------------------------------------------------------------------------------
  335.  
  336. SOM_Scope void  SOMLINK CMStorageUnitAcquire(CMStorageUnit *somSelf, Environment *ev)
  337. {
  338.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  339.     CMStorageUnitMethodDebug("CMStorageUnit","Acquire");
  340.  
  341.     try
  342.     {
  343.  
  344.     parent_Acquire(somSelf, ev);
  345.     }
  346.     catch (ODException _exception)
  347.     {
  348.         ODSetSOMException(ev, _exception);
  349.     }
  350.  
  351. }
  352.     
  353. //------------------------------------------------------------------------------
  354. // CMStorageUnit: Release
  355. //------------------------------------------------------------------------------
  356.  
  357. SOM_Scope void  SOMLINK CMStorageUnitRelease(CMStorageUnit *somSelf, Environment *ev)
  358. {
  359.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  360.     CMStorageUnitMethodDebug("CMStorageUnit","Release");
  361.  
  362.     try
  363.     {
  364.  
  365.     CMStorageUnit_parent_ODStorageUnit_Release(somSelf, ev);
  366.     if (somSelf->GetRefCount(ev) == 0) {
  367.         if (_fCurValue != kODNULL) {
  368.             CMContainer cmContainer = _fDraft->GetCMContainer(ev);
  369.             ODSessionMustHaveCMAllocReserve(cmContainer);
  370.             
  371.             CMReleaseValue(_fCurValue);
  372.             
  373.             _fCurValue = kODNULL;
  374.         }
  375.         _fCurProperty = kODNULL;
  376.         _fDraft->ReleaseStorageUnit(ev, _fID);
  377.     }
  378.         
  379.     }
  380.     catch (ODException _exception)
  381.     {
  382.     
  383.         ODError err = ErrorCode();
  384.  
  385.         WARNMSG(WARN_INDEX(AMSG_670),
  386.             "Error occurred in ODStorageUnit::Release: %d %s", 
  387.             err, ErrorMessage());
  388.  
  389.         if (err == kODErrBentoErr){
  390.             SetErrorCode(kODErrFatalContainerError);
  391.                         ODSetSOMException(ev, _exception);
  392.                 }
  393.         else{
  394.                      if (err != kODErrFatalContainerError)
  395.             SetErrorCode(kODNoError);
  396.                      else
  397.                         ODSetSOMException(ev, _exception);
  398.                 }
  399.             
  400.     }
  401. }
  402.  
  403. //------------------------------------------------------------------------------
  404. // CMStorageUnit: Purge
  405. //------------------------------------------------------------------------------
  406.  
  407. SOM_Scope ODSize             SOMLINK CMStorageUnitPurge(CMStorageUnit *somSelf, Environment *ev,
  408.         ODSize size)
  409. {
  410.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  411.     CMStorageUnitMethodDebug("CMStorageUnit","Purge");
  412.      
  413.         ODULong runningTotal = 0; ODVolatile( runningTotal ); // DR5 code added
  414.  
  415.     try
  416.     {
  417.     
  418.     ODBoolean hasReserve = kODFalse;
  419.     
  420.     if (_fCurValue != kODNULL) {
  421.         CMContainer cmContainer = _fDraft->GetCMContainer(ev);
  422.         ODSessionMustHaveCMAllocReserve(cmContainer);
  423.         hasReserve = kODTrue;
  424.         
  425.         CMReleaseValue(_fCurValue);
  426.         _fCurValue = kODNULL;
  427.     }
  428.     
  429.     _fCurProperty = kODNULL;
  430.     
  431.     if (_fObject != kODNULL) {
  432.     
  433.                 CMDraft* tempDraft = (CMDraft*) somSelf->GetDraft(ev);
  434.         IDList*    idList = kODNULL;
  435.                 if(tempDraft)
  436.                     idList = tempDraft->GetIDList(ev);
  437.         ASSERT(idList != kODNULL, kODErrInvalidIDList);
  438.         
  439.         if (idList->Exists(_fID) == kODFalse) {
  440.             if ( !hasReserve )
  441.             {
  442.                 CMContainer cmContainer = _fDraft->GetCMContainer(ev);
  443.                 ODSessionMustHaveCMAllocReserve(cmContainer);
  444.                 hasReserve = kODTrue;
  445.             }
  446.             CMReleaseObject(_fObject);
  447.             _fObject = kODNULL;
  448.         }
  449.     }
  450.         runningTotal += parent_Purge(somSelf, ev, size);  // DR5 code added
  451.     
  452.     }
  453.     catch (ODException _exception)
  454.     {
  455.          WARNMSG(WARN_INDEX(AMSG_906),"Error %ld trying to purge in CMStorageUnitPurge",ErrorCode()); // DR5 code added.
  456.              // dh - Eat the exception; Purge should not 
  457.              // propagate it because clients function 
  458.              // fine whether memory was purged or not.
  459.          SetErrorCode(kODNoError);   // DR5 code added
  460.          runningTotal = 0;   // DR5 code added
  461.     }
  462.     return runningTotal;
  463. }
  464.  
  465. //------------------------------------------------------------------------------
  466. // CMStorageUnit: Exists
  467. //------------------------------------------------------------------------------
  468.  
  469. SOM_Scope ODBoolean     SOMLINK CMStorageUnitExists(CMStorageUnit *somSelf, Environment *ev,
  470.         ODPropertyName propertyName,
  471.                                     ODValueType valueType, 
  472.                                     ODValueIndex valueIndex)
  473. {
  474.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  475.     CMStorageUnitMethodDebug("CMStorageUnit","Exists");
  476.  
  477.     ODBoolean        valueFound = kODFalse;
  478.  
  479.     try
  480.     {
  481.     CMContainer        container = somSelf->GetCMContainer(ev);
  482.     ODBoolean        propertyFound = kODFalse;
  483.     CMProperty        targetProperty;
  484.     CMProperty        property;
  485.     CMType            targetType;
  486.     CMValue            value;
  487.     CMValue            tmpValue;
  488.     ODValueIndex    index;
  489.  
  490.     FailIfInvalidRefCount();
  491.     somSelf->Internalize(ev);
  492.         
  493.     if (_fObject == kODNULL)
  494.         valueFound =  kODFalse;
  495.  
  496.     ODSessionMustHaveCMAllocReserve(container);
  497.  
  498.     if (propertyName != kODNULL) {
  499.         targetProperty = CMRegisterProperty(container, propertyName);
  500.         if (targetProperty == kODNULL)
  501.             BREAK_AND_THROW(kODErrIllegalPropertyName);
  502.     }
  503.     else
  504.         targetProperty = _fCurProperty;
  505.     
  506.     if (valueType != kODNULL) {
  507.         targetType = CMRegisterType(container, valueType);
  508.         if (targetType == kODNULL)
  509.             BREAK_AND_THROW(kODErrInvalidValueType);
  510.             
  511.         value = CMUseValue(_fObject, targetProperty, targetType);
  512.         if (value != kODNULL) {
  513.             CMReleaseValue(value);
  514.             valueFound = kODTrue;
  515.         }
  516.     }
  517.     else if (valueIndex > 0) {
  518.         index = 1;
  519.         value = CMGetNextValue(_fObject, targetProperty, kODNULL);
  520.         while ((value != kODNULL) && (index < valueIndex)) {
  521.             index++;
  522.             tmpValue = value;
  523.             value = CMGetNextValue(_fObject, targetProperty, tmpValue);
  524.             CMReleaseValue(tmpValue);
  525.         }
  526.         if (value != kODNULL) {
  527.             CMReleaseValue(value);
  528.             if (index == valueIndex)
  529.                 valueFound = kODTrue;
  530.         }
  531.     }
  532.     else {
  533.         if (propertyName != kODNULL) {
  534.             property = CMGetNextObjectProperty(_fObject, kODNULL);
  535.             while ((property != kODNULL) && (valueFound == kODFalse)) {
  536.                 if (targetProperty == property)
  537.                     valueFound = kODTrue;
  538.                 property = CMGetNextObjectProperty(_fObject, property);
  539.             }
  540.         }
  541.         else
  542.             valueFound = kODTrue;
  543.     }
  544.     ODSessionRestoreCMAllocReserve(container);
  545.  
  546.  
  547.     }
  548.     catch (ODException _exception)
  549.     {
  550.            valueFound  = kODFalse;
  551.            ODSetSOMException(ev, _exception);
  552.     }
  553.     return    valueFound;
  554. }
  555.  
  556. //------------------------------------------------------------------------------
  557. // CMStorageUnit: Exists
  558. //------------------------------------------------------------------------------
  559.  
  560. SOM_Scope ODBoolean     SOMLINK CMStorageUnitExistsWithCursor(CMStorageUnit *somSelf, Environment *ev,
  561.         ODStorageUnitCursor* cursor)
  562. {
  563.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  564.     CMStorageUnitMethodDebug("CMStorageUnit","ExistsWithCursor");
  565.  
  566.     ODBoolean        exists = kODFalse;
  567.     ODPropertyName propertyName = kODNULL;
  568.     ODValueType    valueType = kODNULL;
  569.  
  570.     try
  571.     {
  572.     ODValueIndex    valueIndex;
  573.  
  574.     FailIfInvalidRefCount();
  575.     somSelf->Internalize(ev);
  576.     
  577.     cursor->GetProperty(ev, &propertyName);
  578.     cursor->GetValueType(ev, &valueType);
  579.     cursor->GetValueIndex(ev, &valueIndex);
  580.  
  581.     exists = somSelf->Exists(ev, propertyName, valueType, valueIndex);
  582.     
  583.     ODDisposePtr(propertyName);
  584.     ODDisposePtr(valueType);
  585.  
  586.  
  587.     }
  588.     catch (ODException _exception)
  589.     {
  590.       if(propertyName)
  591.         ODDisposePtr(propertyName);
  592.       if(valueType)
  593.         ODDisposePtr(valueType);
  594.     exists  = kODFalse;
  595.         ODSetSOMException(ev, _exception);
  596.     }
  597.     return exists;
  598. }
  599.  
  600. //------------------------------------------------------------------------------
  601. // CMStorageUnit: CountProperties
  602. //------------------------------------------------------------------------------
  603.  
  604. SOM_Scope ODULong     SOMLINK CMStorageUnitCountProperties(CMStorageUnit *somSelf, Environment *ev)
  605. {
  606.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  607.     CMStorageUnitMethodDebug("CMStorageUnit","CountProperties");
  608.           
  609.         ODULong CountProps = 0;
  610.     
  611.     try
  612.     {
  613.     FailIfInvalidRefCount();
  614.     somSelf->Internalize(ev);
  615.  
  616.     CMContainer container = _fDraft->GetCMContainer(ev);
  617.     ODSessionMustHaveCMAllocReserve(container);
  618.     
  619.     CountProps =  CMCountProperties(_fObject, kODNULL);
  620.  
  621.     }
  622.     catch(ODException _exception)
  623.     {
  624.            CountProps =  0;
  625.            ODSetSOMException(ev, _exception);
  626.     }
  627.         return CountProps;
  628. }
  629.  
  630. //------------------------------------------------------------------------------
  631. // CMStorageUnit: CountValues
  632. //------------------------------------------------------------------------------
  633.  
  634. SOM_Scope ODULong     SOMLINK CMStorageUnitCountValues(CMStorageUnit *somSelf, Environment *ev)
  635. {
  636.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  637.     CMStorageUnitMethodDebug("CMStorageUnit","CountValues");
  638.     
  639.     ODULong numValues = 0;
  640.     
  641.     try
  642.         {
  643.     
  644.         FailIfInvalidRefCount();
  645.         somSelf->Internalize(ev);
  646.         
  647.         if (_fCurProperty == kODNULL)
  648.             THROW(kODErrUnfocusedStorageUnit);
  649.     
  650.         CMContainer container = _fDraft->GetCMContainer(ev);
  651.         ODSessionMustHaveCMAllocReserve(container);
  652.         
  653.         numValues = CMCountValues(_fObject, _fCurProperty, kODNULL);
  654.         
  655.         }
  656.         catch (ODException _exception)
  657.         {
  658.             numValues = 0;
  659.             ODSetSOMException(ev, _exception);
  660.         }
  661.     
  662.     return numValues;
  663. }
  664.         
  665. //------------------------------------------------------------------------------
  666. // CMStorageUnit: Focus
  667. //------------------------------------------------------------------------------
  668.  
  669. SOM_Scope ODStorageUnit*     SOMLINK CMStorageUnitFocus(CMStorageUnit *somSelf, Environment *ev,
  670.         ODPropertyName propertyName,
  671.                                     ODPositionCode propertyPosCode,
  672.                                     ODValueType valueType,
  673.                                     ODValueIndex valueIndex,
  674.                                     ODPositionCode    valuePosCode)
  675. {
  676.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  677.     CMStorageUnitMethodDebug("CMStorageUnit","Focus");
  678.  
  679.     
  680.     try
  681.     {
  682.     FailIfInvalidRefCount();
  683.     somSelf->Internalize(ev);
  684.     
  685.     CMProperty    property = _fCurProperty;
  686.     CMValue        value = _fCurValue;
  687.  
  688.                                                                     // FEB-13
  689. #ifdef _PLATFORM_OS2_                                               //116316
  690.         ODBoolean       fReleaseValue = kODTrue;                    //116316
  691. #endif                                                              //116316
  692.     
  693.     CMContainer container = _fDraft->GetCMContainer(ev);
  694.     ODSessionMustHaveCMAllocReserve(container);
  695.     
  696.     if (propertyName != kODNULL) {
  697.         property = CMRegisterProperty(container, propertyName);
  698.         if (property == kODNULL) {
  699. #ifdef ODDebug_ODStorageUnit
  700.     somPrintf("Error in Focus: ID %x PID %x propertyName %s\n", somSelf->GetID(ev), _fObjectID, propertyName);
  701. #endif
  702.             somSelf->CleanupAndFail(ev, kODErrIllegalPropertyName);
  703.         }
  704.     }
  705.     else {
  706.         _fHasPropertyLooped = propertyPosCode & kODPosMWrap;
  707.         switch (propertyPosCode) {
  708.             case kODPosUndefined:
  709.                 somSelf->CleanupAndFail(ev, kODErrUnsupportedPosCode);
  710.                 break;
  711.             case kODPosSame:
  712.                                                                     // FEB-13
  713. #ifdef _PLATFORM_OS2_                                               //116316
  714.                                 fReleaseValue = kODFalse;           //116316
  715. #endif                                                              //116316
  716.                 break;
  717.             case kODPosAll:
  718.                 property = kODNULL;
  719.                 break;
  720.             case kODPosFirstSib:
  721.                 property = CMGetNextObjectProperty(_fObject, kODNULL);
  722.                 if (property == kODNULL)
  723.                     somSelf->CleanupAndFail(ev, kODErrPropertyDoesNotExist);
  724.                 break;
  725.             case kODPosLastSib:
  726.                 property = CMGetPrevObjectProperty(_fObject, kODNULL);
  727.                 if (property == kODNULL)
  728.                     somSelf->CleanupAndFail(ev, kODErrPropertyDoesNotExist);
  729.                 break;
  730.             case kODPosNextSib:
  731.                 property = CMGetNextObjectProperty(_fObject, property);
  732.                 if (property == kODNULL)
  733.                     somSelf->CleanupAndFail(ev, kODErrPropertyDoesNotExist);
  734.                 break;
  735.             case kODPosPrevSib:
  736.                 property = CMGetPrevObjectProperty(_fObject, property);
  737.                 if (property == kODNULL)
  738.                     somSelf->CleanupAndFail(ev, kODErrPropertyDoesNotExist);
  739.                 break;
  740.             case kODPosFirstBelow:
  741.             case kODPosLastBelow:
  742.             case kODPosFirstAbove:
  743.             case kODPosLastAbove:
  744.             default:
  745.                 somSelf->CleanupAndFail(ev, kODErrUnsupportedPosCode);
  746.                 break;
  747.         }
  748.     }
  749.     
  750.     ODBoolean needReleaseValue = kODTrue;
  751.  
  752.     if (valueType != kODNULL) {
  753.         CMType type = kODNULL;
  754.         
  755.         CMType targetType = CMRegisterType(container, valueType);
  756.         
  757.         value = CMUseValue(_fObject, property, targetType);
  758.         if (value == kODNULL)
  759.             somSelf->CleanupAndFail(ev, kODErrSUValueDoesNotExist);
  760.             
  761.     } else if (valueIndex > 0) {
  762.         ODULong        i = 1;
  763.         CMValue        tmpValue;
  764.  
  765.         value = CMGetNextValue(_fObject, property, kODNULL);
  766.         while ((value != kODNULL) && (i < valueIndex)) {
  767.             tmpValue = value;
  768.             value = CMGetNextValue(_fObject, property, tmpValue);
  769.             CMReleaseValue(tmpValue);
  770.             i++;
  771.         }        
  772.         if (value == kODNULL)
  773.             somSelf->CleanupAndFail(ev, kODErrValueIndexOutOfRange);
  774.     }
  775.     else {
  776.         _fHasValueLooped = valuePosCode & kODPosMWrap;
  777.         switch (valuePosCode) {
  778.             case kODPosUndefined:
  779.                 value = kODNULL;
  780.             break;
  781.             case kODPosAll:
  782.                 value = kODNULL;
  783.                 break;
  784.             case kODPosFirstSib:
  785.                 value = CMGetNextValue(_fObject, property, kODNULL);
  786.                 if (value == kODNULL)
  787.                     somSelf->CleanupAndFail(ev, kODErrSUValueDoesNotExist);
  788.                 break;
  789.             case kODPosNextSib:
  790.                 value = CMGetNextValue(_fObject, property, value);
  791.                 if (value == kODNULL)
  792.                     somSelf->CleanupAndFail(ev, kODErrSUValueDoesNotExist);
  793.                 break;
  794.             case kODPosSame:
  795.                 needReleaseValue = kODFalse;
  796.                 break;
  797.             case kODPosLastSib:
  798.                 value = CMGetPrevValue(_fObject, property, kODNULL);
  799.                 if (value == kODNULL)
  800.                     somSelf->CleanupAndFail(ev, kODErrSUValueDoesNotExist);
  801.                 break;
  802.             case kODPosPrevSib:
  803.                 value = CMGetPrevValue(_fObject, property, value);
  804.                 if (value == kODNULL)
  805.                     somSelf->CleanupAndFail(ev, kODErrSUValueDoesNotExist);
  806.                 break;
  807.             case kODPosFirstBelow:
  808.             case kODPosLastBelow:
  809.             case kODPosFirstAbove:
  810.             case kODPosLastAbove:
  811.             default:
  812.                 somSelf->CleanupAndFail(ev, kODErrUnsupportedPosCode);
  813.                 break;
  814.         }
  815.     }
  816.     
  817.                                                                     // FEB-13
  818. #ifdef _PLATFORM_OS2_                                               //116316
  819.       if (fReleaseValue == kODTrue )                                //116316
  820. #else
  821.     if (needReleaseValue != kODFalse) 
  822. #endif
  823.         if (_fCurValue != kODNULL)
  824.             CMReleaseValue(_fCurValue);
  825.     
  826.     _fOffset = 0;
  827.     _fCurValue = value;
  828.     _fCurProperty = property;
  829.  
  830.     ODSessionRestoreCMAllocReserve(container);
  831.     
  832.  
  833.     }
  834.     catch (ODException _exception)
  835.     {
  836.        ODSetSOMException(ev, _exception);
  837.     }
  838.     return somSelf;
  839. }
  840.  
  841. //------------------------------------------------------------------------------
  842. // CMStorageUnit: Focus
  843. //------------------------------------------------------------------------------
  844.  
  845. SOM_Scope ODStorageUnit*     SOMLINK CMStorageUnitFocusWithCursor(CMStorageUnit *somSelf, Environment *ev,
  846.         ODStorageUnitCursor* suCursor)
  847. {
  848.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  849.     CMStorageUnitMethodDebug("CMStorageUnit","FocusWithCursor");
  850.     
  851.     ODPropertyName    propertyName = kODNULL;
  852.     ODValueType    valueType    = kODNULL;
  853.     
  854.     try
  855.     {
  856.     ODValueIndex    valueIndex;
  857.     
  858.     FailIfInvalidRefCount();
  859.     somSelf->Internalize(ev);
  860.  
  861.     if (suCursor == kODNULL)
  862.         BREAK_AND_THROW(kODErrIllegalNullSUCursorInput);
  863.  
  864.     suCursor->GetProperty(ev, &propertyName);
  865.     suCursor->GetValueType(ev, &valueType);
  866.     suCursor->GetValueIndex(ev, &valueIndex);
  867.     
  868.     somSelf->Focus(ev, propertyName,
  869.                 kODPosAll,
  870.                 valueType,
  871.                 valueIndex,
  872.                 kODPosAll);
  873.                 
  874.     if (propertyName != kODNULL)
  875.         ODDisposePtr(propertyName);
  876.         
  877.     if (valueType != kODNULL)
  878.         ODDisposePtr(valueType);
  879.     
  880.  
  881.     }
  882.     catch (ODException _exception)
  883.     {
  884.       if (propertyName != kODNULL)
  885.     ODDisposePtr(propertyName);
  886.  
  887.       if (valueType != kODNULL)
  888.     ODDisposePtr(valueType);
  889.  
  890.         ODSetSOMException(ev, _exception);
  891.     }
  892.    
  893.     return somSelf;
  894. }
  895.  
  896. //------------------------------------------------------------------------------
  897. // CMStorageUnit: Externalize
  898. //------------------------------------------------------------------------------
  899.  
  900. SOM_Scope ODStorageUnit*     SOMLINK CMStorageUnitExternalize(CMStorageUnit *somSelf, Environment *ev)
  901. {
  902.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  903.     CMStorageUnitMethodDebug("CMStorageUnit","Externalize");
  904.  
  905.  
  906.    try
  907.    {
  908.     PreserveFocus    originalFocus(ev, somSelf);
  909.  
  910.     if (_fDirty) {    
  911.         somSelf->ResolveAllPromises(ev);
  912.     
  913.         if (_fSURefKeeper != kODNULL)
  914.             _fSURefKeeper->Externalize();
  915.         
  916.         if (somSelf->Exists(ev, kODPropModDate, kODTime_T, 0)) {
  917.             ODSetTime_TProp(ev, somSelf, kODPropModDate, kODTime_T, (ODTime) _fModificationDate);
  918.         }
  919.         
  920.         _fDirty = kODFalse;
  921.     }
  922.  
  923.      }
  924.      catch (ODException _exception)
  925.      {
  926.          ODSetSOMException(ev, _exception);
  927.      }
  928.     return somSelf;
  929. }
  930.  
  931. //------------------------------------------------------------------------------
  932. // CMStorageUnit: Internalize
  933. //------------------------------------------------------------------------------
  934.  
  935. SOM_Scope ODStorageUnit*     SOMLINK CMStorageUnitInternalize(CMStorageUnit *somSelf, Environment *ev)
  936. {
  937.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  938.     CMStorageUnitMethodDebug("CMStorageUnit","Internalize");
  939.  
  940.         
  941.     try
  942.     {
  943.     if (_fObject == kODNULL) {
  944.  
  945.                 CMDraft* tempDraft = (CMDraft*) somSelf->GetDraft(ev);
  946.         IDList*    idList = kODNULL;
  947.                 if(tempDraft)
  948.                      idList =  tempDraft->GetIDList(ev);
  949.         ASSERT(idList != kODNULL, kODErrInvalidIDList);
  950.                 
  951.         if (idList->Exists(_fID) != kODFalse) {            
  952.             _fObject = (CMObject) idList->Get(_fID);
  953.         }
  954.         else if (_fObjectID != kODNULL) {
  955.             CMContainer container = _fDraft->GetCMContainer(ev);
  956.             ODSessionMustHaveCMAllocReserve(container);
  957.             
  958.             _fObject = CMGetObject(container, _fObjectID);
  959.  
  960.             ODSessionRestoreCMAllocReserve(container);
  961.             
  962. //            if (_fObject == kODNULL)
  963. //                BREAK_AND_THROW(kODErrBentoCannotNewObject);
  964.             if (_fObject != kODNULL)
  965.                 idList->Add(_fID, _fObject);
  966.         }
  967.         else
  968.             BREAK_AND_THROW(kODErrInvalidStorageUnit);
  969.     }
  970.         
  971.  
  972.      }
  973.      catch( ODException _exception)
  974.      {
  975.           ODSetSOMException(ev, _exception);
  976.      }
  977.  
  978.     return somSelf;
  979. }
  980.  
  981. //------------------------------------------------------------------------------
  982. // CMStorageUnit: GetID
  983. //------------------------------------------------------------------------------
  984.  
  985. SOM_Scope ODStorageUnitID  SOMLINK CMStorageUnitGetID(CMStorageUnit *somSelf, Environment *ev)
  986. {
  987.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  988.     CMStorageUnitMethodDebug("CMStorageUnit","GetID");
  989.  
  990.     // Do not call fail if RefCount is zero because GetID can be called
  991.     // from Remove where RefCount MUST be zero -pjh
  992.     // FailIfInvalidRefCount();
  993.     
  994.     return _fID;
  995. }
  996.  
  997. //------------------------------------------------------------------------------
  998. // CMStorageUnit: GetName
  999. //------------------------------------------------------------------------------
  1000.  
  1001. SOM_Scope ODStorageUnitName  SOMLINK CMStorageUnitGetName(CMStorageUnit *somSelf, Environment *ev)
  1002. {
  1003.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1004.     CMStorageUnitMethodDebug("CMStorageUnit","GetName");
  1005.     
  1006.         ODStorageUnitName ODSUName = kODNULL;
  1007.     try
  1008.     {
  1009.     
  1010.     FailIfInvalidRefCount();
  1011.     ODSUName = ODGetISOStrProp(ev, somSelf, kODPropStorageUnitName, kODISOStr, kODNULL, kODNULL);
  1012.  
  1013.    }
  1014.    catch (ODException _exception)
  1015.    {
  1016.     ODSUName = kODNULL;
  1017.         ODSetSOMException(ev, _exception);
  1018.    }
  1019.         return ODSUName;
  1020. }
  1021.     
  1022. //------------------------------------------------------------------------------
  1023. // CMStorageUnit: SetName
  1024. //------------------------------------------------------------------------------
  1025.  
  1026. SOM_Scope void  SOMLINK CMStorageUnitSetName(CMStorageUnit *somSelf, Environment *ev,
  1027.         ODStorageUnitName name)
  1028. {
  1029.     // CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1030.     CMStorageUnitMethodDebug("CMStorageUnit","SetName");
  1031.  
  1032.             
  1033.    try
  1034.    {
  1035.     FailIfInvalidRefCount();
  1036.         
  1037.     ODSetISOStrProp(ev, somSelf, kODPropStorageUnitName, kODISOStr, name);
  1038.  
  1039.    }
  1040.    catch (ODException _exception)
  1041.    {
  1042.         ODSetSOMException(ev, _exception);
  1043.    }
  1044. }
  1045.  
  1046. //------------------------------------------------------------------------------
  1047. // CMStorageUnit: AddProperty
  1048. //------------------------------------------------------------------------------
  1049.  
  1050. SOM_Scope ODStorageUnit*     SOMLINK CMStorageUnitAddProperty(CMStorageUnit *somSelf, Environment *ev,
  1051.         ODPropertyName propertyName)    
  1052. {
  1053.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1054.     CMStorageUnitMethodDebug("CMStorageUnit","AddProperty");
  1055.  
  1056.    try
  1057.    {
  1058.  
  1059. #ifdef ODDebug_ODStorageUnit
  1060.     somPrintf("AddProperty %x %x %s\n", _fID, _fObjectID, propertyName);
  1061. #endif
  1062.     
  1063.     CMContainer container = somSelf->GetCMContainer(ev);    
  1064.     CMProperty    property = kODNULL;
  1065.     
  1066.     FailIfInvalidRefCount();
  1067.     
  1068.     somSelf->Internalize(ev);
  1069.     
  1070.     if (propertyName == kODNULL)
  1071.         BREAK_AND_THROW(kODErrIllegalNullPropertyInput);
  1072.     if (propertyName != kODNULL) {
  1073.  
  1074.         ODSessionMustHaveCMAllocReserve(container);
  1075.         
  1076.         property = CMRegisterProperty(container,propertyName);
  1077.         if (property == kODNULL) {
  1078.             somSelf->CleanupAndFail(ev, kODErrCannotAddProperty);
  1079.         }
  1080.         else {
  1081.         
  1082. #if ODDebug_AddProperty
  1083.             CMProperty tmpProp = kODNULL;
  1084.             do {
  1085.                 ODSessionMustHaveCMAllocReserve(container); // safest once per loop
  1086.                 tmpProp = CMGetNextObjectProperty(_fObject, tmpProp);
  1087.             } while ((tmpProp != kODNULL) && (tmpProp != property));
  1088.             
  1089.             if (tmpProp == property) {
  1090.                 WARNMSG(WARN_INDEX(AMSG_210),"Property exists already.");
  1091.                 somPrintf("propertyName %s exists already.\n", propertyName);
  1092.             }
  1093. #endif
  1094.  
  1095.             if (_fCurValue != kODNULL) {
  1096.                 CMReleaseValue(_fCurValue);
  1097.                 _fCurValue = kODIDAll;
  1098.             }
  1099.             _fCurProperty = property;
  1100.             somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  1101.         }
  1102.         ODSessionRestoreCMAllocReserve(container);
  1103.     }
  1104.  
  1105.     }
  1106.     catch (ODException _exception)
  1107.     {
  1108.         ODSetSOMException(ev, _exception);
  1109.     }
  1110.     return somSelf;
  1111. }
  1112.  
  1113.     
  1114. //------------------------------------------------------------------------------
  1115. // CMStorageUnit: AddValue
  1116. //------------------------------------------------------------------------------
  1117.  
  1118. SOM_Scope ODStorageUnit*  SOMLINK CMStorageUnitAddValue(CMStorageUnit *somSelf, Environment *ev,
  1119.         ODValueType type)
  1120. {
  1121.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1122.     CMStorageUnitMethodDebug("CMStorageUnit","AddValue");
  1123.  
  1124.  
  1125.     try
  1126.     {
  1127.     CMContainer    container = somSelf->GetCMContainer(ev);
  1128.     char*        dummyBuffer = "";
  1129.     CMValue        value;
  1130.     
  1131.     FailIfInvalidRefCount();
  1132.     somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  1133.     
  1134.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL))
  1135.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  1136.     if (type == kODNULL)
  1137.         BREAK_AND_THROW(kODErrIllegalNullValueTypeInput);
  1138.     else {
  1139.         ODSessionMustHaveCMAllocReserve(container);
  1140.  
  1141.         _fCurType = CMRegisterType(container, type);
  1142.         if (_fCurType == kODNULL)
  1143.             BREAK_AND_THROW(kODErrInvalidValueType);
  1144.         value = CMUseValue(_fObject, _fCurProperty, _fCurType);
  1145.         if (value == kODNULL) {
  1146.             value = CMNewValue(_fObject, _fCurProperty, _fCurType);
  1147.             if (value == kODNULL)
  1148.                 somSelf->CleanupAndFail(ev, kODErrBentoCannotNewValue);
  1149.             CMWriteValueData(value, dummyBuffer, 0, 0);
  1150.         }
  1151. #if ODDebug_AddValue
  1152.         else {
  1153.             WARNMSG(WARN_INDEX(AMSG_220),"Value exists already.");
  1154.             CMGlobalName propName = CMGetGlobalName(_fCurProperty);
  1155.             somPrintf("Property %s Value %s exists already.\n", propName, type);
  1156.         }
  1157. #endif
  1158.         if (_fCurValue != kODNULL)
  1159.             CMReleaseValue(_fCurValue);
  1160.         _fCurValue = value;
  1161.         _fOffset = 0;
  1162.  
  1163.         ODSessionRestoreCMAllocReserve(container);
  1164.     }
  1165.  
  1166.     }
  1167.     catch (ODException _exception)
  1168.     {
  1169.          ODSetSOMException(ev, _exception);
  1170.     }
  1171.     return somSelf;
  1172. }
  1173.  
  1174. //------------------------------------------------------------------------------
  1175. // CMStorageUnit: Remove
  1176. //------------------------------------------------------------------------------
  1177.  
  1178. SOM_Scope ODStorageUnit*     SOMLINK CMStorageUnitRemove(CMStorageUnit *somSelf, Environment *ev)
  1179. {
  1180.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1181.     CMStorageUnitMethodDebug("CMStorageUnit","Remove");
  1182.  
  1183.  
  1184.    try
  1185.    {
  1186.     FailIfInvalidRefCount();
  1187.     somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  1188.  
  1189.     CMContainer container = _fDraft->GetCMContainer(ev);
  1190.     ODSessionMustHaveCMAllocReserve(container);
  1191.  
  1192.     if (_fCurValue != kODNULL) {
  1193.         if ((_fPromiseResolver != kODNULL) && (_fPromiseResolver->IsSettingPromise() == kODFalse))
  1194.             _fPromiseResolver->ClearPromise(ev);
  1195.         CMDeleteValue(_fCurValue);
  1196.         _fCurValue = kODNULL;
  1197.     }
  1198.     else if (_fCurProperty != kODNULL) {
  1199.         CMDeleteObjectProperty(_fObject, _fCurProperty);
  1200.         _fCurProperty = kODNULL;
  1201.     }
  1202.     else {
  1203.         CMProperty curProperty = CMGetNextObjectProperty(_fObject, kODNULL);
  1204.         while (curProperty != kODNULL) {
  1205.             CMGlobalName name = CMGetGlobalName(curProperty);
  1206.             CMProperty property = curProperty;
  1207.             curProperty = CMGetNextObjectProperty(_fObject, curProperty);
  1208.             if (ODISOStrNCompare(name, kODBentoPrefix, ODISOStrLength(kODBentoPrefix)) != 0) {
  1209.                 CMDeleteObjectProperty(_fObject, property);
  1210.             }
  1211.         }
  1212.     }
  1213.     ODSessionRestoreCMAllocReserve(container);
  1214.     
  1215.  
  1216.     }
  1217.     catch (ODException _exception)
  1218.     {
  1219.        ODSetSOMException(ev, _exception);
  1220.     }
  1221.     return somSelf;
  1222. }
  1223.  
  1224. //------------------------------------------------------------------------------
  1225. // CMStorageUnit: CloneInto
  1226. //------------------------------------------------------------------------------
  1227.  
  1228. SOM_Scope void  SOMLINK CMStorageUnitCloneInto(CMStorageUnit *somSelf, Environment *ev,
  1229.         ODDraftKey key, ODStorageUnit* toSU, ODID scopeID)
  1230. {
  1231.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1232.     CMStorageUnitMethodDebug("CMStorageUnit","CloneInto");
  1233.     ODPropertyName    propertyName;
  1234.     CMGlobalName    typeName;
  1235.  
  1236.     try
  1237.     {
  1238.  
  1239. #ifdef USE_CLONEHELPER
  1240.     if (_fSUCloneHelper->ShouldClone(key, scopeID) != kODFalse) {
  1241. #endif
  1242. #ifdef ODDebug_SUCloneHelper
  1243.         somPrintf("Cloning %d in scope %d\n", somSelf->GetID(ev), scopeID);
  1244. #endif
  1245.  
  1246. #ifdef ODDebug_ODStorageUnit
  1247.     somPrintf("CMSU CloneInto: from %x %x to %x %x scope %x\n",
  1248.         _fID, _fObjectID, toSU->GetID(ev), ((CMStorageUnit*) toSU)->GetObjectID(ev), scopeID);
  1249. #endif
  1250.     
  1251.     if (((CMDraft*) somSelf->GetDraft(ev))->IsValidDraftKey(ev, key) == kODFalse)
  1252.         THROW(kODErrInvalidDraftKey);
  1253.  
  1254.     FailIfInvalidRefCount();
  1255.         
  1256.     ODULong            numProperties;
  1257.     ODULong            numValues;
  1258.     ODULong            i;
  1259.     ODULong            j;
  1260.     ODULong            tmpSize;
  1261.     ODULong            size;
  1262.     ODPtr            buffer;
  1263.     ODStorageUnitCursor*    originalFocus = kODNULL;
  1264.     
  1265.     somSelf->Internalize(ev);
  1266.  
  1267.     if (_fCurProperty != kODNULL)
  1268.         originalFocus = somSelf->CreateCursorWithFocus(ev);
  1269.         
  1270.     somSelf->Focus(ev, (ODPropertyName) kODNULL, 
  1271.                             kODPosAll,
  1272.                             kODTypeAll,
  1273.                             0,
  1274.                             kODPosUndefined);
  1275.                             
  1276.     numProperties = somSelf->CountProperties(ev);
  1277.     
  1278.     for (i = 0; i < numProperties; i++) {
  1279.     
  1280.         somSelf->Focus(ev, (ODPropertyName) kODNULL,
  1281.                         kODPosNextSib,
  1282.                         kODTypeAll,
  1283.                         0,
  1284.                         kODPosUndefined);
  1285.     
  1286.         propertyName = somSelf->GetProperty(ev);
  1287.         
  1288. #if ODDebug_AddProperty
  1289.         if (toSU->Exists(ev, propertyName, kODNULL, 0) != kODFalse) {
  1290.             toSU->Focus(ev, propertyName, kODPosUndefined, kODNULL, 0, kODPosUndefined);
  1291.         }
  1292.         else
  1293.             toSU->AddProperty(ev, propertyName);
  1294. #else
  1295.         toSU->AddProperty(ev, propertyName);
  1296. #endif
  1297.     
  1298.         numValues = somSelf->CountValues(ev);    
  1299.         
  1300.         for (j = 0; j < numValues; j++) {
  1301.  
  1302.             somSelf->Focus(ev, (ODPropertyName) kODNULL,
  1303.                             kODPosSame,
  1304.                             kODNULL,
  1305.                             0,
  1306.                             kODPosNextSib);
  1307.  
  1308.             typeName = somSelf->GetType(ev);
  1309.             
  1310.             // Do no overrite existing values except for the storage unit type property.
  1311.             // New storage units are created with a generic storage unit type which should
  1312.             // be overwritten.  The link iterators currently use the storage unit type
  1313.             // property to identify link and linkSource storage units.
  1314.             if ((ODISOStrCompare(propertyName, (const ODISOStr) kODPropStorageUnitType) == 0)
  1315.                 || (toSU->Exists(ev, propertyName, typeName, 0) == kODFalse)) { 
  1316.  
  1317. #ifdef ODDebug_ODStorageUnit            
  1318.                 somPrintf("Copying from %x %x %s %s to %x %x\n", 
  1319.                     _fID, _fObjectID, propertyName, typeName, toSU->GetID(ev), ((CMStorageUnit*) toSU)->GetObjectID(ev));
  1320. #endif
  1321.  
  1322. #if ODDebug_AddValue
  1323.                 if (toSU->Exists(ev, propertyName, typeName, 0) != kODFalse) {
  1324.                     toSU->Focus(ev, propertyName, kODPosUndefined, typeName, 0, kODPosUndefined);
  1325.                 }
  1326.                 else
  1327.                     toSU->AddValue(ev, typeName);
  1328. #else                
  1329.                 toSU->AddValue(ev, typeName);
  1330. #endif
  1331.                 
  1332.                 tmpSize = toSU->GetSize(ev);
  1333.                 toSU->DeleteValue(ev, tmpSize);
  1334.                 
  1335.                 PreserveFocus* focus = new PreserveFocus(ev, somSelf);
  1336.                 size = somSelf->GetSize(ev);
  1337.                 delete focus;
  1338.                 numValues = somSelf->CountValues(ev);
  1339.                 numProperties = somSelf->CountProperties(ev);
  1340.                 
  1341. #ifdef _PLATFORM_MACINTOSH_
  1342.                 buffer = ODNewPtr(size, somSelf->GetHeap(ev));
  1343. #endif
  1344. #if defined(_PLATFORM_WIN32_)||defined(_PLATFORM_OS2_)||defined(_PLATFORM_AIX_)
  1345. //                The use of heap is not implemented in Windows
  1346. //                platform
  1347.                 buffer = ODNewPtr(size);
  1348. #endif
  1349.                 StorageUnitGetValue(somSelf, ev, size, (ODValue) buffer);
  1350.                 StorageUnitSetValue(toSU, ev, size, (ODValue) buffer);
  1351.                 ODDisposePtr(buffer);
  1352.             }
  1353.             else {
  1354.                 toSU->Focus(ev, propertyName,
  1355.                             kODPosUndefined,
  1356.                             typeName,
  1357.                             0,
  1358.                             kODPosUndefined);
  1359.             }
  1360.             
  1361.             ODStorageUnitRefIterator*    iter;
  1362.             ODStorageUnitRef            ref;
  1363.  
  1364.             iter = somSelf->CreateStorageUnitRefIterator(ev);                
  1365.                 for(iter->First(ev, ref); iter->IsNotComplete(ev) != kODFalse; iter->Next(ev, ref)) {
  1366.                                 ODBoolean ValidRefFound = kODFalse;
  1367.                                 try{
  1368.                                   ValidRefFound = somSelf->IsValidStorageUnitRef(ev, ref);
  1369.                                 }
  1370.                                 catch(ODException _exception){
  1371.                                   somExceptionFree(ev);  SOM_InitEnvironment(ev);
  1372.                                   SetErrorCode(kODNoError);
  1373.                                 }
  1374.                                 
  1375.                 if (ValidRefFound) {                
  1376.                     ODStorageUnitID            toEmbeddedID = 0;
  1377.                     ODStorageUnitID            containingFrameID = 0;
  1378.                     ODBoolean                strongClone = somSelf->IsStrongStorageUnitRef(ev, ref);
  1379.                     ODStorageUnitID            fromEmbeddedID = somSelf->GetIDFromStorageUnitRef(ev, ref);
  1380.     
  1381. // Do *not* change the value of scopeID, because this will
  1382. // interfere with the cloning of subsequent values and
  1383. // properties. (1371928)
  1384.                                                ODID subScopeID = scopeID; // possible new scope
  1385.                                                if (subScopeID != 0) {
  1386.  
  1387. #ifdef _NO_TMP_OBJS_
  1388.                         ODStorageUnit *fromEmbeddedSU = _fDraft->AcquireStorageUnit(ev, fromEmbeddedID);
  1389. #else
  1390.                         TempODStorageUnit fromEmbeddedSU = _fDraft->AcquireStorageUnit(ev, fromEmbeddedID);
  1391. #endif
  1392.                         if ((containingFrameID = ODGetWeakSURefProp(ev, fromEmbeddedSU, kODPropContainingFrame, kODWeakStorageUnitRef))
  1393.                             != kODNULLID)
  1394.                         {                                
  1395.                             if (containingFrameID == subScopeID)
  1396.                                 subScopeID = fromEmbeddedID;
  1397.                             else
  1398.                                 strongClone = kODFalse;
  1399.                         }
  1400. #ifdef _NO_TMP_OBJS_
  1401.                         fromEmbeddedSU->Release(ev);
  1402. #endif
  1403.                     }
  1404.                     
  1405.                     if (strongClone != kODFalse) {
  1406. #ifdef ODDebug_ODStorageUnit
  1407.                         somPrintf("{{{ Strong Clone %x Begins\n", fromEmbeddedID);
  1408. #endif
  1409.                             toEmbeddedID = _fDraft->Clone(ev, key, fromEmbeddedID, 0, subScopeID);
  1410. #ifdef ODDebug_ODStorageUnit
  1411.                         somPrintf("}}} Strong Clone %x Ends %x\n", fromEmbeddedID, toEmbeddedID);
  1412. #endif
  1413.                     }
  1414.                     else {
  1415. #ifdef ODDebug_ODStorageUnit
  1416.                         somPrintf("Begin Weak Clone from %x\n", fromEmbeddedID);
  1417. #endif
  1418.                             toEmbeddedID = _fDraft->WeakClone(ev, key, fromEmbeddedID, 0, subScopeID);
  1419. #ifdef ODDebug_ODStorageUnit
  1420.                         somPrintf("End Weak Clone from %x %x to %x %x\n", fromEmbeddedID, toEmbeddedID);
  1421. #endif
  1422.                     }
  1423.                     if (toEmbeddedID != 0) {
  1424.     
  1425. #ifdef ODDebug_ODStorageUnit
  1426.     ODPropertyName    tmpPropertyName = somSelf->GetProperty(ev);
  1427.     ODValueType    tmpTypeName = somSelf->GetType(ev);
  1428.     ODPropertyName    tmpToPropertyName = toSU->GetProperty(ev);
  1429.     ODValueType    tmpToTypeName = toSU->GetType(ev);
  1430.     somPrintf("SetStorageUnitRef: FromID %x %s %s to toID %x %s %s\n",
  1431.                                                 fromEmbeddedID,
  1432.                                                 tmpPropertyName,
  1433.                                                 tmpTypeName,
  1434.                                                 toEmbeddedID,
  1435.                                                 tmpToPropertyName,
  1436.                                                 tmpToTypeName);
  1437.     ODDisposePtr(tmpPropertyName);
  1438.     ODDisposePtr(tmpTypeName);
  1439.     ODDisposePtr(tmpToPropertyName);
  1440.     ODDisposePtr(tmpToTypeName);
  1441. #endif
  1442.     
  1443.                         toSU->SetStorageUnitRef(ev, toEmbeddedID, ref);
  1444.                     }
  1445.                 }
  1446.             }
  1447.             delete iter;
  1448.  
  1449.             ODDisposePtr(typeName);
  1450.         }
  1451.         ODDisposePtr(propertyName);
  1452.     }
  1453.     
  1454.     if (originalFocus == kODNULL)
  1455.         somSelf->Focus(ev, kODNULL, kODPosAll, kODNULL, 0, kODPosAll);
  1456.     else {
  1457.         somSelf->FocusWithCursor(ev, originalFocus);
  1458.         delete originalFocus;
  1459.     }
  1460. #ifdef USE_CLONEHELPER
  1461.     }
  1462. #endif
  1463. #ifdef USE_CLONEHELPER
  1464. #ifdef ODDebug_SUCloneHelper
  1465.     else
  1466.         somPrintf("Not cloning %d in scope %d\n", somSelf->GetID(ev), scopeID);
  1467. #endif
  1468. #endif
  1469.  
  1470.     }
  1471.     catch (ODException _exception)
  1472.     {
  1473.       if(propertyName)
  1474.          ODDisposePtr(propertyName);
  1475.       if(typeName)
  1476.          ODDisposePtr(typeName);   
  1477.          ODSetSOMException(ev, _exception);
  1478.     }
  1479. }
  1480.  
  1481. //------------------------------------------------------------------------------
  1482. // CMStorageUnit: CreateView
  1483. //------------------------------------------------------------------------------
  1484.  
  1485. SOM_Scope ODStorageUnitView*     SOMLINK CMStorageUnitCreateView(CMStorageUnit *somSelf, Environment *ev)
  1486. {
  1487.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1488.     CMStorageUnitMethodDebug("CMStorageUnit","CreateView");
  1489.     
  1490.     ODStorageUnitView* suView = kODNULL;
  1491.  
  1492.    try
  1493.    {
  1494.  
  1495.     FailIfInvalidRefCount();
  1496.     
  1497.     ODStorageUnitCursor* cursor = somSelf->CreateCursorWithFocus(ev);
  1498.     
  1499.     suView = NewODStorageUnitView(somSelf->GetHeap(ev));
  1500.     suView->InitStorageUnitView(ev, somSelf, cursor);
  1501.  
  1502.  
  1503.     }
  1504.     catch (ODException _exception)
  1505.     {
  1506.     suView = kODNULL;
  1507.         ODSetSOMException(ev, _exception);
  1508.     }
  1509.     return suView;
  1510. }
  1511.  
  1512. //------------------------------------------------------------------------------
  1513. // CMStorageUnit: CreateCursor
  1514. //------------------------------------------------------------------------------
  1515.  
  1516. SOM_Scope ODStorageUnitCursor*  SOMLINK CMStorageUnitCreateCursor(CMStorageUnit *somSelf, Environment *ev,
  1517.         ODPropertyName propertyName,
  1518.         ODValueType valueType,
  1519.         ODValueIndex valueIndex)
  1520. {
  1521.     /*    CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf); */
  1522.     CMStorageUnitMethodDebug("CMStorageUnit","CMStorageUnitCreateCursor");
  1523.     
  1524.         ODStorageUnitCursor* suCursor = kODNULL;
  1525.     
  1526.    try
  1527.    {
  1528.     suCursor = NewODStorageUnitCursor(somSelf->GetHeap(ev));
  1529.     suCursor->InitStorageUnitCursor(ev, propertyName, valueType, valueIndex);
  1530.     
  1531.  
  1532.     }
  1533.     catch (ODException _exception)
  1534.     {
  1535.     suCursor =  kODNULL;
  1536.         ODSetSOMException(ev, _exception);
  1537.     }
  1538.     return suCursor;
  1539. }
  1540.  
  1541. //------------------------------------------------------------------------------
  1542. // CMStorageUnit: CreateCursorWithFocus
  1543. //------------------------------------------------------------------------------
  1544.  
  1545. SOM_Scope ODStorageUnitCursor*  SOMLINK CMStorageUnitCreateCursorWithFocus(CMStorageUnit *somSelf, Environment *ev)
  1546. {
  1547.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1548.     CMStorageUnitMethodDebug("CMStorageUnit","CreateCursorWithFocus");
  1549.  
  1550.     ODStorageUnitCursor*     suCursor = kODNULL;
  1551.  
  1552.    try
  1553.    {
  1554.     ODPropertyName            propertyName = kODNULL;
  1555.     ODValueType            valueType = kODNULL;
  1556.  
  1557.     FailIfInvalidRefCount();
  1558.     somSelf->Internalize(ev);
  1559.  
  1560.     if (_fCurProperty != kODNULL)
  1561.         propertyName = somSelf->GetProperty(ev);
  1562.     
  1563.     if (_fCurValue != kODNULL)
  1564.         valueType = somSelf->GetType(ev);
  1565.  
  1566.     suCursor = NewODStorageUnitCursor(somSelf->GetHeap(ev));
  1567.     suCursor->InitStorageUnitCursor(ev, propertyName, valueType, _fCurValueIndex);
  1568.     
  1569.     ODDisposePtr(propertyName);
  1570.     ODDisposePtr(valueType);
  1571.                         
  1572.  
  1573.     }
  1574.     catch (ODException _exception)
  1575.     {
  1576.     suCursor = kODNULL;
  1577.         ODSetSOMException(ev, _exception);
  1578.     }
  1579.     return suCursor;
  1580. }
  1581.  
  1582. //------------------------------------------------------------------------------
  1583. // CMStorageUnit: GetProperty
  1584. //------------------------------------------------------------------------------
  1585.  
  1586. SOM_Scope ODPropertyName  SOMLINK CMStorageUnitGetProperty(CMStorageUnit *somSelf, Environment *ev)
  1587. {
  1588.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1589.     CMStorageUnitMethodDebug("CMStorageUnit","GetProperty");
  1590.  
  1591.     ODPropertyName    propertyName = kODNULL;
  1592.     
  1593.     try
  1594.     {
  1595.     CMGlobalName    tmp;
  1596.  
  1597.     FailIfInvalidRefCount();
  1598.  
  1599.     if (_fCurProperty == kODNULL)
  1600.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  1601.     
  1602.     // CMContainer container = _fDraft->GetCMContainer(ev);
  1603.     // ODSessionMustHaveCMAllocReserve(container);
  1604.     // CMIsProperty() and CMGetGlobalName() do not allocate memory.
  1605.  
  1606.     if (CMIsProperty(_fCurProperty)) {
  1607.         tmp = CMGetGlobalName(_fCurProperty);
  1608.         propertyName = ODISOStrFromCStr(tmp);
  1609.     }
  1610.     // ODSessionRestoreCMAllocReserve(container);
  1611.     
  1612.  
  1613.     }
  1614.     catch (ODException _exception)
  1615.     {
  1616.     propertyName = kODNULL;
  1617.         ODSetSOMException(ev, _exception);
  1618.     }
  1619.     return (propertyName);
  1620. }
  1621.  
  1622. //------------------------------------------------------------------------------
  1623. // CMStorageUnit: GetType
  1624. //------------------------------------------------------------------------------
  1625.  
  1626. SOM_Scope ODValueType  SOMLINK CMStorageUnitGetType(CMStorageUnit *somSelf, Environment *ev)
  1627. {
  1628.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1629.     CMStorageUnitMethodDebug("CMStorageUnit","GetType");
  1630.  
  1631.     ODValueType    typeName = kODNULL;
  1632.  
  1633.     try
  1634.     {
  1635.     CMType            type;
  1636.     CMGlobalName    tmp;
  1637.  
  1638.     FailIfInvalidRefCount();
  1639.  
  1640.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  1641.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  1642.     
  1643.     CMContainer container = _fDraft->GetCMContainer(ev);
  1644.     ODSessionMustHaveCMAllocReserve(container);
  1645.  
  1646.     CMGetValueInfo(_fCurValue, kODNULL, kODNULL, kODNULL, &type, kODNULL);
  1647.     
  1648.     if (CMIsType(type)) {
  1649.         tmp = CMGetGlobalName(type);
  1650.         typeName = ODISOStrFromCStr(tmp);
  1651.     }
  1652.     ODSessionRestoreCMAllocReserve(container);
  1653.     
  1654.  
  1655.     }
  1656.     catch (ODException _exception)
  1657.     {
  1658.     typeName =  kODNULL;
  1659.         ODSetSOMException(ev, _exception);
  1660.     }
  1661.     return (typeName);
  1662. }
  1663.  
  1664. //------------------------------------------------------------------------------
  1665. // CMStorageUnit: SetType
  1666. //------------------------------------------------------------------------------
  1667.  
  1668. SOM_Scope void  SOMLINK CMStorageUnitSetType(CMStorageUnit *somSelf, Environment *ev,
  1669.         ODValueType typeName)
  1670. {
  1671.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1672.     CMStorageUnitMethodDebug("CMStorageUnit","SetType");
  1673.  
  1674.  
  1675.     try
  1676.     {
  1677.         if(typeName == kODNULL)                // #25523 KP
  1678.              BREAK_AND_THROW(kODErrInvalidValueType);
  1679.  
  1680.     CMContainer container = _fDraft->GetCMContainer(ev);
  1681.     CMType        type;
  1682.  
  1683.     FailIfInvalidRefCount();
  1684.     somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  1685.     
  1686.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  1687.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  1688.         
  1689.     // CMContainer container = somSelf->GetCMContainer(ev);
  1690.     ODSessionMustHaveCMAllocReserve(container);
  1691.     
  1692.     type = CMRegisterType(container, (CMGlobalName) typeName);
  1693.     if (type != kODNULL)
  1694.         CMSetValueType(_fCurValue, type);
  1695.     else
  1696.         BREAK_AND_THROW(kODErrInvalidValueType);
  1697.  
  1698.     ODSessionRestoreCMAllocReserve(container);
  1699.  
  1700.     }
  1701.     catch (ODException _exception)
  1702.     {
  1703.         ODSetSOMException(ev, _exception);
  1704.     }
  1705. }
  1706.  
  1707. //------------------------------------------------------------------------------
  1708. // CMStorageUnit: SetOffset
  1709. //------------------------------------------------------------------------------
  1710.  
  1711. SOM_Scope void  SOMLINK CMStorageUnitSetOffset(CMStorageUnit *somSelf, Environment *ev,
  1712.         ODULong offset)
  1713. {
  1714.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1715.     CMStorageUnitMethodDebug("CMStorageUnit","SetOffset");
  1716.  
  1717.       try
  1718.       {
  1719.         FailIfInvalidRefCount();
  1720.         
  1721.         if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  1722.             THROW(kODErrUnfocusedStorageUnit);
  1723.     
  1724.         _fOffset = offset;
  1725.     
  1726.       }
  1727.       catch (ODException _exception)
  1728.       {
  1729.           ODSetSOMException(ev, _exception);
  1730.       }
  1731. }
  1732.  
  1733. SOM_Scope ODULong  SOMLINK CMStorageUnitGetOffset(CMStorageUnit *somSelf, Environment *ev)
  1734. {
  1735.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1736.     CMStorageUnitMethodDebug("CMStorageUnit","GetOffset");
  1737.  
  1738.     ODULong offset = 0;
  1739.     
  1740.        try
  1741.        {
  1742.         FailIfInvalidRefCount();
  1743.         
  1744.         if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  1745.             THROW(kODErrUnfocusedStorageUnit);
  1746.             
  1747.         offset = _fOffset;
  1748.        }
  1749.        catch (ODException _exception)
  1750.        {
  1751.            ODSetSOMException(ev, _exception);
  1752.        }        
  1753.     
  1754.     return offset;
  1755. }
  1756.  
  1757. //------------------------------------------------------------------------------
  1758. // CMStorageUnit: GetValue
  1759. //------------------------------------------------------------------------------
  1760.  
  1761. SOM_Scope ODULong  SOMLINK CMStorageUnitGetValue(CMStorageUnit *somSelf, Environment *ev,
  1762.         ODULong length,
  1763.         ODByteArray* value)
  1764. {
  1765.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1766.     CMStorageUnitMethodDebug("CMStorageUnit","GetValue");
  1767.  
  1768.    try
  1769.    {
  1770.     
  1771.     FailIfIllegalByteArray(value);
  1772.     FailIfInvalidRefCount();
  1773.     
  1774.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  1775.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  1776.     
  1777.     if ((_fPromiseResolver != kODNULL) && (_fPromiseResolver->IsSettingPromise() == kODFalse))
  1778.         _fPromiseResolver->ResolvePromise(ev);
  1779.  
  1780.     value->_buffer = (octet*) ODNewPtr(length);
  1781.     value->_maximum = length;
  1782.     
  1783.     CMContainer container = _fDraft->GetCMContainer(ev);
  1784.     ODSessionMustHaveCMAllocReserve(container);
  1785.  
  1786.     value->_length = CMReadValueData(_fCurValue, (CMPtr) value->_buffer, _fOffset, length);
  1787.  
  1788.     ODSessionRestoreCMAllocReserve(container);
  1789.  
  1790.     _fOffset += value->_length;
  1791.  
  1792.  
  1793.      }
  1794.      catch (ODException _exception)
  1795.      {
  1796.     value->_length =  0;
  1797.         ODSetSOMException(ev, _exception);
  1798.      }
  1799.     return value->_length;
  1800. }
  1801.  
  1802. //------------------------------------------------------------------------------
  1803. // CMStorageUnit: SetValue
  1804. //------------------------------------------------------------------------------
  1805.  
  1806. SOM_Scope void  SOMLINK CMStorageUnitSetValue(CMStorageUnit *somSelf, Environment *ev,
  1807.         ODByteArray* value)
  1808. {
  1809.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1810.     CMStorageUnitMethodDebug("CMStorageUnit","SetValue");
  1811.  
  1812.     try
  1813.     {
  1814.     
  1815.     FailIfIllegalByteArray(value);
  1816.     FailIfInvalidRefCount();
  1817.     
  1818.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  1819.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  1820.  
  1821.     if ((_fPromiseResolver != kODNULL) && (_fPromiseResolver->IsSettingPromise() == kODFalse))
  1822.         _fPromiseResolver->ResolvePromise(ev);
  1823.  
  1824.     somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  1825.     
  1826.     CMContainer container = _fDraft->GetCMContainer(ev);
  1827.     ODSessionMustHaveCMAllocReserve(container);
  1828.  
  1829.     CMWriteValueData(_fCurValue, (CMPtr) value->_buffer, _fOffset, value->_length);
  1830.  
  1831.     ODSessionRestoreCMAllocReserve(container);
  1832.  
  1833.     _fOffset += value->_length;
  1834.  
  1835.     }
  1836.     catch (ODException _exception)
  1837.     {
  1838.          ODSetSOMException(ev, _exception);
  1839.     }
  1840. }
  1841.  
  1842. //------------------------------------------------------------------------------
  1843. // CMStorageUnit: InsertValue
  1844. //------------------------------------------------------------------------------
  1845.  
  1846. SOM_Scope void  SOMLINK CMStorageUnitInsertValue(CMStorageUnit *somSelf, Environment *ev,
  1847.         ODByteArray* value)
  1848. {
  1849.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1850.     CMStorageUnitMethodDebug("CMStorageUnit","InsertValue");
  1851.  
  1852.    try
  1853.    {
  1854.     FailIfIllegalByteArray(value);
  1855.     FailIfInvalidRefCount();
  1856.     
  1857.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  1858.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  1859.  
  1860.     if ((_fPromiseResolver != kODNULL) && (_fPromiseResolver->IsSettingPromise() == kODFalse))
  1861.         _fPromiseResolver->ResolvePromise(ev);
  1862.  
  1863.     somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  1864.  
  1865.     CMContainer container = _fDraft->GetCMContainer(ev);
  1866.     ODSessionMustHaveCMAllocReserve(container);
  1867.     
  1868.     CMInsertValueData(_fCurValue,(CMPtr)  value->_buffer, _fOffset, value->_length);
  1869.  
  1870.     ODSessionRestoreCMAllocReserve(container);
  1871.  
  1872.     _fOffset += value->_length;
  1873.  
  1874.     }
  1875.     catch (ODException _exception)
  1876.     {
  1877.          ODSetSOMException(ev, _exception);
  1878.     }
  1879. }
  1880.  
  1881. //------------------------------------------------------------------------------
  1882. // CMStorageUnit: DeleteValue
  1883. //------------------------------------------------------------------------------
  1884.  
  1885. SOM_Scope void  SOMLINK CMStorageUnitDeleteValue(CMStorageUnit *somSelf, Environment *ev,
  1886.         ODULong length)
  1887. {
  1888.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1889.     CMStorageUnitMethodDebug("CMStorageUnit","DeleteValue");
  1890.  
  1891.    try
  1892.    {
  1893.     FailIfInvalidRefCount();
  1894.     
  1895.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  1896.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  1897.  
  1898.     if ((_fPromiseResolver != kODNULL) && (_fPromiseResolver->IsSettingPromise() == kODFalse))
  1899.         _fPromiseResolver->ResolvePromise(ev);
  1900.  
  1901.     somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  1902.  
  1903.     CMContainer container = _fDraft->GetCMContainer(ev);
  1904.     ODSessionMustHaveCMAllocReserve(container);
  1905.     
  1906.     CMDeleteValueData(_fCurValue, _fOffset, length);
  1907.  
  1908.     ODSessionRestoreCMAllocReserve(container);
  1909.  
  1910.     }
  1911.     catch (ODException _exception)
  1912.     {
  1913.         ODSetSOMException(ev, _exception);
  1914.     }
  1915. }
  1916.  
  1917. //------------------------------------------------------------------------------
  1918. // CMStorageUnit: GetSize
  1919. //------------------------------------------------------------------------------
  1920.  
  1921. SOM_Scope ODULong  SOMLINK CMStorageUnitGetSize(CMStorageUnit *somSelf, Environment *ev)
  1922. {
  1923.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1924.     CMStorageUnitMethodDebug("CMStorageUnit","GetSize");
  1925.  
  1926.     ODULong    size = 0;
  1927.     
  1928.    try
  1929.    {
  1930.     FailIfInvalidRefCount();
  1931.     somSelf->Internalize(ev);
  1932.  
  1933.     CMContainer container = _fDraft->GetCMContainer(ev);
  1934.     ODSessionMustHaveCMAllocReserve(container);
  1935.     // GetPropertySize() and GetObjectSize() call CM methods
  1936.         
  1937.     if (_fCurValue != kODNULL) {
  1938.         if ((_fPromiseResolver != kODNULL) && (_fPromiseResolver->IsSettingPromise() == kODFalse))
  1939.             _fPromiseResolver->ResolvePromise(ev);
  1940.         
  1941.         size = CMGetValueSize(_fCurValue);
  1942.     }
  1943.     else if (_fCurProperty != kODNULL)
  1944.         size = GetPropertySize(_fObject, _fCurProperty);
  1945.     else
  1946.         size = GetObjectSize(_fObject);
  1947.  
  1948.     ODSessionRestoreCMAllocReserve(container);
  1949.         
  1950.  
  1951.      }
  1952.      catch (ODException _exception)
  1953.      {
  1954.     size =  0; 
  1955.         ODSetSOMException(ev, _exception);
  1956.      }
  1957.     return size;
  1958. }
  1959.  
  1960. //------------------------------------------------------------------------------
  1961. // CMStorageUnit: GetStrongStorageUnitRef
  1962. //------------------------------------------------------------------------------
  1963.  
  1964. SOM_Scope void  SOMLINK CMStorageUnitGetStrongStorageUnitRef(CMStorageUnit *somSelf, Environment *ev,
  1965.         ODStorageUnitID embeddedSUID, ODStorageUnitRef ref)
  1966. {
  1967.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1968.     CMStorageUnitMethodDebug("CMStorageUnit","GetStrongStorageUnitRef");
  1969.  
  1970.     try
  1971.     {
  1972.     
  1973.     somSelf->GetStorageUnitRef(ev, embeddedSUID, kODTrue, ref);
  1974.  
  1975.     }
  1976.     catch (ODException _exception)
  1977.     {
  1978.          ODSetSOMException(ev, _exception);
  1979.     }
  1980.  
  1981. }
  1982.  
  1983. //------------------------------------------------------------------------------
  1984. // CMStorageUnit: GetWeakStorageUnitRef
  1985. //------------------------------------------------------------------------------
  1986.  
  1987. SOM_Scope void  SOMLINK CMStorageUnitGetWeakStorageUnitRef(CMStorageUnit *somSelf, Environment *ev,
  1988.         ODStorageUnitID embeddedSUID, ODStorageUnitRef ref)
  1989. {
  1990.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  1991.     CMStorageUnitMethodDebug("CMStorageUnit","GetWeakStorageUnitRef");
  1992.  
  1993.     try
  1994.     {
  1995.     
  1996.     somSelf->GetStorageUnitRef(ev, embeddedSUID, kODFalse, ref);
  1997.  
  1998.     }
  1999.     catch (ODException _exception)
  2000.     {
  2001.         ODSetSOMException(ev, _exception);
  2002.     }
  2003. }
  2004.  
  2005. //------------------------------------------------------------------------------
  2006. // CMStorageUnit: IsValidStorageUnitRef
  2007. //------------------------------------------------------------------------------
  2008.  
  2009. SOM_Scope ODBoolean  SOMLINK CMStorageUnitIsValidStorageUnitRef(CMStorageUnit *somSelf, Environment *ev,
  2010.         ODStorageUnitRef ref)
  2011. {
  2012.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2013.     CMStorageUnitMethodDebug("CMStorageUnit","IsValidStorageUnitRef");
  2014.  
  2015.     ODBoolean validSU = kODFalse;
  2016.  
  2017.    try
  2018.    {
  2019.     FailIfInvalidRefCount();
  2020.     
  2021.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  2022.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  2023.     
  2024.     CMObject    object = kODNULL;
  2025.     ODVolatile(object);
  2026. #ifdef PLATFORM_MACINTOSH
  2027.     TRY
  2028. #endif // PLATFORM_MACINTOSH
  2029.         CMContainer container = _fDraft->GetCMContainer(ev);
  2030.         ODSessionMustHaveCMAllocReserve(container);
  2031.         
  2032.         object = CMGetReferencedObject(_fCurValue, ref);
  2033.         if (object != kODNULL)
  2034.             CMReleaseObject(object);
  2035.             
  2036.         ODSessionRestoreCMAllocReserve(container);
  2037.  
  2038. #ifdef PLATFORM_MACINTOSH
  2039.     CATCH_ALL
  2040.         object = kODNULL;
  2041.     ENDTRY
  2042. #endif // PLATFORM_MACINTOSH
  2043.     
  2044.     if(object != kODNULL)
  2045.              validSU = kODTrue;
  2046.  
  2047.     }
  2048.     catch (ODException _exception)
  2049.     {
  2050.          validSU = kODFalse;    
  2051.          ODSetSOMException(ev, _exception);
  2052.     }
  2053.         return validSU;
  2054. }
  2055.  
  2056. //------------------------------------------------------------------------------
  2057. // CMStorageUnit: IsStrongStorageUnitRef
  2058. //------------------------------------------------------------------------------
  2059.  
  2060. SOM_Scope ODBoolean  SOMLINK CMStorageUnitIsStrongStorageUnitRef(CMStorageUnit *somSelf, Environment *ev,
  2061.         ODStorageUnitRef ref)
  2062. {
  2063.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2064.     CMStorageUnitMethodDebug("CMStorageUnit","IsStrongStorageUnitRef");
  2065.  
  2066.     ODBoolean isStrongSUref = kODFalse;
  2067.  
  2068.    try
  2069.    {
  2070.  
  2071.     isStrongSUref =  _fSURefKeeper->IsStrongSURef(ref);
  2072.    }
  2073.    catch (ODException _exception)
  2074.    {
  2075.          isStrongSUref = kODFalse;
  2076.          ODSetSOMException(ev, _exception);
  2077.    }
  2078.    return isStrongSUref;
  2079. }
  2080.  
  2081. //------------------------------------------------------------------------------
  2082. // CMStorageUnit: IsWeakStorageUnitRef
  2083. //------------------------------------------------------------------------------
  2084.  
  2085. SOM_Scope ODBoolean  SOMLINK CMStorageUnitIsWeakStorageUnitRef(CMStorageUnit *somSelf, Environment *ev,
  2086.         ODStorageUnitRef ref)
  2087. {
  2088.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2089.     CMStorageUnitMethodDebug("CMStorageUnit","IsWeakStorageUnitRef");
  2090.  
  2091.     ODBoolean isWeakSUref = kODFalse;
  2092.  
  2093.    try
  2094.    {
  2095.  
  2096.     isWeakSUref =  _fSURefKeeper->IsWeakSURef(ref);
  2097.    }
  2098.    catch (ODException _exception)
  2099.    {
  2100.          isWeakSUref = kODFalse;
  2101.          ODSetSOMException(ev, _exception);
  2102.    }
  2103.    return isWeakSUref;
  2104. }
  2105.  
  2106. //------------------------------------------------------------------------------
  2107. // CMStorageUnit: RemoveStorageUnitRef
  2108. //------------------------------------------------------------------------------
  2109.  
  2110. SOM_Scope ODStorageUnit*  SOMLINK CMStorageUnitRemoveStorageUnitRef(CMStorageUnit *somSelf, Environment *ev,
  2111.         ODStorageUnitRef ref)
  2112. {
  2113.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2114.     CMStorageUnitMethodDebug("CMStorageUnit","RemoveStorageUnitRef");
  2115.  
  2116.     try
  2117.     {
  2118.     FailIfInvalidRefCount();
  2119.  
  2120.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  2121.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  2122.  
  2123.     somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  2124.  
  2125.     CMContainer container = _fDraft->GetCMContainer(ev);
  2126.     ODSessionMustHaveCMAllocReserve(container);
  2127.         
  2128.     CMDeleteReference(_fCurValue, ref);
  2129.                     
  2130.     ODSessionRestoreCMAllocReserve(container);
  2131.     
  2132.  
  2133.     }
  2134.     catch (ODException _exception)
  2135.     {
  2136.         ODSetSOMException(ev, _exception);
  2137.     }
  2138.     return somSelf;
  2139. }
  2140.  
  2141. //------------------------------------------------------------------------------
  2142. // CMStorageUnit: GetIDFromStorageUnitRef
  2143. //------------------------------------------------------------------------------
  2144.  
  2145. SOM_Scope ODStorageUnitID  SOMLINK CMStorageUnitGetIDFromStorageUnitRef(CMStorageUnit *somSelf, Environment *ev,
  2146.         ODStorageUnitRef ref)
  2147. {
  2148.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2149.     CMStorageUnitMethodDebug("CMStorageUnit","GetIDFromStorageUnitRef");
  2150.  
  2151.     ODID    id = 0;
  2152.    try
  2153.    {
  2154.     FailIfInvalidRefCount();
  2155.     
  2156.     CMObject    object;
  2157.     
  2158.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  2159.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  2160.  
  2161.     CMContainer container = _fDraft->GetCMContainer(ev);
  2162.     ODSessionMustHaveCMAllocReserve(container);
  2163.     
  2164.     try
  2165.         {
  2166.         object = CMGetReferencedObject(_fCurValue, ref);
  2167.         }
  2168.     catch (ODException _exception)
  2169.         {
  2170.         BREAK_AND_THROW(kODErrInvalidStorageUnitRef);
  2171.     }
  2172.     
  2173.     if (object == kODNULL)
  2174.         BREAK_AND_THROW(kODErrInvalidStorageUnitRef);
  2175.         
  2176.         CMDraft* tempDraft = (CMDraft*) somSelf->GetDraft(ev);
  2177.     IDList*    idList = kODNULL;
  2178.         if(tempDraft)
  2179.             idList = tempDraft->GetIDList(ev);
  2180.     ASSERT(idList != kODNULL, kODErrInvalidIDList);
  2181.  
  2182.     if (idList->ObjectExists(object) != kODFalse) {
  2183.         id = idList->GetID(object);
  2184.         CMReleaseObject(object);
  2185.     }
  2186.     else {
  2187.         id = idList->Add(object);
  2188.     }
  2189.     ODSessionRestoreCMAllocReserve(container);
  2190.     
  2191.  
  2192.     }
  2193.     catch (ODException _exception)
  2194.     {
  2195.         id = 0;
  2196.         ODSetSOMException(ev, _exception);
  2197.     } 
  2198.     return id;
  2199. }
  2200.  
  2201. //------------------------------------------------------------------------------
  2202. // CMStorageUnit: CreateStorageUnitRefIterator
  2203. //------------------------------------------------------------------------------
  2204.  
  2205. SOM_Scope ODStorageUnitRefIterator*  SOMLINK CMStorageUnitCreateStorageUnitRefIterator(CMStorageUnit *somSelf, Environment *ev)
  2206. {
  2207.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2208.     CMStorageUnitMethodDebug("CMStorageUnit","CrateStorageUnitRefIterator");
  2209.  
  2210.         CMStorageUnitRefIterator* iter = kODNULL;
  2211.    try
  2212.    {
  2213.     FailIfInvalidRefCount();
  2214.  
  2215.     iter = NewCMStorageUnitRefIterator(somSelf->GetHeap(ev));
  2216.     iter->InitStorageUnitRefIterator(ev, somSelf);
  2217.  
  2218.  
  2219.    }
  2220.    catch (ODException _exception)
  2221.    {
  2222.     iter =  kODNULL;
  2223.         ODSetSOMException(ev, _exception);
  2224.    }
  2225.     return iter;
  2226. }
  2227.  
  2228. //------------------------------------------------------------------------------
  2229. // CMStorageUnit: GetGenerationNumber
  2230. //------------------------------------------------------------------------------
  2231.  
  2232. SOM_Scope ODULong  SOMLINK CMStorageUnitGetGenerationNumber(CMStorageUnit *somSelf, Environment *ev)
  2233. {
  2234.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2235.     CMStorageUnitMethodDebug("CMStorageUnit","GetGenerationNumber");
  2236.  
  2237.     ODULong    generation = 0;
  2238.    try
  2239.    {
  2240.     FailIfInvalidRefCount();
  2241.     
  2242.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  2243.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  2244.         
  2245.  
  2246.     CMContainer container = _fDraft->GetCMContainer(ev);
  2247.     ODSessionMustHaveCMAllocReserve(container);
  2248.  
  2249.     CMGetValueInfo(_fCurValue, kODNULL, kODNULL, kODNULL, kODNULL, &generation);
  2250.     
  2251.     ODSessionRestoreCMAllocReserve(container);
  2252.  
  2253.  
  2254.     }
  2255.     catch (ODException _exception)
  2256.     {
  2257.     generation =  0;
  2258.         ODSetSOMException(ev, _exception);
  2259.     }
  2260.     return generation;
  2261. }
  2262.  
  2263. //------------------------------------------------------------------------------
  2264. // CMStorageUnit: IncrementGenerationNumber
  2265. //------------------------------------------------------------------------------
  2266.  
  2267. SOM_Scope ODULong  SOMLINK CMStorageUnitIncrementGenerationNumber(CMStorageUnit *somSelf, Environment *ev)
  2268. {
  2269.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2270.     CMStorageUnitMethodDebug("CMStorageUnit","IncrementGenerationNumber");
  2271.  
  2272.         ODULong generation = 0;
  2273.     try
  2274.     {
  2275.     FailIfInvalidRefCount();
  2276.     
  2277.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  2278.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  2279.  
  2280.     somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  2281.  
  2282.     generation = somSelf->GetGenerationNumber(ev);
  2283.     generation++;
  2284.  
  2285.     CMContainer container = _fDraft->GetCMContainer(ev);
  2286.     ODSessionMustHaveCMAllocReserve(container);
  2287.  
  2288.     CMSetValueGeneration(_fCurValue, generation);
  2289.  
  2290.     ODSessionRestoreCMAllocReserve(container);
  2291.     
  2292.  
  2293.     }
  2294.     catch (ODException _exception)
  2295.     {
  2296.     generation =  0;
  2297.         ODSetSOMException(ev, _exception);
  2298.     }
  2299.     return generation;
  2300. }
  2301.  
  2302. //------------------------------------------------------------------------------
  2303. // CMStorageUnit: GetSession
  2304. //------------------------------------------------------------------------------
  2305.  
  2306. SOM_Scope ODSession*     SOMLINK CMStorageUnitGetSession(CMStorageUnit *somSelf, Environment *ev)
  2307. {
  2308.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2309.     CMStorageUnitMethodDebug("CMStorageUnit","GetSession");
  2310.  
  2311.         ODSession* session = kODNULL;
  2312.  
  2313.     try
  2314.     {
  2315.     session =  (ODSession*) _fDraft->GetDocument(ev)->GetContainer(ev)->GetStorageSystem(ev)->GetSession(ev);
  2316.  
  2317.     }
  2318.     catch (ODException _exception)
  2319.     {
  2320.     session = kODNULL;
  2321.         ODSetSOMException(ev, _exception);
  2322.     }
  2323.         return session;
  2324. }
  2325.  
  2326. //------------------------------------------------------------------------------
  2327. // CMStorageUnit: InitStorageUnit
  2328. //------------------------------------------------------------------------------
  2329.  
  2330. SOM_Scope void  SOMLINK CMStorageUnitInitStorageUnit(CMStorageUnit *somSelf, Environment *ev,
  2331.         ODDraft* draft,ODStorageUnitID suid)
  2332. {
  2333.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2334.     CMStorageUnitMethodDebug("CMStorageUnit","InitStorageUnit");
  2335.     
  2336.    try
  2337.    {
  2338.             
  2339.     /* Moved from somInit. SOM itself sets fields to zero
  2340.     _fDraft = kODNULL;
  2341.     _fID = 0;
  2342.     _fObjectID = kODNULL;
  2343.     _fObject = kODNULL;
  2344.     _fCurProperty = kODIDAll;
  2345.     _fCurValueIndex = 0;
  2346.     _fCurValue = kODIDAll;
  2347.     _fCurValueType = 0;
  2348.     _fCurType = kODNULL;
  2349.     _fOffset = 0;
  2350.     _fHasPropertyLooped = kODFalse;
  2351.     _fHasValueLooped = kODFalse;
  2352.     
  2353.     _fCurrentKey = 0;
  2354.     _fLockCount = 0;
  2355.     
  2356.     _fSURefKeeper = kODNULL;
  2357.     
  2358. #ifdef USE_CLONEHELPER
  2359.     _fSUCloneHelper = kODNULL;
  2360. #endif
  2361.     
  2362.     _fHeap = kDefaultHeapID;
  2363.     
  2364.     _fModificationDate = 0;
  2365.     _fDirty = kODFalse;
  2366.     */
  2367.  
  2368. #ifdef _PLATFORM_OS2_ //<crs> - implement mutex sem under os/2                               //D133590
  2369.   //<crs> - Changing the meaning of _fCurrentKey. It is now going to be                      //D133590
  2370.   //        an HMTX (it's a ODUlong so there's no problem there). We will                    //D133590
  2371.   //        no longer maintain _fLockCount.                                                  //D133590
  2372.   // Create an unowned, unnamed shared mutex semaphore                                       //D133590
  2373.   APIRET rc = DosCreateMutexSem( 0, &(_fCurrentKey), DC_SEM_SHARED, 0 );                     //D133590
  2374. #endif //<crs> - end                                                                         //D133590
  2375.  
  2376.     _fCurProperty = kODIDAll;
  2377.     _fCurValue = kODIDAll;
  2378.     _fHeap = kDefaultHeapID;
  2379.     
  2380.     somSelf->InitRefCntObject(ev);
  2381.     
  2382.     
  2383.     if (draft != kODNULL) {
  2384.     
  2385.         _fDraft = (CMDraft*) draft;
  2386.         _fID = suid;
  2387.     
  2388.         IDList*    idList = kODNULL;
  2389.             
  2390.         idList = _fDraft->GetIDList(ev);
  2391.         ASSERT(idList != kODNULL, kODErrInvalidIDList);
  2392.     
  2393.         _fObject = (CMObject) idList->Get(_fID);
  2394.         if (_fObject == kODNULL)
  2395.             BREAK_AND_THROW(kODErrBentoInvalidObject);
  2396.         
  2397.         _fCMContainer = _fDraft->GetCMContainer(ev);
  2398.             
  2399.         _fObjectID = CMGetObjectID(_fObject);
  2400.         
  2401.         _fHeap = _fDraft->GetHeap(ev);
  2402.  
  2403. #ifdef _PLATFORM_MACINTOSH_
  2404.         _fPromiseResolver = new(_fHeap) PromiseResolver;
  2405. #endif
  2406. #if defined(_PLATFORM_WIN32_)||defined(_PLATFORM_OS2_)||defined(_PLATFORM_AIX_)
  2407. //        The use of heap is not implemented in Windows platform
  2408.         _fPromiseResolver = new PromiseResolver;
  2409. #endif
  2410.         _fPromiseResolver->InitPromiseResolver(somSelf);
  2411.         
  2412. #ifdef _PLATFORM_MACINTOSH_
  2413.         _fSURefKeeper = new(_fHeap) SURefKeeper(somSelf);
  2414. #endif
  2415. #if defined(_PLATFORM_WIN32_)||defined(_PLATFORM_OS2_)||defined(_PLATFORM_AIX_)
  2416. //        The use of heap is not implemented in Windows platform
  2417.         _fSURefKeeper = new SURefKeeper(somSelf);
  2418. #endif
  2419.  
  2420.  
  2421. #ifdef USE_CLONEHELPER        
  2422. #ifdef _PLATFORM_MACINTOSH_
  2423.         _fSUCloneHelper = new(_fHeap) SUCloneHelper;
  2424. #endif
  2425. #if defined(_PLATFORM_WIN32_)||defined(_PLATFORM_OS2_)||defined(_PLATFORM_AIX_)
  2426. //        The use of heap is not implemented in Windows platform
  2427.         _fSUCloneHelper = new SUCloneHelper;
  2428. #endif
  2429. #endif
  2430.     }
  2431.     else
  2432.         THROW(kODErrIllegalNullDraftInput);
  2433.  
  2434.     }
  2435.     catch (ODException _exception)
  2436.     {
  2437.          ODDeleteObject(_fSUCloneHelper);
  2438.          ODDeleteObject(_fPromiseResolver);
  2439.          ODDeleteObject(_fSURefKeeper);
  2440.          ODSetSOMException(ev, _exception);
  2441.     }
  2442. }
  2443.  
  2444.  
  2445. //------------------------------------------------------------------------------
  2446. // CMStorageUnit: ~CMStorageUnit
  2447. //------------------------------------------------------------------------------
  2448.  
  2449. SOM_Scope void  SOMLINK CMStorageUnitsomUninit(CMStorageUnit *somSelf)
  2450. {
  2451.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2452.     CMStorageUnitMethodDebug("CMStorageUnit","somUninit");
  2453.  
  2454.         Environment* ev = somGetGlobalEnvironment();
  2455.     try
  2456.     {
  2457.     delete _fPromiseResolver;
  2458.     delete _fSURefKeeper;
  2459.     
  2460. #ifdef USE_CLONEHELPER        
  2461.     delete _fSUCloneHelper;
  2462. #endif    
  2463.  
  2464. #ifdef _PLATFORM_OS2_ //<crs> - implement mutex sem under os/2                                  //D133590
  2465.     APIRET rc = DosCloseMutexSem( _fCurrentKey );                                               //D133590
  2466. #endif //<crs> - end                                                                            //D133590
  2467.  
  2468.     }
  2469.     catch (ODException _exception)
  2470.     {
  2471.         ODSetSOMException(ev, _exception);
  2472.     }
  2473.     
  2474.     CMStorageUnit_parents_somUninit(somSelf);
  2475.  
  2476. }
  2477.  
  2478. //------------------------------------------------------------------------------
  2479. // CMStorageUnit: CleanupAndFail
  2480. //------------------------------------------------------------------------------
  2481.  
  2482. SOM_Scope void  SOMLINK CMStorageUnitCleanupAndFail(CMStorageUnit *somSelf, Environment *ev,
  2483.         ODError err)
  2484. {
  2485.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2486.     CMStorageUnitMethodDebug("CMStorageUnit","CleanupAndFail");
  2487.  
  2488.     try
  2489.     {
  2490.     
  2491. #ifdef ODDebug_ODStorageUnit
  2492.     somPrintf("Cleanup and fail: ID %x PID %x\n", somSelf->GetID(ev), _fObjectID);
  2493. #endif
  2494.  
  2495.     if (_fCurValue != kODNULL) {
  2496.         CMContainer container = _fDraft->GetCMContainer(ev);
  2497.         ODSessionMustHaveCMAllocReserve(container);
  2498.         
  2499.         CMReleaseValue(_fCurValue);
  2500.         
  2501.         ODSessionRestoreCMAllocReserve(container);
  2502.         
  2503.         _fCurValue = kODNULL;
  2504.     }
  2505.     if (err) {
  2506.         BREAK_AND_THROW(err);
  2507.     }
  2508.  
  2509.     }
  2510.     catch (ODException _exception)
  2511.     {
  2512.         ODSetSOMException(ev, _exception);
  2513.     }
  2514. }
  2515.  
  2516. //------------------------------------------------------------------------------
  2517. // CMStorageUnit: Lock
  2518. //------------------------------------------------------------------------------
  2519.  
  2520. SOM_Scope ODStorageUnitKey  SOMLINK CMStorageUnitLock(CMStorageUnit *somSelf, Environment *ev,
  2521.         ODStorageUnitKey key)
  2522. {
  2523.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2524.     CMStorageUnitMethodDebug("CMStorageUnit","Lock");
  2525. #ifdef _PLATFORM_OS2_
  2526.  
  2527.   // Note: under os/2 we wouldn't want to call DosEnterCritSec() here as            //D133590
  2528.   //       we will be making api calls...                                           //D133590
  2529.                                                                                     //D133590
  2530.   TRY                                                                               //D133590
  2531.                                                                                     //D133590
  2532.      if ( key != 0 && key != _fCurrentKey )                                         //D133590
  2533.         {                                                                           //D133590
  2534.         THROW(kODErrInvalidStorageUnitKey);                                         //D133590
  2535.         }                                                                           //D133590
  2536.                                                                                     //D133590
  2537.      APIRET rc = DosOpenMutexSem( 0, &(_fCurrentKey) );                             //D133590
  2538.      if ( rc == NO_ERROR )                                                          //D133590
  2539.         rc = DosRequestMutexSem( _fCurrentKey, SEM_INDEFINITE_WAIT );               //D133590
  2540.                                                                                     //D133590
  2541.      key = ( rc == NO_ERROR ) ? _fCurrentKey : 0;                                   //D133590
  2542.                                                                                     //D133590
  2543.  CATCH_ALL                                                                          //D133590
  2544.                                                                                     //D133590
  2545.     RERAISE;                                                                        //D133590
  2546.                                                                                     //D133590
  2547.   ENDTRY                                                                            //D133590
  2548. #else
  2549.     
  2550.     ODEnterCriticalSection();
  2551.     
  2552.     if (key == 0) {
  2553.         if (_fLockCount == 0) {
  2554.             _fLockCount = 1;
  2555.             _fCurrentKey++;
  2556.             key = _fCurrentKey;
  2557.         }
  2558.     }
  2559.     else {
  2560.         if ((_fCurrentKey != key) || (_fLockCount == 0))
  2561.             ODSetSOMException(ev,kODErrInvalidStorageUnitKey);
  2562.         else
  2563.             _fLockCount++;
  2564.     }
  2565.         
  2566.     ODExitCriticalSection();
  2567.     
  2568. #endif
  2569.  
  2570.     return key;
  2571. }
  2572.  
  2573. //------------------------------------------------------------------------------
  2574. // CMStorageUnit: Unlock
  2575. //------------------------------------------------------------------------------
  2576.  
  2577. SOM_Scope void  SOMLINK CMStorageUnitUnlock(CMStorageUnit *somSelf, Environment *ev,
  2578.         ODStorageUnitKey key)
  2579. {
  2580.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2581.     CMStorageUnitMethodDebug("CMStorageUnit","Unlock");
  2582.  
  2583. #ifdef _PLATFORM_OS2_
  2584.  
  2585.   PTIB  pptib;                                                                      //D133590
  2586.   PPIB  pppib;                                                                      //D133590
  2587.   PID   pid;                                                                        //D133590
  2588.   TID   tid;                                                                        //D133590
  2589.   ULONG ulCount;                                                                    //D133590
  2590.                                                                                     //D133590
  2591.   TRY                                                                               //D133590
  2592.                                                                                     //D133590
  2593.      if ( key != _fCurrentKey )                                                     //D133590
  2594.         {                                                                           //D133590
  2595.         THROW(kODErrInvalidStorageUnitKey);                                         //D133590
  2596.         }                                                                           //D133590
  2597.                                                                                     //D133590
  2598.      APIRET rc = DosQueryMutexSem( _fCurrentKey, &pid, &tid, &ulCount );            //D133590
  2599.      if ( rc != NO_ERROR )                                                          //D133590
  2600.         {                                                                           //D133590
  2601.         THROW(kODErrInvalidStorageUnitKey);                                         //D133590
  2602.         }                                                                           //D133590
  2603.                                                                                     //D133590
  2604.      DosGetInfoBlocks( &pptib, &pppib );                                            //D133590
  2605.      if ( (pid != pppib->pib_ulpid) ||                                              //D133590
  2606.           (tid != pptib->tib_ptib2->tib2_ultid) )                                   //D133590
  2607.         {                                                                           //D133590
  2608.         THROW(kODErrInvalidStorageUnitKey);                                         //D133590
  2609.         }                                                                           //D133590
  2610.                                                                                     //D133590
  2611.      if ( ulCount == 0 )                                                            //D133590
  2612.         {                                                                           //D133590
  2613.         THROW(kODErrStorageUnitNotLocked);                                          //D133590
  2614.         }                                                                           //D133590
  2615.                                                                                     //D133590
  2616.      rc = DosReleaseMutexSem( _fCurrentKey );                                       //D133590
  2617.      if ( rc == NO_ERROR )                                                          //D133590
  2618.         rc = DosCloseMutexSem( _fCurrentKey );                                      //D133590
  2619.                                                                                     //D133590
  2620.                                                                                     //D133590
  2621.   CATCH_ALL                                                                         //D133590
  2622.                                                                                     //D133590
  2623.     RERAISE;                                                                        //D133590
  2624.                                                                                     //D133590
  2625.   ENDTRY                                                                            //D133590
  2626. #else
  2627.     
  2628.     ODEnterCriticalSection();
  2629.     
  2630.     if (_fCurrentKey != key)
  2631.         ODSetSOMException(ev,kODErrInvalidStorageUnitKey);
  2632.     
  2633.     else if (_fLockCount == 0)
  2634.         ODSetSOMException(ev,kODErrStorageUnitNotLocked);
  2635.     
  2636.     else
  2637.         --_fLockCount;
  2638.         
  2639.     ODExitCriticalSection();
  2640.  
  2641. #endif
  2642. }
  2643.  
  2644.  
  2645. //------------------------------------------------------------------------------
  2646. // CMStorageUnit: GetStorageUnitRef
  2647. //------------------------------------------------------------------------------
  2648.  
  2649. SOM_Scope void  SOMLINK CMStorageUnitGetStorageUnitRef(CMStorageUnit *somSelf, Environment *ev,
  2650.         ODStorageUnitID embeddedSUID, ODBoolean strong, ODStorageUnitRef ref)
  2651. {
  2652.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2653.     CMStorageUnitMethodDebug("CMStorageUnit","GetStorageUnitRef");
  2654.  
  2655.     try
  2656.     {
  2657.     FailIfInvalidRefCount();
  2658.  
  2659.     if (embeddedSUID == kODNULLID)
  2660.         THROW(kODErrIllegalNullStorageUnitInput);
  2661.         
  2662.     // Clear incoming reference
  2663.     _fSURefKeeper->InvalidateSURef(ref);
  2664.  
  2665.     CMReference            theReferenceData;    
  2666.     
  2667.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  2668.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  2669.         
  2670.     CMStorageUnit* embeddedSU = (CMStorageUnit*) somSelf->GetDraft(ev)->AcquireStorageUnit(ev, embeddedSUID);
  2671. #ifdef _NO_TMP_OBJS_
  2672.     ODStorageUnit *tempSU = embeddedSU; // ensure it's released
  2673. #else
  2674.     TempODStorageUnit tempSU = embeddedSU; // ensure it's released
  2675. #endif
  2676.     
  2677.     if (embeddedSU) {
  2678.         CMObject object = embeddedSU->GetObject(ev);
  2679.         if (object) {    
  2680.             CMContainer container = _fDraft->GetCMContainer(ev);
  2681.             ODSessionMustHaveCMAllocReserve(container);
  2682.             
  2683.             if (CMGetReferenceForObject(_fCurValue, object, theReferenceData) != kODNULL) {
  2684.                 // we already have this object in the reference, reuse the id            
  2685.                 ODBlockMove(theReferenceData, ref, sizeof(CMReference));
  2686.                 // if strong, value must be strong, else value must be weak
  2687.                 if ((strong != kODFalse) ? somSelf->IsWeakStorageUnitRef(ev, ref) : somSelf->IsStrongStorageUnitRef(ev, ref))
  2688.                     _fSURefKeeper->InvalidateSURef(ref);
  2689.             }
  2690.             
  2691.             if (somSelf->IsValidStorageUnitRef(ev, ref) == kODFalse) {
  2692.                 _fSURefKeeper->GetNextSURef(ref, strong);
  2693. //                ODBlockMove(ref, theReferenceData, sizeof(CMReference));
  2694.             }
  2695.             
  2696. //            CMSetReference(_fCurValue, embeddedSU->GetObject(ev), theReferenceData);
  2697.             CMSetReference(_fCurValue, object, ref);
  2698.  
  2699.             ODSessionRestoreCMAllocReserve(container);
  2700.         }
  2701.     }
  2702. #ifdef _NO_TMP_OBJS_
  2703.     tempSU->Release(ev);
  2704. #endif
  2705.  
  2706.    }
  2707.    catch (ODException _exception)
  2708.    {
  2709.        ODSetSOMException(ev, _exception);
  2710.    } 
  2711.  
  2712. }
  2713.  
  2714. //------------------------------------------------------------------------------
  2715. // CMStorageUnit: SetStorageUnitRef
  2716. //------------------------------------------------------------------------------
  2717.  
  2718. SOM_Scope void  SOMLINK CMStorageUnitSetStorageUnitRef(CMStorageUnit *somSelf, Environment *ev,
  2719.         ODStorageUnitID embeddedSUID, ODStorageUnitRef ref)
  2720. {
  2721.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2722.     CMStorageUnitMethodDebug("CMStorageUnit","SetStorageUnitRef");
  2723.  
  2724.    try
  2725.    {
  2726.     FailIfInvalidRefCount();
  2727.  
  2728.     CMReference            theReferenceData;
  2729.     
  2730.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL) || (_fCurValue == kODNULL))
  2731.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  2732.     
  2733.     if (embeddedSUID == 0)
  2734.         THROW(kODErrIllegalNullIDInput);
  2735.         
  2736.     CMStorageUnit* embeddedSU = (CMStorageUnit*) somSelf->GetDraft(ev)->AcquireStorageUnit(ev, embeddedSUID);
  2737. #ifdef _NO_TMP_OBJS_
  2738.     ODStorageUnit *tempSU = embeddedSU;
  2739. #else
  2740.     TempODStorageUnit tempSU = embeddedSU;
  2741. #endif
  2742.     
  2743.     somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  2744.     
  2745.     ODBlockMove(ref, theReferenceData, sizeof(CMReference));
  2746.     
  2747.     if (_fSURefKeeper != kODNULL) {
  2748.         _fSURefKeeper->Reset(ref);
  2749.     }
  2750.     
  2751.     CMContainer container = _fDraft->GetCMContainer(ev);
  2752.     ODSessionMustHaveCMAllocReserve(container);
  2753.     
  2754.     CMSetReference(_fCurValue, embeddedSU->GetObject(ev), theReferenceData);
  2755.  
  2756.     ODSessionRestoreCMAllocReserve(container);
  2757. #ifdef _NO_TMP_OBJS_
  2758.     tempSU->Release(ev);
  2759. #endif
  2760.  
  2761.     }
  2762.     catch (ODException _exception)
  2763.     {
  2764.          ODSetSOMException(ev, _exception);
  2765.     }
  2766. }
  2767.  
  2768. //------------------------------------------------------------------------------
  2769. // CMStorageUnit: GetHeap
  2770. //------------------------------------------------------------------------------
  2771.  
  2772. SOM_Scope ODMemoryHeapID  SOMLINK CMStorageUnitGetHeap(CMStorageUnit *somSelf, Environment *ev)
  2773. {
  2774.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2775.     CMStorageUnitMethodDebug("CMStorageUnit","GetHeap");
  2776.  
  2777.     return _fHeap;
  2778. }
  2779.  
  2780. //------------------------------------------------------------------------------
  2781. // CMStorageUnit: GetCMContainer
  2782. //------------------------------------------------------------------------------
  2783.  
  2784. SOM_Scope CMContainer  SOMLINK CMStorageUnitGetCMContainer(CMStorageUnit *somSelf, Environment *ev)
  2785. {
  2786.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2787.     CMStorageUnitMethodDebug("CMStorageUnit","GetCMContainer");
  2788.  
  2789.         CMContainer container = kODNULL;
  2790.    try
  2791.    {
  2792.  
  2793.       if(_fDraft)
  2794.     container =  _fDraft->GetCMContainer(ev);
  2795.  
  2796.    }
  2797.    catch (ODException _exception)
  2798.    {
  2799.     container =  kODNULL;
  2800.         ODSetSOMException(ev, _exception);
  2801.    }
  2802.         return container;
  2803. }
  2804.  
  2805. //------------------------------------------------------------------------------
  2806. // CMStorageUnit: SetChangedFromPrevFlag
  2807. //------------------------------------------------------------------------------
  2808.  
  2809. SOM_Scope void  SOMLINK CMStorageUnitSetChangedFromPrevFlag(CMStorageUnit *somSelf, Environment *ev,
  2810.         ODBoolean changed)
  2811. {
  2812.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2813.     CMStorageUnitMethodDebug("CMStorageUnit","SetChangedFromPrevFlag");
  2814.  
  2815.    try
  2816.    {
  2817.  
  2818.     if (_fDraft != kODNULL)
  2819.         _fDraft->SetChangedFromPrevFlag(ev, changed);
  2820.  
  2821.     time((time_t *)(&_fModificationDate));
  2822.     
  2823.     _fDirty = kODTrue;
  2824.  
  2825.     }
  2826.     catch (ODException _exception)
  2827.     {
  2828.         ODSetSOMException(ev, _exception);
  2829.     }
  2830. }
  2831.  
  2832. //------------------------------------------------------------------------------
  2833. // CMStorageUnit: GetObjectID
  2834. //------------------------------------------------------------------------------
  2835.  
  2836. SOM_Scope ODID  SOMLINK CMStorageUnitGetObjectID(CMStorageUnit *somSelf, Environment *ev)
  2837. {
  2838.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2839.     CMStorageUnitMethodDebug("CMStorageUnit","GetObjectID");
  2840.  
  2841.     return (ODID) _fObjectID;
  2842. }
  2843.  
  2844. //------------------------------------------------------------------------------
  2845. // GetPropertySize
  2846. //------------------------------------------------------------------------------
  2847.  
  2848. // Callers must call ODSessionMustHaveCMAllocReserve(container);
  2849.  
  2850. static ODULong GetPropertySize(CMObject object, CMProperty property)
  2851. {
  2852.     ODULong    size = 0;
  2853.         Environment* ev = somGetGlobalEnvironment();
  2854.    try
  2855.    {
  2856.  
  2857.     ODULong    numValues = CMCountValues(object, property, kODNULL);
  2858.     CMValue        curValue = kODNULL;
  2859.     
  2860.     for (ODULong i = 0; i < numValues; i++) {
  2861.         curValue = CMGetNextValue(object, property, curValue);
  2862.         if (curValue != kODNULL) {
  2863.             size += CMGetValueSize(curValue);
  2864.             CMReleaseValue(curValue);
  2865.         }
  2866.     }
  2867.     }
  2868.     catch (ODException _exception)
  2869.     {
  2870.          size = 0;
  2871.          ODSetSOMException(ev, _exception);
  2872.     }
  2873.     return size;
  2874. }
  2875.  
  2876. //------------------------------------------------------------------------------
  2877. // GetObjectSize
  2878. //------------------------------------------------------------------------------
  2879.  
  2880. // Callers must call ODSessionMustHaveCMAllocReserve(container);
  2881.  
  2882. static ODULong GetObjectSize(CMObject object)
  2883. {
  2884.     ODULong    size = 0;
  2885.         Environment* ev = somGetGlobalEnvironment();
  2886.    try
  2887.    {
  2888.     ODULong    numProperties = CMCountProperties(object, kODNULL);
  2889.     CMProperty    curProperty = kODNULL;
  2890.     
  2891.     for (ODULong i = 0; i < numProperties; i++) {
  2892.         curProperty = CMGetNextObjectProperty(object, curProperty);
  2893.         size += GetPropertySize(object, curProperty);
  2894.     }
  2895.     }
  2896.     catch (ODException _exception)
  2897.     {
  2898.          size = 0;
  2899.          ODSetSOMException(ev, _exception);
  2900.     }
  2901.     return size;
  2902. }
  2903.  
  2904. //------------------------------------------------------------------------------
  2905. // CMStorageUnit: GetCurValue
  2906. //------------------------------------------------------------------------------
  2907.  
  2908. SOM_Scope CMValue  SOMLINK CMStorageUnitGetCurValue(CMStorageUnit *somSelf, Environment *ev)
  2909. {
  2910.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2911.     CMStorageUnitMethodDebug("CMStorageUnit","GetCurValue");
  2912.  
  2913.     return _fCurValue;
  2914. }
  2915.  
  2916. //------------------------------------------------------------------------------
  2917. // CMStorageUnit: SetCurValue
  2918. //------------------------------------------------------------------------------
  2919. //
  2920. //SOM_Scope void  SOMLINK CMStorageUnitSetCurValue(CMStorageUnit *somSelf, Environment *ev, CMValue curValue)
  2921. //{
  2922. //    CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2923. //    CMStorageUnitMethodDebug("CMStorageUnit","SetCurValue");
  2924. //
  2925. //    _fCurValue = curValue;
  2926. //}
  2927.  
  2928. //------------------------------------------------------------------------------
  2929. // CMStorageUnit: GetObject
  2930. //------------------------------------------------------------------------------
  2931.  
  2932. SOM_Scope CMObject  SOMLINK CMStorageUnitGetObject(CMStorageUnit *somSelf, Environment *ev)
  2933. {
  2934.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2935.     CMStorageUnitMethodDebug("CMStorageUnit","GetCurValue");
  2936.  
  2937.     return _fObject;
  2938. }
  2939.  
  2940. //------------------------------------------------------------------------------
  2941. // CMStorageUnit: GetCurProperty
  2942. //------------------------------------------------------------------------------
  2943.  
  2944. SOM_Scope CMObject  SOMLINK CMStorageUnitGetCurProperty(CMStorageUnit *somSelf, Environment *ev)
  2945. {
  2946.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2947.     CMStorageUnitMethodDebug("CMStorageUnit","GetCurProperty");
  2948.  
  2949.     return _fCurProperty;
  2950. }
  2951.  
  2952. //------------------------------------------------------------------------------
  2953. // CMStorageUnit: GetCurType
  2954. //------------------------------------------------------------------------------
  2955.  
  2956. SOM_Scope CMType  SOMLINK CMStorageUnitGetCurType(CMStorageUnit *somSelf, Environment *ev)
  2957. {
  2958.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2959.     CMStorageUnitMethodDebug("CMStorageUnit","GetCurType");
  2960.  
  2961.     return _fCurType;
  2962. }
  2963.  
  2964. //------------------------------------------------------------------------------
  2965. // CMStorageUnit: SetCurType
  2966. //------------------------------------------------------------------------------
  2967.  
  2968. SOM_Scope void  SOMLINK CMStorageUnitSetCurType(CMStorageUnit *somSelf, Environment *ev, CMType curType)
  2969. {
  2970.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2971.     CMStorageUnitMethodDebug("CMStorageUnit","SetCurType");
  2972.  
  2973.     _fCurType = curType;
  2974. }
  2975.  
  2976. //------------------------------------------------------------------------------
  2977. // CMStorageUnit: IsPromiseValue
  2978. //------------------------------------------------------------------------------
  2979.  
  2980. SOM_Scope ODBoolean  SOMLINK CMStorageUnitIsPromiseValue(CMStorageUnit *somSelf, Environment *ev)
  2981. {
  2982.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  2983.     CMStorageUnitMethodDebug("CMStorageUnit","CMStorageUnitIsPromiseValue");
  2984.  
  2985.         ODBoolean valueFound = kODFalse;
  2986.  
  2987.    try
  2988.    {
  2989.     valueFound =  _fPromiseResolver->IsPromiseValue(ev);
  2990.  
  2991.    }
  2992.    catch (ODException _exception)
  2993.    {
  2994.         ODSetSOMException(ev, _exception);
  2995.     valueFound =  kODFalse;
  2996.    }
  2997.         return valueFound;
  2998. }
  2999.  
  3000. //------------------------------------------------------------------------------
  3001. // CMStorageUnit: SetPromiseValue
  3002. //------------------------------------------------------------------------------
  3003.  
  3004. SOM_Scope void  SOMLINK CMStorageUnitSetPromiseValue(CMStorageUnit *somSelf, Environment *ev,
  3005.         ODValueType valueType,
  3006.         ODULong offset,
  3007.         ODByteArray* value,
  3008.         ODPart* sourcePart)
  3009. {
  3010.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  3011.     CMStorageUnitMethodDebug("CMStorageUnit","CMStorageUnitSetPromiseValue");
  3012.  
  3013.    try
  3014.    {
  3015.     FailIfIllegalByteArray(value);
  3016.     FailIfInvalidRefCount();
  3017.  
  3018.     if ((_fObject == kODNULL) || (_fCurProperty == kODNULL))
  3019.         BREAK_AND_THROW(kODErrUnfocusedStorageUnit);
  3020.     
  3021.     if (_fPromiseResolver != kODNULL) {
  3022.         _fPromiseResolver->SettingPromise();
  3023.  
  3024.         try
  3025.                 {
  3026. #if ODDebug_AddValue
  3027.             ODPropertyName propertyName = somSelf->GetProperty(ev);
  3028.             if (somSelf->Exists(ev, propertyName, valueType, 0) != kODFalse) {
  3029.                 somSelf->Focus(ev, propertyName, kODPosUndefined, valueType, 0, kODPosUndefined);
  3030.             }
  3031.             else
  3032.                 somSelf->AddValue(ev, valueType);
  3033.             ODDisposePtr(propertyName);
  3034. #else                
  3035.             somSelf->AddValue(ev, valueType);
  3036. #endif
  3037.             somSelf->SetChangedFromPrevFlag(ev, kODTrue);
  3038.             
  3039.             somSelf->SetOffset(ev, offset);
  3040.             somSelf->SetValue(ev, value);
  3041.             
  3042.             _fPromiseResolver->SetSourcePart(ev, sourcePart);
  3043.             
  3044.             _fPromiseResolver->DoneSettingPromise();
  3045.                 }
  3046.         catch (ODException _exception)
  3047.                 {
  3048.             _fPromiseResolver->DoneSettingPromise();
  3049.             throw;
  3050.         }
  3051.     }
  3052.  
  3053.     }
  3054.     catch (ODException _exception)
  3055.     {
  3056.         ODSetSOMException(ev, _exception);
  3057.     }
  3058. }
  3059.  
  3060. //------------------------------------------------------------------------------
  3061. // CMStorageUnit: GetPromiseValue
  3062. //------------------------------------------------------------------------------
  3063.  
  3064. SOM_Scope ODULong  SOMLINK CMStorageUnitGetPromiseValue(CMStorageUnit *somSelf, Environment *ev,
  3065.         ODValueType valueType,
  3066.         ODULong offset,
  3067.         ODULong length,
  3068.         ODByteArray* value,
  3069.         ODPart** sourcePart)
  3070. {
  3071.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  3072.     CMStorageUnitMethodDebug("CMStorageUnit","CMStorageUnitGetPromiseValue");
  3073.  
  3074.      value->_length = 0;
  3075.  
  3076.    try
  3077.    {
  3078.     FailIfIllegalByteArray(value);
  3079.     FailIfInvalidRefCount();
  3080.  
  3081.     somSelf->Focus(ev, kODNULL, kODPosSame, valueType, 0, kODPosSame);
  3082.     
  3083.     value->_buffer = (octet*) ODNewPtr(length);
  3084.     value->_maximum = length;
  3085.     value->_length = CMReadValueData(_fCurValue, (CMPtr) value->_buffer, offset, length);
  3086.     
  3087.     if ((_fPromiseResolver != kODNULL) && (sourcePart != kODNULL))
  3088.         *sourcePart = _fPromiseResolver->GetSourcePart(ev);
  3089.     
  3090.  
  3091.      }
  3092.      catch (ODException _exception)
  3093.      {
  3094.        if(value->_buffer)
  3095.          ODDisposePtr(value->_buffer);
  3096.      value->_length = 0;
  3097.          ODSetSOMException(ev, _exception);
  3098.      }    
  3099.     return value->_length;
  3100. }
  3101.  
  3102. //------------------------------------------------------------------------------
  3103. // CMStorageUnit: ClearAllPromises
  3104. //------------------------------------------------------------------------------
  3105.  
  3106. SOM_Scope void  SOMLINK CMStorageUnitClearAllPromises(CMStorageUnit *somSelf, Environment *ev)
  3107. {
  3108.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  3109.     CMStorageUnitMethodDebug("CMStorageUnit","CMStorageUnitClearAllPromises");
  3110.  
  3111.     ODBoolean    ret = kODFalse;
  3112.    try
  3113.    {
  3114.     FailIfInvalidRefCount();
  3115.     
  3116.     if (_fPromiseResolver->GetPromiseCount())
  3117.     {
  3118.         somSelf->Focus(ev,
  3119.                         (ODPropertyName) kODNULL, 
  3120.                         kODPosAll,
  3121.                         kODTypeAll,
  3122.                         0,
  3123.                         kODPosUndefined);
  3124.         ODULong numProperties = somSelf->CountProperties(ev);
  3125.         for (ODULong i = 0; i < numProperties; i++)
  3126.         {
  3127.             somSelf->Focus(ev,
  3128.                         (ODPropertyName) kODNULL,
  3129.                         kODPosNextSib,
  3130.                         kODTypeAll,
  3131.                         0,
  3132.                         kODPosUndefined);
  3133.             ODULong numValues = somSelf->CountValues(ev);    
  3134.             for (ODULong j = 0; j < numValues; j++)
  3135.             {
  3136.                 somSelf->Focus(ev,
  3137.                             (ODPropertyName)kODNULL,
  3138.                             kODPosSame,
  3139.                             kODTypeAll,
  3140.                             0,
  3141.                             kODPosNextSib);
  3142.                 _fPromiseResolver->ClearPromise(ev);
  3143.                 if (_fPromiseResolver->GetPromiseCount() == 0)
  3144.                 {
  3145.                     ret = kODTrue;
  3146.                     break;
  3147.                 }
  3148.             }
  3149.             if (ret)
  3150.                 break;
  3151.         }
  3152.     }
  3153.  
  3154.     }
  3155.     catch (ODException _exception)
  3156.     {
  3157.     ret = kODFalse;
  3158.         ODSetSOMException(ev, _exception);
  3159.     }
  3160. }
  3161.  
  3162. //------------------------------------------------------------------------------
  3163. // CMStorageUnit: ResolveAllPromises
  3164. //------------------------------------------------------------------------------
  3165.  
  3166. SOM_Scope void  SOMLINK CMStorageUnitResolveAllPromises(CMStorageUnit *somSelf, Environment *ev)
  3167. {
  3168.     CMStorageUnitData *somThis = CMStorageUnitGetData(somSelf);
  3169.     CMStorageUnitMethodDebug("CMStorageUnit","CMStorageUnitResolveAllPromises");
  3170.  
  3171.     ODBoolean    ret = kODFalse;
  3172.     try
  3173.     {
  3174.  
  3175.     if (_fPromiseResolver && _fPromiseResolver->GetPromiseCount())
  3176.     {
  3177.         somSelf->Focus(ev, (ODPropertyName)kODNULL, 
  3178.                                 kODPosAll,
  3179.                                 kODTypeAll,
  3180.                                 0,
  3181.                                 kODPosUndefined);
  3182.         ODULong numProperties = somSelf->CountProperties(ev);
  3183.         for (ODULong i = 0; i < numProperties; i++)
  3184.         {
  3185.             somSelf->Focus(ev, (ODPropertyName)kODNULL,
  3186.                         kODPosNextSib,
  3187.                         kODTypeAll,
  3188.                         0,
  3189.                         kODPosUndefined);
  3190.             ODULong numValues = somSelf->CountValues(ev);    
  3191.             for (ODULong j = 0; j < numValues; j++)
  3192.             {
  3193.                 somSelf->Focus(ev, (ODPropertyName)kODNULL,
  3194.                             kODPosSame,
  3195.                             kODTypeAll,
  3196.                             0,
  3197.                             kODPosNextSib);
  3198.                 PreserveFocus* focus = new PreserveFocus(ev, somSelf);
  3199.                 _fPromiseResolver->ResolvePromise(ev);
  3200.                 delete focus;
  3201.                 numValues = somSelf->CountValues(ev);
  3202.                 numProperties = somSelf->CountProperties(ev);
  3203.                 if (_fPromiseResolver->GetPromiseCount() == 0)
  3204.                 {
  3205.                     ret = kODTrue;
  3206.                     break;
  3207.                 }
  3208.             }
  3209.             if (ret)
  3210.                 break;
  3211.         }
  3212.     }
  3213.  
  3214.     }
  3215.     catch (ODException _exception)
  3216.     {
  3217.     ret = kODFalse;
  3218.         ODSetSOMException(ev, _exception);
  3219.     }
  3220.  
  3221. }
  3222.  
  3223. //------------------------------------------------------------------------------
  3224. // NewODStorageUnitView
  3225. //------------------------------------------------------------------------------
  3226.  
  3227. static ODStorageUnitView* NewODStorageUnitView(ODMemoryHeapID heapID)
  3228. {
  3229.         Environment* ev = somGetGlobalEnvironment();
  3230.     ODStorageUnitView*    suView = kODNULL;
  3231.    try
  3232.    {
  3233. #ifdef _PLATFORM_MACINTOSH_
  3234.     SOMClass*    suViewClass = somNewClassReference(ODStorageUnitView);
  3235.     ODULong        size = suViewClass->somGetInstanceSize();
  3236.     ODPtr        buffer = ODNewPtr(size, heapID);
  3237.     suView = (ODStorageUnitView*) suViewClass->somRenew(buffer);
  3238.     somReleaseClassReference ( suViewClass );
  3239. #endif
  3240. #if defined(_PLATFORM_WIN32_)||defined(_PLATFORM_OS2_)||defined(_PLATFORM_AIX_)
  3241. //    The use of heap is not implemented in Windows platform
  3242.                   suView  = new ODStorageUnitView;
  3243. #endif
  3244.    }
  3245.    catch (ODException _exception)
  3246.    {
  3247.         ODDeleteObject(suView);
  3248.         ODSetSOMException(ev, _exception);
  3249.    }
  3250.     
  3251.     return suView;
  3252. }
  3253.  
  3254. //------------------------------------------------------------------------------
  3255. // NewODStorageUnitCursor
  3256. //------------------------------------------------------------------------------
  3257.  
  3258. static ODStorageUnitCursor* NewODStorageUnitCursor(ODMemoryHeapID heapID)
  3259. {
  3260.         Environment* ev = somGetGlobalEnvironment();
  3261.     ODStorageUnitCursor*    suCursor = kODNULL;
  3262.    try
  3263.    {
  3264. #ifdef _PLATFORM_MACINTOSH_
  3265.     SOMClass*    suCursorClass = somNewClassReference(ODStorageUnitCursor);
  3266.     ODULong        size = suCursorClass->somGetInstanceSize();
  3267.     ODPtr        buffer = ODNewPtr(size, heapID);
  3268.     suCursor = (ODStorageUnitCursor*) suCursorClass->somRenew(buffer);
  3269.     somReleaseClassReference ( suCursorClass );
  3270. #endif
  3271. #if defined(_PLATFORM_WIN32_)||defined(_PLATFORM_OS2_)||defined(_PLATFORM_AIX_)
  3272. //    The use of heap is not implemented in Windows platform
  3273.                    suCursor  = new ODStorageUnitCursor;
  3274. #endif
  3275.    }
  3276.    catch (ODException _exception)
  3277.    {
  3278.         ODDeleteObject(suCursor);
  3279.         ODSetSOMException(ev, _exception);
  3280.    }
  3281.     
  3282.     return suCursor;
  3283. }
  3284.  
  3285. //------------------------------------------------------------------------------
  3286. // NewCMStorageUnitRefIterator
  3287. //------------------------------------------------------------------------------
  3288.  
  3289. static CMStorageUnitRefIterator* NewCMStorageUnitRefIterator(ODMemoryHeapID heapID)
  3290. {
  3291.         Environment* ev = somGetGlobalEnvironment();
  3292.     CMStorageUnitRefIterator*    refIter = kODNULL;
  3293.    try
  3294.    {
  3295. #ifdef _PLATFORM_MACINTOSH_
  3296.     SOMClass*    refIterClass = somNewClassReference(CMStorageUnitRefIterator);
  3297.     ODULong        size = refIterClass->somGetInstanceSize();
  3298.     ODPtr        buffer = ODNewPtr(size, heapID);
  3299.     refIter = (CMStorageUnitRefIterator*) refIterClass->somRenew(buffer);
  3300.     somReleaseClassReference ( refIterClass );
  3301. #endif
  3302. #if defined(_PLATFORM_WIN32_)||defined(_PLATFORM_OS2_)||defined(_PLATFORM_AIX_)
  3303. //    The use of heap is not implemented in Windows platform
  3304.                refIter = new CMStorageUnitRefIterator;
  3305. #endif
  3306.    }
  3307.    catch (ODException _exception)
  3308.    {
  3309.         ODDeleteObject(refIter);
  3310.         ODSetSOMException(ev, _exception);
  3311.    }
  3312.     
  3313.     return refIter;
  3314. }
  3315.  
  3316.  
  3317.