library of assembled shared sources

http://lass.cocamware.com

exception.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @class lass::util::Exception
00046  *  @brief type of all exceptions in lass
00047  *  @author Bram de Greve [Bramz]
00048  *
00049  *  @sa LASS_THROW
00050  */
00051 
00052 #ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_EXCEPTION_H
00053 #define LASS_GUARDIAN_OF_INCLUSION_UTIL_EXCEPTION_H
00054 
00055 #include "util_common.h"
00056 #include <exception>
00057 #include <string>
00058 #include <sstream>
00059 
00060 #define LASS_UTIL_EXCEPTION_PRIVATE_IMPL(t_exception)\
00061         virtual void doThrowSelf() const { throw *this; }\
00062         virtual ::std::auto_ptr< ::lass::util::experimental::RemoteExceptionBase > doClone() const \
00063         { \
00064             return ::std::auto_ptr< ::lass::util::experimental::RemoteExceptionBase >(new t_exception(*this)); \
00065         }\
00066     /**/
00067 
00068 namespace lass
00069 {
00070 namespace util
00071 {
00072 namespace experimental
00073 {
00074     class RemoteExceptionBase
00075     {
00076     public:
00077         virtual ~RemoteExceptionBase() {}
00078         void throwSelf() const { doThrowSelf(); }
00079         std::auto_ptr<RemoteExceptionBase> clone() const
00080         {
00081             std::auto_ptr<RemoteExceptionBase> copy = doClone();
00082             if (typeid(*copy) != typeid(*this))
00083             {
00084                 std::cerr << "[LASS RUN MSG] UNDEFINED BEHAVIOUR WARNING: Cloned exception has been sliced from '"
00085                     << typeid(*this).name() << "' to '" << typeid(*copy).name() << "'." << std::endl;
00086             }
00087             return copy;
00088         }
00089     private:
00090         virtual void doThrowSelf() const = 0;
00091         virtual std::auto_ptr<RemoteExceptionBase> doClone() const = 0;
00092     };
00093 
00094     template <typename LocalException>
00095     class RemoteExceptionWrapper:
00096         public LocalException,
00097         public RemoteExceptionBase
00098     {
00099     public:
00100         RemoteExceptionWrapper(const LocalException& e): LocalException(e) {}
00101         ~RemoteExceptionWrapper() throw() {}
00102     private:
00103         void doThrowSelf() const { throw *this; }
00104         std::auto_ptr<RemoteExceptionBase> doClone() const 
00105         { 
00106             return std::auto_ptr<RemoteExceptionBase>(new RemoteExceptionWrapper<LocalException>(*this)); 
00107         }
00108     };
00109 }
00110 
00111 
00112 
00113 class Exception: 
00114     public std::exception,
00115     public experimental::RemoteExceptionBase
00116 {
00117 public:
00118 
00119     Exception(const std::string& message, const std::string& location):
00120         std::exception(),
00121         message_(message),
00122         location_(location)
00123     {
00124     }
00125 
00126     explicit Exception(const std::string& message):
00127         std::exception(),
00128         message_(message),
00129         location_("no location")
00130     {
00131     }
00132 
00133     Exception():
00134         std::exception(),
00135         message_("no message"),
00136         location_("no location")
00137     {
00138     }
00139 
00140     ~Exception() throw() {}
00141 
00142     /** return message in STL style
00143      */
00144     virtual const char* what() const throw()
00145     {
00146         return message_.c_str();
00147     }
00148 
00149     /** return string with error message
00150      */
00151     const std::string& message() const
00152     {
00153         return message_;
00154     }
00155 
00156     /** return string with location info where exception is thrown
00157      */
00158     const std::string& location() const
00159     {
00160         return location_;
00161     }
00162 
00163 
00164 private:
00165 
00166     std::string message_;
00167     std::string location_;
00168 
00169     LASS_UTIL_EXCEPTION_PRIVATE_IMPL(Exception)
00170 };
00171 
00172 namespace experimental
00173 {
00174 
00175 template <typename ExceptionType, typename ParentType = Exception>
00176 class ExceptionMixin: public ParentType
00177 {
00178 public:
00179     ExceptionMixin(const std::string& msg, const std::string& loc): ParentType(msg, loc) {}
00180 private:
00181     virtual void doThrowSelf() const 
00182     {
00183         //LASS_ASSERT(typeid(*this) == typeid(ExceptionType));
00184         throw *static_cast<const ExceptionType*>(this); 
00185     }
00186     virtual ::std::auto_ptr<RemoteExceptionBase> doClone() const
00187     { 
00188         //LASS_ASSERT(typeid(*this) == typeid(ExceptionType));
00189         return ::std::auto_ptr<RemoteExceptionBase>(new ExceptionType(*static_cast<const ExceptionType*>(this)));
00190     }
00191 };
00192 
00193 }
00194 
00195 
00196 
00197 class KeyError: public experimental::ExceptionMixin<KeyError>
00198 {
00199 public:
00200     KeyError(const std::string& msg, const std::string& loc): 
00201         experimental::ExceptionMixin<KeyError>(msg, loc) {}
00202 };
00203 
00204 
00205 class ValueError: public experimental::ExceptionMixin<ValueError>
00206 {
00207 public:
00208     ValueError(const std::string& msg, const std::string& loc): 
00209         experimental::ExceptionMixin<ValueError>(msg, loc) {}
00210 };
00211 
00212 }
00213 
00214 }
00215 
00216 /** @relates lass::util::Exception
00217  *  
00218  *  To throw an exception like util::Exception that accepts a message and a location string, you 
00219  *  can supply those strings yourself, ore you can use this macro for your convenience.
00220  *
00221  *  This macro will fill in the location by itself so that you only have to worry about the message.  
00222  *  And as an extra, the message is streamed, so you can use the regular @c operator<< to 
00223  *  concatenate variables in the message.
00224  *
00225  *  @code
00226  *  int foo = -5;
00227  *  if (foo < 0)
00228  *  {
00229  *      LASS_THROW_EX(util::Exception, "foo '" << foo << "' is less than zero");
00230  *  }
00231  *  @endcode
00232  */
00233 #define LASS_THROW_EX(t_exception, s_message)\
00234     do\
00235     {\
00236         ::std::ostringstream lassUtilExceptionImplBuffer;\
00237         lassUtilExceptionImplBuffer << s_message;\
00238         throw t_exception(lassUtilExceptionImplBuffer.str(), LASS_PRETTY_FUNCTION);\
00239     }\
00240     while (false)
00241 
00242 
00243 
00244 /** LASS_THROW_EX for ::lass::util::Exception
00245  *  @relates lass::Util::Exception
00246  *
00247  *  LASS_THROW(something) is identical to LASS_THROW_EX(util::Exception, something)
00248  */
00249 #define LASS_THROW(s_message) LASS_THROW_EX(::lass::util::Exception, s_message)
00250 
00251 
00252 
00253 /** @relates lass::Util::Exception
00254  *
00255  *  catches all exceptions and prints an undefined behaviour warning to std::cerr
00256  */
00257 #define LASS_CATCH_TO_WARNING\
00258     catch (const ::std::exception& error)\
00259     {\
00260         std::cerr << "[LASS RUN MSG] UNDEFINED BEHAVIOUR WARNING: Exception caught in "\
00261             << LASS_PRETTY_FUNCTION << ":\n" << error.what() << std::endl;\
00262     }\
00263     catch (...)\
00264     {\
00265         std::cerr << "[LASS RUN MSG] UNDEFINED BEHAVIOUR WARNING: Unknown exception caught in "\
00266             << LASS_PRETTY_FUNCTION << std::endl;\
00267     }\
00268     /**/
00269 
00270 #endif
00271 
00272 // EOF

Generated on Mon Nov 10 14:20:04 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo