File: mbl_exception.h

package info (click to toggle)
vxl 1.17.0.dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 153,280 kB
  • ctags: 105,123
  • sloc: cpp: 747,420; ansic: 209,130; fortran: 34,230; lisp: 14,915; sh: 6,187; python: 5,856; makefile: 340; perl: 294; xml: 160
file content (377 lines) | stat: -rw-r--r-- 11,762 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#ifndef mbl_exception_h_
#define mbl_exception_h_
//:
// \file
// \brief Exceptions thrown by mbl, and a mechanism for turning them off.
// \author Ian Scott.

#include <vcl_string.h>
#include <vcl_cstdlib.h>
#include <vcl_iostream.h>
#if VCL_HAS_EXCEPTIONS
# include <vcl_stdexcept.h>
#endif


//: Throw an exception indicating a real problem.
// If exceptions have been disabled, this function
// may abort.
template <class T>
void mbl_exception_error(T exception)
{
  vcl_cerr << "\nERROR: " << exception.what() << vcl_endl;
#if !defined MBL_EXCEPTIONS_DISABLE  && VCL_HAS_EXCEPTIONS
  throw exception;
#else
  vcl_abort();
#endif
}

//: Throw an exception indicating a potential problem.
// If exceptions have been disabled, this function
// may return.
template <class T>
void mbl_exception_warning(T exception)
{
  vcl_cerr << "\nWARNING: " << exception.what() << vcl_endl;
#if !defined MBL_EXCEPTIONS_DISABLE  && VCL_HAS_EXCEPTIONS
  throw exception;
#endif
}


#if !VCL_HAS_EXCEPTIONS

  //: Indicates that mbl_cloneables_factory has not heard of value name.
  class mbl_exception_no_name_in_factory
  {
    vcl_string msg_;
   public:
    mbl_exception_no_name_in_factory(vcl_string failed_name, vcl_string valid_names)
      : msg_(vcl_string("No such value: ")+failed_name+"\nValid values are: ["+valid_names+"]") {}
    const char * what() const {return msg_.c_str();}
  };

#else

  //: Indicates that mbl_cloneables_factory has not heard of value name.
  class mbl_exception_no_name_in_factory : public vcl_logic_error
  {
   public:
    vcl_string failed_value, valid_values;
    mbl_exception_no_name_in_factory(const vcl_string& failed_name, const vcl_string& valid_names):
      vcl_logic_error(vcl_string("No such value: ") +failed_name + "\nValid values are: ["+valid_names+"]"),
        failed_value(failed_name), valid_values(valid_names) {}
    virtual ~mbl_exception_no_name_in_factory() throw() {}
  };

#endif


#if !VCL_HAS_EXCEPTIONS

  //: General purpose - a replacement for vcl_abort.
  // The only point of catching this exception, is to
  // give you a chance to save your data. If this exception
  // is thrown, then the program correctness is in doubt.
  class mbl_exception_abort
  {
    vcl_string msg_;
   public:
    mbl_exception_abort(const vcl_string& comment);
    const char * what() const {return msg_.c_str();}
  };

#else

  //: General purpose - a replacement for vcl_abort.
  // The only point of catching this exception, is to
  // give you a chance to save your data. If this exception
  // is thrown, then the program correctness is in doubt.
  class mbl_exception_abort : public vcl_logic_error
  {
   public:
    mbl_exception_abort(const vcl_string& comment);
    virtual ~mbl_exception_abort() throw() {}
  };

#endif


#if !VCL_HAS_EXCEPTIONS

  //: Indicates a problem whilst parsing text configuration data.
  class mbl_exception_parse_error
  {
    vcl_string msg_;
   public:
    mbl_exception_parse_error(const vcl_string &msg)
      : msg_(msg) {}
    const char * what() const {return msg_.c_str();}
  };

#else

  //: Indicates a problem whilst parsing text configuration data.
  class mbl_exception_parse_error: public vcl_runtime_error
  {
   public:
    mbl_exception_parse_error(const vcl_string &msg)
      : vcl_runtime_error(msg) {}
    virtual ~mbl_exception_parse_error() throw() {}
  };

#endif

