1 //===- SlowOperationInformer.h - Keep the user informed ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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, and gives them the ability to
12 // 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 1 second to complete, the progress bar
15 // is automatically shown and updated. As such, the slow operation should not
16 // print stuff to the screen, and should not be confused if an extra line
17 // appears on the screen (ie, the cursor should be at the start of the line).
19 // If the user presses CTRL-C during the operation, the next invocation of the
20 // progress method with throw an std::string object indicating that the
21 // operation was cancelled. As such, client code must be exception safe around
22 // the progress method.
24 // Because SlowOperationInformers fiddle around with signals, they cannot be
25 // nested, and interact poorly with threads. The SIGALRM handler is set back to
26 // SIGDFL, but the SIGINT signal handler is restored when the
27 // SlowOperationInformer is destroyed.
29 //===----------------------------------------------------------------------===//
31 #ifndef SUPPORT_SLOW_OPERATION_INFORMER_H
32 #define SUPPORT_SLOW_OPERATION_INFORMER_H
38 class SlowOperationInformer {
39 std::string OperationName;
40 unsigned LastPrintAmount;
42 SlowOperationInformer(const SlowOperationInformer&); // DO NOT IMPLEMENT
43 void operator=(const SlowOperationInformer&); // DO NOT IMPLEMENT
45 SlowOperationInformer(const std::string &Name);
46 ~SlowOperationInformer();
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);
54 /// progress - Same as the method above, but this performs the division for
55 /// you, and helps you avoid overflow if you are dealing with largish
57 void progress(unsigned Current, unsigned Maximum) {
58 assert(Maximum != 0 &&
59 "Shouldn't be doing work if there is nothing to do!");
60 progress(Current*1000ULL/Maximum);
63 } // end namespace llvm
65 #endif /* SLOW_OPERATION_INFORMER_H */