NCBI C++ ToolKit
errors.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: errors.hpp 99402 2023-03-22 18:09:14Z satskyse $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Sergey Satskiy, NCBI
27  * Credits: Denis Vakatov, NCBI (help with the original design)
28  *
29  */
30 
31 
32 /** @file
33  * Various XML parser's and validator's errors related code for XmlWrapp.
34 **/
35 
36 #ifndef _xmlwrapp_errors_hpp_
37 #define _xmlwrapp_errors_hpp_
38 
39 // standard includes
40 #include <string>
41 #include <stdexcept>
42 #include <list>
43 
44 namespace xml {
45 
46 /**
47  * The xml::error_message class is used to store a single error message
48  * which may appear while parsing or validating an XML document.
49 **/
51 public:
52  /// A type for different type of errors
53  enum message_type {
54  type_fatal_error, ///< fatal error
55  type_error, ///< error
56  type_warning ///< warning
57  };
58 
59  /**
60  * Convert an error type to a string.
61  *
62  * @param mt The error type.
63  * @return The string representation of the error type.
64  **/
66 
67  /**
68  * Create a new xml::error_message object.
69  *
70  * @param message The error message.
71  * @param msg_type The error type.
72  * @param line file line realting to error message. 0 if none.
73  * @param filename file name if available or an empty string.
74  **/
75  error_message(const std::string& message,
76  message_type msg_type,
77  long line,
78  const std::string& filename);
79 
80  /**
81  * Get the error message type.
82  *
83  * @return The error type.
84  **/
85  message_type get_message_type (void) const;
86 
87  /**
88  * Get the error message.
89  *
90  * @return The error message.
91  **/
92  std::string get_message (void) const;
93 
94  /**
95  * Get the line number.
96  *
97  * @return The line number, 0 if not available.
98  **/
99  long get_line (void) const;
100 
101  /**
102  * Get the file name.
103  *
104  * @return The file name, empty string if not available.
105  **/
106  std::string get_filename (void) const;
107 
108  /**
109  * Moving constructor.
110  * @param other The other error_message.
111  **/
112  error_message (const error_message &&other);
113 
114  /**
115  * Moving assignment.
116  * @param other The other error_message.
117  **/
118  error_message& operator=(const error_message &&other);
119 
120  error_message (const error_message &other) = default;
121  error_message& operator=(const error_message &other) = default;
122 
123 private:
126  long line_;
128 };
129 
130 
131 
132 /**
133  * The xml::error_messages class is used to store all the error message
134  * which are collected while parsing or validating an XML document.
135 **/
137 {
138 public:
139  /// A type to store multiple messages
140  typedef std::list<error_message> error_messages_type;
141 
142  /**
143  * Get the error messages.
144  *
145  * @return The error messages.
146  **/
147  const error_messages_type& get_messages (void) const;
148 
149  /**
150  * Get the error messages.
151  *
152  * @return The error messages.
153  **/
155 
156  /**
157  * Check if there are warnings in the error messages.
158  *
159  * @return true if there is at least one warning in the error messages.
160  * It does not consider errors and fatal errors.
161  **/
162  bool has_warnings (void) const;
163 
164  /**
165  * Check if there are errors in the error messages.
166  *
167  * @return true if there is at least one error in the error messages.
168  * It does not consider fatal errors.
169  **/
170  bool has_errors (void) const;
171 
172  /**
173  * Check if there are fatal errors in the error messages.
174  *
175  * @return true if there is at least one fatal error in the error messages.
176  **/
177  bool has_fatal_errors (void) const;
178 
179  /**
180  * Convert error messages into a single printable string.
181  *
182  * @return string representation of the errors list ('\n' separated)
183  **/
184  std::string print (void) const;
185 
186  /**
187  * Appends the messages from the other container.
188  **/
189  void append_messages(const error_messages & other);
190 
191  error_messages() = default;
192  error_messages (const error_messages &other) = default;
193  error_messages & operator=(const error_messages &other) = default;
194 
195  #if !(defined(_MSC_VER) && _MSC_VER < 1900)
196  // Visual studio 2013 does not support this
197  error_messages (error_messages &&other) = default;
199  #endif
200 
201 private:
204 };
205 
206 
207 
208 
209 /**
210  * The xml::parser_exception class is used to store parsing and validating
211  * exception information.
212 **/
213 class parser_exception : public std::exception
214 {
215 public:
216 
217  /**
218  * Convert error messages into a printable C-style string.
219  *
220  * @return string representation of the errors list ('\n' separated).
221  **/
222  virtual const char* what() const noexcept;
223 
224  /**
225  * Get error messages.
226  *
227  * @return The error messages.
228  **/
229  const error_messages& get_messages(void) const;
230 
231  /**
232  * Create a new object using the given list of error messages.
233  *
234  * @param msgs The error messages.
235  **/
236  parser_exception(const error_messages& msgs);
237 
238  /**
239  * Destroy the object.
240  **/
241  virtual ~parser_exception() noexcept {}
242 
243  parser_exception (const parser_exception &other) = default;
244  parser_exception & operator=(const parser_exception &other) = default;
245 
246  #if !(defined(_MSC_VER) && _MSC_VER < 1900)
247  // Visual studio 2013 does not support this
248  parser_exception (parser_exception &&other) = default;
250  #endif
251 
252 private:
254  mutable std::string buffer; // used for list convertion in what()
255 };
256 
257 
258 /// A type for different approaches to process warnings
260  type_warnings_are_errors, ///< Treat warnings as errors
261  type_warnings_not_errors ///< Do not treat warnings as errors
262 };
263 
264 } // xml namespace
265 
266 #endif
The xml::error_message class is used to store a single error message which may appear while parsing o...
Definition: errors.hpp:50
std::string get_message(void) const
Get the error message.
Definition: errors.cpp:64
message_type
A type for different type of errors.
Definition: errors.hpp:53
@ type_fatal_error
fatal error
Definition: errors.hpp:54
@ type_warning
warning
Definition: errors.hpp:56
@ type_error
error
Definition: errors.hpp:55
error_message & operator=(const error_message &other)=default
error_message(const error_message &other)=default
message_type message_type_
Definition: errors.hpp:124
error_message & operator=(const error_message &&other)
Moving assignment.
Definition: errors.cpp:84
error_message(const std::string &message, message_type msg_type, long line, const std::string &filename)
Create a new xml::error_message object.
Definition: errors.cpp:53
std::string filename_
Definition: errors.hpp:127
std::string get_filename(void) const
Get the file name.
Definition: errors.cpp:73
static std::string message_type_str(message_type mt)
Convert an error type to a string.
Definition: errors.cpp:43
std::string message_
Definition: errors.hpp:125
long get_line(void) const
Get the line number.
Definition: errors.cpp:69
message_type get_message_type(void) const
Get the error message type.
Definition: errors.cpp:60
The xml::error_messages class is used to store all the error message which are collected while parsin...
Definition: errors.hpp:137
void append_messages(const error_messages &other)
Appends the messages from the other container.
Definition: errors.cpp:147
error_messages(error_messages &&other)=default
error_messages(const error_messages &other)=default
bool has_fatal_errors(void) const
Check if there are fatal errors in the error messages.
Definition: errors.cpp:126
const error_messages_type & get_messages(void) const
Get the error messages.
Definition: errors.cpp:98
std::list< error_message > error_messages_type
A type to store multiple messages.
Definition: errors.hpp:140
bool has_warnings(void) const
Check if there are warnings in the error messages.
Definition: errors.cpp:118
bool has_errors(void) const
Check if there are errors in the error messages.
Definition: errors.cpp:122
std::string print(void) const
Convert error messages into a single printable string.
Definition: errors.cpp:130
error_messages_type error_messages_
Definition: errors.hpp:202
bool has_messages_of_type(error_message::message_type type) const
Definition: errors.cpp:108
error_messages()=default
error_messages & operator=(error_messages &&other)=default
error_messages & operator=(const error_messages &other)=default
The xml::parser_exception class is used to store parsing and validating exception information.
Definition: errors.hpp:214
virtual const char * what() const noexcept
Convert error messages into a printable C-style string.
Definition: errors.cpp:160
parser_exception & operator=(parser_exception &&other)=default
error_messages messages_
Definition: errors.hpp:253
virtual ~parser_exception() noexcept
Destroy the object.
Definition: errors.hpp:241
parser_exception & operator=(const parser_exception &other)=default
parser_exception(const error_messages &msgs)
Create a new object using the given list of error messages.
Definition: errors.cpp:156
const error_messages & get_messages(void) const
Get error messages.
Definition: errors.cpp:165
parser_exception(const parser_exception &other)=default
std::string buffer
Definition: errors.hpp:254
parser_exception(parser_exception &&other)=default
string
Definition: cgiapp.hpp:687
XML library namespace.
Definition: attributes.hpp:57
warnings_as_errors_type
A type for different approaches to process warnings.
Definition: errors.hpp:259
@ type_warnings_not_errors
Do not treat warnings as errors.
Definition: errors.hpp:261
@ type_warnings_are_errors
Treat warnings as errors.
Definition: errors.hpp:260
Definition: type.c:6
Modified on Sat May 25 14:17:28 2024 by modify_doxy.py rev. 669887