Skip to content

Commit

Permalink
Allow IFunction::setAttribute to be overridden in Python. Refs #970
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Apr 18, 2013
1 parent 8df8680 commit eba2e5c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ namespace Mantid
void declareAttribute(const std::string &name, const boost::python::object &defaultValue);
/// Get a named attribute value
PyObject * getAttributeValue(const std::string & name);
/// Returns the attribute's value as a Python object
PyObject * getAttributeValue(const API::IFunction::Attribute & att);
/// Called by the framework when an attribute has been set
void setAttribute(const std::string& attName,const API::IFunction::Attribute& att);

// Each overload of declareParamter requires a different name as we
// Each overload of declareParameter requires a different name as we
// can't use a function pointer with a virtual base class

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ void export_IFunction()

.def("declareAttribute", &IFunctionAdapter::declareAttribute, "Declare an attribute with an initial value")

.def("getAttributeValue", &IFunctionAdapter::getAttributeValue, "Return the value of the named attribute")
.def("getAttributeValue", (PyObject * (IFunctionAdapter::*)(const std::string &))&IFunctionAdapter::getAttributeValue,
"Return the value of the named attribute")

.def("declareParameter", &IFunctionAdapter::declareFitParameter,
"Declare a fitting parameter settings its default value & description")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,53 @@ namespace Mantid
}

/**
* Get the value of the given attribute as a Python object
* Get the value of the named attribute as a Python object
* @param name :: The name of the new attribute
* @param defaultValue :: The default value for the attribute
* @returns The value of the attribute
*/
PyObject * IFunctionAdapter::getAttributeValue(const std::string & name)
{
auto attr = IFunction::getAttribute(name);
return getAttributeValue(attr);
}


/**
* Get the value of the given attribute as a Python object
* @param attr An attribute object
* @returns The value of the attribute
*/
PyObject * IFunctionAdapter::getAttributeValue(const API::IFunction::Attribute & attr)
{
std::string type = attr.type();
PyObject *result(NULL);
if(type=="int") result = to_python_value<const int&>()(attr.asInt());
else if(type=="double") result = to_python_value<const double&>()(attr.asDouble());
else if(type=="std::string") result = to_python_value<const std::string&>()(attr.asString());
else if(type=="bool") result = to_python_value<const bool&>()(attr.asBool());
else throw std::runtime_error("Unknown attribute type, cannot convert C++ type to Python. Contact developement team.");

return result;
}

/**
* Calls setAttributeValue on the Python object if it exists otherwise calls the base class method
* @param attName The name of the attribute
* @param attr An attribute object
*/
void IFunctionAdapter::setAttribute(const std::string& attName,const Attribute& attr)
{
object value = object(handle<>(getAttributeValue(attr)));
try
{
CallMethod2<void,std::string,object>::dispatchWithException(getSelf(), "setAttributeValue", attName,value);
}
catch(std::runtime_error &)
{
IFunction::setAttribute(attName, attr);
}
}


/**
* Value of i-th active parameter. If this functions is overridden
* in Python then it returns the value of the ith active Parameter
Expand Down

0 comments on commit eba2e5c

Please sign in to comment.