MOAB: Mesh Oriented datABase  (version 5.3.0)
MsqInterrupt.hpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2004 Sandia Corporation and Argonne National
00005     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
00006     with Sandia Corporation, the U.S. Government retains certain
00007     rights in this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023   ***************************************************************** */
00024 
00025 #ifndef MSQ_INTERRUPT_HPP
00026 #define MSQ_INTERRUPT_HPP
00027 
00028 #include "MsqError.hpp"
00029 
00030 namespace MBMesquite
00031 {
00032 
00033 /** \brief Class to watch for user-interrupt (SIGINT, ctrl-C)
00034  *
00035  * A class to watch for SIGINT.
00036  *
00037  * Creating an instance of the class ensures that the Mesquite
00038  * handler for SIGINT is registered.  When all instances are
00039  * destroyed, the Mesquite handler is removed.  The intent is
00040  * that each interruptable API declare an instance on the stack.
00041  * This way the handler is automatically unregistered when the
00042  * API returns.  For example:
00043  * <code>
00044  * void my_api( MsqError& err )
00045  * {
00046  *   MsqInterrupt interrupt;
00047  *   ... //do stuff
00048  *   return;
00049  * }
00050  * </code>
00051  */
00052 class MsqInterrupt
00053 {
00054   public:
00055     /**\brief Disable Mesquite's SIGINT handler */
00056     static void disable( MsqError& err );
00057     /**\brief Allow Mesquite to register a SIGINT handler */
00058     static void allow( MsqError& err );
00059     /**\brief Force Mesquite to register SIGINT handler */
00060     static void enable( MsqError& err );
00061 
00062     /**\brief Check if an interrupt was seen */
00063     static bool interrupt()
00064     {
00065         return sawInterrupt;
00066     }
00067     /**\brief Clear the interrupt flag */
00068     static void clear()
00069     {
00070         sawInterrupt = false;
00071     }
00072     /**\brief Set the interrupt flag */
00073     static void set_interrupt()
00074     {
00075         sawInterrupt = true;
00076     }
00077 
00078     /** Constructor, increment instance count. If
00079      * instance count was zero, register SIGINT handler */
00080     MsqInterrupt();
00081     /** Constructor, decrement instance count. If
00082      * instance count goes to zero, remove SIGINT handler */
00083     ~MsqInterrupt();
00084 
00085     static void set_handler();
00086 
00087   private:
00088     enum InterruptMode
00089     {
00090         CATCH,
00091         IGNORE,
00092         AUTO
00093     };
00094 
00095     static InterruptMode interruptMode;
00096     static unsigned instanceCount;
00097     static bool sawInterrupt;
00098 
00099     // Don't allow any of this stuff (make them private)
00100     void* operator new( size_t size );
00101     MsqInterrupt( const MsqInterrupt& );
00102     MsqInterrupt& operator=( const MsqInterrupt& );
00103 };
00104 }  // namespace MBMesquite
00105 
00106 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines