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  * Copy-constructs a %ZorbaException.
48  *
49  * @param from The %ZorbaException to copy from.
50  */
51  ZorbaException( ZorbaException const &from );
52 
53  /**
54  * Destroys this %ZorbaException.
55  */
56  ~ZorbaException() throw();
57 
58  /**
59  * Assigns this %ZorbaException from another.
60  *
61  * @param from The %ZorbaException to assign from.
62  * @return Returns \c *this.
63  */
64  ZorbaException& operator=( ZorbaException const &from );
65 
66  /**
67  * Gets the diagnostic carried by this exception.
68  *
69  * @return Returns said diagnostic.
70  */
71  Diagnostic const& diagnostic() const throw() {
72  return *diagnostic_;
73  }
74 
75  /**
76  * Throws itself polymorphically; see
77  * http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.16
78  */
79  virtual void polymorphic_throw() const;
80 
81  /**
82  * Sets the diagnostic.
83  *
84  * @param diagnostic The diagnostic.
85  */
86  void set_diagnostic( Diagnostic const &diagnostic ) throw() {
87  diagnostic_ = &diagnostic;
88  }
89 
90  /**
91  * Gets the C++ source-code file name whence this exception was raised.
92  *
93  * @return Returns said file name.
94  */
95  char const* raise_file() const throw() {
96  return raise_file_.c_str();
97  }
98 
99  /**
100  * Gets the C++ source-code line number whence this exception was raised.
101  *
102  * @return Returns said line number.
103  */
104  line_type raise_line() const throw() {
105  return raise_line_;
106  }
107 
108  // inherited
109  char const* what() const throw();
110 
111 protected:
112  /**
113  * Constructs a %ZorbaException.
114  *
115  * @param diagnostic The diagnostic.
116  * @param raise_file The C++ source-code file name whence the exception was
117  * raised.
118  * @param raise_line The C++ source-code line number whence the exception was
119  * raised.
120  * @param message The diagnostic message.
121  */
122  ZorbaException( Diagnostic const &diagnostic, char const *raise_file,
123  line_type raise_line, char const *message );
124 
125  /**
126  * Clones this exception object.
127  *
128  * @return Returns a clone of this exception.
129  */
130  virtual std::unique_ptr<ZorbaException> clone() const;
131 
132  /**
133  * Prints the exception to the given ostream.
134  *
135  * @param o The ostream to print to.
136  * @return Returns \a o.
137  */
138  virtual std::ostream& print( std::ostream &o ) const;
139 
140  friend std::ostream& operator<<( std::ostream&, ZorbaException const& );
141 
142 private:
143  Diagnostic const *diagnostic_;
144  std::string raise_file_;
145  line_type raise_line_;
146  std::string message_;
147 
148  friend std::unique_ptr<ZorbaException> clone( ZorbaException const& );
149 
150  friend ZorbaException make_zorba_exception(
151  char const*, line_type, Diagnostic const&,
152  internal::diagnostic::parameters const&
153  );
154 
155  friend ZorbaException* new_zorba_exception(
156  char const*, line_type, Diagnostic const&,
157  internal::diagnostic::parameters const&
158  );
159 
160 protected:
161  // for plan serialization
162  ZorbaException( serialization::Archiver& );
163  friend void serialization::operator&( serialization::Archiver&,
164  ZorbaException*& );
165 };
166 
167 /**
168  * Emits the given ZorbaException to the given ostream.
169  *
170  * @param o The ostream to emit to.
171  * @param e The ZorbaException to emit.
172  * @return Returns \a o.
173  */
174 inline std::ostream& operator<<( std::ostream &o, ZorbaException const &e ) {
175  return e.print( o );
176 }
177 
178 ///////////////////////////////////////////////////////////////////////////////
179 
180 } // namespace zorba
181 #endif /* ZORBA_ZORBA_EXCEPTION_API_H */
182 /* vim:set et sw=2 ts=2: */
blog comments powered by Disqus