diagnostic.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ZORBA_DIAGNOSTIC_API_H
18 #define ZORBA_DIAGNOSTIC_API_H
19 
20 #include <cstring>
21 #include <iostream>
22 
23 #include <zorba/config.h>
24 
25 namespace zorba {
26 namespace diagnostic {
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 
30 /**
31  * A %QName is the abstract base class for a QName.
32  */
33 class ZORBA_DLL_PUBLIC QName {
34 public:
35  virtual ~QName();
36 
37  /**
38  * Gets this QName's namespace URI.
39  *
40  * @return Returns said URI.
41  */
42  virtual char const* ns() const = 0;
43 
44  /**
45  * Gets this QName's prefix.
46  *
47  * @return Returns said prefix.
48  */
49  virtual char const* prefix() const = 0;
50 
51  /**
52  * Gets this QName's local name.
53  *
54  * @return Returns said local name.
55  */
56  virtual char const* localname() const = 0;
57 };
58 
59 /**
60  * Emits a QName to an ostream.
61  *
62  * @param o The ostream to emit to.
63  * @param qn The QName to emit.
64  * @return Returns \a o.
65  */
66 ZORBA_DLL_PUBLIC
67 std::ostream& operator<<( std::ostream &o, QName const &qn );
68 
69 /**
70  * Compares two QNames for equality.
71  *
72  * @param q1 The first QName.
73  * @param q2 The second QName.
74  * @return Returns \c true only if the QNames' namespaces and local names are
75  * equal.
76  */
77 ZORBA_DLL_PUBLIC
78 bool operator==( QName const &q1, QName const &q2 );
79 
80 /**
81  * Compares two QNames for equality.
82  *
83  * @param q1 The first QName.
84  * @param q2 The second QName. It can be in Clark notation,
85  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
86  * case the namespaces and local-names are compared; or as
87  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
88  * and local-names are compared.
89  * @return Returns \c true only if the QNames are equal.
90  */
91 ZORBA_DLL_PUBLIC
92 bool operator==( QName const &q1, char const *q2 );
93 
94 /**
95  * Compares two QNames for equality.
96  *
97  * @param q1 The first QName. It can be in Clark notation,
98  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
99  * case the namespaces and local-names are compared; or as
100  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
101  * and local-names are compared.
102  * @param q2 The second QName.
103  * @return Returns \c true only if the QNames are equal.
104  */
105 inline bool operator==( char const *q1, QName const &q2 ) {
106  return q2 == q1;
107 }
108 
109 /**
110  * Compares two QNames for equality.
111  *
112  * @tparam StringType The string type of \a q2.
113  * @param q1 The first QName.
114  * @param q2 The second QName. It can be in Clark notation,
115  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
116  * case the namespaces and local-names are compared; or as
117  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
118  * and local-names are compared.
119  * @return Returns \c true only if the QNames are equal.
120  */
121 template<class StringType> inline
122 bool operator==( QName const &q1, StringType const &q2 ) {
123  return q1 == q2.c_str();
124 }
125 
126 /**
127  * Compares two QNames for equality.
128  *
129  * @tparam StringType The string type of \a q1.
130  * @param q1 The first QName. It can be in Clark notation,
131  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
132  * case the namespaces and local-names are compared; or as
133  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
134  * and local-names are compared.
135  * @param q2 The second QName.
136  * @return Returns \c true only if the QNames are equal.
137  */
138 template<class StringType> inline
139 bool operator==( StringType const &q1, QName const &q2 ) {
140  return q1.c_str() == q2;
141 }
142 
143 /**
144  * Compares two QNames for inequality.
145  *
146  * @param q1 The first QName.
147  * @param q2 The second QName.
148  * @return Returns \c true only if either the QNames' namespaces or local names
149  * are not equal.
150  */
151 inline bool operator!=( QName const &q1, QName const &q2 ) {
152  return !(q1 == q2);
153 }
154 
155 /**
156  * Compares two QNames for inequality.
157  *
158  * @param q1 The first QName.
159  * @param q2 The second QName. It can be in Clark notation,
160  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
161  * case the namespaces and local-names are compared; or as
162  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
163  * and local-names are compared.
164  * @return Returns \c true only if either the QNames' namespaces or local names
165  * are not equal.
166  */
167 inline bool operator!=( QName const &q1, char const *q2 ) {
168  return !(q1 == q2);
169 }
170 
171 /**
172  * Compares two QNames for inequality.
173  *
174  * @param q1 The first QName. It can be in Clark notation,
175  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
176  * case the namespaces and local-names are compared; or as
177  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
178  * and local-names are compared.
179  * @param q2 The second QName.
180  * @return Returns \c true only if either the QNames' namespaces or local names
181  * are not equal.
182  */
183 inline bool operator!=( char const *q1, QName const &q2 ) {
184  return !(q1 == q2);
185 }
186 
187 /**
188  * Compares two QNames for inequality.
189  *
190  * @tparam StringType The string type of \a q2.
191  * @param q1 The first QName.
192  * @param q2 The second QName. It can be in Clark notation,
193  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
194  * case the namespaces and local-names are compared; or as
195  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
196  * and local-names are compared.
197  * @return Returns \c true only if the QNames are not equal.
198  */
199 template<class StringType> inline
200 bool operator!=( QName const &q1, StringType const &q2 ) {
201  return q1 != q2.c_str();
202 }
203 
204 /**
205  * Compares two QNames for inequality.
206  *
207  * @tparam StringType The string type of \a q1.
208  * @param q1 The first QName. It can be in Clark notation,
209  * <code>{</code><em>namespace</em><code>}</code><em>local-name</em>, in which
210  * case the namespaces and local-names are compared; or as
211  * <em>prefix</em><code>:</code><em>local-name</em> in which case the prefixes
212  * and local-names are compared.
213  * @param q2 The second QName.
214  * @return Returns \c true only if the QNames are not equal.
215  */
216 template<class StringType> inline
217 bool operator!=( StringType const &q1, QName const &q2 ) {
218  return q1.c_str() != q2;
219 }
220 
221 ///////////////////////////////////////////////////////////////////////////////
222 
223 /**
224  * An diagnostic::category is the category of error.
225  */
226 enum category {
227  UNKNOWN_CATEGORY, // must have integer value of 0
228 
234  XQUERY_USER_DEFINED, // for fn:error()
235 
236  ZORBA_XQP, // Zorba XQuery Processor
237  ZORBA_API, // Zorba API
238  ZORBA_DDF, // Data Definition Facility
239  ZORBA_DEBUGGER, // Zorba Debugger
240  ZORBA_OS, // Operating System
243 
246 };
247 
248 /**
249  * Emits the given diagnostic::category to the given ostream.
250  *
251  * @param o The ostream to emit to.
252  * @param c The category to emit.
253  * @return Returns \a o.
254  */
255 ZORBA_DLL_PUBLIC
256 std::ostream& operator<<( std::ostream &o, category c );
257 
258 /**
259  * An diagnostic::kind is the kind of error.
260  * See: http://www.w3.org/TR/xquery-30/#id-kinds-of-errors
261  */
262 enum kind {
263  UNKNOWN_KIND, // must have integer value of 0
264 
265  /**
266  * A static error is an error that must be detected during the static
267  * analysis phase. A syntax error is an example of a static error.
268  */
270 
271  /**
272  * A dynamic error is an error that must be detected during the dynamic
273  * evaluation phase and may be detected during the static analysis phase.
274  * Numeric overflow is an example of a dynamic error.
275  */
277 
278  /**
279  * A type error may be raised during the static analysis phase or the dynamic
280  * evaluation phase.
281  *
282  * During the static analysis phase, a type error occurs when the static type
283  * of an expression does not match the expected type of the context in which
284  * the expression occurs.
285  *
286  * During the dynamic evaluation phase, a type error occurs when the dynamic
287  * type of a value does not match the expected type of the context in which
288  * the value occurs.
289  */
291 };
292 
293 /**
294  * Emits the given diagnostic::kind to the given ostream.
295  *
296  * @param o The ostream to emit to.
297  * @param k The kind to emit.
298  * @return Returns \a o.
299  */
300 ZORBA_DLL_PUBLIC
301 std::ostream& operator<<( std::ostream &o, kind k );
302 
303 ///////////////////////////////////////////////////////////////////////////////
304 
305 } // namespace diagnostic
306 } // namespace zorba
307 
308 #include <zorba/internal/qname.h>
309 
310 namespace zorba {
311 
312 ///////////////////////////////////////////////////////////////////////////////
313 
314 /**
315  * A %Diagnostic is the base class for all Zorba diagnostics (errors and
316  * warnings).
317  */
318 class ZORBA_DLL_PUBLIC Diagnostic {
319 public:
320  /**
321  * Gets the QName for this diagnostic.
322  *
323  * @return Returns said QName.
324  */
325  virtual diagnostic::QName const& qname() const = 0;
326 
327  /**
328  * Gets the category of this diagnostic.
329  *
330  * @return Returns said category.
331  */
332  virtual diagnostic::category category() const;
333 
334  /**
335  * Gets the kind of this diagnostic.
336  *
337  * @return Returns said kind.
338  */
339  virtual diagnostic::kind kind() const;
340 
341  /**
342  * Gets the message of this diagnostic.
343  *
344  * @return Returns said message.
345  */
346  virtual char const* message() const;
347 
348 protected:
349  virtual ~Diagnostic();
350 
351  virtual Diagnostic const* clone() const = 0;
352 
353  /**
354  * Destroys a %Diagnostic. This is the only way a %Diagnostic should be
355  * destroyed.
356  */
357  virtual void destroy() const;
358 
359  // Only ZorbaException may call clone() and destroy().
360  friend class ZorbaException;
361 };
362 
363 /**
364  * Compares two diagnostics for equality.
365  *
366  * @param d1 The first diagnostic.
367  * @param d2 The second diagnostic.
368  * @return Returns \c true only if the diagnostics' QNames are equal.
369  */
370 inline bool operator==( Diagnostic const &d1, Diagnostic const &d2 ) {
371  return d1.qname() == d2.qname();
372 }
373 
374 /**
375  * Compares two diagnostics for inequality.
376  *
377  * @param d1 The first diagnostic.
378  * @param d2 The second diagnostic.
379  * @return Returns \c true only if the diagnostics' QNames are not equal.
380  */
381 inline bool operator!=( Diagnostic const &d1, Diagnostic const &d2 ) {
382  return !(d1 == d2);
383 }
384 
385 ///////////////////////////////////////////////////////////////////////////////
386 
387 } // namespace zorba
388 
390 
391 #endif /* ZORBA_DIAGNOSTIC_API_H */
392 /*
393  * Local variables:
394  * mode: c++
395  * End:
396  */
397 /* vim:set et sw=2 ts=2: */
blog comments powered by Disqus