#if !VCL_HAS_EXCEPTIONS

  //: Indicates a problem whilst parsing a file.
  class mbl_exception_parse_file_error
  {
    vcl_string msg_;
    vcl_string filename_;
   public:
     mbl_exception_parse_file_error(const vcl_string &msg, const vcl_string& filename)
      : msg_(msg+" "+filename), filename_(filename) {}
    const char * what() const {return msg_.c_str();}
    const char * filename() const {return filename_.c_str();}
  };

#else

  //: Indicates a problem whilst parsing a file.
  class mbl_exception_parse_file_error: public mbl_exception_parse_error
  {
    vcl_string filename_;
   public:
     mbl_exception_parse_file_error(const vcl_string &msg, const vcl_string& filename):
       mbl_exception_parse_error(filename.empty() ? msg : msg+" in "+filename), filename_(filename) {}
    const char * filename() const {return filename_.c_str();}
    virtual ~mbl_exception_parse_file_error() throw() {}
  };

#endif

#if !VCL_HAS_EXCEPTIONS

  //: Data from two sources or files was inconsistent.
  class mbl_exception_inconsistent_external_data
  {
    vcl_string msg_;
    vcl_string source1_, source2_;
   public:
     mbl_exception_inconsistent_external_data(const vcl_string &msg,
       const vcl_string& source1, const vcl_string& source2)
      : msg_(source1.empty() && source2.empty() ? msg : msg+" between "+source1+" and "+source2),
        source1_(source1), source2_(source2) {}
    const char * what() const {return msg_.c_str();}
    const char * source1() const {return source1_.c_str();}
    const char * source2() const {return source2_.c_str();}
  };

#else

  //: Data from two sources or files was inconsistent.
  // This is distinct from a parse error, which can be used when the data within
  // in a single config file is inconsistent. This is most useful at algorithm run time,
  // when there are several external data files, and some external agent has broken the
  // consistency invariant by modifying one of them. The application can then report that
  // someone has messed up its data.
  class mbl_exception_inconsistent_external_data: public vcl_runtime_error
  {
    vcl_string source1_, source2_;
   public:
    mbl_exception_inconsistent_external_data(const vcl_string &msg,
      const vcl_string& source1 = "", const vcl_string& source2 = "")
      : vcl_runtime_error( source1.empty() && source2.empty()
          ? msg : msg+" between "+source1+" and "+source2),
        source1_(source1), source2_(source2) {}
    const char * source1() const {return source1_.c_str();}
    const char * source2() const {return source2_.c_str();}
    virtual ~mbl_exception_inconsistent_external_data() throw() {}
  };

#endif


#if !VCL_HAS_EXCEPTIONS

  //: Indicates that an expected property label was missing.
  class mbl_exception_missing_property
  {
    vcl_string msg_;
   public:
    mbl_exception_missing_property(const vcl_string &missing)
      : msg_(vcl_string("Couldn't find expected property label: \""+missing+'\"')) {}
    const char * what() const {return msg_.c_str();}
  };

#else

  //: Indicates a problem whilst parsing text configuration data.
  class mbl_exception_missing_property: public mbl_exception_parse_error
  {
   public:
   vcl_string missing_label;
    mbl_exception_missing_property(const vcl_string &missing)
      : mbl_exception_parse_error(
          vcl_string("Couldn't find expected property label: \""+missing+'\"')),
        missing_label(missing)
    {}
    virtual ~mbl_exception_missing_property() throw() {}
  };

#endif

#if !VCL_HAS_EXCEPTIONS

  //: Indicates that mbl_exception_look_for_unused_props found some unused properties.
  class mbl_exception_unused_props
  {
    vcl_string msg_;
   public:
    mbl_exception_unused_props(const vcl_string &function_name, const vcl_string &unused_props)
      : msg_(function_name + ": Unused properties found:\n" + unused_props) {}
    const char * what() const {return msg_.c_str();}
  };

#else

  //: Indicates that mbl_exception_look_for_unused_props found some unused properties.
  class mbl_exception_unused_props : public mbl_exception_parse_error
  {
   public:
    vcl_string function_name, unused_properties;
    mbl_exception_unused_props(const vcl_string &fn_name, const vcl_string &unused_props)
      : mbl_exception_parse_error(fn_name + ": Unused properties found:\n" + unused_props),
        function_name(fn_name), unused_properties(unused_props) {}
    virtual ~mbl_exception_unused_props() throw() {}
  };

