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 / UI / DispTabl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  9.8 KB  |  352 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        DispTabl.cpp
  3.  
  4.     Contains:    Implementation of class DispatchTable, which is private
  5.                 to the implementation of ODDispatcher
  6.  
  7.     Owned by:    Richard Rodseth
  8.  
  9.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.  
  13.          <4>     5/26/95    RR        #1251403: Multithreading naming support
  14.          <3>     5/10/95    RR        # 1245459 Don't delete fDispatchModule in
  15.                                                                         ~DispatchInfo
  16.          <2>      5/2/95    RR        # 1245459 Delete fDispatchModule in
  17.                                     ~DispatchInfo
  18.          <1>     5/13/94    RR        first checked in
  19.          <1>     5/13/94    RR        first checked in
  20.         <13>      2/7/94    NP        Tiger Team doings.
  21.         <12>     12/3/93    TÇ        Stop including ODError.h, it is included
  22.                                     as ErrorDef.h inside Except.h
  23.         <11>     12/2/93    RR        Use new eventType definition
  24.         <10>     9/24/93    RR        #pragma segment
  25.          <9>     8/20/93    RR        Fixed array indexing bug
  26.          <8>     8/19/93    RR        Remove redundant workaround in destructor
  27.          <7>     8/19/93    RR        Work around apparent CFront problems with
  28.                                     delete[] in ~DispatchTable
  29.          <6>     8/19/93    NP        Work around CFront's apparent problem with
  30.                                     delete 0
  31.          <5>     8/18/93    RR        Added GetDispatchInfo/UpdateDispatchInfo
  32.          <4>     8/18/93    RR        Use DictionaryList class
  33.          <2>     8/10/93    RCR        Use OrderedCollection instead of linked list, for monitors
  34.          <1>     8/10/93    RCR        first checked in
  35.  
  36.     To Do:
  37. */
  38.  
  39. #ifndef _DISPTABL_
  40. #include "DispTabl.h"
  41. #endif
  42.  
  43. #ifndef _DISPMOD_
  44. #include "DispMod.xh"
  45. #endif
  46.  
  47. #ifndef _ORDCOLL_
  48. #include "OrdColl.h"
  49. #endif
  50.  
  51. #ifndef _DICTLIST_
  52. #include "DictList.h"
  53. #endif
  54.  
  55. #ifndef _EXCEPT_
  56. #include "Except.h"
  57. #endif
  58.  
  59. #pragma segment ODDispatcher
  60.  
  61. //=====================================================================================
  62. // Private Class: DispatchInfo
  63. //=====================================================================================
  64.  
  65. class DispatchInfo 
  66. {
  67. public:
  68.     
  69.     DispatchInfo();
  70.     
  71.         // Constructor
  72.         
  73.     ~DispatchInfo();
  74.     
  75.         // Destructor
  76.         
  77.     void SetModule(ODDispatchModule* module);
  78.     ODDispatchModule* GetModule();
  79.     void AddMonitor(ODDispatchModule* monitor);
  80.     void RemoveMonitor(ODDispatchModule* monitor);
  81.     OrderedCollection* GetMonitors();
  82.  
  83. protected:
  84.     ODDispatchModule* fDispatchModule;
  85.     OrderedCollection* fMonitors;
  86. };
  87.  
  88. //-------------------------------------------------------------------------------------
  89. // DispatchInfo::DispatchInfo
  90. //
  91. // Description
  92. //-------------------------------------------------------------------------------------
  93.  
  94. DispatchInfo::DispatchInfo()
  95. {
  96.     fDispatchModule = kODNULL;
  97.     fMonitors = kODNULL;
  98. }
  99.  
  100. //-------------------------------------------------------------------------------------
  101. // DispatchInfo::~DispatchInfo
  102. //
  103. // Description
  104. //-------------------------------------------------------------------------------------
  105.  
  106. DispatchInfo::~DispatchInfo()
  107. {
  108.     delete fMonitors;
  109.     // Don't delete fDispatchModule, because many entries in the table
  110.     // point to the same module
  111. }
  112.  
  113. //-------------------------------------------------------------------------------------
  114. // DispatchInfo::SetModule
  115. //
  116. // Description
  117. //-------------------------------------------------------------------------------------
  118.  
  119. void DispatchInfo::SetModule(ODDispatchModule* module)
  120. {
  121.     fDispatchModule = module;
  122. }
  123.  
  124. //-------------------------------------------------------------------------------------
  125. // DispatchInfo::GetModule
  126. //
  127. // Description
  128. //-------------------------------------------------------------------------------------
  129.  
  130. ODDispatchModule* DispatchInfo::GetModule()
  131. {
  132.     return fDispatchModule;
  133. }
  134.  
  135. //-------------------------------------------------------------------------------------
  136. // DispatchInfo::AddMonitor
  137. //
  138. // Exceptions thrown:
  139. //         kODErrOutOfMemory
  140. //-------------------------------------------------------------------------------------
  141.  
  142. void DispatchInfo::AddMonitor(ODDispatchModule* monitor)
  143. {
  144.     if (monitor)
  145.     {
  146.         if (!fMonitors)
  147.             fMonitors = new OrderedCollection;
  148.         THROW_IF_NULL(fMonitors);
  149.         
  150.         if (!(fMonitors->Contains((ElementType) monitor)))
  151.             fMonitors->AddLast((ElementType) monitor);
  152.     }
  153. }
  154.     
  155. //-------------------------------------------------------------------------------------
  156. // DispatchInfo::RemoveMonitor
  157. //
  158. // Description
  159. //-------------------------------------------------------------------------------------
  160.  
  161. void DispatchInfo::RemoveMonitor(ODDispatchModule* monitor)
  162. {
  163.     if (fMonitors)
  164.         fMonitors->Remove((ElementType) monitor);
  165. }
  166.  
  167. //-------------------------------------------------------------------------------------
  168. // DispatchInfo::GetMonitors
  169. //
  170. // Description
  171. //-------------------------------------------------------------------------------------
  172.  
  173. OrderedCollection* DispatchInfo::GetMonitors()
  174. {
  175.     return fMonitors;
  176. }
  177.  
  178. //=====================================================================================
  179. // Class DispatchTable
  180. //=====================================================================================
  181.  
  182. //-------------------------------------------------------------------------------------
  183. // DispatchTable::DispatchTable
  184. //
  185. // Constructor. Initializes the array entries to NULL.
  186. //-------------------------------------------------------------------------------------
  187.  
  188. DispatchTable::DispatchTable()
  189. {
  190.     for (short i = 0; i <= kODLastEvent; i++)
  191.         fDispatchInfo[i] = kODNULL;
  192.     fOverflowDispatchInfo = kODNULL;
  193. }
  194.  
  195. //-------------------------------------------------------------------------------------
  196. // DispatchTable::~DispatchTable
  197. //
  198. // Description
  199. //-------------------------------------------------------------------------------------
  200.  
  201. DispatchTable::~DispatchTable()
  202. {
  203.     for (short i = 0; i <= kODLastEvent; i++)    
  204.         delete fDispatchInfo[i];
  205.     //delete[] fDispatchInfo;    // CFront has a problem with this?
  206.     if (fOverflowDispatchInfo)
  207.         fOverflowDispatchInfo->DeleteValues();
  208.     delete fOverflowDispatchInfo;
  209. }
  210.  
  211. //-------------------------------------------------------------------------------------
  212. // DispatchTable::AddMonitor
  213. //
  214. // Description
  215. //-------------------------------------------------------------------------------------
  216.  
  217. void DispatchTable::AddMonitor(ODEventType eventType, 
  218.                                ODDispatchModule* dispatchModule)
  219. {
  220.     // Should check if dispatchModule is already installed as a regular module?
  221.     
  222.     DispatchInfo* info = this->GetDispatchInfo(eventType);
  223.     if (info == kODNULL)
  224.         info = new DispatchInfo;
  225.     THROW_IF_NULL(info);
  226.     info->AddMonitor(dispatchModule);
  227. }
  228.  
  229. //-------------------------------------------------------------------------------------
  230. // DispatchTable::RemoveMonitor
  231. //
  232. // Description
  233. //-------------------------------------------------------------------------------------
  234.  
  235. void DispatchTable::RemoveMonitor(ODEventType eventType, ODDispatchModule* dispatchModule)
  236. {
  237.     DispatchInfo* info = this->GetDispatchInfo(eventType);
  238.     if (info)
  239.         info->RemoveMonitor(dispatchModule);
  240. }
  241.  
  242. //-------------------------------------------------------------------------------------
  243. // DispatchTable::AddDispatchModule
  244. //
  245. // Description
  246. //-------------------------------------------------------------------------------------
  247.  
  248. void DispatchTable::AddDispatchModule(ODEventType eventType, 
  249.                                ODDispatchModule* dispatchModule)
  250. {
  251.     // To Do: THROW exception if already installed
  252.  
  253.     DispatchInfo* info = this->GetDispatchInfo(eventType);
  254.     
  255.     if (info == kODNULL)
  256.         info = new DispatchInfo;
  257.     THROW_IF_NULL(info);
  258.     info->SetModule(dispatchModule);
  259.     
  260.     this->UpdateDispatchInfo(eventType,info);            
  261. }
  262.  
  263. //-------------------------------------------------------------------------------------
  264. // DispatchTable::RemoveDispatchModule
  265. //
  266. // Description
  267. //-------------------------------------------------------------------------------------
  268.  
  269. void DispatchTable::RemoveDispatchModule(ODEventType eventType)
  270. {
  271.     DispatchInfo* info = this->GetDispatchInfo(eventType);
  272.     if (info)
  273.         info->SetModule(kODNULL);
  274. }
  275.  
  276. //-------------------------------------------------------------------------------------
  277. // DispatchTable::GetDispatchInfo
  278. //
  279. // Description
  280. //-------------------------------------------------------------------------------------
  281.  
  282. DispatchInfo* DispatchTable::GetDispatchInfo(ODEventType eventType)
  283. {
  284.     DispatchInfo* info = kODNULL;
  285.  
  286.     if (eventType <= kODLastEvent)
  287.         info = fDispatchInfo[eventType];
  288.     else if (fOverflowDispatchInfo)
  289.         info = (DispatchInfo*) fOverflowDispatchInfo->ValueAtKey((KeyType) eventType);
  290.     return info;
  291. }
  292.  
  293. //-------------------------------------------------------------------------------------
  294. // DispatchTable::UpdateDispatchInfo
  295. //
  296. // Description
  297. //-------------------------------------------------------------------------------------
  298.     
  299. void DispatchTable::UpdateDispatchInfo(ODEventType eventType, DispatchInfo* info)
  300. {
  301.     if (eventType <= kODLastEvent)
  302.         fDispatchInfo[eventType] = info;
  303.     else
  304.     {
  305.         if (!fOverflowDispatchInfo)
  306.             fOverflowDispatchInfo = new DictionaryList;
  307.         fOverflowDispatchInfo->AddPair((KeyType) eventType, (ValueType) info);
  308.     }
  309. }
  310.  
  311. //-------------------------------------------------------------------------------------
  312. // DispatchTable::GetDispatchModule
  313. //
  314. // Description
  315. //-------------------------------------------------------------------------------------
  316.  
  317. ODDispatchModule* DispatchTable::GetDispatchModule(ODEventType eventType)
  318. {
  319.     ODDispatchModule* module = kODNULL;
  320.     DispatchInfo* info = kODNULL;
  321.     
  322.     if (eventType <= kODLastEvent)
  323.         info = fDispatchInfo[eventType];
  324.     else if (fOverflowDispatchInfo)
  325.         info = (DispatchInfo*) fOverflowDispatchInfo->ValueAtKey((KeyType) eventType);
  326.     if (info)
  327.         module = info->GetModule();
  328.     return module;
  329. }
  330.  
  331. //-------------------------------------------------------------------------------------
  332. // DispatchTable::GetMonitors
  333. //
  334. // Description
  335. //-------------------------------------------------------------------------------------
  336.  
  337. OrderedCollection* DispatchTable::GetMonitors(ODEventType eventType)
  338. {
  339.     OrderedCollection* monitors = kODNULL;
  340.     DispatchInfo* info = kODNULL;
  341.     
  342.     if (eventType <= kODLastEvent)
  343.         info = fDispatchInfo[eventType];
  344.     else if (fOverflowDispatchInfo)
  345.         info = (DispatchInfo*) fOverflowDispatchInfo->ValueAtKey((KeyType) eventType);
  346.     if (info)
  347.         monitors = info->GetMonitors();
  348.     return monitors;
  349. }
  350.  
  351.  
  352.