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

Generated on Wed Jun 4 21:45:18 2008 for libs11n by  doxygen 1.5.3