#endif


#if !VCL_HAS_EXCEPTIONS

  //: Indicates a problem whilst parsing text configuration data into an mbl_read_props object.
  class mbl_exception_read_props_parse_error
  {
    vcl_string msg_;
   public:
    mbl_exception_read_props_parse_error(const vcl_string &msg)
      : msg_(vcl_string("mbl_read_props: ") + msg) {}
    const char * what() const {return msg_.c_str();}
  };

#else

  //: Indicates a problem whilst parsing text configuration data into an mbl_read_props object.
  class mbl_exception_read_props_parse_error: public mbl_exception_parse_error
  {
   public:
    mbl_exception_read_props_parse_error(const vcl_string &msg)
      : mbl_exception_parse_error(vcl_string("mbl_read_props: ") + msg) {}
    virtual ~mbl_exception_read_props_parse_error() throw() {}
  };

#endif

#if !VCL_HAS_EXCEPTIONS

  //: Indicates a problem whilst parsing a block of text configuration data.
  class mbl_exception_parse_block_parse_error
  {
    vcl_string msg_;
   public:
    mbl_exception_parse_block_parse_error(const vcl_string &msg,
      const vcl_string &contents)
    : msg_(vcl_string("mbl_parse_block: ") + msg +
      "Contents of block:\n" + contents) {}
    const char * what() const {return msg_.c_str();}
  };

#else

  //: Indicates a problem whilst parsing a block of text configuration data.
  class mbl_exception_parse_block_parse_error: public mbl_exception_parse_error
  {
   public:
    //: Description of problem
    vcl_string msg;
    //: Contents of string which failed to be parsed.
    vcl_string contents;
    mbl_exception_parse_block_parse_error(const vcl_string &msg,
      const vcl_string &contents)
    : mbl_exception_parse_error(vcl_string("mbl_parse_block: ") + msg +
      "Contents of block:\n" + contents), msg(msg), contents(contents) {}
    virtual ~mbl_exception_parse_block_parse_error() throw() {}
  };

#endif

//:Throw mbl_exception_os_error or one of its derivatives, based on errno.
// If exceptions are disabled, this behaves like mbl_exception_warning() above.
void mbl_exception_throw_os_error(const vcl_string& filename,
                                  const vcl_string& additional_comment="");


#if !VCL_HAS_EXCEPTIONS

  //: Indicates a problem reported during an OS call.
  class mbl_exception_os_error
  {
    vcl_string msg_;
   public:
    //: Reported errno
    int errno;
    //: System supplied error message.
    vcl_string error_message;
    //: Filename or pathname or other id on which OS call failed.
    vcl_string filename;
    //: Optional additional comments.
    vcl_string additional_comment;
    mbl_exception_os_error(int err_no, const vcl_string &file_name,
      const vcl_string &comment="");
    virtual ~mbl_exception_os_error() throw() {}
    const char * what() const {return msg_.c_str();}
  };

#else

  //: Indicates a problem reported during an OS call.
  class mbl_exception_os_error: public vcl_runtime_error
  {
   public:
    //: Reported errno
    int err_no;
    //: System supplied error message.
    vcl_string error_message;
    //: Filename or pathname or other id on which OS call failed.
    vcl_string filename;
    //: Optional additional comments.
    vcl_string additional_comment;
    mbl_exception_os_error(int errnum, const vcl_string &file_name,
      const vcl_string &comment="");
    virtual ~mbl_exception_os_error() throw() {}
  };

#endif


#define MACRO( E ) \
class E : public mbl_exception_os_error{ public: \
  E (int err_no, const vcl_string &file_name, const vcl_string &comment=""): \
    mbl_exception_os_error(err_no, file_name, comment) {} }

MACRO(mbl_exception_os_no_such_file_or_directory);
MACRO(mbl_exception_os_permission_denied);
MACRO(mbl_exception_os_file_exists);
MACRO(mbl_exception_os_not_a_directory);
MACRO(mbl_exception_os_is_a_directory);
MACRO(mbl_exception_os_no_space_left_on_device);
MACRO(mbl_exception_os_invalid_value);

#undef MACRO

#endif // mbl_exception_h_