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

Add basic code for FileLoaderRegistry. Refs #7263

Currently based on string names.
parent 399f9b7d
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,7 @@ set ( SRC_FILES ...@@ -28,6 +28,7 @@ set ( SRC_FILES
src/Expression.cpp src/Expression.cpp
src/FermiChopperModel.cpp src/FermiChopperModel.cpp
src/FileFinder.cpp src/FileFinder.cpp
src/FileLoaderRegistry.cpp
src/FileProperty.cpp src/FileProperty.cpp
src/FrameworkManager.cpp src/FrameworkManager.cpp
src/FuncMinimizerFactory.cpp src/FuncMinimizerFactory.cpp
...@@ -152,6 +153,7 @@ set ( INC_FILES ...@@ -152,6 +153,7 @@ set ( INC_FILES
inc/MantidAPI/Expression.h inc/MantidAPI/Expression.h
inc/MantidAPI/FermiChopperModel.h inc/MantidAPI/FermiChopperModel.h
inc/MantidAPI/FileFinder.h inc/MantidAPI/FileFinder.h
inc/MantidAPI/FileLoaderRegistry.h
inc/MantidAPI/FileProperty.h inc/MantidAPI/FileProperty.h
inc/MantidAPI/FrameworkManager.h inc/MantidAPI/FrameworkManager.h
inc/MantidAPI/FuncMinimizerFactory.h inc/MantidAPI/FuncMinimizerFactory.h
...@@ -282,6 +284,7 @@ set ( TEST_FILES ...@@ -282,6 +284,7 @@ set ( TEST_FILES
ExpressionTest.h ExpressionTest.h
FermiChopperModelTest.h FermiChopperModelTest.h
FileFinderTest.h FileFinderTest.h
FileLoaderRegistryTest.h
FilePropertyTest.h FilePropertyTest.h
FrameworkManagerTest.h FrameworkManagerTest.h
FuncMinimizerFactoryTest.h FuncMinimizerFactoryTest.h
......
#ifndef MANTID_API_FILELOADERREGISTRY_H_
#define MANTID_API_FILELOADERREGISTRY_H_
#include "MantidAPI/DllConfig.h"
#include <set>
#include <string>
namespace Mantid
{
namespace API
{
/**
Copyright &copy; 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National 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://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class MANTID_API_DLL FileLoaderRegistry
{
public:
/// Default constructor
FileLoaderRegistry();
/// @returns the number of entries in the registry
inline size_t size() const { return m_names.size(); }
/// Adds an entry
void subscribe(const std::string & name);
/// Pick the best loader for the given filename
std::string findLoader(const std::string & filename) const;
private:
/// The registered names
std::set<std::string> m_names;
};
} // namespace API
} // namespace Mantid
#endif /* MANTID_API_FILELOADERREGISTRY_H_ */
#include "MantidAPI/FileLoaderRegistry.h"
#include "MantidKernel/Exception.h"
#include <Poco/File.h>
namespace Mantid
{
namespace API
{
//----------------------------------------------------------------------------------------------
// Public members
//----------------------------------------------------------------------------------------------
/**
* Creates an empty registry
*/
FileLoaderRegistry::FileLoaderRegistry()
{
}
/**
* @param name The string name of the entry
* @throws std::invalid_argument if an entry with this name already exists
*/
void FileLoaderRegistry::subscribe(const std::string & name)
{
auto insertionResult = m_names.insert(name);
if(!insertionResult.second)
{
throw std::invalid_argument("FileLoaderRegistry::subscribe - Cannot subscribe '"
+ name + "'. An entry with that name already exists");
}
}
/**
* Attempts to pick the best suited loader for the given file name from those in the registry
* @param filename A string that should point to an existing file
* @throws std::invalid_argument if the filename does not point to an existing file or an empty string is given
* @throws Exception::NotFoundError if a loader could not be found
*/
std::string FileLoaderRegistry::findLoader(const std::string & filename) const
{
if(filename.empty() || !Poco::File(filename).exists())
{
throw std::invalid_argument("FileLoaderRegistry::chooserLoader - Cannot open file '" + filename + "'");
}
}
//----------------------------------------------------------------------------------------------
// Private members
//----------------------------------------------------------------------------------------------
} // namespace API
} // namespace Mantid
#ifndef MANTID_API_FILELOADERREGISTRYTEST_H_
#define MANTID_API_FILELOADERREGISTRYTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidAPI/FileLoaderRegistry.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("LoadEventNexus"));
TS_ASSERT_EQUALS(1, registry.size());
}
// ======================== Failure cases ===================================
void test_Adding_Entry_That_Already_Exists_Throws_Error_And_Keeps_The_Size_The_Same()
{
FileLoaderRegistry registry;
registry.subscribe("LoadEventNexus");
TS_ASSERT_THROWS(registry.subscribe("LoadEventNexus"), std::invalid_argument);
TS_ASSERT_EQUALS(1, registry.size());
}
void test_Finding_A_Loader_Throws_Invalid_Argument_If_Filename_Does_Not_Point_To_Valid_File()
{
FileLoaderRegistry registry;
TS_ASSERT_THROWS(registry.findLoader(""), std::invalid_argument);
TS_ASSERT_THROWS(registry.findLoader("__notafile.txt__"), std::invalid_argument);
}
};
#endif /* MANTID_API_FILELOADERREGISTRYTEST_H_ */
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