home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sompar.zip / SOMACT.CPP < prev    next >
C/C++ Source or Header  |  1994-01-10  |  12KB  |  429 lines

  1. //
  2. // Project: Digitalk Parts like Toolbar and SOMObjects Demo
  3. // File:    Somact.cpp
  4. // Author:  Stewart Hyde
  5. // Created: Dec   19, 1993
  6. // Updated: Jan   10, 1993
  7. //
  8. // Description:
  9. //
  10.  
  11. #include <stdio.h>
  12. #include "somact.hpp"
  13.  
  14. #define checkEv(ev) ((ev)->_major != NO_EXCEPTION)
  15.  
  16. // ------------------------------------------------------------------------
  17. //  Class:                SomAction
  18. //  Function:            SomAction  (Constructor)
  19. //                
  20. //  Description:        This is the contructor for SomAction
  21. //       
  22. //  Input:                N/A
  23. //
  24. //  Output:                N/A
  25. //
  26. //  Notes:
  27. //
  28. // ------------------------------------------------------------------------
  29.  
  30. SomAction::SomAction()
  31. {
  32.     UpdateListCreated = false;
  33.     Available = false;
  34.  
  35.     // --------------------------------------------------------------------
  36.     //  Initialize Local and DSOM enviroments
  37.     // --------------------------------------------------------------------
  38.  
  39.       ev = SOM_CreateLocalEnvironment();
  40.      SOMD_Init(ev);    
  41.  
  42.     // --------------------------------------------------------------------
  43.     //  Register Part Class with SOMClassMgr
  44.     // --------------------------------------------------------------------
  45.  
  46.     PartNewClass(0,0);    
  47.     if (checkEv(ev))
  48.     {
  49.         // -----------------------------------------------------------------
  50.         //  Cleanup Local and DSOM enviroments
  51.         // -----------------------------------------------------------------
  52.  
  53.         SOMD_Uninit(ev);
  54.         SOM_DestroyLocalEnvironment(ev);
  55.         return;
  56.     }    
  57.  
  58.     // --------------------------------------------------------------------
  59.     //  Allocate a Part class
  60.     // --------------------------------------------------------------------
  61.  
  62.     part = (Part *) SOMD_ObjectMgr->somdNewObject(ev, "Part", NULL);
  63.     if (checkEv(ev))
  64.     {
  65.         // -----------------------------------------------------------------
  66.         //  Cleanup Local and DSOM enviroments
  67.         // -----------------------------------------------------------------
  68.  
  69.         SOMD_Uninit(ev);
  70.         SOM_DestroyLocalEnvironment(ev);
  71.     }    
  72.     else
  73.         Available = true;
  74. }
  75.  
  76.  
  77. // ------------------------------------------------------------------------
  78. //  Class:                SomAction
  79. //  Function:            ~SomAction  (destructor)
  80. //                
  81. //  Description:        This is the destructor for SomAction
  82. //       
  83. //  Input:                N/A
  84. //
  85. //  Output:                N/A
  86. //
  87. //  Notes:
  88. //
  89. // ------------------------------------------------------------------------
  90.  
  91. SomAction::~SomAction()
  92. {
  93.     // ---------------------------------------------------------------------
  94.     //  If update list is active, destroy it
  95.     // ---------------------------------------------------------------------
  96.     
  97.     if (UpdateListCreated)
  98.     {
  99.         Stack.removeAll();
  100.     }
  101.  
  102.     // ---------------------------------------------------------------------
  103.     //  If SOM is availabe than free up SOM
  104.     // ---------------------------------------------------------------------
  105.     
  106.     if (Available)
  107.     {
  108.         // -----------------------------------------------------------------
  109.         //  Cleanup Local and DSOM enviroments
  110.         // -----------------------------------------------------------------
  111.  
  112.         SOMD_Uninit(ev);
  113.         SOM_DestroyLocalEnvironment(ev);
  114.     }
  115. }
  116.  
  117. // ------------------------------------------------------------------------
  118. //  Class:                SomAction
  119. //  Function:            Process
  120. //                
  121. //  Description:        Process a SomAction, add a new one if not present
  122. //                            otherwise update the current one
  123. //       
  124. //  Input:                ULONG ulSet - set #
  125. //                      ULONG ulAction - function #
  126. //
  127. //  Output:                N/A
  128. //
  129. //  Notes:
  130. //
  131. // ------------------------------------------------------------------------
  132.  
  133. void SomAction::Process(ULONG ulSet, ULONG ulAction)
  134. {
  135.         if (!Add(ulSet, ulAction))
  136.             Update(ulSet, ulAction);
  137. }
  138.  
  139. // ------------------------------------------------------------------------
  140. //  Class:                SomAction
  141. //  Function:            Next
  142. //                
  143. //  Description:        Find next updated or added SomAction
  144. //       
  145. //  Input:                N/A
  146. //
  147. //  Output:                TRUE if SomAction is available
  148. //
  149. //  Notes:
  150. //
  151. // ------------------------------------------------------------------------
  152.  
  153. Boolean SomAction::Next()
  154. {
  155.     ULONG ulSet, ulAction;
  156.  
  157.     // ---------------------------------------------------------------------
  158.     //  If SOM is not available return false
  159.     // ---------------------------------------------------------------------
  160.  
  161.     if (!Available)
  162.         return false;
  163.  
  164.     // ---------------------------------------------------------------------
  165.     //  Get Next Item on the list if available
  166.     // ---------------------------------------------------------------------
  167.  
  168.     if (!NextUpdateList(&ulSet,&ulAction))
  169.         return false;
  170.  
  171.     // ---------------------------------------------------------------------
  172.     //  Search for Set and Action found above and return result
  173.     // ---------------------------------------------------------------------
  174.  
  175.     return(Search(ulSet,ulAction));
  176. }
  177.  
  178. // ------------------------------------------------------------------------
  179. //  Class:                SomAction
  180. //  Function:            Search
  181. //                
  182. //  Description:        Search for a SomAction with out update
  183. //       
  184. //  Input:                ULONG ulSet - set #
  185. //                      ULONG ulAction - function #
  186. //
  187. //  Output:                TRUE if SomAction is found
  188. //
  189. //  Notes:
  190. //
  191. // ------------------------------------------------------------------------
  192.  
  193. Boolean SomAction::Search(ULONG ulSet, ULONG ulAction)
  194. {
  195.     long nIndex, nCount;
  196.  
  197.     // ---------------------------------------------------------------------
  198.     //  If SOM is not available return false
  199.     // ---------------------------------------------------------------------
  200.  
  201.     if (!Available)
  202.         return false;
  203.  
  204.     // ---------------------------------------------------------------------
  205.     //  Search for a Part SOM object using the Search method
  206.     // ---------------------------------------------------------------------
  207.  
  208.     part->Search(ev, ulSet, ulAction, &nIndex, &nCount);
  209.     if (checkEv(ev))
  210.         return false;
  211.     else
  212.     {
  213.         Set = ulSet;
  214.         Action = ulAction;
  215.         Index = (ULONG) nIndex;
  216.         Count = (ULONG) nCount;
  217.         return true;
  218.     }
  219. }    
  220.  
  221. // ------------------------------------------------------------------------
  222. //  Class:                SomAction
  223. //  Function:            Add
  224. //                
  225. //  Description:        Add a new SomAction
  226. //       
  227. //  Input:                ULONG ulSet - set #
  228. //                      ULONG ulAction - function #
  229. //
  230. //  Output:                 TRUE if successful
  231. //
  232. //  Notes:                This addes a new action and initializes count to 1
  233. //
  234. // ------------------------------------------------------------------------
  235.  
  236. Boolean SomAction::Add(ULONG ulSet, ULONG ulAction)
  237. {
  238.     long nIndex, nCount;
  239.     boolean returnVal;
  240.  
  241.     // ---------------------------------------------------------------------
  242.     //  If SOM is not available return false
  243.     // ---------------------------------------------------------------------
  244.  
  245.     if (!Available)
  246.         return false;
  247.  
  248.     // ---------------------------------------------------------------------
  249.     //  Add a Part SOM object using the Add method
  250.     // ---------------------------------------------------------------------
  251.  
  252.     returnVal = part->Add(ev, ulSet, ulAction, &nIndex, &nCount);
  253.     if ((checkEv(ev)) || (returnVal == FALSE))
  254.         return false;
  255.     else
  256.     {
  257.         Set = ulSet;
  258.         Action = ulAction;
  259.         UpdateFlag = false;
  260.         AddToUpdateList(ulSet,ulAction);
  261.         Index = (ULONG) nIndex;
  262.         Count = (ULONG) nCount;
  263.         return true;
  264.     }
  265. }
  266.  
  267. // ------------------------------------------------------------------------
  268. //  Class:                SomAction
  269. //  Function:            Update    
  270. //                
  271. //  Description:       Update a SomAction 
  272. //       
  273. //  Input:                ULONG ulSet - set #
  274. //                      ULONG ulAction - function #
  275. //
  276. //  Output:                TRUE if successful
  277. //
  278. //  Notes:                This basically increase the SomAction Count
  279. //
  280. // ------------------------------------------------------------------------
  281.  
  282. Boolean SomAction::Update(ULONG ulSet, ULONG ulAction)
  283. {
  284.     long nIndex, nCount;
  285.  
  286.     // ---------------------------------------------------------------------
  287.     //  If SOM is not available return false
  288.     // ---------------------------------------------------------------------
  289.  
  290.     if (!Available)
  291.         return false;
  292.  
  293.     // ---------------------------------------------------------------------
  294.     //  Update a Part SOM object using the Update method
  295.     // ---------------------------------------------------------------------
  296.  
  297.     part->Update(ev, ulSet, ulAction, &nIndex, &nCount);
  298.     if (checkEv(ev))
  299.         return false;
  300.     else
  301.     {
  302.         Set = ulSet;
  303.         Action = ulAction;
  304.         UpdateFlag = true;
  305.         AddToUpdateList(ulSet,ulAction);
  306.         Index = (ULONG) nIndex;
  307.         Count = (ULONG) nCount;
  308.         return true;
  309.     }
  310. }
  311.  
  312.  
  313. // ------------------------------------------------------------------------
  314. //  Class:                SomAction
  315. //  Function:            NextUpdateList
  316. //                
  317. //  Description:       get next update list item
  318. //       
  319. //  Input:                ULONG *pulSet - set #
  320. //                      ULONG *pulAction - function #
  321. //                            Boolean pUpdate - true if this just an update
  322. //
  323. //  Output:                TRUE if successful
  324. //
  325. //  Notes:                Retrived next item on update list
  326. //
  327. // ------------------------------------------------------------------------
  328.  
  329. Boolean SomAction::NextUpdateList(ULONG *pulSet,
  330.                                             ULONG *pulAction)
  331. {
  332.     SetAction setAction(0,0,false);
  333.  
  334.     // ---------------------------------------------------------------------
  335.     //  If stack is empty, return false
  336.     // ---------------------------------------------------------------------
  337.     
  338.     if (Stack.isEmpty())
  339.         return false;
  340.  
  341.     // ---------------------------------------------------------------------
  342.     //  Poo Set and Action off of stack
  343.     // ---------------------------------------------------------------------
  344.  
  345.     Stack.pop(setAction);
  346.     (*pulSet) = setAction.getSet();
  347.     (*pulAction) = setAction.getAction();
  348.     UpdateFlag = setAction.getUpdate();    
  349.     return true;
  350. }
  351.  
  352. // ------------------------------------------------------------------------
  353. //  Class:                SomAction
  354. //  Function:            AddToUpdateList
  355. //                
  356. //  Description:       Add item to update list
  357. //       
  358. //  Input:                ULONG ulSet - set #
  359. //                      ULONG ulAction - function #
  360. //
  361. //  Output:                TRUE if successful
  362. //
  363. //  Notes:                Retrived next item on update list
  364. //
  365. // ------------------------------------------------------------------------
  366.  
  367. Boolean SomAction::AddToUpdateList(ULONG ulSet,
  368.                                               ULONG ulAction)
  369. {
  370.     SetAction  setAction(ulSet,ulAction,UpdateFlag);
  371.  
  372.     // ---------------------------------------------------------------------
  373.     //  If Update List is not created than create one
  374.     // ---------------------------------------------------------------------
  375.     
  376.     if (!UpdateListCreated)
  377.     {
  378.         // ------------------------------------------------------------------
  379.         //  We only need to create it once
  380.         // ------------------------------------------------------------------
  381.         
  382.         Stack.removeAll();
  383.         UpdateListCreated = true;
  384.     }
  385.  
  386.     // ---------------------------------------------------------------------
  387.     //  Push Set and Action onto stack
  388.     // ---------------------------------------------------------------------
  389.  
  390.     Stack.push(setAction);
  391.     return true;
  392. }
  393.  
  394.  
  395.  
  396. // ------------------------------------------------------------------------
  397. //  Class:                SomAction
  398. //  Function:            DebugMessage
  399. //                
  400. //  Description:       Enable or Disable Debug Messages
  401. //       
  402. //  Input:                Boolean Status - enable or disable status
  403. //
  404. //  Output:                N/A
  405. //
  406. //  Notes:                
  407. //
  408. // ------------------------------------------------------------------------
  409.  
  410. void SomAction::DebugMessage(Boolean Status)
  411. {
  412.     // ---------------------------------------------------------------------
  413.     //  If DSOM is not available return
  414.     // ---------------------------------------------------------------------
  415.  
  416.     if (!Available)
  417.         return;
  418.  
  419.     // ---------------------------------------------------------------------
  420.     //  Enable or Disable Debug Message by calling DSOM object
  421.     // ---------------------------------------------------------------------
  422.     
  423.     if (Status)
  424.         part->Debug(ev, TRUE);
  425.     else
  426.         part->Debug(ev, FALSE);
  427.         
  428. }
  429.