1 // Copyright (c) 2016  John Abbott
2 // This file is part of the CoCoALib suite of examples.
3 // You are free to use any part of this example in your own programs.
4 
5 #include "CoCoA/library.H"
6 
7 #include <csignal>
8 using namespace std;
9 
10 //----------------------------------------------------------------------
11 const string ShortDescription =
12   "This short example shows an easy way of making CoCoALib programs  \n"
13   "handle signals (or interrupts), by converting them to exceptions. \n";
14 
15 const string LongDescription =
16   "This short example shows an easy way of making CoCoALib programs  \n"
17   "handle signals (or interrupts), by converting them to exceptions. \n"
18   "There are two crucial parts: create a SignalWatcher to say which  \n"
19   "signals to watch for, and call CheckForInterrupt when it is       \n"
20   "convenient to act upon the interrupting signal.                   \n";
21 
22 //----------------------------------------------------------------------
23 
24 namespace CoCoA
25 {
26 
27   //////////////////////////////////////////////////////////////////
28   // A long computation which checks occasionally for interrupts.
29   // Relevant details: main loop calls CheckForInterrupt frequently.
LongComputation()30   void LongComputation()
31   {
32     BigRat prod(1,1);
33     const int MaxPrime = 700000; // so it takes about 4s on my computer
34     for (int p=2; p < MaxPrime; p = NextPrime(p))
35     {
36       CheckForInterrupt("LongComputation"); // arg gives context info
37       prod *= BigRat(p-1,p);
38     }
39     cout << "Product of (1-1/p) for primes p up to " << MaxPrime
40          << " is about " << FloatStr(prod) << endl;
41   }
42 
43 
program()44   void program()
45   {
46     cout << ShortDescription << endl;
47     GlobalManager CoCoAFoundations;
48 
49     SignalWatcher MonitorSIGINT(SIGINT); // RAII object
50     // Now do a long computation which checks for interrupts...
51     // Call it inside a try..catch block so any InterruptedBySignal
52     // exception can be handled appropriately.
53     try
54     {
55       LongComputation();
56     }
57     catch (const InterruptedBySignal& intr)
58     {
59       // LongComputation was interrupted by a signal.
60       // Here we "handle" it by just printing out a message.
61       PrintInFrame(cout, intr);
62     }
63 
64   }
65 
66 } // end of namespace CoCoA
67 
68 //----------------------------------------------------------------------
69 // Use main() to handle any uncaught exceptions and warn the user about them.
main()70 int main()
71 {
72   try
73   {
74     CoCoA::program();
75     return 0;
76   }
77   catch (const CoCoA::ErrorInfo& err)
78   {
79     cerr << "***ERROR***  UNCAUGHT CoCoA error";
80     ANNOUNCE(cerr, err);
81   }
82   catch (const std::exception& exc)
83   {
84     cerr << "***ERROR***  UNCAUGHT std::exception: " << exc.what() << endl;
85   }
86   catch(...)
87   {
88     cerr << "***ERROR***  UNCAUGHT UNKNOWN EXCEPTION" << endl;
89   }
90 
91   CoCoA::BuildInfo::PrintAll(cerr);
92   return 1;
93 }
94 
95 //----------------------------------------------------------------------
96 // RCS header/log in the next few lines
97 // $Header: /Volumes/Home_1/cocoa/cvs-repository/CoCoALib-0.99/examples/ex-interrupt1.C,v 1.8 2017/08/08 13:48:08 abbott Exp $
98 // $Log: ex-interrupt1.C,v $
99 // Revision 1.8  2017/08/08 13:48:08  abbott
100 // Summary: Improved comment, and var name
101 //
102 // Revision 1.7  2017/07/22 12:52:40  abbott
103 // Summary: Updated exception types
104 //
105 // Revision 1.6  2017/07/08 19:07:48  abbott
106 // Summary: updated example for interrupt; added new example too.
107 //
108 // Revision 1.5  2016/11/18 18:06:06  abbott
109 // Summary: Made simpler by using new CoCoALib built-in "signal monitor"
110 //
111 // Revision 1.4  2015/11/04 10:12:41  abbott
112 // Summary: Made it faster
113 //
114 // Revision 1.3  2015/06/26 14:55:20  abbott
115 // Summary: Improved readability
116 // Author: JAA
117 //
118 // Revision 1.2  2015/06/25 14:49:51  abbott
119 // Summary: Added string arg to CheckForInterrupt
120 // Author: JAA
121 //
122 // Revision 1.1  2015/05/20 16:52:43  abbott
123 // Summary: New example illustrating checking and handling interrupts
124 // Author: JAA
125 //
126 //
127