VERB_code_2.2  2
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
error.h
Go to the documentation of this file.
1 /**
2  * \file error.h
3  * Used to work with user's exeptions.
4  * It creates stack of error messages that can be catched and outputed to the screen.
5  * Initial error is generated somewhere, and passed to called function, where mode description can be added. That is stack.
6 
7  * \todo Remove from THE CODE error class, output class, move enclosed classes out, remove variabilities in function collings, change all enum's to strings. In that case, probably, the code can be understanded.
8  *
9  * \author Developed under supervision of the PI Yuri Shprits
10  */
11 
12 
13 #ifndef ERROR_MESSAGES_H
14 #define ERROR_MESSAGES_H
15 
16 #include <string>
17 #include <vector>
18 #include <stdarg.h>
19 #include <stdio.h>
20 
21 using namespace std;
22 
23 /**
24  * Hold some information about error in the code.
25  */
26 class single_error {
27 public:
28  /// Error code.
29  string code;
30 
31  /// Error message.
32  string msg;
33 
34  /// Constructor.
35  single_error(string code) {
36  // store error code and empty message
37  this->code = code;
38  this->msg = "";
39  }
40 
41  /// Constructor.
42  single_error(string code, string msg) {
43  // store error code and message
44  this->code = code;
45  this->msg = msg;
46  }
47 };
48 
49 //typedef vector<error_msg> error_stack;
50 
51 /**
52  * Error message - stack of single_error s.
53  */
54 class error_msg {
55 public:
56  /// Stack of errors.
57  vector<single_error> errors_stack;
58 
59  /// Constructor.
60  error_msg();
61  /** Constructor.
62  * one message added to the stack.
63  */
64  error_msg(char * code) {
65  errors_stack.push_back(single_error(code));
66  }
67  //error_msg(string code, string msg) {
68  // errors_stack.push_back(single_error(code, msg));
69  //}
70 
71  /** Constructor.
72  * one message added to the stack.
73  */
74 // !!! error_msg(char * code, char * msg, ...) {
75  error_msg(const char * code, const char * msg, ...) {
76 
77  // converting msg and args into one message and add error in stack with this message
78 
79  int len;
80  va_list args;
81  va_start( args, msg );
82  char *buffer = new char[1024];
83 
84 //!!! len = _vscprintf( msg, args ) + 1;
85  len = vsnprintf(buffer, 1024, msg, args ) + 1;
86  if (len == 0) throw error_msg("OUTPUT", "Output does not work :-(");
87 
88  errors_stack.push_back(single_error(code, buffer));
89 
90  delete buffer;
91  }
92 
93  /**
94  * Add one error message to the stack.
95  */
97  errors_stack.push_back(err);
98  }
99 
100  /**
101  * Add one error message to the stack.
102  */
103  void add(string code) {
104  errors_stack.push_back(single_error(code));
105  }
106 
107  /**
108  * Add one error message to the stack.
109  */
110  void add(string code, string msg) {
111  errors_stack.push_back(single_error(code, msg));
112  }
113 
114  /**
115  * Return all messages as a text.
116  */
117  string what() {
118  unsigned i;
119  string result = "";
120  for (i = 0; i < errors_stack.size(); i++) {
121  // return all stack of errors as a string
122  result = result + errors_stack[i].code + ": " + errors_stack[i].msg + "\n";
123  }
124  return result;
125  }
126 
127 };
128 
129 #endif