-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic code for FileLoaderRegistry. Refs #7263
Currently based on string names.
- Loading branch information
1 parent
b69909f
commit 886d0b0
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef MANTID_API_FILELOADERREGISTRY_H_ | ||
#define MANTID_API_FILELOADERREGISTRY_H_ | ||
|
||
#include "MantidAPI/DllConfig.h" | ||
|
||
#include <set> | ||
#include <string> | ||
|
||
namespace Mantid | ||
{ | ||
namespace API | ||
{ | ||
|
||
/** | ||
Copyright © 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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#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_ */ |