Ice 3.8
C++ API Reference
Loading...
Searching...
No Matches
CtrlCHandler.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_CTRL_C_HANDLER_H
4#define ICE_CTRL_C_HANDLER_H
5
6#include "Config.h"
7
8#include <functional>
9
10namespace Ice
11{
12 /// The function called by CtrlCHandler when it catches a signal. This function must not throw exceptions. On Linux
13 /// and macOS, this function is NOT a signal handler and can call functions that are not async-signal safe.
14 /// @param sig The signal number that occurred.
15 /// @headerfile Ice/Ice.h
16 using CtrlCHandlerCallback = std::function<void(int sig)>;
17
18 /// Provides a portable way to handle Ctrl+C and Ctrl+C like signals. On Linux and macOS, the CtrlCHandler handles
19 /// SIGHUP, SIGINT and SIGTERM. On Windows, it is a wrapper for SetConsoleCtrlHandler.
20 /// @headerfile Ice/Ice.h
21 class ICE_API CtrlCHandler
22 {
23 public:
24 /// Constructs a CtrlCHandler with a null callback, meaning the callback does nothing.
25 /// @see #CtrlCHandler(CtrlCHandlerCallback)
27
28 /// Constructs a CtrlCHandler. Only one instance of this class can exist in a program at any point in time.
29 /// On Linux and macOS, this constructor masks the SIGHUP, SIGINT and SIGTERM signals and then creates a thread
30 /// that waits for these signals using sigwait. On Windows, this constructor calls SetConsoleCtrlCHandler to
31 /// register a handler routine that calls the supplied callback function.
32 /// @param cb The callback function to invoke when a signal is received.
34
35 CtrlCHandler(const CtrlCHandler&) = delete;
36 CtrlCHandler& operator=(const CtrlCHandler&) = delete;
37
38 /// Unregisters the callback function.
39 /// On Linux and macOS, this destructor joins and terminates the thread created by the constructor but does not
40 /// unmask SIGHUP, SIGINT and SIGTERM. As a result, these signals are ignored after this destructor completes.
41 /// On Windows, this destructor unregisters the SetConsoleCtrlHandler handler routine, and as a result a
42 /// Ctrl+C or similar signal will terminate the application after this destructor completes.
44
45 /// Replaces the signal callback.
46 /// @param cb The new callback.
47 /// @return The old callback, which may be nullptr.
49
50 /// Gets the current signal callback.
51 /// @return The callback.
52 [[nodiscard]] CtrlCHandlerCallback getCallback() const;
53 };
54}
55
56#endif
~CtrlCHandler()
Unregisters the callback function.
CtrlCHandler()
Constructs a CtrlCHandler with a null callback, meaning the callback does nothing.
CtrlCHandler(CtrlCHandlerCallback cb)
Constructs a CtrlCHandler.
CtrlCHandlerCallback setCallback(CtrlCHandlerCallback cb)
Replaces the signal callback.
CtrlCHandlerCallback getCallback() const
Gets the current signal callback.
std::function< void(int sig)> CtrlCHandlerCallback
The function called by CtrlCHandler when it catches a signal.
The Ice RPC framework.
Definition SampleEvent.h:59