exception.hpp

Go to the documentation of this file.
00001 #ifndef s11n_net_s11n_v1_3_EXCEPTION_HPP_INCLUDED
00002 #define s11n_net_s11n_v1_3_EXCEPTION_HPP_INCLUDED 1
00003 
00004 #include <string>
00005 #include <exception>
00006 #include <iostream> // i hate this dependency :(
00007 #include <s11n.net/s11n/s11n_config.hpp>
00008 
00009 // S11N_CURRENT_FUNCTION is basically the same as the conventional
00010 // __FUNCTION__ macro, except that it tries to use compiler-specific
00011 // "pretty-formatted" names. This macro is primarily intended for use
00012 // with the s11n::source_info type to provide useful information in
00013 // exception strings.  The idea of S11N_CURRENT_FUNCTION and
00014 // S11N_SOURCEINFO is taken from the P::Classes and Pt frameworks.
00015 #ifdef __GNUC__
00016 #  define S11N_CURRENT_FUNCTION __PRETTY_FUNCTION__
00017 #elif defined(__BORLANDC__)
00018 #  define S11N_CURRENT_FUNCTION __FUNC__
00019 #elif defined(_MSC_VER)
00020 #  if _MSC_VER >= 1300 // .NET 2003
00021 #    define S11N_CURRENT_FUNCTION __FUNCDNAME__
00022 #  else
00023 #    define S11N_CURRENT_FUNCTION __FUNCTION__
00024 #  endif
00025 #else
00026 #  define S11N_CURRENT_FUNCTION __FUNCTION__
00027 #endif
00028 
00029 
00030 // S11N_SOURCEINFO is intended to simplify the instantiation of
00031 // s11n::source_info objects, which are supposed to be passed values
00032 // which are easiest to get via macros.
00033 #define S11N_SOURCEINFO s11n::source_info(__FILE__,__LINE__,S11N_CURRENT_FUNCTION)
00034 namespace s11n {
00035 
00036 
00037     /**
00038        source_info simplifies the collection of source file
00039        information for purposes of wrapping the info into
00040        exception strings.
00041        
00042        This class is normally not instantiated directly,
00043        but is instead created using the S11N_SOURCEINFO
00044        macro.
00045        
00046        Added in version 1.3.0.
00047     */
00048     struct source_info
00049     {
00050         /**
00051            It is expected that this function be passed
00052            __FILE__, __LINE__, and
00053            S11N_CURRENT_FUNCTION. If file or func are
00054            null then "<unknown>" (or something
00055            similar) is used.
00056         */
00057         source_info( char const * file, unsigned int line, char const * func );
00058         ~source_info();
00059         /**
00060            Returns the line number passed to the ctor.
00061         */
00062         unsigned int line() const throw();
00063         /**
00064            Returns the file name passed to the ctor.
00065         */
00066         char const * file() const throw();
00067         /**
00068            Returns the function name passed to the ctor.
00069         */
00070         char const * func() const throw();
00071         
00072         /** Copies rhs. */
00073         source_info & operator=( source_info const & rhs );
00074         /** Copies rhs. */
00075         source_info( source_info const & rhs );
00076     private:
00077         struct pimpl; // opaque internal type
00078         pimpl * m_p;
00079     };
00080 
00081     /**
00082        Sends si.file():si.line():si.func() to os and returns os.
00083     */
00084     std::ostream & operator<<( std::ostream & os, source_info const & si );
00085 
00086     /**
00087        The base-most exception type used by s11n.
00088     */
00089         struct s11n_exception : public std::exception
00090         {
00091     public:
00092         virtual ~s11n_exception() throw() {}
00093 
00094         /**
00095            Creates an exception with the given formatted
00096            what() string.  Takes a printf-like format
00097            string. If the expanded string is longer than some
00098            arbitrarily-chosen internal limit [hint: probably
00099            2k bytes] then it is truncated.
00100 
00101            If you get overload ambiguities with the
00102            std::string-argument ctor, this is because you've
00103            passed a (char const *) string to those ctors and
00104            relied on implicit conversion to std::string.
00105            Simply wrapping those c-strings in std::string
00106            ctors should get around the problem.
00107 
00108            Historical note:
00109 
00110            This ctor, introduced in version 1.2.6, conflicted
00111            with an older 3-arg ctor taking (std::string,char
00112            const *,uint) arguments, but this one is far more
00113            flexible, so the older was removed. We also had
00114            ctor taking a std::string, but that was removed
00115            to avoid additional ambiguities.
00116         */
00117         explicit s11n_exception( const char *format, ... );
00118 
00119 
00120         /**
00121            Identical to s11n_exception(format,...) except that
00122            file/line/function information from the source_info
00123            object is prefixed to the resulting what() string.
00124            It is expected that this ctor be passed the
00125            S11N_SOURCEINFO macro as its first argument.
00126 
00127            Added in version 1.3.0.
00128         */
00129         s11n_exception( source_info const &, const char *format, ... );
00130 
00131         /**
00132            Returns the 'what' string passed to the ctor.
00133         */
00134                 virtual const char * what() const throw();
00135     protected:
00136         /**
00137            Intended to be used by ctors.
00138         */
00139         void what( std::string const & ) throw();
00140         s11n_exception();
00141         private:
00142                 std::string m_what;
00143         };
00144 
00145     /**
00146        An exception type for classloader-related exceptions. These
00147        need to be caught separately from s11n_exceptions in some
00148        cases because sometimes a classloader can try other
00149        alternatives on an error.
00150     */
00151     struct factory_exception : public s11n_exception
00152     {
00153     public:
00154         virtual ~factory_exception() throw() {}
00155         explicit factory_exception( const char *format, ... );
00156     };
00157 
00158 
00159     /**
00160        Really for use by clients, i/o layers, and s11nlite, not by
00161        the s11n core.
00162     */
00163     struct io_exception : public s11n_exception
00164     {
00165     public:
00166         virtual ~io_exception() throw() {}
00167         explicit io_exception( const char *format, ... );
00168     };
00169 
00170 
00171 } // namespace s11n
00172 
00173 #endif // s11n_net_s11n_v1_3_EXCEPTION_HPP_INCLUDED

Generated on Sun Apr 27 13:16:04 2008 for libs11n by  doxygen 1.5.3