Newer
Older
Roman Tolchenov
committed
#ifndef MANTID_KERNEL_ALGORITHMOBSERVER_H_
#define MANTID_KERNEL_ALGORITHMOBSERVER_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include <boost/shared_ptr.hpp>
#include <Poco/ActiveMethod.h>
#include <Poco/NotificationCenter.h>
#include <Poco/Notification.h>
#include <Poco/NObserver.h>
namespace Mantid
{
namespace API
{
#ifndef IGNORE_ALGORITHM_OBSERVER_ARGUMENT
#define IGNORE_ALGORITHM_OBSERVER_ARGUMENT(x)
#endif
Roman Tolchenov
committed
/** @class AlgorithmObserver AlgorithmObserver.h API/AlgorithmObserver.h
Observes Algorithm notifications: start,progress,finish,error.
Hides Poco::Notification API from the user.
@author Roman Tolchenov, Tessella Support Services plc
@date 09/02/2009
Copyright © 2007-8 STFC Rutherford Appleton Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport AlgorithmObserver
{
public:
Roman Tolchenov
committed
/// Default constructor. Notification hanlders are not connected to any algorithm
Roman Tolchenov
committed
AlgorithmObserver()
:m_progressObserver(*this,&AlgorithmObserver::_progressHandle),
m_startObserver(*this,&AlgorithmObserver::_startHandle),
m_finishObserver(*this,&AlgorithmObserver::_finishHandle),
m_errorObserver(*this,&AlgorithmObserver::_errorHandle)
{
}
Roman Tolchenov
committed
/** Constructs AlgorithmObserver and connects all its handlers to algorithm alg.
@param alg Algorithm to be observed
*/
AlgorithmObserver(IAlgorithm_const_sptr alg)
Roman Tolchenov
committed
:m_progressObserver(*this,&AlgorithmObserver::_progressHandle),
m_startObserver(*this,&AlgorithmObserver::_startHandle),
m_finishObserver(*this,&AlgorithmObserver::_finishHandle),
m_errorObserver(*this,&AlgorithmObserver::_errorHandle)
{
observeAll(alg);
Russell Taylor
committed
}
virtual ~AlgorithmObserver() {}
Roman Tolchenov
committed
/** Connect to algorithm alg and observe all its notifications
@param alg Algorithm to be observed
*/
void observeAll(IAlgorithm_const_sptr alg)
Roman Tolchenov
committed
{
alg->addObserver(m_progressObserver);
alg->addObserver(m_startObserver);
alg->addObserver(m_finishObserver);
alg->addObserver(m_errorObserver);
}
Roman Tolchenov
committed
/** Connect to algorithm alg and observe its progress notification
@param alg Algorithm to be observed
*/
void observeProgress(IAlgorithm_const_sptr alg)
{
alg->addObserver(m_progressObserver);
}
Roman Tolchenov
committed
/** Connect to algorithm alg and observe its start notification
@param alg Algorithm to be observed
*/
void observeStart(IAlgorithm_const_sptr alg)
{
alg->addObserver(m_startObserver);
}
Roman Tolchenov
committed
/** Connect to algorithm alg and observe its finish notification
@param alg Algorithm to be observed
*/
void observeFinish(IAlgorithm_const_sptr alg)
{
alg->addObserver(m_finishObserver);
}
Roman Tolchenov
committed
/** Connect to algorithm alg and observe its error notification
@param alg Algorithm to be observed
*/
void observeError(IAlgorithm_const_sptr alg)
{
alg->addObserver(m_errorObserver);
Roman Tolchenov
committed
}
Roman Tolchenov
committed
/** Handler of the progress notifications. Must be overriden in inherited classes.
The default handler is provided (doing nothing).
@param alg Pointer to the algorithm sending the notification. Note that this can
point to a different object than the argument of a observeZZZ(...) method, e.g.
an observer can be connected to an AlgorithmProxy instance and receive notifications from
the corresponding Algorithm type object.
@param p Progress reported by the algorithm, 0 <= p <= 1
@param msg Optional message string sent by the algorithm
*/
virtual void progressHandle(const IAlgorithm* IGNORE_ALGORITHM_OBSERVER_ARGUMENT(alg),
double p,const std::string& IGNORE_ALGORITHM_OBSERVER_ARGUMENT(msg))
Roman Tolchenov
committed
{
std::cerr<<"Progress "<<p<<'\n';
}
Roman Tolchenov
committed
/** Handler of the start notifications. Must be overriden in inherited classes.
The default handler is provided (doing nothing).
@param alg Pointer to the algorithm sending the notification. Note that this can
point to a different object than the argument of a observeZZZ(...) method, e.g.
an observer can be connected to an AlgorithmProxy instance and receive notifications from
the corresponding Algorithm type object.
*/
virtual void startHandle(const IAlgorithm* IGNORE_ALGORITHM_OBSERVER_ARGUMENT(alg))
Roman Tolchenov
committed
{
std::cerr<<"Started "<<'\n';
}
/** Handler of the finish notifications. Must be overriden in inherited classes.
The default handler is provided (doing nothing).
@param alg Pointer to the algorithm sending the notification. Note that this can
point to a different object than the argument of a observeZZZ(...) method, e.g.
an observer can be connected to an AlgorithmProxy instance and receive notifications from
the corresponding Algorithm type object.
*/
virtual void finishHandle(const IAlgorithm* IGNORE_ALGORITHM_OBSERVER_ARGUMENT(alg))
Roman Tolchenov
committed
{
std::cerr<<"Finished "<<'\n';
}
/** Handler of the error notifications. Must be overriden in inherited classes.
The default handler is provided (doing nothing).
@param alg Pointer to the algorithm sending the notification. Note that this can
point to a different object than the argument of a observeZZZ(...) method, e.g.
an observer can be connected to an AlgorithmProxy instance and receive notifications from
the corresponding Algorithm type object.
@param what The error message
*/
virtual void errorHandle(const IAlgorithm* IGNORE_ALGORITHM_OBSERVER_ARGUMENT(alg),const std::string& what)
Roman Tolchenov
committed
{
std::cerr<<"Error "<<what<<'\n';
}
private:
/** Poco notification handler for Algorithm::ProgressNotification.
@param pNf An pointer to the notification.
*/
Roman Tolchenov
committed
void _progressHandle(const Poco::AutoPtr<Algorithm::ProgressNotification>& pNf)
{
this->progressHandle(pNf->algorithm(),pNf->progress,pNf->message);
}
Roman Tolchenov
committed
/// Poco::NObserver for Algorithm::ProgressNotification.
Roman Tolchenov
committed
Poco::NObserver<AlgorithmObserver, Algorithm::ProgressNotification> m_progressObserver;
Roman Tolchenov
committed
/** Poco notification handler for Algorithm::StartedNotification.
@param pNf An pointer to the notification.
*/
Roman Tolchenov
committed
void _startHandle(const Poco::AutoPtr<Algorithm::StartedNotification>& pNf)
{
this->startHandle(pNf->algorithm());
}
Roman Tolchenov
committed
/// Poco::NObserver for Algorithm::StartedNotification.
Roman Tolchenov
committed
Poco::NObserver<AlgorithmObserver, Algorithm::StartedNotification> m_startObserver;
Roman Tolchenov
committed
/** Poco notification handler for Algorithm::FinishedNotification.
@param pNf An pointer to the notification.
*/
Roman Tolchenov
committed
void _finishHandle(const Poco::AutoPtr<Algorithm::FinishedNotification>& pNf)
{
this->finishHandle(pNf->algorithm());
}
Roman Tolchenov
committed
/// Poco::NObserver for Algorithm::FinishedNotification.
Roman Tolchenov
committed
Poco::NObserver<AlgorithmObserver, Algorithm::FinishedNotification> m_finishObserver;
Roman Tolchenov
committed
/** Poco notification handler for Algorithm::ErrorNotification.
@param pNf An pointer to the notification.
*/
Roman Tolchenov
committed
void _errorHandle(const Poco::AutoPtr<Algorithm::ErrorNotification>& pNf)
{
this->errorHandle(pNf->algorithm(),pNf->what);
}
Roman Tolchenov
committed
/// Poco::NObserver for Algorithm::ErrorNotification.
Roman Tolchenov
committed
Poco::NObserver<AlgorithmObserver, Algorithm::ErrorNotification> m_errorObserver;
};
} // namespace API
} // namespace Mantid
#endif /*MANTID_KERNEL_ALGORITHMOBSERVER_H_*/