diff --git a/Code/Mantid/Framework/API/CMakeLists.txt b/Code/Mantid/Framework/API/CMakeLists.txt index 95810be6e9e06c02b1c156a87e1b2797a38b6538..3233f426679d5d6b391d8870c0d3e04d05b1471b 100644 --- a/Code/Mantid/Framework/API/CMakeLists.txt +++ b/Code/Mantid/Framework/API/CMakeLists.txt @@ -286,7 +286,6 @@ set ( TEST_FILES ExpressionTest.h FermiChopperModelTest.h FileFinderTest.h - FileLoaderRegistryTest.h FilePropertyTest.h FrameworkManagerTest.h FuncMinimizerFactoryTest.h diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h b/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h index 3255cb9cc8e1a1902e308b8624e3c295878071eb..5d365aa815fda01e02f7331d0313989a91148d82 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h @@ -3,6 +3,7 @@ #include "MantidAPI/DllConfig.h" #include "MantidAPI/AlgorithmFactory.h" +#include "MantidKernel/SingletonHolder.h" #include <set> #include <string> @@ -20,7 +21,7 @@ namespace Mantid /** Keeps a registry of algorithm's that are file loading algorithms to allow them to be searched - to find the correct one to load a particular file. Uses FileLoaderPicker to do the most of the work + to find the correct one to load a particular file. A macro, DECLARE_FILELOADER_ALGORITHM is defined in RegisterFileLoader.h. Use this in place of the standard DECLARE_ALGORITHM macro @@ -45,7 +46,7 @@ namespace Mantid File change history is stored at: <https://github.com/mantidproject/mantid> Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class MANTID_API_DLL FileLoaderRegistry + class MANTID_API_DLL FileLoaderRegistryImpl { public: @@ -53,9 +54,6 @@ namespace Mantid enum LoaderFormat { NonHDF, HDF }; public: - /// Default constructor - FileLoaderRegistry(); - /// @returns the number of entries in the registry inline size_t size() const { return m_totalSize; } @@ -81,6 +79,14 @@ namespace Mantid const std::string chooseLoader(const std::string &filename) const; private: + /// Friend so that CreateUsingNew + friend struct Mantid::Kernel::CreateUsingNew<FileLoaderRegistryImpl>; + + /// Default constructor (for singleton) + FileLoaderRegistryImpl(); + /// Destructor + ~FileLoaderRegistryImpl(); + /// The list of names. The index pointed to by LoaderFormat defines a set for that format std::vector<std::set<std::string> > m_names; /// Total number of names registered @@ -90,6 +96,15 @@ namespace Mantid Kernel::Logger & m_log; }; + ///Forward declaration of a specialisation of SingletonHolder for FileLoaderRegistryImpl (needed for dllexport/dllimport) and a typedef for it. + #ifdef _WIN32 + // this breaks new namespace declaration rules; need to find a better fix + template class MANTID_API_DLL Mantid::Kernel::SingletonHolder<FileLoaderRegistryImpl>; + #endif /* _WIN32 */ + + /// Type for the actual singleton instance + typedef MANTID_API_DLL Mantid::Kernel::SingletonHolder<FileLoaderRegistryImpl> FileLoaderRegistry; + } // namespace API } // namespace Mantid diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/FrameworkManager.h b/Code/Mantid/Framework/API/inc/MantidAPI/FrameworkManager.h index 0861cef25a2cbd3a46656cbbe35f850bac3eb2fc..3fb03f3ea06ef6aa45b93c38f8c7efc4f170b5de 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/FrameworkManager.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/FrameworkManager.h @@ -90,11 +90,6 @@ namespace Mantid /// Creates an algorithm and runs it, with variadic arguments boost::shared_ptr<IAlgorithm> exec(const std::string& algorithmName, int count, ...); - /// Returns a const version of the main registry of file loader algorithms - inline const FileLoaderRegistry & fileLoaderRegistry() const { return m_fileLoaderRegistry; } - /// Returns a non-const version of the main registry of file loader algorithms - inline FileLoaderRegistry & fileLoaderRegistry() { return m_fileLoaderRegistry; } - /// Returns a shared pointer to the workspace requested Workspace* getWorkspace(const std::string& wsName); @@ -122,8 +117,6 @@ namespace Mantid /// Silence NeXus output void disableNexusOutput(); - /// The registry of FileLoader algorithms - FileLoaderRegistry m_fileLoaderRegistry; /// Reference to the logger class Kernel::Logger& g_log; diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/RegisterFileLoader.h b/Code/Mantid/Framework/API/inc/MantidAPI/RegisterFileLoader.h index 2c9e0c6eb65668021156fec2192831d202bd69f2..d7ebe1d540950946f964fa125ea428574c5acfbc 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/RegisterFileLoader.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/RegisterFileLoader.h @@ -1,8 +1,7 @@ #ifndef MANTID_API_REGISTERFILELOADER_H_ #define MANTID_API_REGISTERFILELOADER_H_ -#include "MantidAPI/AlgorithmFactory.h" -#include "MantidAPI/FrameworkManager.h" +#include "MantidAPI/FileLoaderRegistry.h" /** * DECLARE_FILELOADER_ALGORITHM should be used in place of the standard @@ -16,7 +15,7 @@ {\ Mantid::Kernel::RegistrationHelper \ reg_loader_##classname((Mantid::API::\ - FrameworkManager::Instance().fileLoaderRegistry().subscribe<classname>(Mantid::API::FileLoaderRegistry::NonHDF), 0));\ + FileLoaderRegistry::Instance().subscribe<classname>(Mantid::API::FileLoaderRegistryImpl::NonHDF), 0));\ } /** @@ -31,7 +30,7 @@ {\ Mantid::Kernel::RegistrationHelper \ reg_hdf_loader_##classname((Mantid::API::\ - FrameworkManager::Instance().fileLoaderRegistry().subscribe<classname>(Mantid::API::FileLoaderRegistry::HDF), 0)); \ + FileLoaderRegistry::Instance().subscribe<classname>(Mantid::API::FileLoaderRegistryImpl::HDF), 0)); \ } diff --git a/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp b/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp index 14e8305514288d94ce0638009494bc8735951332..0554a9b85e121096790fc492ec4d4dbd26789455 100644 --- a/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp +++ b/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp @@ -73,14 +73,6 @@ namespace Mantid //---------------------------------------------------------------------------------------------- // Public members //---------------------------------------------------------------------------------------------- - /** - * Creates an empty registry - */ - FileLoaderRegistry::FileLoaderRegistry() : - m_names(2, std::set<std::string>()), m_totalSize(0), - m_log(Kernel::Logger::get("FileLoaderRegistry")) - { - } /** * Queries each registered algorithm and asks it how confident it is that it can @@ -89,7 +81,7 @@ namespace Mantid * @return A string containing the name of an algorithm to load the file * @throws Exception::NotFoundError if an algorithm cannot be found */ - const std::string FileLoaderRegistry::chooseLoader(const std::string &filename) const + const std::string FileLoaderRegistryImpl::chooseLoader(const std::string &filename) const { using Kernel::FileDescriptor; using Kernel::HDFDescriptor; @@ -122,6 +114,20 @@ namespace Mantid //---------------------------------------------------------------------------------------------- // Private members //---------------------------------------------------------------------------------------------- + /** + * Creates an empty registry + */ + FileLoaderRegistryImpl::FileLoaderRegistryImpl() : + m_names(2, std::set<std::string>()), m_totalSize(0), + m_log(Kernel::Logger::get("FileLoaderRegistry")) + { + } + + /** + */ + FileLoaderRegistryImpl::~FileLoaderRegistryImpl() + { + } } // namespace API } // namespace Mantid diff --git a/Code/Mantid/Framework/API/src/FrameworkManager.cpp b/Code/Mantid/Framework/API/src/FrameworkManager.cpp index 40f23e1d99fb2de4562b573863c3feb1dd9e61cf..bb3e3801fc99ab459a4966b3dace2b4ba312fd00 100644 --- a/Code/Mantid/Framework/API/src/FrameworkManager.cpp +++ b/Code/Mantid/Framework/API/src/FrameworkManager.cpp @@ -42,8 +42,7 @@ namespace API /// Default constructor -FrameworkManagerImpl::FrameworkManagerImpl() - : m_fileLoaderRegistry(), g_log(Kernel::Logger::get("FrameworkManager")) +FrameworkManagerImpl::FrameworkManagerImpl() : g_log(Kernel::Logger::get("FrameworkManager")) #ifdef MPI_BUILD , m_mpi_environment() #endif diff --git a/Code/Mantid/Framework/API/test/FileLoaderRegistryTest.h b/Code/Mantid/Framework/API/test/FileLoaderRegistryTest.h deleted file mode 100644 index 8325820893cf8733ef8da6ef46d34d8bbc579a43..0000000000000000000000000000000000000000 --- a/Code/Mantid/Framework/API/test/FileLoaderRegistryTest.h +++ /dev/null @@ -1,117 +0,0 @@ -#ifndef MANTID_API_FILELOADERREGISTRYTEST_H_ -#define MANTID_API_FILELOADERREGISTRYTEST_H_ - -#include <cxxtest/TestSuite.h> -#include "MantidAPI/Algorithm.h" -#include "MantidAPI/FileFinder.h" -#include "MantidAPI/FileLoaderRegistry.h" -#include "MantidAPI/IFileLoader.h" -#include "MantidKernel/FileDescriptor.h" - -using Mantid::API::FileLoaderRegistry; - -class FileLoaderRegistryTest : public CxxTest::TestSuite -{ -public: - // This pair of boilerplate methods prevent the suite being created statically - // This means the constructor isn't called when running other tests - static FileLoaderRegistryTest *createSuite() { return new FileLoaderRegistryTest(); } - static void destroySuite( FileLoaderRegistryTest *suite ) { delete suite; } - - void test_Construction_Gives_Empty_Registry() - { - FileLoaderRegistry registry; - - TS_ASSERT_EQUALS(0, registry.size()); - } - - void test_Subscribing_Entry_That_Does_Not_Exist_Increases_Size_By_One() - { - FileLoaderRegistry registry; - - TS_ASSERT_THROWS_NOTHING(registry.subscribe<StubNonLoader>(FileLoaderRegistry::NonHDF)); - TS_ASSERT_EQUALS(1, registry.size()); - - // We can't mock the factory as it's a singleton so make sure we clean up - Mantid::API::AlgorithmFactory::Instance().unsubscribe("StubNonLoader", 1); - } - - void test_chooseLoader_Throws_For_NonExistant_File() - { - FileLoaderRegistry registry; - - TS_ASSERT_THROWS(registry.chooseLoader("__not_a_file.txt"), std::invalid_argument); - } - - void test_chooseLoader_Returns_Expected_Loader_Name_For_Given_File() - { - FileLoaderRegistry registry; - registry.subscribe<RawLoader>(FileLoaderRegistry::NonHDF); - registry.subscribe<TxtLoader>(FileLoaderRegistry::NonHDF); - - const std::string filename = Mantid::API::FileFinder::Instance().getFullPath("AsciiExample.txt"); - - std::string algName; - TS_ASSERT_THROWS_NOTHING(algName = registry.chooseLoader(filename)); - TS_ASSERT_EQUALS("TxtLoader", algName); - - // We can't mock the factory as it's a singleton so make sure we clean up - Mantid::API::AlgorithmFactory::Instance().unsubscribe("RawLoader", 1); - Mantid::API::AlgorithmFactory::Instance().unsubscribe("TxtLoader", 1); - } - - // ======================== Failure cases =================================== - void test_Adding_Entry_That_Already_Exists_Throws_Error_And_Keeps_The_Size_The_Same() - { - FileLoaderRegistry registry; - registry.subscribe<StubNonLoader>(FileLoaderRegistry::NonHDF); - - TS_ASSERT_THROWS(registry.subscribe<StubNonLoader>(FileLoaderRegistry::NonHDF), std::runtime_error); - TS_ASSERT_EQUALS(1, registry.size()); - - // We can't mock the factory as it's a singleton so make sure we clean up - Mantid::API::AlgorithmFactory::Instance().unsubscribe("StubNonLoader", 1); - } - -private: - // Stub algorithm for test - struct StubNonLoader : Mantid::API::Algorithm - { - const std::string name() const { return "StubNonLoader"; } - int version() const { return 1; } - void init() {}; - void exec() {}; - }; - - // Stub algorithm for test - struct RawLoader : Mantid::API::IFileLoader - { - const std::string name() const { return "RawLoader"; } - int version() const { return 1; } - void init() {}; - void exec() {}; - int confidence(const Mantid::Kernel::FileDescriptor & descr) const - { - if(descr.extension() == ".raw") return 80; - else return 0; - } - }; - - // Stub algorithm for test - struct TxtLoader : Mantid::API::IFileLoader - { - const std::string name() const { return "TxtLoader"; } - int version() const { return 1; } - void init() {}; - void exec() {}; - int confidence(const Mantid::Kernel::FileDescriptor & descr) const - { - if(descr.extension() == ".txt") return 80; - else return 0; - } - }; - -}; - - -#endif /* MANTID_API_FILELOADERREGISTRYTEST_H_ */