Newer
Older
#ifndef MANTID_KERNEL_IPROPERTYMANAGER_H_
#define MANTID_KERNEL_IPROPERTYMANAGER_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <vector>
#include <map>
#include "MantidKernel/PropertyWithValue.h"
namespace Mantid
{
namespace Kernel
{
//----------------------------------------------------------------------
// Forward Declaration
//----------------------------------------------------------------------
class Logger;
/** @class IPropertyManager IPropertyManager.h Kernel/IPropertyManager.h
Interface to PropertyManager
@author Russell Taylor, Tessella Support Services plc
@author Based on the Gaudi class PropertyMgr (see http://proj-gaudi.web.cern.ch/proj-gaudi/)
@date 20/11/2007
@author Roman Tolchenov, Tessella plc
@date 02/03/2009
Copyright © 2007-8 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://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport IPropertyManager
{
public:
//IPropertyManager(){}
virtual ~IPropertyManager(){}
Roman Tolchenov
committed
/// Function to declare properties (i.e. store them)
Steve Williams
committed
virtual void declareProperty(Property *p, const std::string &doc="" ) = 0;
Roman Tolchenov
committed
/** Sets all the declared properties from a string.
Janik Zikovsky
committed
@param propertiesArray :: A list of name = value pairs separated by a semicolon
Roman Tolchenov
committed
*/
virtual void setProperties(const std::string &propertiesArray) = 0;
Roman Tolchenov
committed
/** Sets property value from a string
Janik Zikovsky
committed
@param name :: Property name
@param value :: New property value
Roman Tolchenov
committed
*/
virtual void setPropertyValue(const std::string &name, const std::string &value) = 0;
Roman Tolchenov
committed
/// Set the value of a property by an index
virtual void setPropertyOrdinal(const int &index, const std::string &value) = 0;
Roman Tolchenov
committed
/// Checks whether the named property is already in the list of managed property.
virtual bool existsProperty(const std::string &name) const = 0;
Roman Tolchenov
committed
/// Validates all the properties in the collection
Gigg, Martyn Anthony
committed
/// Returns the number of properties under management
virtual size_t propertyCount() const = 0;
Roman Tolchenov
committed
/// Get the value of a property as a string
virtual std::string getPropertyValue(const std::string &name) const = 0;
Roman Tolchenov
committed
/// Get the list of managed properties.
virtual const std::vector< Property*>& getProperties() const = 0;
/** Templated method to set the value of a PropertyWithValue
Janik Zikovsky
committed
* @param name :: The name of the property (case insensitive)
* @param value :: The value to assign to the property
* @throw Exception::NotFoundError If the named property is unknown
* @throw std::invalid_argument If an attempt is made to assign to a property of different type
*/
template <typename T>
IPropertyManager* setProperty(const std::string &name, const T & value)
{
PropertyWithValue<T> *prop = dynamic_cast<PropertyWithValue<T>*>(getPointerToProperty(name));
if (prop)
{
*prop = value;
}
else
{
throw std::invalid_argument("Attempt to assign to property (" + name + ") of incorrect type");
}
return this;
}
/// Specialised version of setProperty template method
IPropertyManager* setProperty(const std::string &name, const char* value)
{
this->setPropertyValue(name, std::string(value));
return this;
Gigg, Martyn Anthony
committed
/// Return the property manager serialized as a string.
Gigg, Martyn Anthony
committed
virtual std::string toString(bool withDefaultValues = false) const = 0;
protected:
/** Add a property of the template type to the list of managed properties
Janik Zikovsky
committed
* @param name :: The name to assign to the property
* @param value :: The initial value to assign to the property
* @param validator :: Pointer to the (optional) validator. Ownership will be taken over.
* @param doc :: The (optional) documentation string
* @param direction :: The (optional) direction of the property, in, out or inout
* @throw Exception::ExistsError if a property with the given name already exists
* @throw std::invalid_argument if the name argument is empty
*/
template <typename T>
void declareProperty(const std::string &name, T value,
IValidator<T> *validator = new NullValidator<T>, const std::string &doc="", const unsigned int direction = Direction::Input)
{
Property *p = new PropertyWithValue<T>(name, value, validator, direction);
Steve Williams
committed
declareProperty(p, doc);
Steve Williams
committed
/** Add a property to the list of managed properties with no validator
Janik Zikovsky
committed
* @param name :: The name to assign to the property
* @param value :: The initial value to assign to the property
* @param doc :: The documentation string
* @param direction :: The (optional) direction of the property, in (default), out or inout
Steve Williams
committed
* @throw Exception::ExistsError if a property with the given name already exists
* @throw std::invalid_argument if the name argument is empty
*/
template <typename T>
void declareProperty(const std::string &name, T value, const std::string &doc,
const unsigned int direction = Direction::Input)
{
Steve Williams
committed
Property *p = new PropertyWithValue<T>(name, value, new NullValidator<T>, direction);
Steve Williams
committed
declareProperty(p, doc);
}
/** Add a property of the template type to the list of managed properties
Janik Zikovsky
committed
* @param name :: The name to assign to the property
* @param value :: The initial value to assign to the property
* @param direction :: The direction of the property, in, out or inout
* @throw Exception::ExistsError if a property with the given name already exists
* @throw std::invalid_argument if the name argument is empty
*/
template <typename T>
void declareProperty(const std::string &name, T value, const unsigned int direction)
{
Property *p = new PropertyWithValue<T>(name, value, new NullValidator<T>, direction);
declareProperty(p);
}
/** Specialised version of declareProperty template method to prevent the creation of a
* PropertyWithValue of type const char* if an argument in quotes is passed (it will be
* converted to a string). The validator, if provided, needs to be a string validator.
Janik Zikovsky
committed
* @param name :: The name to assign to the property
* @param value :: The initial value to assign to the property
* @param validator :: Pointer to the (optional) validator. Ownership will be taken over.
* @param doc :: The (optional) documentation string
* @param direction :: The (optional) direction of the property, in, out or inout
* @throw Exception::ExistsError if a property with the given name already exists
* @throw std::invalid_argument if the name argument is empty
*/
void declareProperty( const std::string &name, const char* value,
IValidator<std::string> *validator = new NullValidator<std::string>, const std::string &doc="", const unsigned int direction = Direction::Input )
{
// Simply call templated method, converting character array to a string
declareProperty(name, std::string(value), validator, doc, direction);
Steve Williams
committed
}
/** Specialised version of declareProperty template method to prevent the creation of a
* PropertyWithValue of type const char* if an argument in quotes is passed (it will be
* converted to a string). The validator, if provided, needs to be a string validator.
Janik Zikovsky
committed
* @param name :: The name to assign to the property
* @param value :: The initial value to assign to the property
* @param doc :: The (optional) documentation string
* @param validator :: Pointer to the (optional) validator. Ownership will be taken over.
* @param direction :: The (optional) direction of the property, in, out or inout
Steve Williams
committed
* @throw Exception::ExistsError if a property with the given name already exists
* @throw std::invalid_argument if the name argument is empty
*/
void declareProperty( const std::string &name, const char* value,
const std::string &doc, IValidator<std::string> *validator = new NullValidator<std::string>, const unsigned int direction = Direction::Input )
{
// Simply call templated method, converting character array to a string
declareProperty(name, std::string(value), validator, doc, direction);
}
/** Add a property of string type to the list of managed properties
Janik Zikovsky
committed
* @param name :: The name to assign to the property
* @param value :: The initial value to assign to the property
* @param direction :: The direction of the property, in, out or inout
* @throw Exception::ExistsError if a property with the given name already exists
* @throw std::invalid_argument if the name argument is empty
*/
void declareProperty(const std::string &name, const char* value, const unsigned int direction)
{
declareProperty(name, std::string(value), new NullValidator<std::string>, "", direction);
}
Roman Tolchenov
committed
/// Get a property by an index
virtual Property* getPointerToPropertyOrdinal(const int &index) const = 0;
Russell Taylor
committed
/** Templated method to get the value of a property.
* No generic definition, only specialised ones. Although the definitions are mostly the
* same, Visual Studio can't cope with
Janik Zikovsky
committed
* @param name :: The name of the property (case insensitive)
* @return The value of the property
* @throw std::runtime_error If an attempt is made to assign a property to a different type
* @throw Exception::NotFoundError If the property requested does not exist
*/
template<typename T>
T getValue(const std::string &name) const;
Gigg, Martyn Anthony
committed
/// Removes the property from management
virtual void removeProperty(const std::string &name) = 0;
/// Clears all properties under management
virtual void clear() = 0;
Janik Zikovsky
committed
/// Utility class that enables the getProperty() method to effectively be templated on the return type
Russell Taylor
committed
struct DLLExport TypedValue
{
/// Reference to the containing PropertyManager
const IPropertyManager& pm;
/// The name of the property desired
const std::string prop;
/// Constructor
TypedValue(const IPropertyManager& p, const std::string &name) : pm(p), prop(name) {}
Russell Taylor
committed
// Unfortunately, MSVS2010 can't handle just having a single templated cast operator T()
// (it has problems with the string one). This operator is needed to convert a TypedValue
// into what we actually want. It can't even handle just having a specialization for strings.
// So we have to explicitly define an operator for each type of property that we have.
operator int16_t ();
operator uint16_t ();
operator int32_t ();
operator uint32_t ();
operator int64_t ();
operator uint64_t ();
Gigg, Martyn Anthony
committed
operator unsigned long ();
/// explicit specialization for bool()
Russell Taylor
committed
operator bool ();
/// explicit specialization for double()
Russell Taylor
committed
operator double ();
/// explicit specialization for std::string()
Russell Taylor
committed
operator std::string ();
/// explicit specialization for Property*()
Russell Taylor
committed
operator Property* ();
// We can have some partial specialisation for Array and Workspace properties
/// explicit specialization for std::vector
Russell Taylor
committed
operator std::vector<T> () { return pm.getValue<std::vector<T> >(prop);}
/// explicit specialization for boost::shared_ptr
Russell Taylor
committed
template<typename T>
operator boost::shared_ptr<T> () { return pm.getValue<boost::shared_ptr<T> >(prop); }
};
public:
/// Get the value of a property
virtual TypedValue getProperty(const std::string &name) const = 0;
Gigg, Martyn Anthony
committed
/// Get a pointer to property by name
virtual Property* getPointerToProperty(const std::string &name) const = 0;
};
} // namespace Kernel
} // namespace Mantid
#endif /*MANTID_KERNEL_IPROPERTYMANAGER_H_*/