mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 21:15:33 +08:00
refs #297 Added CStatusException, a throwable exception class containing a CStatusMessage.
This commit is contained in:
56
src/blackmisc/statusexception.h
Normal file
56
src/blackmisc/statusexception.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/* Copyright (C) 2015
|
||||
* Swift Project Community / Contributors
|
||||
*
|
||||
* This file is part of Swift Project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKMISC_STATUSEXCEPTION_H
|
||||
#define BLACKMISC_STATUSEXCEPTION_H
|
||||
|
||||
#include "statusmessage.h"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
/*!
|
||||
* Throwable exception class containing a CStatusMessage.
|
||||
*
|
||||
* This is the exception type which may be thrown by CStatusMessage::maybeThrow().
|
||||
*/
|
||||
class BLACKMISC_EXPORT CStatusException : public std::exception
|
||||
{
|
||||
public:
|
||||
//! Constructor.
|
||||
explicit CStatusException(const CStatusMessage &payload) :
|
||||
m_payload(payload)
|
||||
{}
|
||||
|
||||
//! Return null-terminated message string.
|
||||
virtual const char *what() const Q_DECL_NOEXCEPT override
|
||||
{
|
||||
return m_temp = m_payload.getMessage().toLocal8Bit();
|
||||
}
|
||||
|
||||
//! Return the contained status message.
|
||||
const CStatusMessage &status() const
|
||||
{
|
||||
return m_payload;
|
||||
}
|
||||
|
||||
//! Destructor.
|
||||
~CStatusException() Q_DECL_NOEXCEPT {}
|
||||
|
||||
private:
|
||||
const CStatusMessage m_payload;
|
||||
mutable QByteArray m_temp;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -8,6 +8,7 @@
|
||||
*/
|
||||
|
||||
#include "statusmessage.h"
|
||||
#include "statusexception.h"
|
||||
#include "blackmiscfreefunctions.h"
|
||||
#include "propertyindex.h"
|
||||
#include "iconlist.h"
|
||||
@@ -91,6 +92,19 @@ namespace BlackMisc
|
||||
}
|
||||
}
|
||||
|
||||
CStatusException CStatusMessage::asException() const
|
||||
{
|
||||
return CStatusException(*this);
|
||||
}
|
||||
|
||||
void CStatusMessage::maybeThrow() const
|
||||
{
|
||||
if (! this->isEmpty())
|
||||
{
|
||||
throw this->asException();
|
||||
}
|
||||
}
|
||||
|
||||
QString CStatusMessage::getHumanReadableCategory() const
|
||||
{
|
||||
if (this->m_humanReadableCategory.isEmpty())
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class CStatusException;
|
||||
|
||||
/*!
|
||||
* Streamable status message, e.g. from Core -> GUI
|
||||
*/
|
||||
@@ -68,6 +70,12 @@ namespace BlackMisc
|
||||
//! \sa QtMessageHandler
|
||||
void toQtLogTriple(QtMsgType *o_type, QString *o_category, QString *o_message) const;
|
||||
|
||||
//! Return a throwable exception object containing this status message.
|
||||
CStatusException asException() const;
|
||||
|
||||
//! If message is empty then do nothing, otherwise throw a CStatusException.
|
||||
void maybeThrow() const;
|
||||
|
||||
//! Message category
|
||||
const CLogCategoryList &getCategories() const { return this->m_categories; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user