From 58fa55cf44c8b87ae3edb8f24fbac128649a7255 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 31 Mar 2022 23:52:06 -0700 Subject: Added real exceptions based on QException. These may not be stl compatible, which would be sad. They seem to work in practice, though. --- c++-qt/gats-qt/parseexception.h | 25 +++++++++++++++++++++++++ c++-qt/gats-qt/typeexception.h | 26 ++++++++++++++++++++++++++ c++-qt/src/dictionary.cpp | 31 +++++++++++++++++-------------- c++-qt/src/object.cpp | 4 +++- c++-qt/src/parseexception.cpp | 31 +++++++++++++++++++++++++++++++ c++-qt/src/typeexception.cpp | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 c++-qt/gats-qt/parseexception.h create mode 100644 c++-qt/gats-qt/typeexception.h create mode 100644 c++-qt/src/parseexception.cpp create mode 100644 c++-qt/src/typeexception.cpp diff --git a/c++-qt/gats-qt/parseexception.h b/c++-qt/gats-qt/parseexception.h new file mode 100644 index 0000000..24b26ae --- /dev/null +++ b/c++-qt/gats-qt/parseexception.h @@ -0,0 +1,25 @@ +#ifndef GATS_PARSE_EXCEPTION_H +#define GATS_PARSE_EXCEPTION_H + +#include + +namespace Gats +{ + class ParseException : public QException + { + public: + ParseException( const char * const sMessage ); + ParseException( const ParseException &rSrc ); + virtual ~ParseException(); + + virtual QException *clone() const; + virtual void raise() const; + + virtual const char *what() const noexcept; + + private: + const char *sMessage; + }; +}; + +#endif diff --git a/c++-qt/gats-qt/typeexception.h b/c++-qt/gats-qt/typeexception.h new file mode 100644 index 0000000..93d00ba --- /dev/null +++ b/c++-qt/gats-qt/typeexception.h @@ -0,0 +1,26 @@ +#ifndef GATS_TYPE_EXCEPTION_H +#define GATS_TYPE_EXCEPTION_H + +#include + +namespace Gats +{ + class TypeException : public QException + { + public: + TypeException( const char * const sMessage ); + TypeException( const TypeException &rSrc ); + virtual ~TypeException(); + + virtual QException *clone() const; + virtual void raise() const; + + virtual const char *what() const noexcept; + + private: + const char *sMessage; + }; +}; + +#endif + diff --git a/c++-qt/src/dictionary.cpp b/c++-qt/src/dictionary.cpp index 9b528f1..10ec6c5 100644 --- a/c++-qt/src/dictionary.cpp +++ b/c++-qt/src/dictionary.cpp @@ -13,6 +13,9 @@ #include "gats-qt/string.h" #include "gats-qt/list.h" +#include "gats-qt/parseexception.h" +#include "gats-qt/typeexception.h" + Gats::Dictionary::Dictionary() { } @@ -51,7 +54,7 @@ void Gats::Dictionary::write( QIODevice &rOut ) const rOut.write("e", 1 ); } -void Gats::Dictionary::read( QIODevice &rIn, char cType ) +void Gats::Dictionary::read( QIODevice &rIn, char /*cType*/ ) { for(;;) { @@ -60,7 +63,7 @@ void Gats::Dictionary::read( QIODevice &rIn, char cType ) if( cNext == 'e' ) break; if( cNext != 's' ) - throw "PUT GOOD EXCEPTION HERE"; + throw ParseException("Bad format reading binary Gats Dictionary."); Gats::String sKey; sKey.read( rIn, cNext ); @@ -243,7 +246,7 @@ bool Gats::Dictionary::valueBool( const QByteArray &sKey ) { Gats::Boolean *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as bool but isn't."); return pOb->getValue(); } @@ -252,7 +255,7 @@ int64_t Gats::Dictionary::valueInt( const QByteArray &sKey ) { Gats::Integer *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as int but isn't."); return pOb->getValue(); } @@ -261,7 +264,7 @@ double Gats::Dictionary::valueFloat( const QByteArray &sKey ) { Gats::Float *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as float but isn't."); return pOb->getValue(); } @@ -270,7 +273,7 @@ QByteArray Gats::Dictionary::valueStr( const QByteArray &sKey ) { Gats::String *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as str but isn't."); return *pOb; } @@ -279,7 +282,7 @@ Gats::List *Gats::Dictionary::valueList( const QByteArray &sKey ) { Gats::List *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as list but isn't."); return pOb; } @@ -288,7 +291,7 @@ Gats::Dictionary *Gats::Dictionary::valueDict( const QByteArray &sKey ) { Gats::Dictionary *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as dict but isn't."); return pOb; } @@ -297,7 +300,7 @@ bool Gats::Dictionary::valueBool( const QByteArray &sKey ) const { Gats::Boolean *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as bool but isn't."); return pOb->getValue(); } @@ -306,7 +309,7 @@ int64_t Gats::Dictionary::valueInt( const QByteArray &sKey ) const { Gats::Integer *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as int but isn't."); return pOb->getValue(); } @@ -315,7 +318,7 @@ double Gats::Dictionary::valueFloat( const QByteArray &sKey ) const { Gats::Float *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as float but isn't."); return pOb->getValue(); } @@ -324,7 +327,7 @@ QByteArray Gats::Dictionary::valueStr( const QByteArray &sKey ) const { Gats::String *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as str but isn't."); return *pOb; } @@ -333,7 +336,7 @@ Gats::List *Gats::Dictionary::valueList( const QByteArray &sKey ) const { Gats::List *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as list but isn't."); return pOb; } @@ -342,7 +345,7 @@ Gats::Dictionary *Gats::Dictionary::valueDict( const QByteArray &sKey ) const { Gats::Dictionary *pOb = dynamic_cast( value( sKey ) ); if( !pOb ) - throw "PUT GOOD EXCEPTION HERE"; + throw TypeException("Dictionary value requested as dict but isn't."); return pOb; } diff --git a/c++-qt/src/object.cpp b/c++-qt/src/object.cpp index 85851df..d148774 100644 --- a/c++-qt/src/object.cpp +++ b/c++-qt/src/object.cpp @@ -15,6 +15,8 @@ #include "gats-qt/dictionary.h" #include "gats-qt/null.h" +#include "gats-qt/parseexception.h" + #include #include @@ -68,7 +70,7 @@ Gats::Object *Gats::Object::read( QIODevice &rIn ) return NULL; default: - throw "Invalid Gats type discovered: "; + throw ParseException("Invalid Gats type discovered."); } pObj->read( rIn, buf ); diff --git a/c++-qt/src/parseexception.cpp b/c++-qt/src/parseexception.cpp new file mode 100644 index 0000000..03d547a --- /dev/null +++ b/c++-qt/src/parseexception.cpp @@ -0,0 +1,31 @@ +#include "gats-qt/parseexception.h" + +Gats::ParseException::ParseException( const char * const sMessage ) : + sMessage( sMessage ) +{ +} + +Gats::ParseException::ParseException( const ParseException &rSrc ) : + sMessage( rSrc.sMessage ) +{ +} + +Gats::ParseException::~ParseException() +{ +} + +QException *Gats::ParseException::clone() const +{ + return new ParseException( *this ); +} + +void Gats::ParseException::raise() const +{ + throw *this; +} + +const char *Gats::ParseException::what() const noexcept +{ + return sMessage; +} + diff --git a/c++-qt/src/typeexception.cpp b/c++-qt/src/typeexception.cpp new file mode 100644 index 0000000..13ec3de --- /dev/null +++ b/c++-qt/src/typeexception.cpp @@ -0,0 +1,32 @@ +#include "gats-qt/typeexception.h" + +Gats::TypeException::TypeException( const char * const sMessage ) : + sMessage( sMessage ) +{ +} + +Gats::TypeException::TypeException( const TypeException &rSrc ) : + sMessage( rSrc.sMessage ) +{ +} + +Gats::TypeException::~TypeException() +{ +} + +QException *Gats::TypeException::clone() const +{ + return new TypeException( *this ); +} + +void Gats::TypeException::raise() const +{ + throw *this; +} + +const char *Gats::TypeException::what() const noexcept +{ + return sMessage; +} + + -- cgit v1.2.3