home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / SRC / SRCHBRK.CPP < prev    next >
C/C++ Source or Header  |  1996-07-08  |  6KB  |  193 lines

  1. /****************************************************************************
  2.     $Id: srchbrk.cpp 501.0 1995/03/07 12:26:22 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:     Ron van der Wal
  8.  
  9.     Implementation of class TLSearchBreaker.
  10.  
  11.     $Log: srchbrk.cpp $
  12.     Revision 501.0  1995/03/07 12:26:22  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.6  1995/02/22 12:42:54  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.5  1995/01/10  16:33:52  ron
  18.     Changed wording of message slightly
  19.  
  20.     Revision 1.4  1995/01/06  15:58:25  ron
  21.     Corrected Revision keyword
  22.  
  23.     Revision 1.3  1995/01/05  15:28:46  ron
  24.     Naming and formatting changes
  25.  
  26.     Revision 1.2  1994/11/16  15:43:20  ron
  27.     Added module info; rearranged #include directives
  28.  
  29.     Revision 1.1  1994/10/13  11:53:06  ron
  30.     Initial revision
  31.  
  32. ****************************************************************************/
  33.  
  34. #include <tlx\501\_build.h>
  35.  
  36. TLX_MODULE_INFO("$Revision: 501.0 $");
  37.  
  38. #include <ctype.h>
  39. #include <iostream.h>
  40. #include <signal.h>
  41.  
  42. #include <tlx\501\log.h>
  43. #include <tlx\501\solve\searcher.h>
  44.  
  45. #if (defined(__BORLANDC__) && defined(__FLAT__)) || defined(__WATCOMC__)
  46. #define SIGBREAK_DEFINED        1
  47. #endif
  48.  
  49. /*---------------------------------------------------------------------------
  50.     Global and static data
  51. ---------------------------------------------------------------------------*/
  52.  
  53. bool    TLSearchBreaker::sBreak    = false;
  54. bool    TLSearchBreaker::sNotified = false;
  55. int     TLSearchBreaker::sBCount   = 0;
  56.  
  57. /*-------------------------------------------------------------------------*/
  58.     TLSearchBreaker::TLSearchBreaker()
  59.  
  60. /*  Constructor. Installs the Ctrl-Break and Ctrl-C signal handlers.
  61. ---------------------------------------------------------------------------*/
  62. {
  63.     if (++sBCount == 1)
  64.     {
  65.       #ifdef SIGBREAK_DEFINED
  66.        #ifdef __WATCOMC__
  67.         signal(SIGBREAK, (__sig_func)BreakHandler);
  68.        #else
  69.         signal(SIGBREAK, BreakHandler);
  70.        #endif 
  71.       #endif
  72.       #ifdef __IBMCPP__
  73.         signal(SIGINT, (_SigFunc)BreakHandler);
  74.       #else
  75.        #ifdef __WATCOMC__
  76.         signal(SIGINT, (__sig_func)BreakHandler);
  77.        #else
  78.         signal(SIGINT, BreakHandler);
  79.        #endif 
  80.       #endif
  81.         sBreak    = false;
  82.         sNotified = false;
  83.     }
  84. }
  85.  
  86. /*-------------------------------------------------------------------------*/
  87.     TLSearchBreaker::~TLSearchBreaker()
  88.  
  89. /*  Destructor. Deinstalls the Ctrl-Break and Ctrl-C signal handlers.
  90. ---------------------------------------------------------------------------*/
  91. {
  92.     if (--sBCount < 1)
  93.     {
  94.       #ifdef SIGBREAK_DEFINED
  95.         signal(SIGBREAK, SIG_DFL);
  96.       #endif
  97.         signal(SIGINT, SIG_DFL);
  98.     }
  99. }
  100.  
  101. /*-------------------------------------------------------------------------*/
  102.     int TLSearchBreaker::DefaultEvents() const
  103.  
  104. /*  Returns the set of search events that the monitor is interested in.
  105. ---------------------------------------------------------------------------*/
  106. {
  107.     return TLSearcher::sePreProcess | TLSearcher::seEnter;
  108. }
  109.  
  110. /*-------------------------------------------------------------------------*/
  111.     void TLSearchBreaker::OnSearchEvent(
  112.         TLSearcher *    aSearcher,
  113.         const Event &   aEvent)
  114.  
  115. /*  Call-back for events handled by this monitor. It updates the node
  116.     counter and terminates the searcher if expired.
  117. ---------------------------------------------------------------------------*/
  118. {
  119.     TLX_ASSERT_PTR(aSearcher);
  120.  
  121.     switch (aEvent.mCode)
  122.     {
  123.         case TLSearcher::sePreProcess:
  124.         {
  125.             // The preprocess event is used to reset the break indicator
  126.  
  127.             sBreak    = false;
  128.             sNotified = false;
  129.             break;
  130.         }
  131.  
  132.         case TLSearcher::seEnter:
  133.             // Whenever a node is entered, the break indicator is checked.
  134.             // If set, the searcher is terminated.
  135.  
  136.             if (sBreak && !sNotified)
  137.             {
  138.                 cerr << "\nCtrl-Break: Do you want to terminate "
  139.                         "the search process (y/n)? ";
  140.                 char rsp;
  141.                 cin >> rsp;
  142.  
  143.                 if (toupper(rsp) == 'Y')
  144.                     sNotified = true;
  145.                 else
  146.                     sBreak = false;
  147.             }
  148.             if (sBreak)
  149.             {
  150.                 TLX_LOG_ENTRY("Ctrl-Break terminates searcher");
  151.                 aSearcher->Terminate();
  152.             }
  153.             break;
  154.  
  155.         // default: all others are ignored
  156.     }
  157. }
  158.  
  159. /*-------------------------------------------------------------------------*/
  160.     ostream &TLSearchBreaker::PrintOn(ostream &os) const
  161.  
  162. /*  Function to print the contents of the monitor on the given stream,
  163.     usually as part of the searcher's ReportStats() processing.
  164. ---------------------------------------------------------------------------*/
  165. {
  166.     return os << "Breaker: flag=" << sBreak << "\n";
  167. }
  168.  
  169. /*-------------------------------------------------------------------------*/
  170.     void __cdecl TLSearchBreaker::BreakHandler(int)
  171.  
  172. /*  Signal handler for search breaker.
  173. ---------------------------------------------------------------------------*/
  174. {
  175.     sBreak = true;
  176. #ifdef SIGBREAK_DEFINED
  177.   #ifdef __WATCOMC__
  178.     signal(SIGBREAK, (__sig_func)BreakHandler);
  179.   #else
  180.     signal(SIGBREAK, BreakHandler);
  181.   #endif 
  182. #endif
  183. #ifdef __IBMCPP__
  184.     signal(SIGINT, (_SigFunc)BreakHandler);
  185. #else
  186.   #ifdef __WATCOMC__
  187.     signal(SIGINT, (__sig_func)BreakHandler);
  188.   #else
  189.     signal(SIGINT, BreakHandler);
  190.   #endif 
  191. #endif
  192. }
  193.