Skip to content

Commit

Permalink
Solve compiler warnings about const casting noted by gcc 4.7.2
Browse files Browse the repository at this point in the history
Note technically related to this ticket but while we're improving tests...
Refs #8000
  • Loading branch information
martyngigg committed Sep 24, 2013
1 parent 8a1706b commit 5d0f37b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Mantid
inline PyObject* operator()(const T & p) const
{
PyObject *pyo = boost::python::to_python_value<T>()(p);
const PyTypeObject * derivedType = Registry::getDerivedType(pyo);
PyTypeObject * derivedType = const_cast<PyTypeObject*>(Registry::getDerivedType(pyo));
if( derivedType )
{
PyObject_SetAttrString(pyo, "__class__", (PyObject*)derivedType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Mantid
/// Returns an downcasting converter
DLLExport void registerIDForDowncasting(const std::string & id, const PyTypeObject * type);
/// Get an downcasted type object for the given object
DLLExport const PyTypeObject * getDerivedType(boost::python::object value);
DLLExport const PyTypeObject * getDerivedType(const boost::python::object &value);
/// Overload. Get an downcasted type object for the given object
DLLExport const PyTypeObject * getDerivedType(PyObject *value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ namespace
/// Python algorithm registration mutex in anonymous namespace (aka static)
Poco::Mutex FUNCTION_REGISTER_MUTEX;

GCC_DIAG_ON(cast-qual)
/**
* A free function to register a fit function from Python
* @param obj :: A Python object that should either be a class type derived from IFunction
Expand All @@ -57,15 +56,16 @@ GCC_DIAG_ON(cast-qual)
void subscribe(FunctionFactoryImpl & self, const boost::python::object & obj )
{
Poco::ScopedLock<Poco::Mutex> lock(FUNCTION_REGISTER_MUTEX);
static PyObject * const baseClass = (PyObject*)converter::registered<IFunction>::converters.to_python_target_type();
static PyTypeObject * baseClass =
const_cast<PyTypeObject*>(converter::registered<IFunction>::converters.to_python_target_type());

// obj could be or instance/class, check instance first
PyObject *classObject(NULL);
if( PyObject_IsInstance(obj.ptr(), baseClass) )
if( PyObject_IsInstance(obj.ptr(), (PyObject*)baseClass) )
{
classObject = PyObject_GetAttrString(obj.ptr(), "__class__");
}
else if(PyObject_IsSubclass(obj.ptr(), baseClass))
else if(PyObject_IsSubclass(obj.ptr(), (PyObject*)baseClass))
{
classObject = obj.ptr(); // We need to ensure the type of lifetime management so grab the raw pointer
}
Expand All @@ -85,7 +85,6 @@ GCC_DIAG_ON(cast-qual)
}
///@endcond
}
GCC_DIAG_OFF(cast-qual)

void export_FunctionFactory()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace Mantid
* @param value :: An object derived from DataItem
* @return A pointer to an downcasted type or NULL if one cannot be found
*/
const PyTypeObject * getDerivedType(boost::python::object value)
const PyTypeObject * getDerivedType(const boost::python::object & value)
{
// This has to be a search as it is at runtime.
// Each of the registered type handlers is checked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ namespace Mantid
{
TypeIDMap & typeHandlers = typeRegistry();
TypeIDMap::const_iterator iend = typeHandlers.end();
const PyTypeObject *result(NULL);
PyTypeObject *result(NULL);

for(TypeIDMap::const_iterator it = typeHandlers.begin(); it != iend; ++it)
{
if( it->second->checkExtract(value) )
{
const PyTypeObject *derivedType = it->second->pythonType();
PyTypeObject *derivedType = const_cast<PyTypeObject*>(it->second->pythonType());
if( !result )
{
result = derivedType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import unittest

from mantid.api import FunctionFactory
from mantid.api import IFunction1D, FunctionFactory

class TestFunction(IFunction1D):

def init(self):
pass

class FunctionFactoryTest(unittest.TestCase):

def test_get_function_factory_does_not_return_None(self):
self.assertTrue(FunctionFactory is not None)

Expand All @@ -18,5 +24,19 @@ def test_get_Gaussian(self):
self.assertTrue(len(func.__repr__()) > len(name))
self.assertTrue("Peak" in func.categories())


def test_function_subscription(self):
nfuncs_orig = len(FunctionFactory.getFunctionNames())
FunctionFactory.subscribe(TestFunction)
new_funcs = FunctionFactory.getFunctionNames()
self.assertEquals(nfuncs_orig+1, len(new_funcs))
self.assertTrue("TestFunction" in new_funcs)

FunctionFactory.unsubscribe("TestFunction")
new_funcs = FunctionFactory.getFunctionNames()
self.assertEquals(nfuncs_orig, len(new_funcs))
self.assertTrue("TestFunction" not in new_funcs)


if __name__ == '__main__':
unittest.main()

0 comments on commit 5d0f37b

Please sign in to comment.