Astral-codex/Errorseal.h

66 lines
1.4 KiB
C
Raw Normal View History

2024-12-18 13:06:15 +00:00
//
// Created by TheSkyWinds on 10/25/24.
//
// TODO: Add documentation on how it is used.
2024-12-20 14:13:19 +00:00
// TODO: Figure out how to document code.
2024-12-18 13:06:15 +00:00
#ifndef ERRORSEAL_H
#define ERRORSEAL_H
#include <iostream>
#include <limits>
namespace error {
// Clears the buffer for std::cin.
inline void ignoreLine() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
/*
* cinError() returns true if an error occurs.
* cinError() also clears up the error and buffer, so that the program can continue to work.
*/
inline bool cinError(){
bool errorState{false};
if (!std::cin) {
if (std::cin.eof()) {
std::exit(0); // Shut down the program now
}
std::cin.clear();
ignoreLine();
errorState = true;
}
return errorState;
}
/*
* Throws a generic, undefined error message, but does the same as the function above.
* Primarily used for quick and easy debugging.
*/
inline bool cinErrorVerbose() {
bool errorState{false};
if (!std::cin) {
if (std::cin.eof()) {
std::exit(0);
}
std::cin.clear();
ignoreLine();
std::cerr << "Whoops, you broke the program!\n";
errorState = true;
}
return errorState;
}
}
#endif //ERRORSEAL_H