f27a05571b4aafc5fff49e37b4c086696cc99a01
[oota-llvm.git] / include / Support / SlowOperationInformer.h
1 //===- SlowOperationInformer.h - Keep the user informed ---------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a simple object which can be used to let the user know what
11 // is going on when a slow operation is happening,l and gives them the ability
12 // to cancel it.  Potentially slow operations can stack allocate one of these
13 // objects, and periodically call the "progress" method to update the progress
14 // bar.  If the operation takes more than 3 seconds to complete, the progress
15 // bar is automatically shown and updated every second.  As such, the slow
16 // operation should not print stuff to the screen, and should not be confused if
17 // an extra line appears on the screen (ie, the cursor should be at the start of
18 // the line).
19 //
20 // If the user presses CTRL-C during the operation, the next invocation of the
21 // progress method with throw an std::string object indicating that the
22 // operation was cancelled.  As such, client code must be exception safe around
23 // the progress method.
24 //
25 // Because SlowOperationInformers fiddle around with signals, they cannot be
26 // nested.  The SIGINT signal handler is restored after the
27 // SlowOperationInformer is destroyed, but the SIGALRM handlers is set back to
28 // the default.
29 //
30 //===----------------------------------------------------------------------===//
31
32 #ifndef SUPPORT_SLOW_OPERATION_INFORMER_H
33 #define SUPPORT_SLOW_OPERATION_INFORMER_H
34
35 #include <string>
36
37 namespace llvm {
38   class SlowOperationInformer {
39     std::string OperationName;
40     unsigned LastPrintAmount;
41     
42     SlowOperationInformer(const SlowOperationInformer&);   // DO NOT IMPLEMENT
43     void operator=(const SlowOperationInformer&);          // DO NOT IMPLEMENT
44   public:
45     SlowOperationInformer(const std::string &Name);
46     ~SlowOperationInformer();
47     
48     /// progress - Clients should periodically call this method when they are in
49     /// an exception-safe state.  The Amount variable should indicate how far
50     /// along the operation is, given in 1/10ths of a percent (in other words,
51     /// Amount should range from 0 to 1000).
52     void progress(unsigned Amount);
53   };
54 } // end namespace llvm
55
56 #endif /* SLOW_OPERATION_INFORMER_H */