| 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 | #ifndef NDEBUG
|
|---|
| 49 |
|
|---|
| 50 | Assert::Action Assert::_my_assert::defaultAction = Ask;
|
|---|
| 51 | std::vector<Assert::hook_t> Assert::_my_assert::hooks;
|
|---|
| 52 |
|
|---|
| 53 | std::map<std::string,bool> Assert::_wrapper::ignores;
|
|---|
| 54 | const char* Assert::_wrapper::message_ptr = "source pointer did not point to object of desired type";
|
|---|
| 55 | const char* Assert::_wrapper::message_ref = "source reference did not contain object of desired type";
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | bool Assert::_my_assert::check(const bool res,
|
|---|
| 59 | const char* condition,
|
|---|
| 60 | const char* message,
|
|---|
| 61 | const char* filename,
|
|---|
| 62 | const int line,
|
|---|
| 63 | bool& ignore)
|
|---|
| 64 | {
|
|---|
| 65 | if(!res){
|
|---|
| 66 | cout << "Assertion \"" << condition << "\" failed in file " << filename << " at line " << line << endl;
|
|---|
| 67 | cout << "Assertion Message: " << message << std::endl;
|
|---|
| 68 | while(true){
|
|---|
| 69 | char choice;
|
|---|
| 70 | if(defaultAction==Assert::Ask) {
|
|---|
| 71 | cout << "Please choose: (a)bort, (t)hrow execption, (i)gnore, al(w)ays ignore" << endl;
|
|---|
| 72 | cin >> choice;
|
|---|
| 73 | }
|
|---|
| 74 | else{
|
|---|
| 75 | choice = ActionKeys[defaultAction];
|
|---|
| 76 | }
|
|---|
| 77 | switch(choice){
|
|---|
| 78 | case 'a':
|
|---|
| 79 | return true;
|
|---|
| 80 | break;
|
|---|
| 81 | case 't':
|
|---|
| 82 | throw AssertionFailure(condition,filename,line,message);
|
|---|
| 83 | break;
|
|---|
| 84 | case 'w':
|
|---|
| 85 | ignore = true;
|
|---|
| 86 | // fallthrough
|
|---|
| 87 | case 'i':
|
|---|
| 88 | return false;
|
|---|
| 89 | break;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | return false;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | void Assert::_my_assert::doHooks(){
|
|---|
| 97 | for(vector<hook_t>::reverse_iterator iter = hooks.rbegin(); iter!=hooks.rend(); ++iter ){
|
|---|
| 98 | (*iter)();
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | void Assert::_my_assert::addHook(hook_t hook){
|
|---|
| 103 | hooks.push_back(hook);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void Assert::_my_assert::removeHook(Assert::hook_t hook){
|
|---|
| 107 | for(vector<hook_t>::iterator iter = hooks.begin(); iter!=hooks.end();){
|
|---|
| 108 | if((*iter)==hook){
|
|---|
| 109 | iter = hooks.erase(iter);
|
|---|
| 110 | }
|
|---|
| 111 | else{
|
|---|
| 112 | ++iter;
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | void Assert::_my_assert::setDefault(Assert::Action action){
|
|---|
| 118 | defaultAction = action;
|
|---|
| 119 | }
|
|---|
| 120 | Assert::Action Assert::_my_assert::getDefault(){
|
|---|
| 121 | return defaultAction;
|
|---|
| 122 | }
|
|---|
| 123 | std::string Assert::_my_assert::printDefault(){
|
|---|
| 124 | return ActionNames[defaultAction];
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | #endif
|
|---|
| 128 |
|
|---|