Design Pattern Examples
Overview of object-oriented design patterns
enablevtmode.cpp
Go to the documentation of this file.
1
5
6#ifdef _MSC_VER
7#include <iostream>
8#include <windows.h>
9#endif
10
11namespace // Anonymous
12{
13
14#ifdef _MSC_VER
28 class EnableVTMode
29 {
30 private:
31 const DWORD INVALID_MODE = 0xffffffff;
32
33 private:
34 DWORD dwOriginalOutMode;
35 HANDLE hOut;
36 public:
38 : dwOriginalOutMode(INVALID_MODE)
40 {
41 hOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
42 if (hOut != INVALID_HANDLE_VALUE)
43 {
44 if (!::GetConsoleMode(hOut, &dwOriginalOutMode))
45 {
46 dwOriginalOutMode = INVALID_MODE;
47 std::cout << "Failed to get the Console output's original mode." << std::endl;
48 }
49 else
50 {
51 DWORD dwRequestedOutModes = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
52 DWORD dwOutMode = dwOriginalOutMode | dwRequestedOutModes;
53 if (!SetConsoleMode(hOut, dwOutMode))
54 {
55 std::cout << "Failed to enable the Console output's virtual terminal mode." << std::endl;
56 }
57 }
58 }
59 else
60 {
61 std::cout << "Failed to get the Console's output handle." << std::endl;
62 }
63 }
64
65 ~EnableVTMode()
66 {
67 if (dwOriginalOutMode != INVALID_MODE)
68 {
69 ::SetConsoleMode(hOut, dwOriginalOutMode);
70 }
71 }
72 };
73
74#endif
75
76} // end anonymous namespace
77
78namespace Helpers
79{
86 {
87#ifdef _MSC_VER
88 static EnableVTMode enableVTMode;
89#endif
90 }
91
92} // end namespace
def EnableVTMode(callable function)
Wrap the given function with enabling and disabling the virtual terminal processing for a Windows Con...
Definition: enablevtmode.py:66
int ENABLE_VIRTUAL_TERMINAL_PROCESSING
Flag that, when set, enables virtual terminal processing and thus support for ANSI escape code handli...
Definition: enablevtmode.py:21
int INVALID_HANDLE_VALUE
Windows indicator of an invalid handle.
The namespace containing all the "helper" functions in the C++ code.
void enableVTMode()
On Windows, enable the virtual terminal processing mode on the Console's output handle....