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

  1. /****************************************************************************
  2.     $Id: dommonit.cpp 501.0 1995/03/07 12:26:14 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 TLDomainMonitor.
  10.  
  11.     $Log: dommonit.cpp $
  12.     Revision 501.0  1995/03/07 12:26:14  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.14  1995/02/28 15:11:36  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.13  1995/01/06  15:57:46  ron
  18.     Corrected Revision keyword
  19.  
  20.     Revision 1.12  1994/11/16  15:39:14  ron
  21.     Added module info; rearranged #include directives
  22.  
  23.     Revision 1.11  1994/10/12  10:04:10  ron
  24.     Adapted to explicit TLDictionary<K,V> source code file
  25.     Adapted to changes in dictionary iterator
  26.  
  27.     Revision 1.10  1994/10/11  19:03:11  ron
  28.     Switched to a dictionary iterator instead of addressing the
  29.     keys and values directly
  30.  
  31.     Revision 1.9  1994/10/10  16:50:05  ron
  32.     Changed to <tlx\solve\csp.h>
  33.  
  34.     Revision 1.8  1994/10/07  17:02:08  ron
  35.     Chnaged Unregister...() to Deregister...()
  36.  
  37.     Revision 1.7  1994/10/05  18:37:40  ron
  38.     Added support for Watcom C++ templates
  39.  
  40.     Revision 1.6  1994/09/28  14:45:36  ron
  41.     Removed Macintosh-style #include references
  42.  
  43.     Revision 1.5  1994/09/28  14:18:03  ron
  44.     Renamed TLVarDomainMonitor to TLDomainMonitor
  45.  
  46.     Revision 1.4  1994/09/27  20:23:22  ron
  47.     Changed path separator from / to \
  48.  
  49.     Revision 1.3  1994/09/26  15:50:21  ron
  50.     Changed include file references
  51.  
  52.     Revision 1.2  1994/09/06  20:25:28  ron
  53.     Renamed RegisterDomain() to CaptureDomain()
  54.  
  55.     Revision 1.1  1994/09/06  14:12:30  ron
  56.     Initial revision
  57.  
  58. ****************************************************************************/
  59.  
  60. #include <tlx\501\_build.h>
  61.  
  62. TLX_MODULE_INFO("$Revision: 501.0 $");
  63.  
  64. //----- System headers
  65.  
  66. #include <iostream.h>
  67.  
  68. //----- Project headers
  69.  
  70. #include <tlx\501\solve\csp.h>
  71.  
  72. /*---------------------------------------------------------------------------
  73.     Template source code
  74. ---------------------------------------------------------------------------*/
  75.  
  76. #if defined (THINK_CPLUS)
  77.     #include "diction.cpp"
  78.     #include "seq.cpp"
  79.     #pragma template_access public
  80.     #pragma template TLDictionary<TLVariable *, TLDomain *>
  81.     #pragma template TLSeq<TLVariable *>
  82.     #pragma template TLSeq<TLDomain *>
  83. #elif defined(__BORLANDC__) || defined(_MSC_VER) || defined(__SC__) || defined(__WATCOMC__)
  84.     #include <tlx\501\template\diction.cpp>
  85.     #include <tlx\501\template\seq.cpp>
  86. #elif defined (__SUNPRO_CC)
  87.     #include <tlx\501\template\diction.cpp>
  88.     #include <tlx\501\template\seq.cpp>
  89. #elif defined(__IBMCPP__)
  90.   #if __IBMCPP__ < 300
  91.     #pragma implementation("tlx\\template\\diction.cpp")
  92.     #pragma implementation("tlx\\template\\seq.cpp")
  93.   #else
  94.     #include <tlx\501\template\diction.cpp>
  95.     #include <tlx\501\template\seq.cpp>
  96.   #endif
  97. #else
  98.     #error Unsupported compiler; contact Tarma Software Research
  99. #endif
  100.  
  101. /*-------------------------------------------------------------------------*/
  102.     TLDomainMonitor::TLDomainMonitor()
  103.  
  104. /*  Constructor. Initializes the instance.
  105. ---------------------------------------------------------------------------*/
  106. : mDomains(50, 100)
  107. {
  108.     mPrevMonitor = 0;
  109. }
  110.  
  111. /*-------------------------------------------------------------------------*/
  112.     TLDomainMonitor::~TLDomainMonitor()
  113.  
  114. /*  Destructor. Ensures that the monitor is unregistered with the
  115.     TLVariable class (if registered) and discards all domain copies.
  116. ---------------------------------------------------------------------------*/
  117. {
  118.     DeregisterMonitor();
  119.     DiscardDomains();
  120. }
  121.  
  122. /*-------------------------------------------------------------------------*/
  123.     void TLDomainMonitor::CaptureDomain(TLVariable *var)
  124.  
  125. /*  Called to capture the domain of a variable, presumably because it is
  126.     about to be modified. If there is currently no copy of the variable's
  127.     domain stored in the mDomains dictionary, the variable is asked to
  128.     produce a copy of its domain, which is then stored. If a domain copy
  129.     is already stored, no further action is taken.
  130. ---------------------------------------------------------------------------*/
  131. {
  132.     TLX_ASSERT_PTR(var);
  133.  
  134.     if (!mDomains.Contains(var))
  135.         mDomains.Insert(var, var->CopyDomain());
  136. }
  137.  
  138. /*-------------------------------------------------------------------------*/
  139.     void TLDomainMonitor::DiscardDomains()
  140.  
  141. /*  Deletes the domains of all variables whose domains have been captured
  142.     by this monitor.
  143. ---------------------------------------------------------------------------*/
  144. {
  145.     for (TLDictIter<TLVariable*, TLDomain*> iter(mDomains); iter.Next(); )
  146.     {
  147.     delete iter.Peek();
  148.     iter.Peek() = 0;
  149.     }
  150.     mDomains.RemoveAll();
  151. }
  152.  
  153. /*-------------------------------------------------------------------------*/
  154.     bool TLDomainMonitor::IsActive() const
  155.  
  156. /*  Returns true iff the current monitor is the active monitor, i.e. it
  157.     is the monitor to which TLVariable::sDomainMonitor points.
  158. ---------------------------------------------------------------------------*/
  159. {
  160.     return this == TLVariable::sDomainMonitor;
  161. }
  162.  
  163. /*-------------------------------------------------------------------------*/
  164.     bool TLDomainMonitor::IsCapturing() const
  165.  
  166. /*  Returns true iff the current monitor is capturing, i.e. if it is part
  167.     of the stack of domain monitors starting at TLVariable::sDomainMonitor.
  168.     Note that this does not imply that the monitor does really capture
  169.     any domains right now; use IsActiveMonitor() to check that.
  170. ---------------------------------------------------------------------------*/
  171. {
  172.     for (TLDomainMonitor *mon = TLVariable::sDomainMonitor; mon;
  173.      mon = mon->mPrevMonitor)
  174.     {
  175.     if (mon == this)
  176.         return true;
  177.     }
  178.     return false;
  179. }
  180.  
  181. /*-------------------------------------------------------------------------*/
  182.     void TLDomainMonitor::RestoreDomains()
  183.  
  184. /*  Restores the domains of all variables whose domains have been captured
  185.     by this monitor.
  186. ---------------------------------------------------------------------------*/
  187. {
  188.     for (TLDictIter<TLVariable*, TLDomain*> iter(mDomains); iter.Next(); )
  189.     {
  190.     TLX_ASSERT_PTR(iter.PeekKey());
  191.     iter.PeekKey()->RestoreDomain(iter.Peek());
  192.     }
  193. }
  194.  
  195. /*-------------------------------------------------------------------------*/
  196.     void TLDomainMonitor::StartCapturing()
  197.  
  198. /*  Starts the capturing of changes to the domains of variables by
  199.     registering the current monitor with the TLVariable class. It is
  200.     an error to call this routine if the monitor is already capturing.
  201. ---------------------------------------------------------------------------*/
  202. {
  203.     TLX_ASSERT(!IsCapturing());
  204.     mPrevMonitor = TLVariable::sDomainMonitor;    // Could be 0
  205.     TLVariable::sDomainMonitor = this;
  206.     TLX_ASSERT(IsCapturing());
  207. }
  208.  
  209. /*-------------------------------------------------------------------------*/
  210.     void TLDomainMonitor::StopCapturing()
  211.  
  212. /*  Stops the capturing of changes to the domains of variables by
  213.     unregistering the current monitor with the TLVariable class. This
  214.     routine may be called at any time while the monitor is capturing,
  215.     regardless of whether or not it is the active monitor. However, it
  216.     is an error to call this routine when the monitor is not capturing.
  217. ---------------------------------------------------------------------------*/
  218. {
  219.     TLX_ASSERT(IsCapturing());
  220.     DeregisterMonitor();
  221.     TLX_ASSERT(!IsCapturing());
  222. }
  223.  
  224. /*-------------------------------------------------------------------------*/
  225.     void TLDomainMonitor::DeregisterMonitor()
  226.  
  227. /*  Deregisters the current monitor by removing it from the monitor stack
  228.     that is formed by TLVariable::sDomainMonitor and the mPrevMonitor
  229.     fields in the monitors. It is *not* an error to call this routine
  230.     if the current monitor is not capturing.
  231. ---------------------------------------------------------------------------*/
  232. {
  233.     if (this == TLVariable::sDomainMonitor)
  234.     {
  235.     // Topmost; just replace by previous monitor
  236.  
  237.     TLVariable::sDomainMonitor = mPrevMonitor;
  238.     mPrevMonitor = 0;
  239.     }
  240.     else
  241.     {
  242.         for (TLDomainMonitor *mon = TLVariable::sDomainMonitor; mon;
  243.          mon = mon->mPrevMonitor)
  244.     {
  245.         if (mon->mPrevMonitor == this)
  246.         {
  247.         mon->mPrevMonitor = mPrevMonitor;
  248.         mPrevMonitor = 0;
  249.         break;
  250.         }
  251.     }
  252.     }
  253. }
  254.  
  255.