static class in c++ with operator overloading?? IS POSSIBLE ??

  • Thread starter Thread starter josh.bauer2020
  • Start date Start date
J

josh.bauer2020

Guest
I made following class to interact with console in c++.

//----------------------------------------------------------
static class console {
// HMODULE hKernel32 = LoadLibraryA("kernel32.dll");
// string yy=GetCommandLineA();
// _SYSTEMTIME SYSTEMTIME;
// GetSystemTime(&SYSTEMTIME);
// Sleep(88);
// GetSystemDirectoryA(ppp, 99);
private:
HANDLE stdOutput;
HANDLE stdInput;
HWND consoleWindow;
bool InitFlag = 0;
void CheckInit() {
if (InitFlag)return;
AllocConsole();
SetConsoleTitle(_T("DOSBox Debugger"));
consoleWindow = GetConsoleWindow();
stdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
stdInput = GetStdHandle(STD_INPUT_HANDLE);
DWORD lpMode;
GetConsoleMode(stdInput, &lpMode);
SetConsoleMode(stdInput, lpMode | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_QUICK_EDIT_MODE);
SetCursorVisible();
InitFlag = 1;
}

DWORD const_chrs_length(const char* pConstChars) {
DWORD strLength = 0;
while (pConstChars[strLength] != 0)strLength++;
return strLength;
}

void WriteConstChars(const char* doWypisania) {
CheckInit();
DWORD rozmiar = const_chrs_length(doWypisania);
WriteConsoleA(stdOutput, doWypisania, rozmiar, NULL, NULL);
}

void WriteString(string& doWypisania) {
CheckInit();
WriteConsoleA(stdOutput, (char*)doWypisania, doWypisania.len(), NULL, NULL);
}

void WriteChar(char& myChar) {
CheckInit();
string myString(1);
myString[0] = myChar;
WriteConsoleA(stdOutput, (char*)myString, myString.len(), NULL, NULL);
}

string ReadString() {
CheckInit();
string toBeRead(255);
DWORD howManyRead = 0;
BOOL what = ReadConsoleA(stdInput, (char*)toBeRead, 254, &howManyRead, 0);
toBeRead[howManyRead - 2] = 0;
return toBeRead;
}

void WriteInt(int& paramInt) {
CheckInit();
int tmpInt = paramInt;
char signFlag = 0;
if (tmpInt < 0) {
signFlag = 1;
tmpInt = -tmpInt;
}
const int maxString = 13;
string myString(maxString);
int index = maxString - 2;
do {
char reminder = (char)(tmpInt % 10 + 0x30);
tmpInt = tmpInt / 10;
myString[index] = reminder;
index--;
} while (tmpInt > 0);
if (signFlag > 0) {
myString[index] = '-';
}
else {
myString[index] = '+';
}
for (int i = 0;i < maxString-index-1;i++)WriteChar(myString[i+index]);
}

WORD ReadVirtualKeyCode() {
CheckInit();
WORD VirtualKeyCode = 0;
DWORD numEventsAvailable;
while (GetNumberOfConsoleInputEvents(stdInput, &numEventsAvailable) == TRUE && numEventsAvailable > 0) {
INPUT_RECORD input_record;
DWORD events = 0;
ReadConsoleInput(stdInput, &input_record, 1, &events);
if (input_record.EventType == KEY_EVENT && input_record.Event.KeyEvent.bKeyDown) {
VirtualKeyCode = input_record.Event.KeyEvent.wVirtualKeyCode;
}
}
return VirtualKeyCode;
}

void SetCursorVisible() {
CONSOLE_CURSOR_INFO cci;
cci.dwSize = 10;
cci.bVisible = TRUE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}

friend console& operator<<(console& paramConsole, const char* paramChars);
friend console& operator<<(console& paramConsole, string& paramString);
friend console& operator>>(console& paramConsole, string& paramString);
friend console& operator<<(console& paramConsole, int& paramInt);
friend console& operator>>(console& paramConsole, int& paramInt);
friend console& operator<<(console& paramConsole, WORD& paramWord);
friend console& operator>>(console& paramConsole, WORD& paramWord);
public:

int GetMAXx() {
CheckInit();
CONSOLE_SCREEN_BUFFER_INFO bufInfo;
GetConsoleScreenBufferInfo(stdOutput, &bufInfo);
return bufInfo.dwSize.X;
}
int GetMAXy() {
CheckInit();
CONSOLE_SCREEN_BUFFER_INFO bufInfo;
GetConsoleScreenBufferInfo(stdOutput, &bufInfo);
return bufInfo.dwSize.Y;
}
void gotoXY(int x, int y) {
CheckInit();
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(stdOutput, c);
}
int GetTextColor() {
CheckInit();
CONSOLE_SCREEN_BUFFER_INFO bufInfo;
GetConsoleScreenBufferInfo(stdOutput, &bufInfo);
return bufInfo.wAttributes;
}
void SetTextColor(int wAttributes) {//Bits 0-3 are the foreground color, and bits 4 - 7 are the background color.
CheckInit();
SetConsoleTextAttribute(stdOutput, wAttributes);
}
void ClrScr() {
CheckInit();
CONSOLE_SCREEN_BUFFER_INFO bufInfo;
GetConsoleScreenBufferInfo(stdOutput, &bufInfo);
int lineLength = bufInfo.dwSize.X;
int columnLength = bufInfo.dwSize.Y;
short textAttribute = bufInfo.wAttributes;
string _oneLineSpace(lineLength);
for (int i = 0;i < lineLength;i++)_oneLineSpace = ' ';
WORD* _oneLineAttributes = new WORD[lineLength];
for (int i = 0;i < lineLength;i++)_oneLineAttributes = textAttribute;
COORD dwWriteCoord = { 0,0 };
DWORD numWritten;
WORD attribute = 0x07;
for (int i = 0;i < columnLength;i++) {
dwWriteCoord.Y = i;
WriteConsoleOutputAttribute(stdOutput, _oneLineAttributes, lineLength, dwWriteCoord, &numWritten);
WriteConsoleOutputCharacterA(stdOutput, _oneLineSpace, lineLength, dwWriteCoord, &numWritten);
}
gotoXY(0, 0);
}
void Free() {
FreeConsole();
InitFlag = 0;
}
};
console& operator<<(console& paramConsole, const char* paramChars)
{
string tempString = paramChars;
paramConsole.WriteString(tempString);
return paramConsole;
}
console& operator<<(console& paramConsole, string& paramString)
{
paramConsole.WriteString(paramString);
return paramConsole;
}
console& operator>>(console& paramConsole, string& paramString)
{
paramString = paramConsole.ReadString();
return paramConsole;
}
console& operator<<(console& paramConsole, int& paramInt)
{
paramConsole.WriteInt(paramInt);
return paramConsole;
}
console& operator>>(console& paramConsole, int& paramInt)
{
string tempString = paramConsole.ReadString();
paramInt = 0;
int index = 0;
char sign = 1;
if (tempString[0] == '+') {
index++;
}
else if (tempString[0] == '-') {
sign = -1;
index++;
}
else if (tempString[0] == '0' && (tempString[1] == 'x' || tempString[1] == 'X')) {
index += 2;
while ((tempString[index] >= '0' && tempString[index] <= '9') || (tempString[index] >= 'a' && tempString[index] <= 'f') || (tempString[index] >= 'A' && tempString[index] <= 'F')) {
if (tempString[index] >= 'a' && tempString[index] <= 'f') {
tempString[index] &= 0b11011111;//na duże
}
if (tempString[index] >= 'A' && tempString[index] <= 'F') {
tempString[index] = tempString[index] - 'A' + 10;
}
else {
tempString[index] -= '0';
}
paramInt *= 16;
paramInt += tempString[index];
index++;
}
return paramConsole;
}
while (tempString[index] >= '0' && tempString[index] <= '9') {
tempString[index] &= 0xF;
paramInt *= 10;
paramInt += tempString[index];
index++;
}
paramInt *= sign;
return paramConsole;
}
console& operator<<(console& paramConsole, WORD& paramWord)
{
int ileWypisać = paramWord;
paramConsole.WriteInt(ileWypisać);
return paramConsole;
}
console& operator>>(console& paramConsole, WORD& paramWord)
{
paramWord = paramConsole.ReadVirtualKeyCode();
return paramConsole;
}
console cons;
//----------------------------------------------------------

