home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWL1.PAK / APPDICT.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  4KB  |  155 lines

  1. // Borland C++ - (C) Copyright 1992 by Borland International
  2.  
  3. /* ---------------------------------------------------------
  4.    APPDICT.CPP
  5.    Implementation for the TAppDictionary class, a dictionary of
  6.    associations between task handles and TApplication pointers.
  7.    Used to support GetApplicationObject().
  8.    --------------------------------------------------------- */
  9.  
  10. #include "appdict.h"
  11.  
  12. #if defined(__DLL__)
  13. TAppDictionary *AppDictionary = new TAppDictionary();
  14. #else
  15. PTApplication pTApplication;
  16. #endif
  17.  
  18. PTApplication _EXPOWLFUNC GetApplicationObject()
  19. {
  20. #if defined (__DLL__)
  21.     return AppDictionary->Lookup();
  22. #else
  23.     return pTApplication;
  24. #endif
  25. }
  26.  
  27. #if defined (__DLL__)
  28. TAppDictionary::TAppDictionary(int InitialCount)
  29. {
  30.     if (InitialCount != 0)
  31.         Table = AllocTable(InitialCount);
  32.  
  33.     if (Table)
  34.         NumEntries = InitialCount;
  35.     else
  36.         NumEntries = 0;
  37. }
  38.  
  39. TAppDictionary::~TAppDictionary()
  40. {
  41.     if (NumEntries != 0 && Table)
  42.     {
  43.         FreeTable(Table);
  44.         Table = NULL;
  45.         NumEntries = 0;
  46.     }
  47. }
  48.  
  49. TAppDictionaryEntry * TAppDictionary::AllocTable(int Count)
  50. {
  51.     TAppDictionaryEntry * pTable;
  52.  
  53.     pTable = (TAppDictionaryEntry *)
  54.         LocalLock(LocalAlloc(LMEM_ZEROINIT,
  55.             Count * sizeof(TAppDictionaryEntry)));
  56.     return pTable;
  57. }
  58.  
  59. void TAppDictionary::FreeTable(TAppDictionaryEntry *pTable)
  60. {
  61.     HANDLE hMem =
  62. #ifdef STRICT    
  63.         LocalHandle((void NEAR*)FP_OFF(pTable));
  64. #else
  65.         LocalHandle(FP_OFF(pTable));
  66. #endif
  67.     if (LocalUnlock(hMem))
  68.         LocalFree(hMem);
  69. }
  70.  
  71. TAppDictionaryEntry *TAppDictionary::GrowTable(int Increment)
  72. {
  73.     int OldNumEntries = NumEntries;
  74.     TAppDictionaryEntry * OldInstanceDataTable = Table;
  75.  
  76.     NumEntries = NumEntries + Increment;
  77.  
  78.     Table = AllocTable(NumEntries);
  79.  
  80.     // copy old table to new location
  81.     _fmemcpy(Table, OldInstanceDataTable,
  82.         OldNumEntries * sizeof(TAppDictionaryEntry));
  83.  
  84.     FreeTable(OldInstanceDataTable);
  85.  
  86.     // return pointer to first entry in new block
  87.  
  88.     return &Table[OldNumEntries];
  89. }
  90.  
  91. void TAppDictionary::Add(PTApplication pApplication)
  92. {
  93.     HANDLE hTask = GetCurrentTask();
  94.     TAppDictionaryEntry *pFreeEntry = NULL;  // no free entry yet
  95.  
  96.     // First see if table already.  If so, replace entry
  97.  
  98.     for (TAppDictionaryEntry * pEntry = Table; pEntry < &Table[NumEntries];
  99.          pEntry++)
  100.     {
  101.         if (pEntry->hTask == hTask) // already in table?
  102.         {
  103.             pEntry->pApplication = pApplication;
  104.             return;
  105.         }
  106.         else                    // see if entry is free
  107.         {
  108.             if (!pFreeEntry && pEntry->hTask == 0)  // remember this entry
  109.                 pFreeEntry = pEntry;
  110.         }
  111.     }
  112.  
  113.     // Not in table.  See if we encountered a free entry in table.  If
  114.     // so, use it.  Otherwise grow table.
  115.  
  116.     if ((pEntry = (pFreeEntry ? pFreeEntry : GrowTable())) != 0)
  117.     {
  118.         pEntry->hTask = hTask;
  119.         pEntry->pApplication = pApplication;
  120.         return;
  121.     }
  122. }
  123.  
  124. PTApplication TAppDictionary::Lookup()
  125. {
  126.     HANDLE hTask = GetCurrentTask();
  127.  
  128.     for (TAppDictionaryEntry * pEntry = Table; pEntry < &Table[NumEntries];
  129.          pEntry++)
  130.     {
  131.         if (pEntry->hTask == hTask)
  132.             return pEntry->pApplication;
  133.     }
  134.     // Didn't find an entry in the table
  135.     return NULL;
  136. }
  137.  
  138. void TAppDictionary::Delete()
  139. {
  140.     HANDLE hTask = GetCurrentTask();
  141.  
  142.     for (TAppDictionaryEntry * pEntry = Table; pEntry < &Table[NumEntries];
  143.          pEntry++)
  144.     {
  145.         if (pEntry->hTask == hTask)
  146.         {
  147.             pEntry->hTask = 0;       // found entry.  Zero it.
  148.             pEntry->pApplication = 0;
  149.             return;
  150.         }
  151.     }
  152. }
  153.  
  154. #endif // __DLL__
  155.