Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add exception class.

  • Loading branch information...
commit 0a97df7f2dfb017a6c1d82e05b28950302e44f01 1 parent 8891f18
Wei-Cheng Pan authored
35 core/include/qbtd/exception.hpp
View
@@ -0,0 +1,35 @@
+#ifndef QBTD_EXCEPTION_EXCEPTION_HPP
+#define QBTD_EXCEPTION_EXCEPTION_HPP
+
+#include <exception>
+#include <memory>
+
+#include <QtCore/QString>
+
+namespace qbtd {
+namespace exception {
+
+class Exception: public std::exception {
+public:
+ explicit Exception( int errnum );
+ explicit Exception( const char * message );
+ Exception( const char * message, std::size_t n );
+ explicit Exception( const wchar_t * message );
+ Exception( const wchar_t * message, std::size_t n );
+ explicit Exception( const std::string & message );
+ explicit Exception( const std::wstring & message );
+ explicit Exception( const QString & message );
+ virtual ~Exception() throw();
+
+ virtual const char * what() const throw();
+ const QString & getMessage() const;
+
+private:
+ class Private;
+ std::shared_ptr< Private > p_;
+};
+
+}
+}
+
+#endif
41 core/src/exception/exception.cpp
View
@@ -0,0 +1,41 @@
+#include "exception_p.hpp"
+
+using qbtd::exception::Exception;
+
+Exception::Private::Private( const QString & msg ): msg( msg ) {
+}
+
+Exception::Exception( int errnum ): std::exception(), p_( std::make_shared< Private >( QString::fromUtf8( strerror( errnum ) ) ) ) {
+}
+
+Exception::Exception( const char * message ): std::exception(), p_( std::make_shared< Private >( QString::fromUtf8( message ) ) ) {
+}
+
+Exception::Exception( const char * message, std::size_t n ): std::exception(), p_( std::make_shared< Private >( QString::fromUtf8( message, n ) ) ) {
+}
+
+Exception::Exception( const wchar_t * message ): std::exception(), p_( std::make_shared< Private >( QString::fromWCharArray( message ) ) ) {
+}
+
+Exception::Exception( const wchar_t * message, std::size_t n ): std::exception(), p_( std::make_shared< Private >( QString::fromWCharArray( message, n ) ) ) {
+}
+
+Exception::Exception( const std::string & message ): std::exception(), p_( std::make_shared< Private >( QString::fromUtf8( message.c_str() ) ) ) {
+}
+
+Exception::Exception( const std::wstring & message ): std::exception(), p_( std::make_shared< Private >( QString::fromStdWString( message ) ) ) {
+}
+
+Exception::Exception( const QString & message ): std::exception(), p_( std::make_shared< Private >( message ) ) {
+}
+
+Exception::~Exception() throw() {
+}
+
+const char * Exception::what() const throw() {
+ return this->p_->msg.toLocal8Bit();
+}
+
+const QString & Exception::getMessage() const {
+ return this->p_->msg;
+}
19 core/src/exception/exception_p.hpp
View
@@ -0,0 +1,19 @@
+#ifndef QBTD_EXCEPTION_EXCEPTION_HPP_
+#define QBTD_EXCEPTION_EXCEPTION_HPP_
+
+#include "qbtd/exception.hpp"
+
+namespace qbtd {
+namespace exception {
+
+class Exception::Private {
+public:
+ explicit Private( const QString & msg );
+
+ QString msg;
+};
+
+}
+}
+
+#endif
Please sign in to comment.
Something went wrong with that request. Please try again.