Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // faults.hpp
  2. //
  3. // Copyright (C) MicroNeil Research Corporation 2009
  4. // This file is part of the CodeDweller library.
  5. // See www.codedweller.com for details.
  6. //
  7. // Faults and Checks are classes we can use in place of assert() to handle
  8. // unreasonable or necessary conditions in our code. They are constructed with
  9. // friendly descriptions (and optionally error codes) and then used just
  10. // like assert() would be used-- except they are designed to remain in the
  11. // production code. After all, assert() is a C (not C++) concept.
  12. //
  13. // A ...Check(test_expression) activates when the test_expression is not true.
  14. // A ...Fault(text_expression) activates when the test_expression is true.
  15. //
  16. // An Abort...() sends it's description to cerr then aborts (no cleanup).
  17. // An Exit...() sends it's description to cerr then exits with a result code.
  18. // A Runtime...() throws a runtime_error (self) with it's description in what().
  19. // A Logic...() throws a logic_error (self) with it's description in what().
  20. #ifndef MNR_faults
  21. #define MNR_faults
  22. #include <stdexcept>
  23. #include <cstdlib>
  24. #include <iostream>
  25. #include <string>
  26. using namespace std;
  27. const int DefaultExitCode = EXIT_FAILURE; // Use this when no code is provided.
  28. class AbortCheck { // If this check is false we will abort.
  29. private:
  30. const string myDescription; // This is what I have to say.
  31. public:
  32. AbortCheck(const string& Text) : myDescription(Text) {} // I am constructed with a description
  33. void operator()(bool X) const { // Apply me like assert(exp)
  34. if(false == X) { // If the expression is false then we
  35. cerr << myDescription << endl; // failed the check so we display our
  36. abort(); // description and abort.
  37. }
  38. }
  39. const string Description() { return myDescription; } // You can ask for my Description.
  40. };
  41. class AbortFault { // If this fault occurs we will abort.
  42. private:
  43. const string myDescription; // This is what I have to say.
  44. public:
  45. AbortFault(const string& Text) : myDescription(Text) {} // I am constructed with a description
  46. void operator()(bool X) const { // Apply me like assert(! exp)
  47. if(true == X) { // If the expression is true then we
  48. cerr << myDescription << endl; // have a fault so we display our fault
  49. abort(); // description and abort.
  50. }
  51. }
  52. const string Description() const { return myDescription; } // You can ask for my Description.
  53. };
  54. class ExitCheck { // If this check is false we will exit.
  55. private:
  56. const string myDescription; // This is what I have to say.
  57. const int myExitCode; // This is what I send to exit().
  58. public:
  59. ExitCheck(const string& Text, int Code=DefaultExitCode) : // I am constructed with a description
  60. myDescription(Text), myExitCode(Code) {} // and (optionlly) an exit code.
  61. void operator()(bool X) const { // Apply me like assert(exp)
  62. if(false == X) { // If the expression is false then we
  63. cerr << myDescription << endl; // failed the check so we display our
  64. exit(myExitCode); // description and exit with our code.
  65. }
  66. }
  67. const string Description() { return myDescription; } // You can ask for my Description.
  68. const int ExitCode() { return myExitCode; } // You can ask for my ExitCode.
  69. };
  70. class ExitFault { // If this fault occurs we will exit.
  71. private:
  72. const string myDescription; // This is what I have to say.
  73. const int myExitCode; // This is what I send to exit().
  74. public:
  75. ExitFault(const string& Text, int Code=DefaultExitCode) : // I am constructed with a description
  76. myDescription(Text), myExitCode(Code) {} // and (optionlly) an exit code.
  77. void operator()(bool X) const { // Apply me like assert(! exp)
  78. if(true == X) { // If the expression is true then we
  79. cerr << myDescription << endl; // have a fault so we display our fault
  80. exit(myExitCode); // description and exit with our code.
  81. }
  82. }
  83. const string Description() const { return myDescription; } // You can ask for my Description.
  84. const int ExitCode() const { return myExitCode; } // You can ask for my ExitCode.
  85. };
  86. class RuntimeCheck : public runtime_error { // Throw if this check fails.
  87. public:
  88. RuntimeCheck(const string& Text) : runtime_error(Text) {} // Construct me with a description.
  89. void operator()(bool X) const { // Apply me like assert(exp)
  90. if(false == X) { // If the expression is false then we
  91. throw *this; // failed the check so we throw.
  92. }
  93. }
  94. };
  95. class RuntimeFault : public runtime_error { // Throw if we find this fault.
  96. public:
  97. RuntimeFault(const string& Text) : runtime_error(Text) {} // Construct me with a description.
  98. void operator()(bool X) const { // Apply me like assert(exp)
  99. if(true == X) { // If the expression is true then we
  100. throw *this; // found the fault so we throw.
  101. }
  102. }
  103. };
  104. class LogicCheck : public logic_error { // Throw if this check fails.
  105. public:
  106. LogicCheck(const string& Text) : logic_error(Text) {} // Construct me with a description.
  107. void operator()(bool X) const { // Apply me like assert(exp)
  108. if(false == X) { // If the expression is false then we
  109. throw *this; // failed the check so we throw.
  110. }
  111. }
  112. };
  113. class LogicFault : public logic_error { // Throw if we find this fault.
  114. public:
  115. LogicFault(const string& Text) : logic_error(Text) {} // Construct me with a description.
  116. void operator()(bool X) const { // Apply me like assert(exp)
  117. if(true == X) { // If the expression is true then we
  118. throw *this; // found the fault so we throw.
  119. }
  120. }
  121. };
  122. #endif
  123. // End Of Include MNR_faults Once Only =========================================