From 9dc140841fdd8d29ca1d6ba2bdd949c7c41ee1af Mon Sep 17 00:00:00 2001
From: Martyn Gigg <martyn.gigg@stfc.ac.uk>
Date: Tue, 25 Jun 2013 17:28:29 +0100
Subject: [PATCH] Add DECLARE_FILELOADER_ALGORITHM macro. Refs #7263

---
 .../API/inc/MantidAPI/FileLoaderRegistry.h    | 11 ++++++++++
 .../API/inc/MantidAPI/FrameworkManager.h      | 19 +++++++++++++-----
 .../API/inc/MantidAPI/RegisterFileLoader.h    | 20 +++++++++++++++++++
 .../Framework/API/src/FileLoaderRegistry.cpp  |  6 +++++-
 4 files changed, 50 insertions(+), 6 deletions(-)
 create mode 100644 Code/Mantid/Framework/API/inc/MantidAPI/RegisterFileLoader.h

diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h b/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h
index b3a06cd6efe..cda2b301c05 100644
--- a/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h
+++ b/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h
@@ -8,11 +8,20 @@
 
 namespace Mantid
 {
+  // Forward declaration
+  namespace Kernel
+  {
+    class Logger;
+  }
   namespace API
   {
 
     /**
+    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
 
+    A macro, DECLARE_FILELOADER_ALGORITHM is defined in RegisterFileLoader.h. Use this in place of the standard
+    DECLARE_ALGORITHM macro
 
     Copyright &copy; 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
 
@@ -50,6 +59,8 @@ namespace Mantid
     private:
       /// The registered names
       std::set<std::string> m_names;
+      /// Reference to a logger
+      Kernel::Logger & m_log;
     };
 
   } // namespace API
diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/FrameworkManager.h b/Code/Mantid/Framework/API/inc/MantidAPI/FrameworkManager.h
index c82cfb827ac..0861cef25a2 100644
--- a/Code/Mantid/Framework/API/inc/MantidAPI/FrameworkManager.h
+++ b/Code/Mantid/Framework/API/inc/MantidAPI/FrameworkManager.h
@@ -1,5 +1,5 @@
-#ifndef MANTID_KERNEL_FRAMEWORKMANAGER_H_
-#define MANTID_KERNEL_FRAMEWORKMANAGER_H_
+#ifndef MANTID_API_FRAMEWORKMANAGER_H_
+#define MANTID_API_FRAMEWORKMANAGER_H_
 
 //----------------------------------------------------------------------
 // Includes
@@ -11,6 +11,7 @@
 #endif
 
 #include "MantidAPI/DllConfig.h"
+#include "MantidAPI/FileLoaderRegistry.h"
 #include "MantidKernel/SingletonHolder.h"
 #include <boost/shared_ptr.hpp>
 
@@ -89,6 +90,11 @@ 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);
 
@@ -116,9 +122,12 @@ namespace Mantid
       /// Silence NeXus output
       void disableNexusOutput();
 
-      Kernel::Logger& g_log;    ///< Reference to the logger class
+      /// The registry of FileLoader algorithms
+      FileLoaderRegistry m_fileLoaderRegistry;
+      /// Reference to the logger class
+      Kernel::Logger& g_log;
 
-#ifdef MPI_BUILD
+      #ifdef MPI_BUILD
       /** Member variable that initialises the MPI environment on construction (in the
        *  FrameworkManager constructor) and finalises it on destruction.
        *  The class has no non-static member functions, so is not exposed in the class interface.
@@ -137,4 +146,4 @@ namespace Mantid
   } // namespace Kernel
 } // namespace Mantid
 
-#endif /*MANTID_KERNEL_FRAMEWORKMANAGER_H_*/
+#endif /*MANTID_API_FRAMEWORKMANAGER_H_*/
diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/RegisterFileLoader.h b/Code/Mantid/Framework/API/inc/MantidAPI/RegisterFileLoader.h
new file mode 100644
index 00000000000..f85c4457b2a
--- /dev/null
+++ b/Code/Mantid/Framework/API/inc/MantidAPI/RegisterFileLoader.h
@@ -0,0 +1,20 @@
+#ifndef MANTID_API_REGISTERFILELOADER_H_
+#define MANTID_API_REGISTERFILELOADER_H_
+
+#include "MantidAPI/AlgorithmFactory.h"
+#include "MantidAPI/FrameworkManager.h"
+
+/**
+ * DECLARE_FILELOADER_ALGORITHM should be used in place of the standard
+ * DECLARE_ALGORITHM macro that both registers the algorithm as usual and subscribes it to the
+ * registry held in the FrameworkManager
+ */
+#define DECLARE_FILELOADER_ALGORITHM(classname) \
+  DECLARE_ALGORITHM(classname) \
+  namespace \
+  {\
+    Mantid::Kernel::RegistrationHelper \
+      reg_loader_##classname((Mantid::API::FrameworkManager::Instance().fileLoaderRegistry().subscribe(#classname), 0));\
+  }
+
+#endif /* MANTID_API_REGISTERFILELOADER_H_ */
diff --git a/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp b/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp
index 229c92c608c..e14f6cae0f9 100644
--- a/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp
+++ b/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp
@@ -1,5 +1,6 @@
 #include "MantidAPI/FileLoaderRegistry.h"
 #include "MantidKernel/Exception.h"
+#include "MantidKernel/Logger.h"
 
 #include <Poco/File.h>
 
@@ -13,7 +14,7 @@ namespace Mantid
     /**
      * Creates an empty registry
      */
-    FileLoaderRegistry::FileLoaderRegistry()
+    FileLoaderRegistry::FileLoaderRegistry() : m_log(Kernel::Logger::get("FileLoaderRegistry"))
     {
     }
 
@@ -29,6 +30,7 @@ namespace Mantid
         throw std::invalid_argument("FileLoaderRegistry::subscribe - Cannot subscribe '"
             + name + "'. An entry with that name already exists");
       }
+      m_log.debug() << "Registered '" << name << "' as file loader\n";
     }
 
     /**
@@ -43,6 +45,8 @@ namespace Mantid
       {
         throw std::invalid_argument("FileLoaderRegistry::chooserLoader - Cannot open file '" + filename + "'");
       }
+      m_log.debug() << "Attempting to find loader for '" << filename << "'\n";
+      throw std::runtime_error("not implemented yet");
     }
 
     //----------------------------------------------------------------------------------------------
-- 
GitLab