-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IFunctionAdapter for access to IFunction's protected attrs.
Refs #970
- Loading branch information
1 parent
5542c77
commit c7307e9
Showing
8 changed files
with
197 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...d/Framework/PythonInterface/inc/MantidPythonInterface/api/FitFunctions/IFunctionAdapter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#ifndef MANTID_PYTHONINTERFACE_IFUNCTIONADAPTER_H_ | ||
#define MANTID_PYTHONINTERFACE_IFUNCTIONADAPTER_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://github.com/mantidproject/mantid>. | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
//----------------------------------------------------------------------------- | ||
// Includes | ||
//----------------------------------------------------------------------------- | ||
#include "MantidAPI/IFunction.h" | ||
#include "MantidKernel/ClassMacros.h" | ||
|
||
#include <boost/python/object.hpp> | ||
|
||
namespace Mantid | ||
{ | ||
namespace PythonInterface | ||
{ | ||
/** | ||
* Provides a layer to hook into the protected functions | ||
* of IFunction | ||
*/ | ||
class IFunctionAdapter : public virtual API::IFunction | ||
{ | ||
public: | ||
/// A constructor that looks like a Python __init__ method | ||
IFunctionAdapter(PyObject* self); | ||
|
||
/// Returns the name of the function | ||
std::string name() const; | ||
|
||
/// Declare an attribute with an initial value | ||
void declareAttribute(const std::string &name, const boost::python::object &defaultValue); | ||
/// Get a named attribute value | ||
PyObject * getAttributeValue(const std::string & name); | ||
|
||
private: | ||
/// The PyObject must be supplied to construct the object | ||
DISABLE_DEFAULT_CONSTRUCT(IFunctionAdapter); | ||
DISABLE_COPY_AND_ASSIGN(IFunctionAdapter); | ||
|
||
/// The name of the function | ||
std::string m_name; | ||
}; | ||
} | ||
} | ||
|
||
|
||
#endif /* MANTID_PYTHONINTERFACE_IFUNCTIONADAPTER_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
Code/Mantid/Framework/PythonInterface/mantid/api/src/FitFunctions/IFunctionAdapter.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "MantidPythonInterface/api/FitFunctions/IFunctionAdapter.h" | ||
|
||
#include <boost/python/class.hpp> | ||
|
||
namespace Mantid | ||
{ | ||
namespace PythonInterface | ||
{ | ||
using namespace boost::python; | ||
|
||
/** | ||
* Construct the wrapper and stores the reference to the PyObject | ||
* * @param self A reference to the calling Python object | ||
*/ | ||
IFunctionAdapter::IFunctionAdapter(PyObject* self) | ||
: IFunction(), m_name(self->ob_type->tp_name) | ||
{ | ||
} | ||
|
||
/** | ||
* Returns the class name of the function. This cannot be overridden in Python. | ||
*/ | ||
std::string IFunctionAdapter::name() const | ||
{ | ||
return m_name; | ||
} | ||
|
||
/** | ||
* Declare an attribute on the given function from a python object | ||
* @param name :: The name of the new attribute | ||
* @param defaultValue :: The default value for the attribute | ||
*/ | ||
void IFunctionAdapter::declareAttribute(const std::string &name, | ||
const object &defaultValue) | ||
{ | ||
PyObject *rawptr = defaultValue.ptr(); | ||
IFunction::Attribute attr; | ||
if(PyInt_Check(rawptr) == 1) attr = IFunction::Attribute(extract<int>(rawptr)()); | ||
else if(PyFloat_Check(rawptr) == 1) attr = IFunction::Attribute(extract<double>(rawptr)()); | ||
else if(PyString_Check(rawptr) == 1) attr = IFunction::Attribute(extract<std::string>(rawptr)()); | ||
else if(PyBool_Check(rawptr) == 1) attr = IFunction::Attribute(extract<bool>(rawptr)()); | ||
else throw std::invalid_argument("Invalid attribute type. Allowed types=float,int,str,bool"); | ||
|
||
IFunction::declareAttribute(name, attr); | ||
} | ||
|
||
/** | ||
* Get the value of the given 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); | ||
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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters