home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c031 / 10.ddi / MFC / SRC / WINHAND_.H$ / winhand_
Encoding:
Text File  |  1992-03-16  |  3.3 KB  |  99 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 Microsoft Corporation 
  3. // All rights reserved. 
  4. //  
  5. // This source code is only intended as a supplement to the 
  6. // Microsoft Foundation Classes Reference and Microsoft 
  7. // QuickHelp documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11. /////////////////////////////////////////////////////////////////////////////
  12. // INTERNAL INTERNAL INTERNAL 
  13. // must be included after afxwin.h
  14. // DOS I/O functions
  15.  
  16. #ifndef __HANDLE_H__
  17. #define __HANDLE_H__
  18.  
  19. #ifndef __AFXCOLL_H__
  20. #include "afxcoll.h"
  21. #endif
  22.  
  23.  
  24. #undef THIS_FILE
  25. #define THIS_FILE "winhand_.h"
  26.  
  27. /* Included in this file:
  28.  *  Most Windows objects are represented with a HANDLE, including
  29.  *      the most important ones, HWND, HDC, HPEN, HFONT etc.
  30.  *  We want C++ objects to wrap these handle based objects whenever we can.
  31.  *  Since Windows objects can be created outside of C++ (eg: calling
  32.  *      ::CreateWindow will return an HWND with no C++ wrapper) we must
  33.  *      support a reasonably uniform mapping from permanent handles
  34.  *      (i.e. the ones allocated in C++) and temporary handles (i.e.
  35.  *      the ones allocated in C, but passed through a C++ interface.
  36.  *  We keep two dictionaries for this purpose.  The permanent dictionary
  37.  *      stores those C++ objects that have been explicitly created by
  38.  *      the developer.  The C++ constructor for the wrapper class will
  39.  *      insert the mapping into the permanent dictionary and the C++
  40.  *      destructor will remove it and possibly free up the associated
  41.  *      Windows object (true for everything except CDevice's).
  42.  *  When a handle passes through a C++ interface that doesn't exist in
  43.  *      the permanent dictionary, we allocate a temporary wrapping object
  44.  *      and store that mapping into the temporary dictionary.
  45.  *  At idle time the temporary wrapping objects are flushed (since you better
  46.  *      not be holding onto something you didn't create).
  47.  */
  48.  
  49. // forward class declarations
  50. class NEAR CHandleMap
  51. {
  52. public:
  53.     CMapWordToPtr permanentMap;
  54.     CMapWordToPtr temporaryMap;
  55.  
  56. // Callbacks
  57.     virtual CObject* NewTempObject(HANDLE h) = 0;
  58.     virtual void DeleteTempObject(CObject *) = 0;
  59.  
  60. // Operations
  61.     CObject* FromHandle(HANDLE h);
  62.     void     DeleteTemp();
  63.  
  64.     BOOL    LookupPermanent(HANDLE h, void*& pv)
  65.                 { return permanentMap.Lookup((WORD)h, pv); }
  66.     BOOL    LookupTemporary(HANDLE h, void*& pv)
  67.                 { return temporaryMap.Lookup((WORD)h, pv); }
  68.  
  69.     CObject* GetPermanent(HANDLE h)
  70.                 {
  71.                     void* ob;
  72.                     VERIFY(LookupPermanent(h, ob));
  73.                     return (CObject*) ob;
  74.                 }
  75.  
  76.     void    SetPermanent(HANDLE h, CObject* permOb)
  77.                 {
  78. #ifdef _DEBUG
  79.                     void* pv;
  80.                     ASSERT(!LookupPermanent(h, pv)); // must not be in there
  81.                     BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  82. #endif
  83.                     permanentMap[(WORD)h] = permOb;
  84. #ifdef _DEBUG
  85.                     AfxEnableMemoryTracking(bEnable);
  86. #endif
  87.                 }
  88.  
  89.     void    RemovePermanent(HANDLE h)       // remove, don't assert if not there
  90.                 { permanentMap.RemoveKey((WORD)h); }
  91.     void    RemoveTemporary(HANDLE h)       // remove, don't assert if not there
  92.                 { temporaryMap.RemoveKey((WORD)h); }
  93. };
  94.  
  95. #undef THIS_FILE
  96. #define THIS_FILE __FILE__
  97.  
  98. #endif //__HANDLE_H__
  99.