exception.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2005, 2006, 2007, 2008 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_EXCEPTION_HH
00021 #define PALUDIS_GUARD_PALUDIS_EXCEPTION_HH 1
00022 
00023 #include <paludis/util/attributes.hh>
00024 #include <string>
00025 #include <exception>
00026 
00027 /** \file
00028  * Declaration for the Exception base class, the InternalError exception
00029  * class, the NameError class and related utilities.
00030  *
00031  * \ingroup g_exceptions
00032  *
00033  * \section Examples
00034  *
00035  * - None at this time.
00036  */
00037 
00038 namespace paludis
00039 {
00040     /**
00041      * Backtrace context class.
00042      *
00043      * \ingroup g_exceptions
00044      * \nosubgrouping
00045      */
00046     class PALUDIS_VISIBLE Context
00047     {
00048         private:
00049             Context(const Context &);
00050             const Context & operator= (const Context &);
00051 
00052         public:
00053             ///\name Basic operations
00054             ///\{
00055 
00056             Context(const std::string &);
00057 
00058             ~Context();
00059 
00060             ///\}
00061 
00062             /**
00063              * Current context.
00064              */
00065             static std::string backtrace(const std::string & delim);
00066     };
00067 
00068     /**
00069      * Base exception class.
00070      *
00071      * \ingroup g_exceptions
00072      * \nosubgrouping
00073      */
00074     class PALUDIS_VISIBLE Exception :
00075         public std::exception
00076     {
00077         private:
00078             const std::string _message;
00079             mutable std::string _what_str;
00080             struct ContextData;
00081             ContextData * const _context_data;
00082 
00083             const Exception & operator= (const Exception &);
00084 
00085         protected:
00086             ///\name Basic operations
00087             ///\{
00088 
00089             Exception(const std::string & message) throw ();
00090 
00091             Exception(const Exception &);
00092 
00093             ///\}
00094 
00095         public:
00096             ///\name Basic operations
00097             ///\{
00098 
00099             virtual ~Exception() throw () PALUDIS_ATTRIBUTE((nothrow));
00100 
00101             ///\}
00102 
00103             /**
00104              * Return our descriptive error message.
00105              */
00106             const std::string & message() const throw () PALUDIS_ATTRIBUTE((nothrow));
00107 
00108             /**
00109              * Make a backtrace.
00110              */
00111             std::string backtrace(const std::string & delim) const;
00112 
00113             /**
00114              * Is our backtrace empty?
00115              */
00116             bool empty() const;
00117 
00118             /**
00119              * A better what, if possible.
00120              */
00121             const char * what() const throw ();
00122     };
00123 
00124     /**
00125      * An InternalError is an Exception that is thrown if something that is
00126      * never supposed to happen happens.
00127      *
00128      * \ingroup g_exceptions
00129      * \nosubgrouping
00130      */
00131     class PALUDIS_VISIBLE InternalError :
00132         public Exception
00133     {
00134         public:
00135             /**
00136              * Constructor.
00137              *
00138              * \param location Should be set to the PALUDIS_HERE macro.
00139              *
00140              * \param message A short message.
00141              */
00142             InternalError(const std::string & location, const std::string & message) throw ();
00143     };
00144 
00145     /**
00146      * A NotAvailableError is an Exception that is thrown if something that is
00147      * not available (for example due to compile time configure options or platform
00148      * limitations) is used.
00149      *
00150      * \ingroup g_exceptions
00151      * \nosubgrouping
00152      */
00153     class PALUDIS_VISIBLE NotAvailableError :
00154         public Exception
00155     {
00156         public:
00157             /**
00158              * Constructor.
00159              */
00160             NotAvailableError(const std::string & message) throw ();
00161     };
00162 
00163     /**
00164      * A NameError is an Exception that is thrown when some kind of invalid
00165      * name is encountered.
00166      *
00167      * \ingroup g_exceptions
00168      * \ingroup g_names
00169      * \nosubgrouping
00170      */
00171     class PALUDIS_VISIBLE NameError :
00172         public Exception
00173     {
00174         protected:
00175             /**
00176              * Constructor.
00177              *
00178              * \param name The invalid name encountered.
00179              * \param role The role for the name, for example "package name".
00180              */
00181             NameError(const std::string & name, const std::string & role) throw ();
00182 
00183             /**
00184              * Constructor.
00185              *
00186              * \param name The invalid name encountered.
00187              * \param role The role for the name, for example "package name".
00188              * \param msg  Any extra message.
00189              */
00190             NameError(const std::string & name, const std::string & role,
00191                     const std::string & msg) throw ();
00192     };
00193 
00194     /**
00195      * A ConfigurationError is thrown when an invalid configuration occurs.
00196      *
00197      * \ingroup g_exceptions
00198      * \nosubgrouping
00199      */
00200     class PALUDIS_VISIBLE ConfigurationError :
00201         public Exception
00202     {
00203         public:
00204             /**
00205              * Constructor.
00206              */
00207             ConfigurationError(const std::string & msg) throw ();
00208     };
00209 
00210     /** \def PALUDIS_HERE
00211      * Expands to the current function name, file and line, for use with
00212      * paludis::InternalError.
00213      *
00214      * \ingroup g_exceptions
00215      */
00216 #define PALUDIS_HERE (std::string(__PRETTY_FUNCTION__) + " at " + \
00217         std::string(__FILE__) + ":" + paludis::stringify(__LINE__))
00218 }
00219 
00220 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4