Newer
Older
Peterson, Peter
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidKernel/Logger.h"
namespace Mantid
{
namespace API
{
Kernel::Logger& WorkspaceGroup::g_log = Kernel::Logger::get("WorkspaceGroup");
Gigg, Martyn Anthony
committed
WorkspaceGroup::WorkspaceGroup() :
Workspace(), m_deleteObserver(*this, &WorkspaceGroup::workspaceDeleteHandle),
m_renameObserver(*this, &WorkspaceGroup::workspaceRenameHandle)
{
// Listen for delete and rename notifications to update the group
// accordingly
Mantid::API::AnalysisDataService::Instance().notificationCenter.addObserver(m_deleteObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.addObserver(m_renameObserver);
}
Peterson, Peter
committed
WorkspaceGroup::~WorkspaceGroup()
Gigg, Martyn Anthony
committed
{
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_deleteObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_renameObserver);
}
Peterson, Peter
committed
/** Add the named workspace to the group
* @param name The name of the workspace (in the AnalysisDataService) to add
*/
void WorkspaceGroup::add(const std::string& name)
{
m_wsNames.push_back(name);
g_log.debug() << "workspacename added to group vector = " << name <<std::endl;
}
Gigg, Martyn Anthony
committed
/**
* Does this group contain the named workspace?
* @param wsName A string to compare
* @returns True if the name is part of this group, false otherwise
*/
bool WorkspaceGroup::contains(const std::string & wsName) const
{
std::vector<std::string>::const_iterator itr = std::find(m_wsNames.begin(), m_wsNames.end(), wsName);
return (itr != m_wsNames.end());
}
Peterson, Peter
committed
/// Empty all the entries out of the workspace group. Does not remove the workspaces from the ADS.
void WorkspaceGroup::removeAll()
{
m_wsNames.clear();
}
/** Remove the named workspace from the group. Does not delete the workspace from the AnalysisDataService.
* @param name The name of the workspace to be removed from the group.
*/
void WorkspaceGroup::remove(const std::string& name)
{
Gigg, Martyn Anthony
committed
std::vector<std::string>::iterator itr = std::find(m_wsNames.begin(), m_wsNames.end(), name);
if( itr != m_wsNames.end() )
Peterson, Peter
committed
{
Gigg, Martyn Anthony
committed
m_wsNames.erase(itr);
Peterson, Peter
committed
}
}
/// Removes all members of the group from the group AND from the AnalysisDataService
void WorkspaceGroup::deepRemoveAll()
{
Gigg, Martyn Anthony
committed
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_deleteObserver);
while (!m_wsNames.empty())
{
AnalysisDataService::Instance().remove(m_wsNames.back());
m_wsNames.pop_back();
}
Gigg, Martyn Anthony
committed
Mantid::API::AnalysisDataService::Instance().notificationCenter.addObserver(m_deleteObserver);
Peterson, Peter
committed
}
/// Print the names of all the workspaces in this group to the logger (at debug level)
void WorkspaceGroup::print() const
{
std::vector<std::string>::const_iterator itr;
for (itr = m_wsNames.begin(); itr != m_wsNames.end(); itr++)
{
Gigg, Martyn Anthony
committed
g_log.debug() << "Workspace name in group vector = " << *itr << std::endl;
}
}
/**M
* Callback for a workspace delete notification
* @param notice A pointer to a workspace delete notificiation object
*/
void WorkspaceGroup::workspaceDeleteHandle(Mantid::API::WorkspaceDeleteNotification_ptr notice)
{
if( notice->object_name() != this->getName() )
{
remove(notice->object_name());
}
}
/**
* Callback for a workspace rename notification
* @param notice A pointer to a workspace rename notfication object
*/
void WorkspaceGroup::workspaceRenameHandle(Mantid::API::WorkspaceRenameNotification_ptr notice)
{
std::vector<std::string>::iterator itr =
std::find(m_wsNames.begin(), m_wsNames.end(), notice->object_name());
if( itr != m_wsNames.end() )
{
(*itr) = notice->new_objectname();
Peterson, Peter
committed
}
}
} // namespace API
} // namespace Mantid