home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Utilities / ODUtils.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  5.0 KB  |  275 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ODUtils.cpp
  3.  
  4.     Contains:    Utility functions for objects
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1994-1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     5/24/96    jpa        1292388: Added special-case ODGetDraft &
  13.                                     ODGetSession for nonpersistent frames.
  14.          <2>     3/15/96    DM        1292140: throw when persistent object has
  15.                                     no storage unit getting draft or session
  16.  
  17.     To Do:
  18.     
  19.     In Progress:
  20.         
  21. */
  22.  
  23.  
  24. #ifndef _ODUTILS_
  25. #include "ODUtils.h"
  26. #endif
  27.  
  28. #ifndef SOM_ODFrame_xh
  29. #include <Frame.xh>
  30. #endif
  31.  
  32. #ifndef SOM_ODShape_xh
  33. #include <Shape.xh>
  34. #endif
  35.  
  36. #ifndef SOM_ODTransform_xh
  37. #include <Trnsform.xh>
  38. #endif
  39.  
  40. #ifndef SOM_Module_OpenDoc_Errors_defined
  41. #include <ErrorDef.xh>
  42. #endif
  43.  
  44. #ifndef SOM_ODDocument_xh
  45. #include <Document.xh>
  46. #endif
  47.  
  48. #ifndef SOM_ODContainer_xh
  49. #include <ODCtr.xh>
  50. #endif
  51.  
  52. #ifndef SOM_ODStorageSystem_xh
  53. #include <ODStor.xh>
  54. #endif
  55.  
  56. #ifndef _EXCEPT_
  57. #include <Except.h>
  58. #endif
  59.  
  60. #ifndef _ODDEBUG_
  61. #include <ODDebug.h>
  62. #endif
  63.  
  64. #ifndef SOM_ODFacet_xh
  65. #include <Facet.xh>
  66. #endif
  67.  
  68. #ifndef SOM_ODWindow_xh
  69. #include <Window.xh>
  70. #endif
  71.  
  72. #ifndef SOM_ODDraft_xh
  73. #include <Draft.xh>
  74. #endif
  75.  
  76. #ifndef SOM_ODStorageUnit_xh
  77. #include <StorageU.xh>
  78. #endif
  79.  
  80. #ifndef SOM_ODPart_xh
  81. #include <Part.xh>
  82. #endif
  83.  
  84. #ifndef _TEMPOBJ_
  85. #include <TempObj.h>
  86. #endif
  87.  
  88.  
  89. void ODAcquireObject(Environment* ev, ODRefCntObject* object)
  90. {
  91.     if (object != kODNULL) 
  92.     {
  93.         object->Acquire(ev);
  94.     }
  95. }
  96.  
  97.  
  98. void ODRelease( Environment *ev, ODRefCntObject* obj )
  99. {
  100.     if( obj )
  101.         obj->Release(ev);
  102. }
  103.  
  104.  
  105. void ODFinalRelease( Environment *ev, ODRefCntObject* obj )
  106. {
  107.     if( obj ) {
  108. #if ODDebug
  109.         ODULong ref = obj->GetRefCount(ev);
  110.         if( ref != 1 ) {
  111.             WARN("%s %p's refcount is %ld, not 1",
  112.                     obj->somGetClassName(),obj,ref);
  113.         }
  114. #endif
  115.         obj->Release(ev);
  116.     }
  117. }
  118.  
  119.  
  120. void ODSafeReleaseObject( ODRefCntObject *obj )
  121. {
  122.     if( obj ) {
  123.         TRY{
  124.             obj->Release(somGetGlobalEnvironment());
  125.         }CATCH_ALL{
  126.             // do nothing
  127.         }ENDTRY
  128.     }
  129. }
  130.  
  131.  
  132. void ODTransferReference( Environment *ev, ODRefCntObject *oldObj, ODRefCntObject *newObj )
  133. {
  134.     if( oldObj != newObj ) {
  135.         if( newObj )
  136.             newObj->Acquire(ev);
  137.         if( oldObj ) {
  138.             TRY{
  139.                 oldObj->Release(ev);
  140.             }CATCH_ALL{
  141.                 if( newObj ) newObj->Release(ev);
  142.                 RERAISE;
  143.             }ENDTRY
  144.         }
  145.     }
  146. }
  147.  
  148.  
  149. ODShape* ODCopyAndRelease( Environment *ev, ODShape *s )
  150. {
  151.     ASSERT(s!=kODNULL, kODErrIllegalNullInput);
  152.     if( s->GetRefCount(ev) == 1 )
  153.         return s;
  154.     TempODShape tempS = s;        // Ensures that s gets released
  155.     ODShape *copy = s->Copy(ev);
  156.     return copy;
  157. }
  158.  
  159. ODTransform* ODCopyAndRelease( Environment *ev, ODTransform *t )
  160. {
  161.     ASSERT(t!=kODNULL, kODErrIllegalNullInput);
  162.     if( t->GetRefCount(ev) == 1 )
  163.         return t;
  164.     TempODTransform tempT = t;        // Ensures that t gets released
  165.     ODTransform *copy = t->Copy(ev);
  166.     return copy;
  167. }
  168.  
  169. #if ODDebug || !defined(_OD_IMPL_)
  170. ODBoolean ODObjectsAreEqual(Environment* ev, ODObject* a, ODObject* b)
  171. {
  172.     return (a == b) || (a && a->IsEqualTo(ev,b));
  173. }
  174. #endif
  175.  
  176.  
  177. ODPart* ODAcquirePart( Environment *ev, ODFrame *f )
  178. {
  179.     return f ?f->AcquirePart(ev) :kODNULL;
  180. }
  181.  
  182. ODPart* ODAcquirePart( Environment *ev, ODFacet *f )
  183. {
  184.     return f ?f->GetFrame(ev)->AcquirePart(ev) :kODNULL;
  185. }
  186.  
  187.  
  188. ODDraft* ODGetDraft( Environment *ev, ODStorageUnit *su )
  189. {
  190.     return su ?su->GetDraft(ev) :kODNULL;
  191. }
  192.  
  193. ODDraft* ODGetDraft( Environment *ev, ODPersistentObject *po )
  194. {
  195.     if ( po )
  196.     {
  197.         ODStorageUnit* su = po->GetStorageUnit(ev);
  198.         if ( su )
  199.             return su->GetDraft(ev);
  200.         else
  201.         {
  202.             WARN("ODGetDraft failed, obj has no SU");
  203.             THROW(kODErrInvalidPersistentObject);
  204.         }
  205.     }
  206.     return kODNULL;
  207. }
  208.  
  209. ODDraft* ODGetDraft( Environment *ev, ODFrame *frame )
  210. {
  211.     if( frame ) {
  212.         ODStorageUnit* su = frame->GetStorageUnit(ev);
  213.         if ( su )
  214.             return su->GetDraft(ev);
  215.         else
  216.         {
  217.             // Workaround for nonpersistent frames:
  218.             ODPart *part = frame->AcquirePart(ev);    // TempODPart would be overkill here
  219.             ODDraft *draft = ODGetDraft(ev,part);
  220.             ODReleaseObject(ev,part);
  221.             return draft;
  222.         }
  223.     }
  224.     return kODNULL;
  225. }
  226.  
  227. ODSession* ODGetSession( Environment *ev, ODStorageUnit *su )
  228. {
  229.     return su ?su->GetSession(ev) :kODNULL;
  230. }
  231.  
  232. ODSession* ODGetSession( Environment *ev, ODPersistentObject *po )
  233. {
  234.     if ( po )
  235.     {
  236.         ODStorageUnit* su = po->GetStorageUnit(ev);
  237.         if ( su )
  238.             return su->GetSession(ev);
  239.         else
  240.         {
  241.             WARN("ODGetSession failed, obj has no SU");
  242.             THROW(kODErrInvalidPersistentObject);
  243.         }
  244.     }
  245.     return kODNULL;
  246. }
  247.  
  248. ODSession* ODGetSession( Environment *ev, ODFrame *frame )
  249. {
  250.     if( frame ) {
  251.         ODStorageUnit* su = frame->GetStorageUnit(ev);
  252.         if ( su )
  253.             return su->GetSession(ev);
  254.         else
  255.         {
  256.             // Workaround for nonpersistent frames:
  257.             ODPart *part = frame->AcquirePart(ev);    // TempODPart would be overkill here
  258.             ODSession *session = ODGetSession(ev,part);
  259.             ODReleaseObject(ev,part);
  260.             return session;
  261.         }
  262.     }
  263.     return kODNULL;
  264. }
  265.  
  266. ODSession* ODGetSession( Environment *ev, ODDocument *d )
  267. {
  268.     return d ?d->GetContainer(ev)->GetStorageSystem(ev)->GetSession(ev) :kODNULL;
  269. }
  270.  
  271. ODSession* ODGetSession( Environment *ev, ODDraft *d )
  272. {
  273.     return d ?d->GetDocument(ev)->GetContainer(ev)->GetStorageSystem(ev)->GetSession(ev) :kODNULL;
  274. }
  275.