From 272b34c1a4639cd0f909bfb52d30339c93b0c42b Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Fri, 6 Jun 2014 22:00:39 +0200 Subject: Implemented WarningCapacitor as a counterpart to ErrorCapacitor * in difference to ErrorCapacitor this class doesn't throw an exception on "discharge" but returns the gathered warnings * adapted FunctionTransform and frontend error handling to include warnings * added static "try_create" method to TransformationFacade ** wraps construction error handling ** custom logic may be embedded using the std::function argument *** this was implemented to prevent unneccessary code duplication for handling both construction and generation errors * adapted FunctionTransform to return warning as "warning" nodes in the result tree ** added functional lambda expression factory method "handleErrors" *** returns a error handling lambda expression for a given ResultNodeFacade * implemented WarningGuard class in frontend executable ** guarantees warnings to be printed to std::cerr independent of any exceptions --- src/support/error/error_capacitor.cc | 18 ++++++----------- src/support/error/error_capacitor.h | 8 +++----- src/support/error/error_multiplexer.cc | 29 +++++++++++++++++---------- src/support/error/error_multiplexer.h | 36 +++++++++++++++++++++------------- src/support/error/warning_capacitor.cc | 26 ++++++++++++++++++++++++ src/support/error/warning_capacitor.h | 31 +++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+), 41 deletions(-) create mode 100644 src/support/error/warning_capacitor.cc create mode 100644 src/support/error/warning_capacitor.h (limited to 'src/support/error') diff --git a/src/support/error/error_capacitor.cc b/src/support/error/error_capacitor.cc index 58cfb81..cc7dd01 100644 --- a/src/support/error/error_capacitor.cc +++ b/src/support/error/error_capacitor.cc @@ -3,14 +3,8 @@ namespace InputXSLT { ErrorCapacitor::ErrorCapacitor(ErrorMultiplexer* multiplexer): - multiplexer_(multiplexer), - error_cache_(new error_cache()) { - this->multiplexer_->connectReceiver(this); -} - -ErrorCapacitor::~ErrorCapacitor() { - this->multiplexer_->disconnectReceiver(this); -} + ErrorMultiplexer::receiver(multiplexer), + error_cache_(new error_cache()) { } void ErrorCapacitor::discharge() { if ( !this->error_cache_->empty() ) { @@ -19,10 +13,10 @@ void ErrorCapacitor::discharge() { } void ErrorCapacitor::receive( - const ErrorMultiplexer::ErrorType type, + const ErrorMultiplexer::error_type type, const std::string& message ) { - if ( type == ErrorMultiplexer::ErrorType::Error ) { + if ( type == ErrorMultiplexer::error_type::error ) { this->error_cache_->emplace_back(message); } } @@ -30,8 +24,8 @@ void ErrorCapacitor::receive( ErrorCapacitor::exception::exception(error_cache_ptr ptr): error_cache_(std::move(ptr)) { } -auto ErrorCapacitor::exception::getCachedErrors() const -> const error_cache* { - return this->error_cache_.get(); +auto ErrorCapacitor::exception::operator*() const -> const error_cache& { + return *(this->error_cache_.get()); } } diff --git a/src/support/error/error_capacitor.h b/src/support/error/error_capacitor.h index 6017cb4..1c225e9 100644 --- a/src/support/error/error_capacitor.h +++ b/src/support/error/error_capacitor.h @@ -7,7 +7,7 @@ namespace InputXSLT { -class ErrorCapacitor : public ErrorMultiplexer::Receiver { +class ErrorCapacitor : public ErrorMultiplexer::receiver { public: class exception; @@ -15,17 +15,15 @@ class ErrorCapacitor : public ErrorMultiplexer::Receiver { typedef std::unique_ptr error_cache_ptr; ErrorCapacitor(ErrorMultiplexer*); - ~ErrorCapacitor(); void discharge(); virtual void receive( - const ErrorMultiplexer::ErrorType, + const ErrorMultiplexer::error_type, const std::string& ); private: - ErrorMultiplexer* const multiplexer_; error_cache_ptr error_cache_; }; @@ -34,7 +32,7 @@ class ErrorCapacitor::exception { public: exception(error_cache_ptr); - const error_cache* getCachedErrors() const; + const error_cache& operator*() const; private: error_cache_ptr error_cache_; diff --git a/src/support/error/error_multiplexer.cc b/src/support/error/error_multiplexer.cc index f5d4e0f..bbf0e71 100644 --- a/src/support/error/error_multiplexer.cc +++ b/src/support/error/error_multiplexer.cc @@ -27,15 +27,15 @@ inline std::string getMessage(const xercesc::SAXParseException& exception) { ); } -inline ErrorMultiplexer::ErrorType toErrorType( +inline ErrorMultiplexer::error_type toErrorType( const xalan::ProblemListenerBase::eClassification classification) { switch ( classification ) { case xalan::ProblemListenerBase::eClassification::eMessage || xalan::ProblemListenerBase::eClassification::eWarning: { - return ErrorMultiplexer::ErrorType::Warning; + return ErrorMultiplexer::error_type::warning; } default: { - return ErrorMultiplexer::ErrorType::Error; + return ErrorMultiplexer::error_type::error; } } } @@ -56,11 +56,11 @@ ErrorMultiplexer::~ErrorMultiplexer() { this->transformer_->setProblemListener(nullptr); } -void ErrorMultiplexer::connectReceiver(Receiver* receiver) { +void ErrorMultiplexer::connectReceiver(receiver* receiver) { this->receivers_.push_back(receiver); } -void ErrorMultiplexer::disconnectReceiver(Receiver* receiver) { +void ErrorMultiplexer::disconnectReceiver(receiver* receiver) { this->receivers_.erase( std::remove( this->receivers_.begin(), @@ -72,21 +72,21 @@ void ErrorMultiplexer::disconnectReceiver(Receiver* receiver) { void ErrorMultiplexer::warning(const xercesc::SAXParseException& exception) { this->multiplex( - ErrorType::Warning, + error_type::warning, "Warning: " + getMessage(exception) ); } void ErrorMultiplexer::error(const xercesc::SAXParseException& exception) { this->multiplex( - ErrorType::Error, + error_type::error, "Error: " + getMessage(exception) ); } void ErrorMultiplexer::fatalError(const xercesc::SAXParseException& exception) { this->multiplex( - ErrorType::Error, + error_type::error, "Fatal error: " + getMessage(exception) ); } @@ -155,16 +155,25 @@ void ErrorMultiplexer::problem( void ErrorMultiplexer::setPrintWriter(xalan::PrintWriter*) { } void ErrorMultiplexer::multiplex( - const ErrorType type, + const error_type type, const std::string& message ) { std::for_each( this->receivers_.begin(), this->receivers_.end(), - [&type, &message](Receiver* const receiver) -> void { + [&type, &message](receiver* const receiver) -> void { receiver->receive(type, message); } ); } +ErrorMultiplexer::receiver::receiver(ErrorMultiplexer* multiplexer): + multiplexer_(multiplexer) { + this->multiplexer_->connectReceiver(this); +} + +ErrorMultiplexer::receiver::~receiver() { + this->multiplexer_->disconnectReceiver(this); +} + } diff --git a/src/support/error/error_multiplexer.h b/src/support/error/error_multiplexer.h index 02cfaf8..7d8544a 100644 --- a/src/support/error/error_multiplexer.h +++ b/src/support/error/error_multiplexer.h @@ -16,14 +16,14 @@ namespace InputXSLT { class ErrorMultiplexer : public xercesc::ErrorHandler, public xalan::ProblemListener { public: - enum class ErrorType; - struct Receiver; + enum class error_type; + class receiver; ErrorMultiplexer(xalan::XalanTransformer*); ~ErrorMultiplexer(); - void connectReceiver(Receiver*); - void disconnectReceiver(Receiver*); + void connectReceiver(receiver*); + void disconnectReceiver(receiver*); virtual void warning(const xercesc::SAXParseException&); virtual void error(const xercesc::SAXParseException&); @@ -61,22 +61,30 @@ class ErrorMultiplexer : public xercesc::ErrorHandler, private: xalan::XalanTransformer* const transformer_; - std::vector receivers_; + std::vector receivers_; - void multiplex(const ErrorType, const std::string&); + void multiplex(const error_type, const std::string&); }; -enum class ErrorMultiplexer::ErrorType { - Warning, - Error +enum class ErrorMultiplexer::error_type { + warning, + error }; -struct ErrorMultiplexer::Receiver { - virtual void receive( - const ErrorMultiplexer::ErrorType, - const std::string& - ) = 0; +class ErrorMultiplexer::receiver { + public: + receiver(ErrorMultiplexer*); + virtual ~receiver(); + + virtual void receive( + const ErrorMultiplexer::error_type, + const std::string& + ) = 0; + + private: + ErrorMultiplexer* const multiplexer_; + }; } diff --git a/src/support/error/warning_capacitor.cc b/src/support/error/warning_capacitor.cc new file mode 100644 index 0000000..f8d2b99 --- /dev/null +++ b/src/support/error/warning_capacitor.cc @@ -0,0 +1,26 @@ +#include "warning_capacitor.h" + +namespace InputXSLT { + +WarningCapacitor::WarningCapacitor(ErrorMultiplexer* multiplexer): + ErrorMultiplexer::receiver(multiplexer), + warning_cache_(new warning_cache()) { } + +auto WarningCapacitor::discharge() -> warning_cache_ptr { + warning_cache_ptr tmp(std::move(this->warning_cache_)); + + this->warning_cache_.reset(new warning_cache()); + + return std::move(tmp); +} + +void WarningCapacitor::receive( + const ErrorMultiplexer::error_type type, + const std::string& message +) { + if ( type == ErrorMultiplexer::error_type::warning ) { + this->warning_cache_->emplace_back(message); + } +} + +} diff --git a/src/support/error/warning_capacitor.h b/src/support/error/warning_capacitor.h new file mode 100644 index 0000000..6b7058b --- /dev/null +++ b/src/support/error/warning_capacitor.h @@ -0,0 +1,31 @@ +#ifndef INPUTXSLT_SRC_SUPPORT_ERROR_WARNING_CAPACITOR_H_ +#define INPUTXSLT_SRC_SUPPORT_ERROR_WARNING_CAPACITOR_H_ + +#include + +#include "error_multiplexer.h" + +namespace InputXSLT { + +class WarningCapacitor : public ErrorMultiplexer::receiver { + public: + typedef std::vector warning_cache; + typedef std::unique_ptr warning_cache_ptr; + + WarningCapacitor(ErrorMultiplexer*); + + warning_cache_ptr discharge(); + + virtual void receive( + const ErrorMultiplexer::error_type, + const std::string& + ); + + private: + warning_cache_ptr warning_cache_; + +}; + +} + +#endif // INPUTXSLT_SRC_SUPPORT_ERROR_WARNING_CAPACITOR_H_ -- cgit v1.2.3