xtd 0.2.0
environment_cancel_signal.cpp

Shows how to use xtd::environment::cancel_signal event.

#include <xtd/console>
#include <xtd/environment>
#include <xtd/startup>
using namespace xtd;
namespace environment_cancel_sgnal_example {
class program static_ {
public:
// The main entry point for the application.
static auto main() {
console::write_line("A {} signal occured!", e.signal());
e.cancel(cancel);
};
show_help();
while (true) {
auto key_info = console::read_key(true);
if (key_info.modifiers() == console_modifiers::none && key_info.key() == console_key::escape) exit();
else if (key_info.modifiers() == console_modifiers::control && key_info.key() == console_key::c) generate_signal(signal::interrupt);
else switch (key_info.key_char()) {
case 'a': generate_signal(signal::abnormal_termination); break;
case 'c': generate_signal(signal::interrupt); break;
case 'e': exit(); break;
case 'f': generate_signal(signal::floating_point_exception); break;
case 'h': show_help(); break;
case 'i': generate_signal(signal::illegal_instruction); break;
case 't': generate_signal(signal::software_termination); break;
case 'u': cancel = !cancel; show_help(); break;
case 'v': generate_signal(signal::segmentation_violation); break;
default: break;
}
}
}
}
private:
static void exit() {
}
static void generate_signal(xtd::signal signal) {
console::write_line("Generate {} signal", signal);
}
static void show_help() {
console::write_line("Select a key :");
console::write_line(" a : Generate abort signal");
console::write_line(" c : (or ctrl-c) Generate interrupt signal");
console::write_line(" e : (or Escape) Exit example");
console::write_line(" f : Generate floating point exception signal");
console::write_line(" h : Display this help");
console::write_line(" i : Generate illegal instruction signal");
console::write_line(" t : Generate software termination signal");
console::write_line(" u : Update cancel signal status");
console::write_line(" v : Generate segmentation violation signal");
console::write_line("The signal will {}be cancelled!", cancel ? "" : "not ");
}
inline static bool cancel = true;
};
}
startup_(environment_cancel_sgnal_example::program::main);
// This code produces the following output :
//
// Select a key :
// a : Generate abort signal
// c : (or ctrl-c) Generate interrupt signal
// e : (or Escape) Exit example
// f : Generate floating point exception signal
// h : Display this help
// i : Generate illegal instruction signal
// t : Generate software termination signal
// u : Update cancel signal status
// v : Generate segmentation violation signal
// The signal will be cancelled!
static bool treat_control_c_as_input()
Gets a value indicating whether the combination of the Control modifier key and C console key (Ctrl+C...
static void clear()
Clears the console buffer and corresponding console window of display information.
static console_key_info read_key()
Obtains the next character or function key pressed by the user. The pressed key is displayed in the c...
static bool key_available()
Gets a value indicating whether a key press is available in the input stream.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static void exit()
Terminates this process and returns an exit code to the operating system.
static void raise(xtd::signal signal)
Sends xtd::signal to the program. The xtd::environment::cancel_signal event is invoked with the speci...
static event< environment, signal_cancel_event_handler > cancel_signal
Occurs when a signal is sent to the current process.
Definition environment.hpp:531
Provides data for the environment signal event. This class cannot be inherited.
Definition signal_cancel_event_args.hpp:18
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:37
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:167
signal
Specifies signals that can interrupt the current process.
Definition signal.hpp:18
@ floating_point_exception
Erroneous arithmetic operation such as divide by zero. Is equal to SIGFPE.
Definition signal.hpp:22
@ segmentation_violation
Invalid memory access (segmentation fault). Is equal to SIGSEGV.
Definition signal.hpp:28
@ interrupt
external interrupt, usually initiated by the user. Is equal to SIGINT.
Definition signal.hpp:26
@ abnormal_termination
Abnormal termination triggered by abort call. Is equal to SIGABRT.
Definition signal.hpp:20
@ software_termination
Termination request, sent to the program. Is equal to SIGTERM.
Definition signal.hpp:30
@ illegal_instruction
Invalid program image, such as invalid instruction. Is equal to SIGILL.
Definition signal.hpp:24
@ success
Successful execution of a program. Is equal to EXIT_SUCCESS.
Definition exit_status.hpp:20
@ none
No modifier key.
Definition console_modifiers.hpp:22
@ control
The left or right CTRL modifier key.
Definition console_modifiers.hpp:28
@ c
The C key.
Definition console_key.hpp:92
@ escape
The ESC (ESCAPE) key.
Definition console_key.hpp:34
@ e
The E key.
Definition console_key.hpp:96
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8