diff options
Diffstat (limited to 'cpp/include/IceUtil/Exception.h')
-rw-r--r-- | cpp/include/IceUtil/Exception.h | 137 |
1 files changed, 115 insertions, 22 deletions
diff --git a/cpp/include/IceUtil/Exception.h b/cpp/include/IceUtil/Exception.h index 2e9dd34af3f..e7e6a21a52f 100644 --- a/cpp/include/IceUtil/Exception.h +++ b/cpp/include/IceUtil/Exception.h @@ -25,13 +25,13 @@ public: Exception(); Exception(const char*, int); #ifndef ICE_CPP11_COMPILER - virtual ~Exception() throw(); + virtual ~Exception() throw() = 0; #endif virtual std::string ice_id() const = 0; virtual void ice_print(std::ostream&) const; virtual const char* what() const ICE_NOEXCEPT; #ifdef ICE_CPP11_MAPPING - std::exception_ptr ice_clone() const; + std::unique_ptr<Exception> ice_clone() const; #else virtual Exception* ice_clone() const = 0; ICE_DEPRECATED_API("ice_name() is deprecated, use ice_id() instead.") @@ -43,6 +43,12 @@ public: int ice_line() const; std::string ice_stackTrace() const; +protected: + +#ifdef ICE_CPP11_MAPPING + virtual Exception* ice_cloneImpl() const = 0; +#endif + private: const char* _file; @@ -53,36 +59,87 @@ private: ICE_API std::ostream& operator<<(std::ostream&, const Exception&); -class ICE_API NullHandleException : public Exception + +#ifdef ICE_CPP11_MAPPING + +template<typename E, typename B = Exception> +class ExceptionHelper : public B { public: - NullHandleException(const char*, int); -#ifndef ICE_CPP11_COMPILER - virtual ~NullHandleException() throw(); + using B::B; + + std::unique_ptr<E> ice_clone() const + { + return std::unique_ptr<E>(static_cast<E*>(ice_cloneImpl())); + } + + virtual void ice_throw() const override + { + throw static_cast<const E&>(*this); + } + +protected: + + virtual Exception* ice_cloneImpl() const override + { + return new E(static_cast<const E&>(*this)); + } +}; + +#else // C++98 mapping + +template<typename E> +class ExceptionHelper : public Exception +{ +public: + + ExceptionHelper() + { + } + + ExceptionHelper(const char* file, int line) : Exception(file, line) + { + } + + virtual void ice_throw() const + { + throw static_cast<const E&>(*this); + } +}; + #endif + + +class ICE_API NullHandleException : public ExceptionHelper<NullHandleException> +{ +public: + + NullHandleException(const char*, int); virtual std::string ice_id() const; + #ifndef ICE_CPP11_MAPPING virtual NullHandleException* ice_clone() const; #endif - virtual void ice_throw() const; }; -class ICE_API IllegalArgumentException : public Exception +class ICE_API IllegalArgumentException : public ExceptionHelper<IllegalArgumentException> { public: IllegalArgumentException(const char*, int); IllegalArgumentException(const char*, int, const std::string&); + #ifndef ICE_CPP11_COMPILER virtual ~IllegalArgumentException() throw(); #endif + virtual std::string ice_id() const; virtual void ice_print(std::ostream&) const; + #ifndef ICE_CPP11_MAPPING virtual IllegalArgumentException* ice_clone() const; #endif - virtual void ice_throw() const; std::string reason() const; @@ -94,40 +151,48 @@ private: // // IllegalConversionException is raised to report a string conversion error // -class ICE_API IllegalConversionException : public Exception +class ICE_API IllegalConversionException : public ExceptionHelper<IllegalConversionException> { public: IllegalConversionException(const char*, int); IllegalConversionException(const char*, int, const std::string&); + #ifndef ICE_CPP11_COMPILER virtual ~IllegalConversionException() throw(); #endif + virtual std::string ice_id() const; virtual void ice_print(std::ostream&) const; + #ifndef ICE_CPP11_MAPPING virtual IllegalConversionException* ice_clone() const; #endif - virtual void ice_throw() const; std::string reason() const; + private: const std::string _reason; }; -class ICE_API SyscallException : public Exception +class ICE_API SyscallException : public ExceptionHelper<SyscallException> { public: SyscallException(const char*, int, int); + +#ifndef ICE_CPP11_COMPILER + virtual ~SyscallException() throw(); +#endif + virtual std::string ice_id() const; virtual void ice_print(std::ostream&) const; + #ifndef ICE_CPP11_MAPPING virtual SyscallException* ice_clone() const; #endif - virtual void ice_throw() const; int error() const; @@ -136,20 +201,49 @@ private: const int _error; }; -class ICE_API FileLockException : public Exception + +#ifdef ICE_CPP11_MAPPING + +template<typename E> +using SyscallExceptionHelper = ExceptionHelper<E, SyscallException>; + +#else // C++98 mapping + +template<typename E> +class SyscallExceptionHelper : public SyscallException +{ +public: + + SyscallExceptionHelper(const char* file, int line, int errorCode) : + SyscallException(file, line, errorCode) + { + } + + virtual void ice_throw() const + { + throw static_cast<const E&>(*this); + } +}; + +#endif + + +class ICE_API FileLockException : public ExceptionHelper<FileLockException> { public: FileLockException(const char*, int, int, const std::string&); + #ifndef ICE_CPP11_COMPILER virtual ~FileLockException() throw(); #endif + virtual std::string ice_id() const; virtual void ice_print(std::ostream&) const; + #ifndef ICE_CPP11_MAPPING virtual FileLockException* ice_clone() const; #endif - virtual void ice_throw() const; std::string path() const; int error() const; @@ -160,36 +254,35 @@ private: std::string _path; }; -class ICE_API OptionalNotSetException : public Exception +class ICE_API OptionalNotSetException : public ExceptionHelper<OptionalNotSetException> { public: OptionalNotSetException(const char*, int); -#ifndef ICE_CPP11_COMPILER - virtual ~OptionalNotSetException() throw(); -#endif virtual std::string ice_id() const; + #ifndef ICE_CPP11_MAPPING virtual OptionalNotSetException* ice_clone() const; #endif - virtual void ice_throw() const; }; #ifndef _WIN32 -class ICE_API IconvInitializationException : public Exception +class ICE_API IconvInitializationException : public ExceptionHelper<IconvInitializationException> { public: IconvInitializationException(const char*, int, const std::string&); + #ifndef ICE_CPP11_COMPILER virtual ~IconvInitializationException() throw(); #endif + virtual std::string ice_id() const; virtual void ice_print(std::ostream&) const; + #ifndef ICE_CPP11_MAPPING virtual IconvInitializationException* ice_clone() const; #endif - virtual void ice_throw() const; std::string reason() const; |