Skip to content

Commit

Permalink
Add base-class checks to FILELOADER* check macros. Refs #7263
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
martyngigg committed Jul 4, 2013
1 parent 4ff024a commit e3e18db
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
33 changes: 32 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#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>
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/IHDFFileLoader.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#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
Expand Down
9 changes: 9 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/RegisterFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));\
Expand All @@ -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)); \
Expand Down

0 comments on commit e3e18db

Please sign in to comment.