Skip to content

Commit

Permalink
Refs #4399. Intermediate type handler class
Browse files Browse the repository at this point in the history
Adds a place for common code between handler types.
  • Loading branch information
martyngigg committed Feb 29, 2012
1 parent 32b9713 commit da946f0
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef MANTID_PYTHONINTERFACE_PROEPRTYWITHVALUEFACTORY_H_
#define MANTID_PYTHONINTERFACE_PROEPRTYWITHVALUEFACTORY_H_
/**
Copyright © 2011 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>
*/
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "MantidKernel/System.h"
#include <string>
#include <boost/python/object.hpp>

namespace Mantid
{
//---------------------------------------------------------------------------
// Forward declarations
//---------------------------------------------------------------------------
namespace Kernel
{
class Property;
}

namespace PythonInterface
{
/**
* Defines a static factory class that creates PropertyWithValue
* instances from python objects.
*/
class PropertyWithValueFactory
{
/// Creates a property from the given value and direction
static Kernel::Property * createProperty(const std::string & name, const boost::python::object & defaultValue,
const unsigned int direction);
/// Creates a property from the value, validator and direction
static Kernel::Property * createProperty(const std::string & name, const boost::python::object & defaultValue,
const boost::python::object & validator, const unsigned int direction);
};
}
}

