| 1 | /*
 | 
|---|
| 2 |  * Assert.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Mar 18, 2010
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Helpers/Assert.hpp"
 | 
|---|
| 11 | #include <iostream>
 | 
|---|
| 12 | 
 | 
|---|
| 13 | using namespace std;
 | 
|---|
| 14 | 
 | 
|---|
| 15 | namespace Assert{
 | 
|---|
| 16 |   AssertionFailure::AssertionFailure(std::string _condition,
 | 
|---|
| 17 |                                      std::string _file,
 | 
|---|
| 18 |                                      int _line,
 | 
|---|
| 19 |                                      std::string _message) :
 | 
|---|
| 20 |     condition(_condition),
 | 
|---|
| 21 |     file(_file),
 | 
|---|
| 22 |     line(_line),
 | 
|---|
| 23 |     message(_message)
 | 
|---|
| 24 |   {}
 | 
|---|
| 25 | 
 | 
|---|
| 26 |   std::string AssertionFailure::getFile(){
 | 
|---|
| 27 |     return file;
 | 
|---|
| 28 |   }
 | 
|---|
| 29 | 
 | 
|---|
| 30 |   int AssertionFailure::getLine(){
 | 
|---|
| 31 |     return line;
 | 
|---|
| 32 |   }
 | 
|---|
| 33 | 
 | 
|---|
| 34 |   std::string AssertionFailure::getMessage(){
 | 
|---|
| 35 |     return message;
 | 
|---|
| 36 |   }
 | 
|---|
| 37 | 
 | 
|---|
| 38 |   std::ostream& AssertionFailure::operator<<(std::ostream& out){
 | 
|---|
| 39 |     out << "Assertion \"" << condition << "\" failed in file " << file << " at line " << line << endl;
 | 
|---|
| 40 |     out << "Assertion Message: " << message << std::endl;
 | 
|---|
| 41 |     return out;
 | 
|---|
| 42 |   }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |   const char  ActionKeys[]  = {'\0','a','t','i'};
 | 
|---|
| 45 |   const char* ActionNames[] = {"Ask","Abort","Throw","Ignore"};
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | using namespace Assert;
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #ifndef NDEBUG
 | 
|---|
| 51 | 
 | 
|---|
| 52 | Action _my_assert::defaultAction = Ask;
 | 
|---|
| 53 | std::vector<Assert::hook_t> _my_assert::hooks;
 | 
|---|
| 54 | 
 | 
|---|
| 55 | std::map<std::string,bool> _wrapper::ignores;
 | 
|---|
| 56 | const char* _wrapper::message_ptr = "source pointer did not point to object of desired type";
 | 
|---|
| 57 | const char* _wrapper::message_ref = "source reference did not contain object of desired type";
 | 
|---|
| 58 | 
 | 
|---|
| 59 | 
 | 
|---|
| 60 | bool _my_assert::check(const bool res,
 | 
|---|
| 61 |                        const char* condition,
 | 
|---|
| 62 |                        const char* message,
 | 
|---|
| 63 |                        const char* filename,
 | 
|---|
| 64 |                        const int line,
 | 
|---|
| 65 |                        bool& ignore)
 | 
|---|
| 66 | {
 | 
|---|
| 67 |   if(!res){
 | 
|---|
| 68 |     cout << "Assertion \"" << condition << "\" failed in file " << filename << " at line " << line << endl;
 | 
|---|
| 69 |     cout << "Assertion Message: " << message << std::endl;
 | 
|---|
| 70 |     while(true){
 | 
|---|
| 71 |       char choice;
 | 
|---|
| 72 |       if(defaultAction==Ask) {
 | 
|---|
| 73 |         cout << "Please choose: (a)bort, (t)hrow execption, (i)gnore, al(w)ays ignore" << endl;
 | 
|---|
| 74 |         cin >> choice;
 | 
|---|
| 75 |       }
 | 
|---|
| 76 |       else{
 | 
|---|
| 77 |         choice = ActionKeys[defaultAction];
 | 
|---|
| 78 |       }
 | 
|---|
| 79 |       switch(choice){
 | 
|---|
| 80 |         case 'a':
 | 
|---|
| 81 |           return true;
 | 
|---|
| 82 |           break;
 | 
|---|
| 83 |         case 't':
 | 
|---|
| 84 |           throw AssertionFailure(condition,filename,line,message);
 | 
|---|
| 85 |           break;
 | 
|---|
| 86 |         case 'w':
 | 
|---|
| 87 |           ignore = true;
 | 
|---|
| 88 |           // fallthrough
 | 
|---|
| 89 |         case 'i':
 | 
|---|
| 90 |           return false;
 | 
|---|
| 91 |           break;
 | 
|---|
| 92 |       }
 | 
|---|
| 93 |     }
 | 
|---|
| 94 |   }
 | 
|---|
| 95 |   return false;
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | void _my_assert::doHooks(){
 | 
|---|
| 99 |   for(vector<hook_t>::reverse_iterator iter = hooks.rbegin(); iter!=hooks.rend(); ++iter ){
 | 
|---|
| 100 |     (*iter)();
 | 
|---|
| 101 |   }
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | void _my_assert::addHook(hook_t hook){
 | 
|---|
| 105 |   hooks.push_back(hook);
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | void _my_assert::removeHook(Assert::hook_t hook){
 | 
|---|
| 109 |   for(vector<hook_t>::iterator iter = hooks.begin(); iter!=hooks.end();){
 | 
|---|
| 110 |     if((*iter)==hook){
 | 
|---|
| 111 |       iter = hooks.erase(iter);
 | 
|---|
| 112 |     }
 | 
|---|
| 113 |     else{
 | 
|---|
| 114 |       ++iter;
 | 
|---|
| 115 |     }
 | 
|---|
| 116 |   }
 | 
|---|
| 117 | }
 | 
|---|
| 118 | 
 | 
|---|
| 119 | void _my_assert::setDefault(Assert::Action action){
 | 
|---|
| 120 |   defaultAction = action;
 | 
|---|
| 121 | }
 | 
|---|
| 122 | Assert::Action _my_assert::getDefault(){
 | 
|---|
| 123 |   return defaultAction;
 | 
|---|
| 124 | }
 | 
|---|
| 125 | std::string _my_assert::printDefault(){
 | 
|---|
| 126 |   return ActionNames[defaultAction];
 | 
|---|
| 127 | }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | #endif
 | 
|---|
| 130 | 
 | 
|---|