LLVM API Documentation

SlowOperationInformer.h

Go to the documentation of this file.
00001 //===- llvm/Support/SlowOperationInformer.h - Keep user informed *- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a simple object which can be used to let the user know what
00011 // is going on when a slow operation is happening, and gives them the ability to
00012 // cancel it.  Potentially slow operations can stack allocate one of these
00013 // objects, and periodically call the "progress" method to update the progress
00014 // bar.  If the operation takes more than 1 second to complete, the progress bar
00015 // is automatically shown and updated.  As such, the slow operation should not
00016 // print stuff to the screen, and should not be confused if an extra line
00017 // appears on the screen (ie, the cursor should be at the start of the line).
00018 //
00019 // If the user presses CTRL-C during the operation, the next invocation of the
00020 // progress method with throw an std::string object indicating that the
00021 // operation was cancelled.  As such, client code must be exception safe around
00022 // the progress method.
00023 //
00024 // Because SlowOperationInformers fiddle around with signals, they cannot be
00025 // nested, and interact poorly with threads.  The SIGALRM handler is set back to
00026 // SIGDFL, but the SIGINT signal handler is restored when the
00027 // SlowOperationInformer is destroyed.
00028 //
00029 //===----------------------------------------------------------------------===//
00030 
00031 #ifndef LLVM_SUPPORT_SLOW_OPERATION_INFORMER_H
00032 #define LLVM_SUPPORT_SLOW_OPERATION_INFORMER_H
00033 
00034 #include <string>
00035 #include <cassert>
00036 
00037 namespace llvm {
00038   class SlowOperationInformer {
00039     std::string OperationName;
00040     unsigned LastPrintAmount;
00041 
00042     SlowOperationInformer(const SlowOperationInformer&);   // DO NOT IMPLEMENT
00043     void operator=(const SlowOperationInformer&);          // DO NOT IMPLEMENT
00044   public:
00045     SlowOperationInformer(const std::string &Name);
00046     ~SlowOperationInformer();
00047 
00048     /// progress - Clients should periodically call this method when they are in
00049     /// an exception-safe state.  The Amount variable should indicate how far
00050     /// along the operation is, given in 1/10ths of a percent (in other words,
00051     /// Amount should range from 0 to 1000).
00052     void progress(unsigned Amount);
00053 
00054     /// progress - Same as the method above, but this performs the division for
00055     /// you, and helps you avoid overflow if you are dealing with largish
00056     /// numbers.
00057     void progress(unsigned Current, unsigned Maximum) {
00058       assert(Maximum != 0 &&
00059              "Shouldn't be doing work if there is nothing to do!");
00060       progress(Current*1000ULL/Maximum);
00061     }
00062   };
00063 } // end namespace llvm
00064 
00065 #endif /* SLOW_OPERATION_INFORMER_H */