Newer
Older
#ifndef MANTID_KERNEL_CONFIGSERVICE_H_
#define MANTID_KERNEL_CONFIGSERVICE_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
Gigg, Martyn Anthony
committed
#include <vector>
#include <map>
#include "MantidKernel/DllExport.h"
#include "MantidKernel/SingletonHolder.h"
//----------------------------------------------------------------------
// Forward declaration
//----------------------------------------------------------------------
/// @cond Exclude from doxygen documentation
namespace Poco
{
Gigg, Martyn Anthony
committed
namespace Util
{
class PropertyFileConfiguration;
class SystemConfiguration;
}
}
namespace Mantid
{
Gigg, Martyn Anthony
committed
namespace Kernel
{
class Logger;
Gigg, Martyn Anthony
committed
/** The ConfigService class provides a simple facade to access the Configuration functionality of the Mantid Framework.
The class gathers information from config files and the system variables.
This information is available to all the objects within the framework as well as being used to configure the logging framework.
This class currently uses the Logging functionality provided through the POCO (portable components library).
Gigg, Martyn Anthony
committed
@author Nicholas Draper, Tessella Support Services plc
@date 15/10/2007
Gigg, Martyn Anthony
committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Copyright © 2007 STFC Rutherford Appleton Laboratories
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://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class EXPORT_OPT_MANTID_KERNEL ConfigServiceImpl
{
/** Inner templated class to wrap the poco library objects that have protected
* desctructors and expose them as public.
*/
template<typename T >
class WrappedObject : public T
{
public:
/// The template type of class that is being wrapped
typedef T element_type;
/// Simple constructor
WrappedObject() : T()
{
m_pPtr = static_cast<T*>(this);
}
Gigg, Martyn Anthony
committed
/** Constructor with a class to wrap
* @param F The object to wrap
*/
template<typename Field>
WrappedObject(Field& F) : T(F)
{
m_pPtr = static_cast<T*>(this);
}
Gigg, Martyn Anthony
committed
/// Copy constructor
WrappedObject(const WrappedObject<T>& A) : T(A)
{
Gigg, Martyn Anthony
committed
m_pPtr = static_cast<T*>(this);
}
Gigg, Martyn Anthony
committed
/// Virtual destructor
virtual ~WrappedObject()
{}
Gigg, Martyn Anthony
committed
/// Overloaded * operator returns the wrapped object pointer
const T& operator*() const { return *m_pPtr; }
/// Overloaded * operator returns the wrapped object pointer
T& operator*() { return m_pPtr; }
/// Overloaded -> operator returns the wrapped object pointer
const T* operator->() const{ return m_pPtr; }
/// Overloaded -> operator returns the wrapped object pointer
T* operator->() { return m_pPtr; }
private:
/// Private pointer to the wrapped class
T* m_pPtr;
};
// Back to the ConfigService class itself...
Gigg, Martyn Anthony
committed
public:
// Loads a config file
void loadConfig(const std::string& filename, const bool append=false);
Gigg, Martyn Anthony
committed
// Searches for a configuration property
std::string getString(const std::string& keyName);
// Searches for a configuration property and returns its value
template<typename T>
int getValue(const std::string& keyName, T& out);
// Searches for the given environment variable and returns it as a string
std::string getEnvironment(const std::string& keyName);
// Getters for properties of the host system
std::string getOSName();
std::string getComputerName();
std::string getOSArchitecture();
std::string getOSVersion();
std::string getCurrentDir();
std::string getTempDir();
std::string getBaseDir() const;
std::string getOutputDir() const;
/// Get the list of search paths
const std::vector<std::string>& getDataSearchDirs() const;
private:
friend struct Mantid::Kernel::CreateUsingNew<ConfigServiceImpl>;
Gigg, Martyn Anthony
committed
// Private constructors and destructor for singleton class
ConfigServiceImpl();
/// Private copy constructor. Prevents singleton being copied.
ConfigServiceImpl(const ConfigServiceImpl&);
Gigg, Martyn Anthony
committed
virtual ~ConfigServiceImpl();
/// Provies a string of a default configuration
const std::string defaultConfig() const;
Gigg, Martyn Anthony
committed
/// Writes out a fresh user properties file
void createUserPropertiesFile() const;
Gigg, Martyn Anthony
committed
/// Convert any relative paths to absolute ones and store them locally so that
/// if the working directory is altered the paths will not be affected
void convertRelativeToAbsolute();
Gigg, Martyn Anthony
committed
/// Create the storage of the data search directories
void defineDataSearchPaths();
Gigg, Martyn Anthony
committed
private:
/// the POCO file config object
WrappedObject<Poco::Util::PropertyFileConfiguration>* m_pConf;
/// the POCO system Config Object
WrappedObject<Poco::Util::SystemConfiguration>* m_pSysConfig;
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
/// static reference to the logger class
Logger& g_log;
Gigg, Martyn Anthony
committed
/// A list of keys that may contain relative paths and need to be altered
std::vector<std::string> m_vConfigPaths;
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
/// Local storage for the relative path key/values that have been changed
std::map<std::string, std::string> m_mAbsolutePaths;
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
/// The directory that is considered to be the base directory
std::string m_strBaseDir;
Gigg, Martyn Anthony
committed
///The configuration properties in string format
std::string m_PropertyString;
Gigg, Martyn Anthony
committed
/// The filename of the Mantid properties file
const std::string m_properties_file_name;
/// The filename of the Mantid user properties file
const std::string m_user_properties_file_name;
/// Store a list of data search paths
std::vector<std::string> m_vDataSearchDirs;
};
Gigg, Martyn Anthony
committed
///Forward declaration of a specialisation of SingletonHolder for AlgorithmFactoryImpl (needed for dllexport/dllimport) and a typedef for it.
Gigg, Martyn Anthony
committed
inline
Gigg, Martyn Anthony
committed
template class EXPORT_OPT_MANTID_KERNEL Mantid::Kernel::SingletonHolder<ConfigServiceImpl>;
typedef EXPORT_OPT_MANTID_KERNEL Mantid::Kernel::SingletonHolder<ConfigServiceImpl> ConfigService;
Gigg, Martyn Anthony
committed
} // namespace Kernel
} // namespace Mantid
#endif /*MANTID_KERNEL_CONFIGSERVICE_H_*/