zorba_exception.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_ZORBA_EXCEPTION_API_H
18 #define ZORBA_ZORBA_EXCEPTION_API_H
19 
20 #include <iostream>
21 #include <exception>
22 #include <string>
23 
24 #include <zorba/config.h>
26 #include <zorba/error.h>
27 
28 namespace zorba {
29 
30 class ZorbaException;
31 
32 namespace serialization {
33  class Archiver;
34  void operator&( Archiver&, ZorbaException*& );
35 }
36 
37 //////////////////////////////////////////////////////////////////////////////
38 
39 /**
40  * A %ZorbaException is the base class for all Zorba exceptions.
41  */
42 class ZORBA_DLL_PUBLIC ZorbaException : public std::exception {
43 public:
45 
46  /**
47  * The format to print exceptions as to an ostream.
48  */
49  enum print_format {
50  format_text = 0, ///< plain text
51  format_xml = 1, ///< XML without unnecessary whitespace
52  format_xml_indented = 3 ///< XML with newlines and indentation
53  };
54 
55  /**
56  * Copy-constructs a %ZorbaException.
57  *
58  * @param from The %ZorbaException to copy from.
59  */
60  ZorbaException( ZorbaException const &from );
61 
62  /**
63  * Destroys this %ZorbaException.
64  */
65  ~ZorbaException() throw();
66 
67  /**
68  * Assigns this %ZorbaException from another.
69  *
70  * @param from The %ZorbaException to assign from.
71  * @return Returns \c *this.
72  */
73  ZorbaException& operator=( ZorbaException const &from );
74 
75  /**
76  * Gets the diagnostic carried by this exception.
77  *
78  * @return Returns said diagnostic.
79  */
80  Diagnostic const& diagnostic() const throw() {
81  return *diagnostic_;
82  }
83 
84  /**
85  * Gets the current print_format associated with the given ostream.
86  *
87  * @param o The ostream to get the print_format of.
88  * @return Returns said print_format.
89  */
90  static print_format get_print_format( std::ostream &o ) {
91  return static_cast<print_format>( o.iword( get_ios_format_index() ) );
92  }
93 
94  /**
95  * Throws itself polymorphically; see
96  * http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.16
97  */
98  virtual void polymorphic_throw() const;
99 
100  /**
101  * Sets the diagnostic.
102  *
103  * @param diagnostic The diagnostic.
104  */
105  void set_diagnostic( Diagnostic const &diagnostic ) throw() {
106  diagnostic_ = &diagnostic;
107  }
108 
109  /**
110  * Gets the C++ source-code file name whence this exception was raised.
111  *
112  * @return Returns said file name.
113  */
114  char const* raise_file() const throw() {
115  return raise_file_.c_str();
116  }
117 
118  /**
119  * Gets the C++ source-code line number whence this exception was raised.
120  *
121  * @return Returns said line number.
122  */
123  line_type raise_line() const throw() {
124  return raise_line_;
125  }
126 
127  /**
128  * Sets the print_format of the given ostream.
129  *
130  * @param o The ostream to set the print_format of.
131  * @param f The print_format value.
132  */
133  static void set_print_format( std::ostream &o, print_format f ) {
134  o.iword( get_ios_format_index() ) = static_cast<long>( f );
135  }
136 
137  // inherited
138  char const* what() const throw();
139 
140 protected:
141  /**
142  * Constructs a %ZorbaException.
143  *
144  * @param diagnostic The diagnostic.
145  * @param raise_file The C++ source-code file name whence the exception was
146  * raised.
147  * @param raise_line The C++ source-code line number whence the exception was
148  * raised.
149  * @param message The diagnostic message.
150  */
151  ZorbaException( Diagnostic const &diagnostic, char const *raise_file,
152  line_type raise_line, char const *message );
153 
154  /**
155  * Clones this exception object.
156  *
157  * @return Returns a clone of this exception.
158  */
159  virtual std::unique_ptr<ZorbaException> clone() const;
160 
161  /**
162  * Prints the exception to the given ostream.
163  *
164  * @param o The ostream to print to.
165  * @return Returns \a o.
166  */
167  std::ostream& print( std::ostream& ) const;
168 
169  virtual std::ostream& print_impl( std::ostream &o ) const;
170 
171 private:
172  Diagnostic const *diagnostic_;
173  std::string raise_file_;
174  line_type raise_line_;
175  std::string message_;
176 
177  static int get_ios_format_index();
178 
179  friend std::unique_ptr<ZorbaException> clone( ZorbaException const& );
180 
181  friend ZorbaException make_zorba_exception(
182  char const*, line_type, Diagnostic const&,
183  internal::diagnostic::parameters const&
184  );
185 
186  friend ZorbaException* new_zorba_exception(
187  char const*, line_type, Diagnostic const&,
188  internal::diagnostic::parameters const&
189  );
190 
191  friend std::ostream& operator<<( std::ostream&, ZorbaException const& );
192 
193 protected:
194  // for plan serialization
195  ZorbaException( serialization::Archiver& );
196  friend void serialization::operator&( serialization::Archiver&,
197  ZorbaException*& );
198 };
199 
200 ///////////////////////////////////////////////////////////////////////////////
201 
202 /**
203  * Emits the given ZorbaException to the given ostream.
204  *
205  * @param o The ostream to emit to.
206  * @param e The ZorbaException to emit.
207  * @return Returns \a o.
208  */
209 inline std::ostream& operator<<( std::ostream &o, ZorbaException const &e ) {
210  return e.print( o );
211 }
212 
213 /**
214  * Sets the format for the next ZorbaException that's printed.
215  *
216  * @param o The ostream to affect.
217  * @param f The print_format to use.
218  * @return Returns \a o.
219  */
220 inline std::ostream& operator<<( std::ostream &o,
223  return o;
224 }
225 
226 ///////////////////////////////////////////////////////////////////////////////
227 
228 } // namespace zorba
229 #endif /* ZORBA_ZORBA_EXCEPTION_API_H */
230 /* vim:set et sw=2 ts=2: */
std::ostream & print(std::ostream &) const
Prints the exception to the given ostream.
A ZorbaException is the base class for all Zorba exceptions.
static print_format get_print_format(std::ostream &o)
Gets the current print_format associated with the given ostream.
A Diagnostic is the base class for all Zorba diagnostics (errors and warnings).
Definition: diagnostic.h:325
unsigned line_type
The line-number type.
static void set_print_format(std::ostream &o, print_format f)
Sets the print_format of the given ostream.
char const * raise_file() const
Gets the C++ source-code file name whence this exception was raised.
line_type raise_line() const
Gets the C++ source-code line number whence this exception was raised.
void set_diagnostic(Diagnostic const &diagnostic)
Sets the diagnostic.
internal::diagnostic::location::line_type line_type
print_format
The format to print exceptions as to an ostream.
ZORBA_DLL_PUBLIC std::ostream & operator<<(std::ostream &os, const QueryLocation &aQuery)
void operator&(serialization::Archiver &, const Diagnostic *&)