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 Development Framework / ODFDev / ODF / Found / ODUtils / TempObj.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-17  |  14.2 KB  |  603 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TempObj.h
  3.  
  4.     Contains:    Template utilities for exception-safe temporary object references
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1995 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     5/31/96    jpa        T10012: Added TempAEDescStruct.
  13.          <2>     5/24/96    jpa        1322106: Add operator "&" so taking address
  14.                                     works as expected. 1.1MRD: pragma internal.
  15.  
  16.     Theory Of Operation:
  17.     
  18.         *** See the Tech Note "Temporary References/Objects" for full documentation.
  19.  
  20.     In Progress:
  21.         
  22. */
  23.  
  24. #ifndef _TEMPOBJ_
  25. #define _TEMPOBJ_
  26.  
  27. #ifndef _EXCEPT_
  28. #include <Except.h>
  29. #endif
  30.  
  31. #ifndef SOM_ODRefCntObject_xh
  32. #include "Part.xh"
  33. #endif
  34.  
  35. #ifndef SOM_ODWindow_xh
  36. #include "Window.xh"
  37. #endif
  38.  
  39. #ifndef _ODTYPES_
  40. #include <ODTypes.h>
  41. #endif
  42.  
  43. #ifndef __APPLEEVENTS__
  44. #include <AppleEvents.h>
  45. #endif
  46.  
  47.  
  48. #ifdef PRAGMA_INTERNAL_SUPPORTED
  49. #pragma internal on
  50. #endif
  51.  
  52.  
  53. class TempODPtr :Destructo
  54. {
  55.     public:
  56.     TempODPtr( );
  57.     TempODPtr( void *block );
  58.     ~TempODPtr( );
  59.     operator void* ( )                {return fBlock;}
  60.     void* operator= ( void *b )        {return (fBlock = b);}
  61.     
  62.     protected:
  63.     void* fBlock;
  64.  
  65.     private: // disallow these:
  66.     TempODPtr(const TempODPtr& );
  67.     void operator=(const TempODPtr& );
  68. };
  69.  
  70. class BaseTempObj :Destructo
  71. {
  72.     public:
  73.     ~BaseTempObj();
  74.     
  75.     protected:
  76.     ODObject *fObj;
  77.     BaseTempObj( )        { }
  78.  
  79.     private: // disallow these:
  80.     BaseTempObj(const BaseTempObj& );
  81.     void operator=(const BaseTempObj& );
  82.     // Bitwise assigning one destructo to another smashes
  83.     // a list link with potentially unpleasant effects.
  84. };
  85.  
  86. class BaseTempRef :Destructo
  87. {
  88.     public:
  89.     void Release();
  90.     ~BaseTempRef();
  91.     
  92.     protected:
  93.     ODRefCntObject *fObj;
  94.     BaseTempRef( )        { }
  95.  
  96.     private: // disallow these:
  97.     BaseTempRef(const BaseTempRef& );
  98.     void operator=(const BaseTempRef& );
  99.     // Bitwise assigning one destructo to another smashes
  100.     // a list link with potentially unpleasant effects.
  101. };
  102.  
  103.  
  104. #ifdef _USE_TEMPLATES_
  105.  
  106.  
  107.     //===========================================================================
  108.     //    TempObj <T>
  109.     //===========================================================================
  110.     
  111.     
  112.     template<class T> class TempObj :public BaseTempObj
  113.     {
  114.         public:
  115.         TempObj( T* );
  116.         T* operator-> ()        {return (T*)fObj;}
  117.         T** operator& ()        {return (T**)&fObj;}
  118.         operator T* ()            {return (T*)fObj;}
  119.     
  120.         T* operator= (T* t)        {fObj=t; return t;}
  121.         
  122.         T* DontDelete()            {T* temp=(T*)fObj; fObj=kODNULL; return temp;}
  123.     };
  124.     
  125.     // Implementation of the TempObj constructor:
  126.     template<class T> TempObj<T>::TempObj( T *obj )
  127.     {
  128.         fObj = obj;
  129.     }
  130.     
  131.     
  132.     //===========================================================================
  133.     //    TempRef <T>
  134.     //
  135.     //    Supports a few extra goodies:
  136.     //        Release()        Releases the object and sets the pointer to NULL.
  137.     //        DontRelease()    Sets the pointer to NULL (so the destructor will not
  138.     //                            release the object) but returns the old pointer.
  139.     //
  140.     //===========================================================================
  141.     
  142.     
  143.     template<class T> class TempRef :public BaseTempRef
  144.     {
  145.         public:
  146.         TempRef( T* );
  147.         T* operator-> ()        {return (T*)fObj;}
  148.         T** operator& ()        {return (T**)&fObj;}
  149.         operator T* ()            {return (T*)fObj;}
  150.         T* operator=( T *t )    {fObj=t; return t;}
  151.         
  152.         T* DontRelease()        {T* temp=(T*)fObj; fObj=kODNULL; return temp;}
  153.     };
  154.     
  155.     
  156.     // Implementation of the TempRef constructor:
  157.     template<class T> TempRef<T>::TempRef( T *obj )
  158.     {
  159.         fObj = obj;
  160.     }
  161.  
  162.  
  163. #endif /*_USE_TEMPLATES_*/    
  164.  
  165.  
  166. //===========================================================================
  167. //    Instantiations of TempObj and TempRef. Add your own if necessary.
  168. //===========================================================================
  169.  
  170. class ODFocusSetIterator;
  171. class ODSession;
  172. class ODTypeList;
  173. class ODStorageUnitView;
  174.  
  175. class ODExtension;
  176. class ODSettingExtension;
  177.  
  178. class ODFrame;
  179. class ODPart;
  180. class ODShape;
  181. class ODTransform;
  182. class ODStorageUnit;
  183.  
  184. class ODDraft;
  185. class ODDocument;
  186. class ODContainer;
  187. class ODLink;
  188. class ODLinkSource;
  189. class ODMenuBar;
  190.  
  191. #ifdef _USE_TEMPLATES_
  192.  
  193.     typedef TempObj<ODObject>                TempODObject;
  194.     typedef TempObj<ODSession>                TempODSession;
  195.     typedef TempObj<ODTypeList>                TempODTypeList;
  196.     typedef TempObj<ODStorageUnitView>        TempODStorageUnitView;
  197.     
  198.     //typedef TempRef<ODExtension>            TempODExtension;
  199.     //typedef TempRef<ODSettingExtension>        TempODSettingExtension;
  200.  
  201.     typedef TempRef<ODRefCntObject>            TempODRefCntObject;
  202.     typedef TempRef<ODWindow>                TempODWindow;
  203.     typedef TempRef<ODFrame>                TempODFrame;
  204.     typedef TempRef<ODPart>                    TempODPart;
  205.     typedef TempRef<ODShape>                TempODShape;
  206.     typedef TempRef<ODTransform>            TempODTransform;
  207.     typedef TempRef<ODStorageUnit>            TempODStorageUnit;
  208.  
  209.     typedef TempRef<ODDraft>                TempODDraft;
  210.     typedef TempRef<ODDocument>                TempODDocument;
  211.     typedef TempRef<ODContainer>            TempODContainer;
  212.     typedef TempRef<ODLink>                    TempODLink;
  213.     typedef TempRef<ODLinkSource>            TempODLinkSource;
  214.     typedef TempRef<ODMenuBar>                TempODMenuBar;
  215.  
  216. #else /* not _USE_TEMPLATES_ */
  217.  
  218.     #ifdef __MWERKS__
  219.         // Make sure 'pragma once' mode is off so the .th files can be included
  220.         // more than once!
  221.         #pragma push
  222.         #pragma once off
  223.     #endif
  224.  
  225.     /* If not using templates, we rely on the preprocessor. A "template" for an
  226.         instantiation of TempObj is in TempObj.th, and for TempRef in TempRef.th.
  227.         Include one of these headers per instantiation. */
  228.  
  229.     #define _T_        ODObject
  230.     #define _C_        TempODObject
  231.     #include "TempObj.th"
  232.     #define _T_        ODSession
  233.     #define _C_        TempODSession
  234.     #include "TempObj.th"
  235.     #define _T_        ODTypeList
  236.     #define _C_        TempODTypeList
  237.     #include "TempObj.th"
  238.     #define _T_        ODStorageUnitView
  239.     #define _C_        TempODStorageUnitView
  240.     #include "TempObj.th"
  241.     
  242.     #define _T_        ODRefCntObject
  243.     #define _C_        TempODRefCntObject
  244.     #include "TempRef.th"
  245.     #define _T_        ODWindow
  246.     #define _C_        TempODWindow
  247.     #include "TempRef.th"
  248.     #define _T_        ODFrame
  249.     #define _C_        TempODFrame
  250.     #include "TempRef.th"
  251.     #define _T_        ODPart
  252.     #define _C_        TempODPart
  253.     #include "TempRef.th"
  254.     #define _T_        ODShape
  255.     #define _C_        TempODShape
  256.     #include "TempRef.th"
  257.     #define _T_        ODTransform
  258.     #define _C_        TempODTransform
  259.     #include "TempRef.th"
  260.     #define _T_        ODStorageUnit
  261.     #define _C_        TempODStorageUnit
  262.     #include "TempRef.th"
  263.  
  264.     #define _T_        ODDraft
  265.     #define _C_        TempODDraft
  266.     #include "TempRef.th"
  267.     #define _T_        ODDocument
  268.     #define _C_        TempODDocument
  269.     #include "TempRef.th"
  270.     #define _T_        ODContainer
  271.     #define _C_        TempODContainer
  272.     #include "TempRef.th"
  273.     #define _T_        ODLink
  274.     #define _C_        TempODLink
  275.     #include "TempRef.th"
  276.     #define _T_        ODLinkSource
  277.     #define _C_        TempODLinkSource
  278.     #include "TempRef.th"
  279.     #define _T_        ODMenuBar
  280.     #define _C_        TempODMenuBar
  281.     #include "TempRef.th"
  282.  
  283. /*
  284.     #define _T_        ODExtension
  285.     #define _C_        TempODExtension
  286.     #include "TempRef.th"
  287.     #define _T_        ODSettingsExtension
  288.     #define _C_        TempODSettingsExtension
  289.     #include "TempRef.th"
  290. */
  291.     #ifdef __MWERKS__
  292.         #pragma pop
  293.     #endif
  294.  
  295. #endif /*_USE_TEMPLATES_*/
  296.  
  297.  
  298. //===========================================================================
  299. //    Temp strings.
  300. //===========================================================================
  301.  
  302. class TempODString :Destructo
  303. {
  304.     protected:
  305.     char* fStr; 
  306.  
  307.     public:
  308.     ~TempODString();
  309.     TempODString( char* );
  310.     operator char* ()         { return fStr; }
  311.  
  312.     char* operator= (char* s) { fStr = s; return s; }
  313.     
  314.     char* DontDelete()        { char* s = fStr; fStr = kODNULL; return s;}
  315.  
  316.     private: // disallow these:
  317.     TempODString( );
  318.     TempODString(const TempODString& );
  319.     void operator=(const TempODString& );
  320. };
  321.  
  322. // typedef string  ODISOStr;
  323.     // typedef ODISOStr ODContainerType;
  324.     // typedef ODISOStr  ODPropertyName;
  325.     // typedef ODISOStr  ODStorageUnitName;
  326.     // typedef    ODISOStr    ODDraftName;
  327.  
  328.     // typedef ODISOStr  ODType;
  329.         // typedef ODType    ODValueType;
  330.         // typedef ODType   ODObjectType;
  331.         // typedef ODType   ODFocusType;
  332.  
  333. // typedef char* ODEditor;
  334.  
  335.  
  336. typedef TempODString TempODISOStr;
  337. typedef TempODString TempODType;
  338. typedef TempODString TempODValueType;
  339. typedef TempODString TempODObjectType;
  340. typedef TempODString TempODFocusType;
  341. typedef TempODString TempODContainerType;
  342. typedef TempODString TempODPropertyName;
  343. typedef TempODString TempODStorageUnitName;
  344. typedef TempODString TempODDraftName;
  345. typedef TempODString TempStringPtr;
  346.  
  347. typedef TempODString TempODEditor;
  348.  
  349.  
  350. //===========================================================================
  351. //    Temp handles.
  352. //===========================================================================
  353.  
  354. class TempODHandle :Destructo
  355. {
  356.     protected:
  357.     ODHandle fHandle; 
  358.  
  359.     public:
  360.     ~TempODHandle();
  361.     TempODHandle( ODHandle );
  362.     operator ODHandle ()           { return fHandle; }
  363.  
  364.     ODHandle operator= (ODHandle h)
  365.     { fHandle = h; return h; }
  366.     
  367.     ODHandle DontDelete()
  368.     { ODHandle h = fHandle; fHandle = kODNULL; return h;}
  369.  
  370.     private: // disallow these:
  371.     TempODHandle( );
  372.     TempODHandle(const TempODHandle& );
  373.     void operator=(const TempODHandle& );
  374. };
  375.  
  376. class TempODHandleLock :Destructo
  377. {
  378.     protected:
  379.     ODHandle fHandle; 
  380.  
  381.     public:
  382.     ~TempODHandleLock();            // unlocks the handle
  383.     TempODHandleLock( ODHandle );   // locks the handle
  384.     operator ODHandle ()           { return fHandle; }
  385.  
  386.     ODHandle operator= (ODHandle h)
  387.     { fHandle = h; return h; }
  388.  
  389.     private: // disallow these:
  390.     TempODHandleLock( );
  391.     TempODHandleLock(const TempODHandleLock& );
  392.     void operator=(const TempODHandleLock& );
  393. };
  394.  
  395. //===========================================================================
  396. //    Temp platform files.
  397. //===========================================================================
  398.  
  399. class PlatformFile;
  400.  
  401. class TempPlatformFile :Destructo
  402. {
  403.     protected:
  404.     PlatformFile* fFile; 
  405.  
  406.     public:
  407.     ~TempPlatformFile();
  408.     TempPlatformFile( PlatformFile* );
  409.     PlatformFile* operator-> ()         { return fFile; }
  410.     operator PlatformFile* ()           { return fFile; }
  411.  
  412.     PlatformFile* operator= (PlatformFile* f)
  413.     { fFile = f; return f; }
  414.     
  415.     PlatformFile* DontDelete()
  416.     { PlatformFile* f = fFile; fFile = kODNULL; return f;}
  417.  
  418.     private: // disallow these:
  419.     TempPlatformFile( );
  420.     TempPlatformFile(const TempPlatformFile& );
  421.     void operator=(const TempPlatformFile& );
  422. };
  423.  
  424. //===========================================================================
  425. //    Temp ODPasteAsResult.
  426. //===========================================================================
  427.  
  428. // struct ODPasteAsResult; // from ODTypesM.xh
  429.  
  430. class TempODPasteAsResult :Destructo
  431. {
  432.     protected:
  433.     ODPasteAsResult* fResult; 
  434.  
  435.     public:
  436.     ~TempODPasteAsResult();
  437.     TempODPasteAsResult( ODPasteAsResult* );
  438.     ODPasteAsResult* operator-> ()         { return fResult; }
  439.     operator ODPasteAsResult* ()           { return fResult; }
  440.  
  441.     ODPasteAsResult* operator= (ODPasteAsResult* r)
  442.     { fResult = r; return r; }
  443.     
  444.     ODPasteAsResult* DontDelete()
  445.     { ODPasteAsResult* r = fResult; fResult = kODNULL; return r;}
  446.  
  447.     private: // disallow these:
  448.     TempODPasteAsResult( );
  449.     TempODPasteAsResult(const TempODPasteAsResult& );
  450.     void operator=(const TempODPasteAsResult& );
  451. };
  452.  
  453.  
  454. //===========================================================================
  455. //    Temp AEDescs.
  456. //===========================================================================
  457.  
  458. struct AEDesc;
  459.  
  460. class TempAEDesc :Destructo
  461. {
  462.     protected:
  463.     AEDesc* fDesc; 
  464.  
  465.     public:
  466.     ~TempAEDesc();
  467.     TempAEDesc( AEDesc* );
  468.     AEDesc* operator-> ()         { return fDesc; }
  469.     operator AEDesc* ()           { return fDesc; }
  470.  
  471.     AEDesc* operator= (AEDesc* d)
  472.     { fDesc = d; return d; }
  473.     
  474.     AEDesc* DontDelete()
  475.     { AEDesc* d = fDesc; fDesc = kODNULL; return d;}
  476.  
  477.     private: // disallow these:
  478.     TempAEDesc( );
  479.     TempAEDesc(const TempAEDesc& );
  480.     void operator=(const TempAEDesc& );
  481. };
  482.  
  483.  
  484. class TempAEDescStruct :public AEDesc, private Destructo
  485. {
  486.     public:
  487.     
  488. #ifdef _NATIVE_EXCEPTIONS_
  489.     TempAEDescStruct( )            {dataHandle = kODNULL;}
  490.     ~TempAEDescStruct( )        {AEDisposeDesc(this);}
  491. #else
  492.     TempAEDescStruct( );
  493.     ~TempAEDescStruct( );
  494. #endif
  495. };
  496.  
  497.  
  498. //===========================================================================
  499. //    Temp ODIText.
  500. //===========================================================================
  501.  
  502. class ODIText;
  503.  
  504. class TempODIText :Destructo
  505. {
  506.     protected:
  507.     ODIText* fText; 
  508.  
  509.     public:
  510.     ~TempODIText();
  511.     TempODIText( ODIText* );
  512.     ODIText* operator-> ()          { return fText; }
  513.     operator ODIText* ()            { return fText; }
  514.  
  515.     ODIText* operator= (ODIText* t)
  516.     { fText = t; return t; }
  517.     
  518.     ODIText* DontDelete()
  519.     { ODIText* t = fText; fText = kODNULL; return t;}
  520.  
  521.     private: // disallow these:
  522.     TempODIText( );
  523.     TempODIText(const TempODIText& );
  524.     void operator=(const TempODIText& );
  525. };
  526.  
  527. // typedef ODIText ODName; 
  528. // typedef ODName  ODDocumentName;
  529.  
  530. typedef TempODIText TempODName;
  531. typedef TempODIText TempODDocumentName;
  532.  
  533. //===========================================================================
  534. //    Temp byte arrays.
  535. //===========================================================================
  536.  
  537. class TempODByteArray :Destructo
  538. {
  539.     protected:
  540.     ODByteArray* fBA; 
  541.  
  542.     public:
  543.     ~TempODByteArray();
  544.     TempODByteArray( ODByteArray* );
  545.     ODByteArray* operator-> ()          { return fBA; }
  546.     operator ODByteArray* ()            { return fBA; }
  547.  
  548.     ODByteArray* operator= (ODByteArray* ba) 
  549.     { fBA = ba; return ba; }
  550.     
  551.     ODByteArray* DontDelete()           
  552.     { ODByteArray* ba = fBA; fBA = kODNULL; return ba;}
  553.  
  554.     private: // disallow these:
  555.     TempODByteArray( );
  556.     TempODByteArray(const TempODByteArray& );
  557.     void operator=(const TempODByteArray& );
  558. };
  559.  
  560. class TempODByteArrayStruct :Destructo
  561. {
  562.     protected:
  563.     ODByteArray fBA; 
  564.  
  565.     public:
  566.     ~TempODByteArrayStruct();
  567.     TempODByteArrayStruct( );
  568.     TempODByteArrayStruct( ODByteArray ba );
  569.     ODByteArray* operator-> ()          { return &fBA; }
  570.     operator ODByteArray* ()            { return &fBA; }
  571.  
  572.     ODByteArray operator= ( ODByteArray ba ) 
  573.     { fBA = ba; return ba; }
  574.     
  575.     void DontDelete()           
  576.     { fBA._buffer = kODNULL;}
  577.  
  578.     private: // disallow these:
  579.     TempODByteArrayStruct(const TempODByteArrayStruct& );
  580.     void operator=(const TempODByteArrayStruct& );
  581. };
  582.  
  583. // typedef ODByteArray ODContainerID;
  584. // typedef ODByteArray ODActionData;
  585. // typedef ODByteArray ODContour;
  586. // typedef ODByteArray ODPolygon;
  587.  
  588. typedef TempODByteArray TempODContainerID;
  589. typedef TempODByteArray TempODActionData;
  590. typedef TempODByteArray TempODContour;
  591. // typedef TempODByteArray TempODPolygon; // use ODTempPolygon instead
  592.  
  593. typedef TempODByteArrayStruct TempODContainerIDStruct;
  594. typedef TempODByteArrayStruct TempODActionDataStruct;
  595. typedef TempODByteArrayStruct TempODContourStruct;
  596. // typedef TempODByteArrayStruct TempODPolygonStruct;
  597.  
  598. #ifdef PRAGMA_INTERNAL_SUPPORTED
  599. #pragma internal reset
  600. #endif
  601.  
  602. #endif /*_TEMPOBJ_*/
  603.