source: src/Helpers/Assert.cpp@ 0d1ad0

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 0d1ad0 was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

  • Property mode set to 100644
File size: 3.1 KB
Line 
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
13using namespace std;
14
15namespace 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
48using namespace Assert;
49
50#ifndef NDEBUG
51
52Action _my_assert::defaultAction = Ask;
53std::vector<Assert::hook_t> _my_assert::hooks;
54
55std::map<std::string,bool> _wrapper::ignores;
56const char* _wrapper::message_ptr = "source pointer did not point to object of desired type";
57const char* _wrapper::message_ref = "source reference did not contain object of desired type";
58
59
60bool _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
98void _my_assert::doHooks(){
99 for(vector<hook_t>::reverse_iterator iter = hooks.rbegin(); iter!=hooks.rend(); ++iter ){
100 (*iter)();
101 }
102}
103
104void _my_assert::addHook(hook_t hook){
105 hooks.push_back(hook);
106}
107
108void _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
119void _my_assert::setDefault(Assert::Action action){
120 defaultAction = action;
121}
122Assert::Action _my_assert::getDefault(){
123 return defaultAction;
124}
125std::string _my_assert::printDefault(){
126 return ActionNames[defaultAction];
127}
128
129#endif
130
Note: See TracBrowser for help on using the repository browser.