Skip to content
Snippets Groups Projects
Commit e3e18db9 authored by Gigg, Martyn Anthony's avatar Gigg, Martyn Anthony
Browse files

Add base-class checks to FILELOADER* check macros. Refs #7263

Also added runtime checks to the subscribe method. This allows it to static
cast at runtime when checking rather than a more expensive dynamic cast.
parent 4ff024a6
No related branches found
No related tags found
No related merge requests found
#ifndef MANTID_API_FILELOADERREGISTRY_H_
#define MANTID_API_FILELOADERREGISTRY_H_
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidAPI/IFileLoader.h"
#include "MantidAPI/IHDFFileLoader.h"
#include "MantidKernel/SingletonHolder.h"
#include <boost/type_traits/is_base_of.hpp>
#include <map>
#include <string>
#include <vector>
......@@ -70,6 +73,7 @@ namespace Mantid
template<typename Type>
void subscribe(LoaderFormat format)
{
SubscriptionValidator<Type>::check(format);
const auto nameVersion = AlgorithmFactory::Instance().subscribe<Type>();
// If the factory didn't throw then the name is valid
m_names[format].insert(nameVersion);
......@@ -89,6 +93,33 @@ namespace Mantid
/// Destructor
~FileLoaderRegistryImpl();
/// Helper for subscribe to check base class
template <typename T>
struct SubscriptionValidator
{
static void check(LoaderFormat format)
{
switch(format)
{
case HDF:
if(!boost::is_base_of<IHDFFileLoader,T>::value)
{
throw std::runtime_error(std::string("FileLoaderRegistryImpl::subscribe - Class '") + typeid(T).name() +
"' registered as HDF loader but it does not inherit from Mantid::API::IHDFFileLoader");
}
break;
case NonHDF:
if(!boost::is_base_of<IFileLoader,T>::value)
{
throw std::runtime_error(std::string("FileLoaderRegistryImpl::subscribe - Class '") + typeid(T).name() +
"' registered as Non-HDF loader but it does not inherit from Mantid::API::IFileLoader");
}
break;
default: throw std::runtime_error("Invalid LoaderFormat given");
}
}
};
/// The list of names. The index pointed to by LoaderFormat defines a set for that format
std::vector<std::multimap<std::string,int> > m_names;
/// Total number of names registered
......
#ifndef MANTID_API_IHDFFILELOADER_H_
#define MANTID_API_IHDFFILELOADER_H_
#include "MantidAPI/IFileLoader.h"
#include "MantidKernel/FileDescriptor.h"
#include "MantidAPI/Algorithm.h"
#include "MantidKernel/HDFDescriptor.h"
namespace Mantid
......
......@@ -3,6 +3,9 @@
#include "MantidAPI/FileLoaderRegistry.h"
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_base_of.hpp>
/**
* DECLARE_FILELOADER_ALGORITHM should be used in place of the standard
* DECLARE_ALGORITHM macro when writing a file loading algorithm that
......@@ -13,6 +16,9 @@
#define DECLARE_FILELOADER_ALGORITHM(classname) \
namespace \
{\
typedef boost::is_base_of<Mantid::API::IFileLoader,classname> base_checker;\
BOOST_STATIC_ASSERT_MSG(base_checker::value,\
#classname" must inherit from Mantid::API::IFileLoader to be used with DECLARE_FILELOADER_ALGORITHM");\
Mantid::Kernel::RegistrationHelper \
reg_loader_##classname((Mantid::API::\
FileLoaderRegistry::Instance().subscribe<classname>(Mantid::API::FileLoaderRegistryImpl::NonHDF), 0));\
......@@ -28,6 +34,9 @@
#define DECLARE_HDF_FILELOADER_ALGORITHM(classname) \
namespace \
{\
typedef boost::is_base_of<Mantid::API::IHDFFileLoader,classname> base_checker;\
BOOST_STATIC_ASSERT_MSG(base_checker::value,\
#classname" must inherit from Mantid::API::IHDFFileLoader to be used with DECLARE_HDF_FILELOADER_ALGORITHM");\
Mantid::Kernel::RegistrationHelper \
reg_hdf_loader_##classname((Mantid::API::\
FileLoaderRegistry::Instance().subscribe<classname>(Mantid::API::FileLoaderRegistryImpl::HDF), 0)); \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment