Permalink
Browse files

Adds console event handler.

  • Loading branch information...
1 parent c3ad857 commit 82437b4b4d5dd1451972807e0448382a28ca178f @AndreLouisCaron committed Apr 27, 2012
Showing with 141 additions and 0 deletions.
  1. +2 −0 code/w32/CMakeLists.txt
  2. +54 −0 code/w32/ConsoleEventHandler.cpp
  3. +84 −0 code/w32/ConsoleEventHandler.hpp
  4. +1 −0 code/w32/__module__.hpp
View
@@ -33,6 +33,7 @@ set(${module}_headers
Codepage.hpp
computername.hpp
Console.hpp
+ ConsoleEventHandler.hpp
currentdirectory.hpp
Delta.hpp
Environment.hpp
@@ -63,6 +64,7 @@ set(${module}_sources
Codepage.cpp
computername.cpp
Console.cpp
+ ConsoleEventHandler.cpp
currentdirectory.cpp
Delta.cpp
Environment.cpp
@@ -0,0 +1,54 @@
+// Copyright (c) 2009-2012, Andre Caron (andre.l.caron@gmail.com)
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+/*!
+ * @file w32/ConsoleEventHandler.cpp
+ */
+
+#include "ConsoleEventHandler.hpp"
+#include "Error.hpp"
+
+namespace w32 {
+
+ ConsoleEventHandler::ConsoleEventHandler (Function function)
+ : myFunction(function)
+ {
+ const ::BOOL result = ::SetConsoleCtrlHandler(myFunction, TRUE);
+ if (result == FALSE) {
+ const ::DWORD error = ::GetLastError();
+ UNCHECKED_WIN32C_ERROR(SetConsoleCtrlHandler, error);
+ }
+ }
+
+ ConsoleEventHandler::~ConsoleEventHandler ()
+ {
+ const ::BOOL result = ::SetConsoleCtrlHandler(myFunction, FALSE);
+ if (result == FALSE) {
+ // ..?
+ }
+ }
+
+}
@@ -0,0 +1,84 @@
+#ifndef _w32_ConsoleEventHandler_hpp__
+#define _w32_ConsoleEventHandler_hpp__
+
+// Copyright (c) 2009-2012, Andre Caron (andre.l.caron@gmail.com)
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+/*!
+ * @file w32/ConsoleEventHandler.hpp
+ */
+
+#include "__configure__.hpp"
+#include <w32/NotCopyable.hpp>
+
+namespace w32 {
+
+ //! @addtogroup w32
+ //! @{
+
+ /*!
+ * @brief Console event handler resgistration.
+ *
+ * Installing a console event handler allows your console application to
+ * intercept control-C, control-break, console close events and the like.
+ * This allows you to perform proper shutdown even if your main loop is
+ * blocking on some specific event (provided that you can unblock it at
+ * will).
+ *
+ * Console event handlers are registered in a LIFO (stack) order. The last
+ * console event handler is called first. If the first (most recently
+ * installed) event handler does not process the event, the second handler
+ * is called and so forth.
+ */
+ class ConsoleEventHandler :
+ private NotCopyable
+ {
+ /* nested types. */
+ public:
+ typedef ::PHANDLER_ROUTINE Function;
+
+ /* data. */
+ private:
+ Function myFunction;
+
+ /* construction. */
+ public:
+ /*!
+ * @brief Installs @a function as a console event handler.
+ */
+ ConsoleEventHandler (Function function);
+
+ /*!
+ * @brief Removes the console event handler installed.
+ */
+ ~ConsoleEventHandler ();
+ };
+
+ //! @}
+
+}
+
+#endif /* _w32_ConsoleEventHandler_hpp__ */
View
@@ -45,6 +45,7 @@ namespace w32 {}
#include <w32/computername.hpp>
#include <w32/currentdirectory.hpp>
#include <w32/Console.hpp>
+#include <w32/ConsoleEventHandler.hpp>
#include <w32/Environment.hpp>
#include <w32/Delta.hpp>
#include <w32/Error.hpp>

0 comments on commit 82437b4

Please sign in to comment.