SheafSystem  0.0.0.0
assert_contract.cc
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
20 
21 #include "SheafSystem/assert_contract.h"
22 #include "SheafSystem/std_stdexcept.h"
23 #include "SheafSystem/std_iostream.h"
24 #include "SheafSystem/std_sstream.h"
25 #include "SheafSystem/std_string.h"
26 
27 #ifdef _WIN32
28 #include "windows.h" // Compiling with MS C++, needed for OutputDebugString.
29 #endif
30 
31 using namespace std;
32 using namespace sheaf; // Workaround for MSVC++ bug
33 
34 namespace
35 {
36 
37 //
38 // Output message xmsg of type xmsg_type to the VisualStudio Output Window.
39 // Only implemented if _WIN32 is defined.
40 //
41 void
42 print_output_message(const string& xmsg_type, const string& xmsg)
43 {
44 #ifdef _WIN32
45 
46  // Output Header.
47 
48  int ltype_len = xmsg_type.length();
49  int lmsg_len = xmsg.length();
50  int lline_len = lmsg_len - ltype_len - 4;
51 
52  OutputDebugString("\n== ");
53  OutputDebugString(xmsg_type.c_str());
54  OutputDebugString(" ");
55  for(int i=0; i < lline_len; i++)
56  {
57  OutputDebugString("=");
58  }
59 
60  // Output Message.
61 
62  OutputDebugString("\n\n");
63  OutputDebugString(xmsg.c_str());
64  OutputDebugString("\n\n");
65 
66  // Output Footer
67 
68  for(int i=0; i < lmsg_len; i++)
69  {
70  OutputDebugString("=");
71  }
72 
73  OutputDebugString("\n\n");
74 
75  // Output message to standard error so it appears in the
76  // Command Console.
77 
78  std::cerr << xmsg << std::endl;
79 
80 #else
81 
82  // Output Header.
83 
84  int ltype_len = xmsg_type.length();
85  int lmsg_len = xmsg.length();
86  int lline_len = lmsg_len - ltype_len - 4;
87 
88  std::cerr << endl << "== " << xmsg_type << " ";
89  for(int i=0; i < lline_len; i++)
90  {
91  std::cerr << "=";
92  }
93  std::cerr << endl;
94 
95  // Output Message.
96 
97  std::cerr << endl << xmsg.c_str() << endl;
98 
99  // Output Footer
100 
101  std::cerr << endl;
102  for(int i=0; i < lmsg_len; i++)
103  {
104  std::cerr << "=";
105  }
106  std::cerr << endl << endl;
107 
108 #endif
109 };
110 
111 } // end unnamed namespace.
112 
113 // ===========================================================
114 // ASSERT_CONTRACT FACET
115 // ===========================================================
116 
117 // void
118 // sheaf::
119 // check_contract(bool xcond, const char* xmsg, const char* xfile, int xline)
120 // {
121 // if(!xcond)
122 // {
123 // string lfilepath(xfile);
124 // string::size_type lidx = lfilepath.find_last_of("/\\");
125 // string lfilename = (lidx != string::npos) ? lfilepath.substr(lidx+1) : lfilepath;
126 
127 // stringstream lmsg;
128 // lmsg << "'" << xmsg << "'" << " in file " << lfilename << " at line " << xline;
129 
130 // print_output_message("CONTRACT VIOLATION", lmsg.str());
131 // throw std::logic_error(lmsg.str());
132 // }
133 // };
134 
135 void
136 sheaf::
137 check_contract(bool xcond, const char* xmsg, const char* xfunction_name, const char* xfile, int xline)
138 {
139  if(!xcond)
140  {
141  string lfilepath(xfile);
142  string::size_type lidx = lfilepath.find_last_of("/\\");
143  string lfilename = (lidx != string::npos) ? lfilepath.substr(lidx+1) : lfilepath;
144 
145  stringstream lmsg;
146  lmsg << "'" << xmsg << "'" << " in function " << xfunction_name << " in file " << lfilename << " at line " << xline;
147 
148  print_output_message("CONTRACT VIOLATION", lmsg.str());
149  throw std::logic_error(lmsg.str());
150  }
151 };
152 
153 // void
154 // sheaf::
155 // check_contract(bool xcond, const char* xcond_msg, int xi, const char* xi_msg, const char* xfile, int xline)
156 // {
157 // if(!xcond)
158 // {
159 // string lfilepath(xfile);
160 // string::size_type lidx = lfilepath.find_last_of("/\\");
161 // string lfilename = (lidx != string::npos) ? lfilepath.substr(lidx+1) : lfilepath;
162 
163 // stringstream lmsg;
164 // lmsg << "'" << xcond_msg << "'"
165 // << " failed at " << xi_msg << " = " << xi
166 // << " in file " << lfilename << " at line " << xline;
167 
168 // print_output_message("CONTRACT VIOLATION", lmsg.str());
169 // throw std::logic_error(lmsg.str());
170 // }
171 // };
172 
173 
174 void
175 sheaf::
176 check_contract(bool xcond, const char* xcond_msg, int xi, const char* xi_msg, const char* xfunction_name, const char* xfile, int xline)
177 {
178  if(!xcond)
179  {
180  string lfilepath(xfile);
181  string::size_type lidx = lfilepath.find_last_of("/\\");
182  string lfilename = (lidx != string::npos) ? lfilepath.substr(lidx+1) : lfilepath;
183 
184  stringstream lmsg;
185  lmsg << "'" << xcond_msg << "'"
186  << " failed at " << xi_msg << " = " << xi
187  << " in function " << xfunction_name << " in file " << lfilename << " at line " << xline;
188 
189  print_output_message("CONTRACT VIOLATION", lmsg.str());
190  throw std::logic_error(lmsg.str());
191  }
192 };
193 
194 // void
195 // sheaf::
196 // post_there_exists_failed(const char* xcond_msg, int xi, const char* xi_msg, int xmin, int xub, const char* xfile, int xline)
197 // {
198 // string lfilepath(xfile);
199 // string::size_type lidx = lfilepath.find_last_of("/\\");
200 // string lfilename = (lidx != string::npos) ? lfilepath.substr(lidx+1) : lfilepath;
201 
202 // stringstream lmsg;
203 // lmsg << "'" << xcond_msg << "'"
204 // << " not true for any " << xi_msg << " in [" << xmin << ", " << xub << ")"
205 // << " in file " << lfilename << " at line " << xline;
206 
207 // print_output_message("CONTRACT VIOLATION", lmsg.str());
208 // throw std::logic_error(lmsg.str());
209 // };
210 
211 void
212 sheaf::
213 post_there_exists_failed(const char* xcond_msg, int xi, const char* xi_msg, int xmin, int xub,
214  const char* xfunction_name, const char* xfile, int xline)
215 {
216  string lfilepath(xfile);
217  string::size_type lidx = lfilepath.find_last_of("/\\");
218  string lfilename = (lidx != string::npos) ? lfilepath.substr(lidx+1) : lfilepath;
219 
220  stringstream lmsg;
221  lmsg << "'" << xcond_msg << "'"
222  << " not true for any " << xi_msg << " in [" << xmin << ", " << xub << ")"
223  << " in function " << xfunction_name << " in file " << lfilename << " at line " << xline;
224 
225  print_output_message("CONTRACT VIOLATION", lmsg.str());
226  throw std::logic_error(lmsg.str());
227 };
228 
229 // void
230 // sheaf::
231 // post_unimplemented(const char* xcond_msg, const char* xfile, int xline)
232 // {
233 // string lfilepath(xfile);
234 // string::size_type lidx = lfilepath.find_last_of("/\\");
235 // string lfilename = (lidx != string::npos) ? lfilepath.substr(lidx+1) : lfilepath;
236 
237 // stringstream lmsg;
238 // lmsg << "Function in file " << lfilename << " at line " << xline << " " << xcond_msg;
239 
240 // print_output_message("UNIMPLEMENTED", lmsg.str());
241 // throw std::logic_error(lmsg.str());
242 // };
243 
244 void
245 sheaf::
246 post_unimplemented(const char* xcond_msg, const char* xfunction_name, const char* xfile, int xline)
247 {
248  string lfilepath(xfile);
249  string::size_type lidx = lfilepath.find_last_of("/\\");
250  string lfilename = (lidx != string::npos) ? lfilepath.substr(lidx+1) : lfilepath;
251 
252  stringstream lmsg;
253  lmsg << "Function " << xfunction_name << " in file " << lfilename << " at line " << xline << " " << xcond_msg;
254 
255  print_output_message("UNIMPLEMENTED", lmsg.str());
256  throw std::logic_error(lmsg.str());
257 };
STL namespace.
SHEAF_DLL_SPEC void post_there_exists_failed(const char *xcond_msg, int xi, const char *xi_msg, int xmin, int xub, const char *xfunction_name, const char *xfile, int xline)
Throws exception for existential quantification failure.
Namespace for the sheaves component of the sheaf system.
SHEAF_DLL_SPEC void check_contract(bool xcond, const char *xmsg, const char *xfunction_name, const char *xfile, int xline)
Tests condition xcond and throws exception if false; exception message includes xmsg, file name xfile, and line number xline.
SHEAF_DLL_SPEC void post_unimplemented(const char *xcond_msg, const char *xfunction_name, const char *xfile, int xline)
Throws exception for attempt to invoke is_abstract or not implemented.