#endif //MANTID_PYTHONINTERFACE_PROEPRTYWITHVALUEFACTORY_H_
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ namespace Mantid
*/
class PythonAlgorithm : public API::Algorithm
{
public:
/// Declare a property using the type of the defaultValue
void declareProperty(const std::string & name, const boost::python::object & defaultValue,
const int direction);
private:
/// Hide the base class variants as they are not required on this interface
using Mantid::API::Algorithm::declareProperty;
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace Mantid
{
// Forward declarations
class IPropertyManager;
class Property;
}
namespace PythonInterface
{
Expand All @@ -47,10 +48,13 @@ namespace Mantid
{
/// Virtual Destructor
virtual ~PropertyValueHandler() {};
/// Set function to handle Python -> C++ calls
virtual void set(Kernel::IPropertyManager* alg, const std::string &name, boost::python::object value) = 0;
/// Overload to set the named property's value on the property manager
virtual void set(Kernel::IPropertyManager* alg, const std::string &name, const boost::python::object & value) = 0;
/// Overload to create a Property type from the given value with no validation
virtual Kernel::Property * create(const std::string & name, const boost::python::object & value,
const unsigned int direction) const = 0;
/// Is the given object a derived type of this objects Type
virtual bool isDerivedType(const boost::python::object & value) const = 0;
virtual bool checkExtract(const boost::python::object & value) const = 0;
/// Return the Python type corresponding to this object. May return NULL
virtual const PyTypeObject * pythonType() const = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidPythonInterface/kernel/Registry/PropertyValueHandler.h"
#include "MantidPythonInterface/kernel/Registry/TypedPropertyValueHandler.h"

namespace Mantid
{
Expand All @@ -35,19 +35,10 @@ namespace Mantid
* should contain a type called value_type indicating the element type.
*/
template<typename ContainerType>
struct DLLExport SequenceTypeHandler : PropertyValueHandler
struct DLLExport SequenceTypeHandler : TypedPropertyValueHandler<ContainerType>
{
/// Call to set a named property where the value is some container type
virtual void set(Kernel::IPropertyManager* alg, const std::string &name, boost::python::object value);
/**
* Is the object actually an instance of the derived type
* @param value :: A Python wrapped C object
*/
bool isDerivedType(const boost::python::object & value) const
{
UNUSED_ARG(value);
return false;
}
void set(Kernel::IPropertyManager* alg, const std::string &name, const boost::python::object & value);
/**
* Return the PyTypeObject of the DerivedType
* @returns A PyTypeObject for the given DerivedType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidPythonInterface/kernel/Registry/PropertyValueHandler.h"
#include "MantidPythonInterface/kernel/Registry/TypedPropertyValueHandler.h"
#include "MantidKernel/IPropertyManager.h"

#if defined(__GNUC__) && !(defined(__INTEL_COMPILER))
Expand Down Expand Up @@ -52,29 +52,8 @@ namespace Mantid
* @tparam PropertyType :: The PropertyWithValue<> template type
*/
template<typename PropertyType>
struct DLLExport SingleValueTypeHandler : public PropertyValueHandler
struct DLLExport SingleValueTypeHandler : public TypedPropertyValueHandler<PropertyType>
{
/**
* Set function to handle Python -> C++ calls and get the correct type
* @param alg :: A pointer to an IPropertyManager
* @param name :: The name of the property
* @param value :: A boost python object that stores the value
*/
void set(Kernel::IPropertyManager* alg, const std::string &name, boost::python::object value)
{
alg->setProperty<PropertyType>(name, boost::python::extract<PropertyType>(value));
}

/**
* Is the object actually an instance of the derived type
* @param value :: A Python wrapped C object
*/
bool isDerivedType(const boost::python::object & value) const
{
boost::python::extract<PropertyType> extractor(value);
return extractor.check();
}

/**
* Return the PyTypeObject of the DerivedType
* @returns A PyTypeObject for the given DerivedType
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#ifndef MANTID_PYTHONINTERFACE_TYPEDPROPERTYVALUEHANDLER_H_
#define MANTID_PYTHONINTERFACE_TYPEDPROPERTYVALUEHANDLER_H_
/**
Copyright &copy; 2011 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>
*/
#include "MantidPythonInterface/kernel/Registry/PropertyValueHandler.h"
#include "MantidKernel/PropertyWithValue.h"
#include <boost/python/object.hpp>
#include <boost/python/extract.hpp>
#include <string>

namespace Mantid
{
namespace Kernel
{
class IPropertyManager;
}
namespace PythonInterface
{
namespace Registry
{
/**
* This class provides a templated class object that is able to take a
* python object and perform operations with a given C type.
*/
template<typename ValueType>
struct DLLExport TypedPropertyValueHandler : public PropertyValueHandler
{
/**
* Set function to handle Python -> C++ calls and get the correct type
* @param alg :: A pointer to an IPropertyManager
* @param name :: The name of the property
* @param value :: A boost python object that stores the value
*/
void set(Kernel::IPropertyManager* alg, const std::string &name, const boost::python::object & value)
{
alg->setProperty<ValueType>(name, boost::python::extract<ValueType>(value));
}
/**
* Create a PropertyWithValue from the given python object value
* @param name :: The name of the property
* @param defaultValue :: The defaultValue of the property. The object attempts to extract
* a value of type ValueType from the python object
* @param direction :: The direction of the property
* @returns A pointer to a newly constructed property instance
*/
Kernel::Property * create(const std::string & name, const boost::python::object & defaultValue,
const unsigned int direction) const
{
using boost::python::extract;
const ValueType valueInC = extract<ValueType>(defaultValue)();
return new Kernel::PropertyWithValue<ValueType>(name, valueInC, direction);
}
/// Is the given object a derived type of this objects Type
bool checkExtract(const boost::python::object & value) const
{
boost::python::extract<ValueType> extractor(value);
return extractor.check();
}
};
}
}
}

#endif /* MANTID_PYTHONINTERFACE_TYPEDPROPERTYVALUEHANDLER_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ set ( EXPORT_FILES
# Files containing additional helper code that are not related to exporting class/functions
set ( SRC_FILES
src/PythonAlgorithm/AlgorithmWrapper.cpp
src/PythonAlgorithm/PropertyWithValueFactory.cpp
src/PythonAlgorithm/PythonAlgorithm.cpp
src/CloneMatrixWorkspace.cpp
)

set ( INC_FILES
${HEADER_DIR}/api/PythonAlgorithm/AlgorithmWrapper.h
${HEADER_DIR}/api/PythonAlgorithm/PropertyWithValueFactory.h
${HEADER_DIR}/api/PythonAlgorithm/PythonAlgorithm.h
${HEADER_DIR}/api/BinaryOperations.h
${HEADER_DIR}/api/CloneMatrixWorkspace.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ using namespace boost::python;

void export_leaf_classes()
{
// Function pointer to pick out correct declareProperty
typedef void(AlgorithmWrapper::*declarePropertyOverload1)(const std::string &, const boost::python::object &, const int);

/**
* Export the algorithm wrapper that boost.python makes look like a PythonAlgorithm
*/
class_<AlgorithmWrapper, bases<Algorithm>, boost::noncopyable>("PythonAlgorithm", "Base class for all Python algorithms")
.def("declareProperty", (declarePropertyOverload1)&AlgorithmWrapper::declareProperty, args("name", "defaultValue", "direction"),
"Declares a named property where the type is taken from the type of the defaultValue and mapped to an appropriate C++ type")
;
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "MantidPythonInterface/api/PythonAlgorithm/PropertyWithValueFactory.h"
#include "MantidKernel/PropertyWithValue.h"

namespace Mantid
{
namespace PythonInterface
{

/**
* Creates a PropertyWithValue<Type> instance from the given information.
* The python type is mapped to a C type using the mapping defined by initPythonTypeMap()
* @param name :: The name of the property
* @param defaultValue :: A default value for this property.
* @param direction :: Specifies whether the property is Input, InOut or Output
* @returns A pointer to a new Property object
*/
Kernel::Property *
PropertyWithValueFactory::createProperty(const std::string & name , const boost::python::object & value,
const unsigned int direction)
{
return NULL;
}

/**
* Creates a PropertyWithValue<Type> instance from the given information.
* The python type is mapped to a C type using the mapping defined by initPythonTypeMap()
* @param name :: The name of the property
* @param defaultValue :: A default value for this property.
* @param validator :: A validator object
* @param direction :: Specifies whether the property is Input, InOut or Output
* @returns A pointer to a new Property object
*/
Kernel::Property *
PropertyWithValueFactory::createProperty(const std::string & name , const boost::python::object & value,
const boost::python::object & validator, const unsigned int direction)
{
return NULL;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@
// Includes
//-----------------------------------------------------------------------------
#include "MantidPythonInterface/api/PythonAlgorithm/PythonAlgorithm.h"
#include "MantidKernel/PropertyWithValue.h"
#include <boost/python/extract.hpp>

namespace Mantid
{
namespace PythonInterface
{
using Mantid::Kernel::Property;
using Mantid::Kernel::PropertyWithValue;

/**
* Declare a property using the type of the defaultValue
* @param name :: The name of the new property
* @param defaultValue :: A default value for the property. The type is mapped to a C++ type
* @param direction :: The direction of the property
*/
void PythonAlgorithm::declareProperty(const std::string & name, const boost::python::object & defaultValue,
const int direction)
{
using namespace boost::python;
//Property *p = new PropertyWithValue<int>(name, extract<int>(defaultValue)(), direction);
//Property *algProp = PropertyWithValueFactory::create(defaultValue);
//this->declareProperty(p);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ set ( INC_FILES
${HEADER_DIR}/kernel/Registry/RegisterSingleValueHandler.h
${HEADER_DIR}/kernel/Registry/SequenceTypeHandler.h
${HEADER_DIR}/kernel/Registry/SingleValueTypeHandler.h
${HEADER_DIR}/kernel/Registry/TypedPropertyValueHandler.h
${HEADER_DIR}/kernel/Registry/TypeRegistry.h
${HEADER_DIR}/kernel/Registry/UpcastRegistry.h
${HEADER_DIR}/kernel/PythonObjectInstantiator.h
Expand Down

0 comments on commit da946f0

Please sign in to comment.