Browse Source

cleaned up namespace in faults

master
Pete McNeil 4 years ago
parent
commit
ac969f3fb9
1 changed files with 27 additions and 30 deletions
  1. 27
    30
      faults.hpp

+ 27
- 30
faults.hpp View File

// A Runtime...() throws a runtime_error (self) with it's description in what(). // A Runtime...() throws a runtime_error (self) with it's description in what().
// A Logic...() throws a logic_error (self) with it's description in what(). // A Logic...() throws a logic_error (self) with it's description in what().
#ifndef MNR_faults
#define MNR_faults
#pragma once
#include <stdexcept> #include <stdexcept>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <string> #include <string>
using namespace std;
namespace codedweller {
const int DefaultExitCode = EXIT_FAILURE; // Use this when no code is provided. const int DefaultExitCode = EXIT_FAILURE; // Use this when no code is provided.
private: private:
const string myDescription; // This is what I have to say.
const std::string myDescription; // This is what I have to say.
public: public:
AbortCheck(const string& Text) : myDescription(Text) {} // I am constructed with a description
AbortCheck(const std::string& Text) : myDescription(Text) {} // I am constructed with a description
void operator()(bool X) const { // Apply me like assert(exp) void operator()(bool X) const { // Apply me like assert(exp)
if(false == X) { // If the expression is false then we if(false == X) { // If the expression is false then we
cerr << myDescription << endl; // failed the check so we display our
std::cerr << myDescription << std::endl; // failed the check so we display our
abort(); // description and abort. abort(); // description and abort.
} }
} }
const string Description() { return myDescription; } // You can ask for my Description.
const std::string Description() { return myDescription; } // You can ask for my Description.
}; };
class AbortFault { // If this fault occurs we will abort. class AbortFault { // If this fault occurs we will abort.
private: private:
const string myDescription; // This is what I have to say.
const std::string myDescription; // This is what I have to say.
public: public:
AbortFault(const string& Text) : myDescription(Text) {} // I am constructed with a description
AbortFault(const std::string& Text) : myDescription(Text) {} // I am constructed with a description
void operator()(bool X) const { // Apply me like assert(! exp) void operator()(bool X) const { // Apply me like assert(! exp)
if(true == X) { // If the expression is true then we if(true == X) { // If the expression is true then we
cerr << myDescription << endl; // have a fault so we display our fault
std::cerr << myDescription << std::endl; // have a fault so we display our fault
abort(); // description and abort. abort(); // description and abort.
} }
} }
const string Description() const { return myDescription; } // You can ask for my Description.
const std::string Description() const { return myDescription; } // You can ask for my Description.
}; };
class ExitCheck { // If this check is false we will exit. class ExitCheck { // If this check is false we will exit.
private: private:
const string myDescription; // This is what I have to say.
const std::string myDescription; // This is what I have to say.
const int myExitCode; // This is what I send to exit(). const int myExitCode; // This is what I send to exit().
public: public:
ExitCheck(const string& Text, int Code=DefaultExitCode) : // I am constructed with a description
ExitCheck(const std::string& Text, int Code=DefaultExitCode) : // I am constructed with a description
myDescription(Text), myExitCode(Code) {} // and (optionlly) an exit code. myDescription(Text), myExitCode(Code) {} // and (optionlly) an exit code.
void operator()(bool X) const { // Apply me like assert(exp) void operator()(bool X) const { // Apply me like assert(exp)
if(false == X) { // If the expression is false then we if(false == X) { // If the expression is false then we
cerr << myDescription << endl; // failed the check so we display our
std::cerr << myDescription << std::endl; // failed the check so we display our
exit(myExitCode); // description and exit with our code. exit(myExitCode); // description and exit with our code.
} }
} }
const string Description() { return myDescription; } // You can ask for my Description.
const std::string Description() { return myDescription; } // You can ask for my Description.
const int ExitCode() { return myExitCode; } // You can ask for my ExitCode. const int ExitCode() { return myExitCode; } // You can ask for my ExitCode.
}; };
private: private:
const string myDescription; // This is what I have to say.
const std::string myDescription; // This is what I have to say.
const int myExitCode; // This is what I send to exit(). const int myExitCode; // This is what I send to exit().
public: public:
ExitFault(const string& Text, int Code=DefaultExitCode) : // I am constructed with a description
ExitFault(const std::string& Text, int Code=DefaultExitCode) : // I am constructed with a description
myDescription(Text), myExitCode(Code) {} // and (optionlly) an exit code. myDescription(Text), myExitCode(Code) {} // and (optionlly) an exit code.
void operator()(bool X) const { // Apply me like assert(! exp) void operator()(bool X) const { // Apply me like assert(! exp)
if(true == X) { // If the expression is true then we if(true == X) { // If the expression is true then we
cerr << myDescription << endl; // have a fault so we display our fault
std::cerr << myDescription << std::endl; // have a fault so we display our fault
exit(myExitCode); // description and exit with our code. exit(myExitCode); // description and exit with our code.
} }
} }
const string Description() const { return myDescription; } // You can ask for my Description.
const std::string Description() const { return myDescription; } // You can ask for my Description.
const int ExitCode() const { return myExitCode; } // You can ask for my ExitCode. const int ExitCode() const { return myExitCode; } // You can ask for my ExitCode.
}; };
class RuntimeCheck : public runtime_error { // Throw if this check fails.
class RuntimeCheck : public std::runtime_error { // Throw if this check fails.
public: public:
RuntimeCheck(const string& Text) : runtime_error(Text) {} // Construct me with a description.
RuntimeCheck(const std::string& Text) : std::runtime_error(Text) {} // Construct me with a description.
void operator()(bool X) const { // Apply me like assert(exp) void operator()(bool X) const { // Apply me like assert(exp)
if(false == X) { // If the expression is false then we if(false == X) { // If the expression is false then we
} }
}; };
class RuntimeFault : public runtime_error { // Throw if we find this fault.
class RuntimeFault : public std::runtime_error { // Throw if we find this fault.
public: public:
RuntimeFault(const string& Text) : runtime_error(Text) {} // Construct me with a description.
RuntimeFault(const std::string& Text) : std::runtime_error(Text) {} // Construct me with a description.
void operator()(bool X) const { // Apply me like assert(exp) void operator()(bool X) const { // Apply me like assert(exp)
if(true == X) { // If the expression is true then we if(true == X) { // If the expression is true then we
} }
}; };
class LogicCheck : public logic_error { // Throw if this check fails.
class LogicCheck : public std::logic_error { // Throw if this check fails.
public: public:
LogicCheck(const string& Text) : logic_error(Text) {} // Construct me with a description.
LogicCheck(const std::string& Text) : std::logic_error(Text) {} // Construct me with a description.
void operator()(bool X) const { // Apply me like assert(exp) void operator()(bool X) const { // Apply me like assert(exp)
if(false == X) { // If the expression is false then we if(false == X) { // If the expression is false then we
} }
}; };
class LogicFault : public logic_error { // Throw if we find this fault.
class LogicFault : public std::logic_error { // Throw if we find this fault.
public: public:
LogicFault(const string& Text) : logic_error(Text) {} // Construct me with a description.
LogicFault(const std::string& Text) : std::logic_error(Text) {} // Construct me with a description.
void operator()(bool X) const { // Apply me like assert(exp) void operator()(bool X) const { // Apply me like assert(exp)
if(true == X) { // If the expression is true then we if(true == X) { // If the expression is true then we
} }
}; };
#endif
// End Of Include MNR_faults Once Only =========================================
} // End namespace codedweller

Loading…
Cancel
Save