]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
3 | // $Id: error.h,v 1.1 1998/07/02 02:58:13 jgg Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | Global Erorr Class - Global error mechanism | |
7 | ||
8 | This class has a single global instance. When a function needs to | |
9 | generate an error condition, such as a read error, it calls a member | |
10 | in this class to add the error to a stack of errors. | |
11 | ||
12 | By using a stack the problem with a scheme like errno is removed and | |
13 | it allows a very detailed account of what went wrong to be transmitted | |
14 | to the UI for display. (Errno has problems because each function sets | |
15 | errno to 0 if it didn't have an error thus eraseing erno in the process | |
16 | of cleanup) | |
17 | ||
18 | Several predefined error generators are provided to handle common | |
19 | things like errno. The general idea is that all methods return a bool. | |
20 | If the bool is true then things are OK, if it is false then things | |
21 | should start being undone and the stack should unwind under program | |
22 | control. | |
23 | ||
24 | A Warning should not force the return of false. Things did not fail, but | |
25 | they might have had unexpected problems. Errors are stored in a FIFO | |
26 | so Pop will return the first item.. | |
27 | ||
28 | I have some thoughts about extending this into a more general UI<-> | |
29 | Engine interface, ie allowing the Engine to say 'The disk is full' in | |
30 | a dialog that says 'Panic' and 'Retry'.. The error generator functions | |
31 | like errno, Warning and Error return false always so this is normal: | |
32 | if (open(..)) | |
33 | return _error->Errno(..); | |
34 | ||
35 | This source is placed in the Public Domain, do with it what you will | |
36 | It was originally written by Jason Gunthorpe. | |
37 | ||
38 | ##################################################################### */ | |
39 | /*}}}*/ | |
40 | // Header section: pkglib | |
41 | #ifndef PKGLIB_ERROR_H | |
42 | #define PKGLIB_ERROR_H | |
43 | ||
44 | #include <string> | |
45 | #include <vector.h> | |
46 | ||
47 | class GlobalError | |
48 | { | |
49 | struct Item | |
50 | { | |
51 | string Text; | |
52 | bool Error; | |
53 | }; | |
54 | ||
55 | vector<Item> List; | |
56 | bool PendingFlag; | |
57 | ||
58 | public: | |
59 | ||
60 | // Call to generate an error from a library call. | |
61 | bool Errno(const char *Function,const char *Description,...); | |
62 | ||
63 | /* A warning should be considered less severe than an error, and may be | |
64 | ignored by the client. */ | |
65 | bool Error(const char *Description,...); | |
66 | bool Warning(const char *Description,...); | |
67 | ||
68 | // Simple accessors | |
69 | inline bool PendingError() {return PendingFlag;}; | |
70 | inline bool empty() {return List.empty();}; | |
71 | bool PopMessage(string &Text); | |
72 | void Discard() {List.erase(List.begin(),List.end()); PendingFlag = false;}; | |
73 | ||
74 | // Usefull routine to dump to cerr | |
75 | void DumpErrors(); | |
76 | ||
77 | GlobalError(); | |
78 | }; | |
79 | ||
80 | /* The 'extra-ansi' syntax is used to help with collisions. This is the | |
81 | single global instance of this class. */ | |
82 | extern GlobalError *_error; | |
83 | ||
84 | #endif |