zorba_exception.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2008 The FLWOR Foundation.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef ZORBA_ZORBA_EXCEPTION_API_H
00018 #define ZORBA_ZORBA_EXCEPTION_API_H
00019 
00020 #include <iostream>
00021 #include <exception>
00022 #include <string>
00023 
00024 #include <zorba/config.h>
00025 #include <zorba/internal/unique_ptr.h>
00026 #include <zorba/error.h>
00027 
00028 namespace zorba {
00029 
00030 class ZorbaException;
00031 
00032 namespace serialization {
00033   class Archiver;
00034   void operator&( Archiver&, ZorbaException*& );
00035 }
00036 
00037 //////////////////////////////////////////////////////////////////////////////
00038 
00039 /**
00040  * A %ZorbaException is the base class for all Zorba exceptions.
00041  */
00042 class ZORBA_DLL_PUBLIC ZorbaException : public std::exception {
00043 public:
00044   typedef internal::diagnostic::location::line_type line_type;
00045 
00046   /**
00047    * Copy-constructs a %ZorbaException.
00048    *
00049    * @param from The %ZorbaException to copy from.
00050    */
00051   ZorbaException( ZorbaException const &from );
00052 
00053   /**
00054    * Destroys this %ZorbaException.
00055    */
00056   ~ZorbaException() throw();
00057 
00058   /**
00059    * Assigns this %ZorbaException from another.
00060    *
00061    * @param from The %ZorbaException to assign from.
00062    * @return Returns \c *this.
00063    */
00064   ZorbaException& operator=( ZorbaException const &from );
00065 
00066   /**
00067    * Gets the diagnostic carried by this exception.
00068    *
00069    * @return Returns said diagnostic.
00070    */
00071   Diagnostic const& diagnostic() const throw() {
00072     return *diagnostic_;
00073   }
00074 
00075   /**
00076    * Throws itself polymorphically; see
00077    * http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.16
00078    */
00079   virtual void polymorphic_throw() const;
00080 
00081   /**
00082    * Sets the diagnostic.
00083    *
00084    * @param diagnostic The diagnostic.
00085    */
00086   void set_diagnostic( Diagnostic const &diagnostic ) throw() {
00087     diagnostic_ = &diagnostic;
00088   }
00089 
00090   /**
00091    * Gets the C++ source-code file name whence this exception was raised.
00092    *
00093    * @return Returns said file name.
00094    */
00095   char const* raise_file() const throw() {
00096     return raise_file_.c_str();
00097   }
00098 
00099   /**
00100    * Gets the C++ source-code line number whence this exception was raised.
00101    *
00102    * @return Returns said line number.
00103    */
00104   line_type raise_line() const throw() {
00105     return raise_line_;
00106   }
00107 
00108   // inherited
00109   char const* what() const throw();
00110 
00111 protected:
00112   /**
00113    * Constructs a %ZorbaException.
00114    *
00115    * @param diagnostic The diagnostic.
00116    * @param raise_file The C++ source-code file name whence the exception was
00117    * raised.
00118    * @param raise_line The C++ source-code line number whence the exception was
00119    * raised.
00120    * @param message The diagnostic message.
00121    */
00122   ZorbaException( Diagnostic const &diagnostic, char const *raise_file,
00123                   line_type raise_line, char const *message );
00124 
00125   /**
00126    * Clones this exception object.
00127    *
00128    * @return Returns a clone of this exception.
00129    */
00130   virtual std::unique_ptr<ZorbaException> clone() const;
00131 
00132   /**
00133    * Prints the exception to the given ostream.
00134    *
00135    * @param o The ostream to print to.
00136    * @return Returns \a o.
00137    */
00138   virtual std::ostream& print( std::ostream &o ) const;
00139 
00140   friend std::ostream& operator<<( std::ostream&, ZorbaException const& );
00141 
00142 private:
00143   Diagnostic const *diagnostic_;
00144   std::string raise_file_;
00145   line_type raise_line_;
00146   std::string message_;
00147 
00148   friend std::unique_ptr<ZorbaException> clone( ZorbaException const& );
00149 
00150   friend ZorbaException make_zorba_exception(
00151     char const*, line_type, Diagnostic const&,
00152     internal::diagnostic::parameters const&
00153   );
00154 
00155   friend ZorbaException* new_zorba_exception(
00156     char const*, line_type, Diagnostic const&,
00157     internal::diagnostic::parameters const&
00158   );
00159 
00160 protected:
00161   // for plan serialization
00162   ZorbaException( serialization::Archiver& );
00163   friend void serialization::operator&( serialization::Archiver&,
00164                                         ZorbaException*& );
00165 };
00166 
00167 /**
00168  * Emits the given ZorbaException to the given ostream.
00169  *
00170  * @param o The ostream to emit to.
00171  * @param e The ZorbaException to emit.
00172  * @return Returns \a o.
00173  */
00174 inline std::ostream& operator<<( std::ostream &o, ZorbaException const &e ) {
00175   return e.print( o );
00176 }
00177 
00178 ///////////////////////////////////////////////////////////////////////////////
00179 
00180 } // namespace zorba
00181 #endif /* ZORBA_ZORBA_EXCEPTION_API_H */
00182 /* vim:set et sw=2 ts=2: */
blog comments powered by Disqus