I also use written by me class for string:

//----------------------------------------------------------
class string {
char* _string = nullptr;

public:

string() { }

string(int length) {
_string = new char[length+1];
_string[length] = 0;
}

string(const char* newChars)
{
size_t nChars = strlen(newChars) + 1;
_string = new char[nChars];
strcpy_s(_string, nChars, newChars);
}

string(string& rhs)
{
if (_string) delete[] _string;
size_t nChars = strlen(rhs._string) + 1;
_string = new char[nChars];
strcpy_s(_string, nChars, rhs._string);
}

string(string&& rhs)
{
if (_string) delete[] _string;
_string = rhs._string;
rhs._string = nullptr;
}

string& operator=(char* rhs)
{
_string=rhs;
return *this;
}

string& operator=(string& rhs)
{
if (_string) delete[] _string;
size_t nChars = strlen(rhs._string) + 1;
_string = new char[nChars];
strcpy_s(_string, nChars, rhs._string);
return *this;
}

string& operator=(string&& rhs)
{
if (_string) delete[] _string;
_string = rhs._string;
rhs._string = nullptr;
return *this;
}

~string() {
if (_string) delete[] _string;
}

char& operator[](int index)
{
return _string[index];
}

operator char*& () { return _string; }

DWORD len() {
DWORD strLength = 0;
while (_string[strLength] != 0)strLength++;
return strLength;
}
};

//----------------------------------------------------------


QUESTION IS HOW TO MAKE THAT CLASS STATIC?

There is variable cons under console class. I use this in following way:

string myString="this is my string";

cons<<myString>>myString<<myString;


QUESTION IS: WOULD I MAKE console CLASS STATIC so I would not have to use cons variable BUT

MAKE console<<myString>>myString<<myString;

?????????

Continue reading...
 
Back
Top