Skip to content

Commit

Permalink
Refs #4399. Split out the workspace property types
Browse files Browse the repository at this point in the history
Introduces a macro to decalare WorkspaceProperty exports.
  • Loading branch information
martyngigg committed Mar 6, 2012
1 parent f59015d commit a10c5f0
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef MANTID_PYTHONINTERFACE_WORKSPACEPROPERTYMACRO_H_
#define MANTID_PYTHONINTERFACE_WORKSPACEPROPERTYMACRO_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>
*/
#include "MantidPythonInterface/kernel/PropertyWithValue.h"
#include "MantidAPI/WorkspaceProperty.h"
#include <boost/python/register_ptr_to_python.hpp>

/**
* Defines a macro for exporting new WorkspaceProperty types.
* This automatically exports a new PropertyWithValue class for the given type.
*
* @param Type :: The workspace type (not the shared_ptr wrapped type)
* @param ClassName :: A string defining the final class name in Python
*/
#define EXPORT_WORKSPACE_PROPERTY(Type, ClassName) \
EXPORT_PROP_W_VALUE(boost::shared_ptr<Type>, Type); \
boost::python::register_ptr_to_python<Mantid::API::WorkspaceProperty<Type>*>(); \
boost::python::class_<Mantid::API::WorkspaceProperty<Type>,\
boost::python::bases<Mantid::Kernel::PropertyWithValue<boost::shared_ptr<Type> >, Mantid::API::IWorkspaceProperty>,\
boost::noncopyable>(ClassName, boost::python::no_init) \
;

#endif /* MANTID_PYTHONINTERFACE_WORKSPACEPROPERTYMACRO_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ set ( EXPORT_FILES
src/Exports/ExperimentInfo.cpp
src/Exports/MultipleExperimentInfos.cpp
src/Exports/Workspace.cpp
src/Exports/IWorkspaceProperty.cpp
src/Exports/WorkspaceProperty.cpp
src/Exports/ITableWorkspace.cpp
src/Exports/ITableWorkspaceProperty.cpp
src/Exports/IMDWorkspace.cpp
src/Exports/IMDHistoWorkspace.cpp
src/Exports/IMDEventWorkspace.cpp
src/Exports/MatrixWorkspace.cpp
src/Exports/MatrixWorkspaceProperty.cpp
src/Exports/IEventWorkspace.cpp
src/Exports/IEventWorkspaceProperty.cpp
src/Exports/IPeaksWorkspace.cpp
src/Exports/BinaryOperations.cpp
src/Exports/WorkspaceGroup.cpp
Expand All @@ -53,6 +58,7 @@ set ( INC_FILES
${HEADER_DIR}/api/PythonAlgorithm/PythonAlgorithm.h
${HEADER_DIR}/api/BinaryOperations.h
${HEADER_DIR}/api/CloneMatrixWorkspace.h
${HEADER_DIR}/api/WorkspacePropertyMacro.h
)

set ( PY_FILES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ namespace
* @param extensions :: A list of possible extensions (default = [])
* @param The direction of the property (default = input)
*/
boost::shared_ptr<FileProperty> createFileProperty(const std::string & name,const std::string& defaultValue,
unsigned int action,
const bpl::object & extensions = bpl::object(),
unsigned direction = Mantid::Kernel::Direction::Input)
FileProperty * createFileProperty(const std::string & name,const std::string& defaultValue,
unsigned int action,
const bpl::object & extensions = bpl::object(),
unsigned direction = Mantid::Kernel::Direction::Input)
{
std::vector<std::string> extsAsVector;
if( !extensions.is_none() )
Expand All @@ -57,7 +57,7 @@ namespace
extsAsVector = PySequenceToVectorConverter<std::string>(extensions)();
}
}
return boost::make_shared<FileProperty>(name, defaultValue, action, extsAsVector, direction);
return new FileProperty(name, defaultValue, action, extsAsVector, direction);
}

}
Expand All @@ -66,12 +66,11 @@ void export_FileProperty()
{
bpl::class_<FileProperty, bpl::bases<PropertyWithValue<std::string> >, boost::noncopyable>("FileProperty", bpl::no_init)
.def("__init__",
bpl::make_constructor(&createFileProperty, bpl::default_call_policies(),
(bpl::arg("name"), bpl::arg("defaultValue"), bpl::arg("action"),
bpl::arg("extensions") = bpl::object(),
bpl::arg("direction") = Mantid::Kernel::Direction::Input)
)
)
bpl::make_constructor(&createFileProperty, bpl::default_call_policies(),
(bpl::arg("name"), bpl::arg("defaultValue"), bpl::arg("action"),
bpl::arg("extensions") = bpl::object(),
bpl::arg("direction") = Mantid::Kernel::Direction::Input)
))
;
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,3 @@ void export_IEventWorkspace()
REGISTER_SINGLEVALUE_HANDLER(IEventWorkspace_sptr);
}

void export_IEventWorkspaceProperty()
{
EXPORT_PROP_W_VALUE(IEventWorkspace_sptr, IEventWorkspace);
register_ptr_to_python<WorkspaceProperty<IEventWorkspace>*>();
class_<WorkspaceProperty<IEventWorkspace>, bases<PropertyWithValue<IEventWorkspace_sptr>,IWorkspaceProperty>,
boost::noncopyable>("WorkspaceProperty_IEventWorkspace", no_init)
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "MantidPythonInterface/api/WorkspacePropertyMacro.h"
#include "MantidAPI/IEventWorkspace.h"

void export_IEventWorkspaceProperty()
{
using Mantid::API::IEventWorkspace;
EXPORT_WORKSPACE_PROPERTY(IEventWorkspace, "IEventWorkspaceProperty");
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,3 @@ void export_ITableWorkspace()
REGISTER_SINGLEVALUE_HANDLER(ITableWorkspace_sptr);
}

void export_ITableWorkspaceProperty()
{
using Mantid::API::WorkspaceProperty;
using Mantid::API::IWorkspaceProperty;
using Mantid::Kernel::PropertyWithValue;

// PropertyWithValue<ITableWorkspace_sptr>
EXPORT_PROP_W_VALUE(ITableWorkspace_sptr, ITableWorkspace);
// Register a bare pointer to the type
register_ptr_to_python<WorkspaceProperty<ITableWorkspace>*>();
// Finally the WorkspaceProperty<ITableWorkspace> hierarchy
class_<WorkspaceProperty<ITableWorkspace>, bases<PropertyWithValue<ITableWorkspace_sptr>,IWorkspaceProperty>,
boost::noncopyable>("WorkspaceProperty_ITableWorkspace", no_init)
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "MantidPythonInterface/api/WorkspacePropertyMacro.h"
#include "MantidAPI/ITableWorkspace.h"

void export_ITableWorkspaceProperty()
{
using Mantid::API::ITableWorkspace;
EXPORT_WORKSPACE_PROPERTY(ITableWorkspace, "ITableWorkspaceProperty");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "MantidAPI/IWorkspaceProperty.h"
#include <boost/python/class.hpp>

void export_IWorkspaceProperty()
{
using namespace boost::python;
using Mantid::API::IWorkspaceProperty;

class_<IWorkspaceProperty, boost::noncopyable>("IWorkspaceProperty", no_init)
.def("isOptional", &IWorkspaceProperty::isOptional, "Is the input workspace property optional")
.def("isLocking", &IWorkspaceProperty::isLocking, "Will the workspace be locked when starting an algorithm")
;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidAPI/WorkspaceOpOverloads.h"

#include "MantidPythonInterface/kernel/PropertyWithValue.h"
Expand Down Expand Up @@ -105,13 +104,3 @@ void export_MatrixWorkspace()

REGISTER_SINGLEVALUE_HANDLER(MatrixWorkspace_sptr);
}

void export_MatrixWorkspaceProperty()
{
EXPORT_PROP_W_VALUE(MatrixWorkspace_sptr, MatrixWorkspace);
register_ptr_to_python<WorkspaceProperty<MatrixWorkspace>*>();
class_<WorkspaceProperty<MatrixWorkspace>, bases<PropertyWithValue<MatrixWorkspace_sptr>,IWorkspaceProperty>,
boost::noncopyable>("WorkspaceProperty_MatrixWorkspace", no_init)
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "MantidPythonInterface/api/WorkspacePropertyMacro.h"

void export_MatrixWorkspaceProperty()
{
using Mantid::API::MatrixWorkspace;
EXPORT_WORKSPACE_PROPERTY(MatrixWorkspace, "MatrixWorkspaceProperty");
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,3 @@ void export_Workspace()

REGISTER_SINGLEVALUE_HANDLER(Workspace_sptr);
}

void export_WorkspaceProperty()
{
// Interface
class_<IWorkspaceProperty, boost::noncopyable>("IWorkspaceProperty", no_init)
;

// Export the WorkspaceProperty hierarchy so that the plain Property* return from getPointerToProperty is automatically upcast to a WorkspaceProperty*
EXPORT_PROP_W_VALUE(Workspace_sptr, Workspace);
register_ptr_to_python<WorkspaceProperty<Workspace>*>();
class_<WorkspaceProperty<Workspace>, bases<PropertyWithValue<Workspace_sptr>,IWorkspaceProperty>, boost::noncopyable>("WorkspaceProperty_Workspace", no_init)
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "MantidPythonInterface/api/WorkspacePropertyMacro.h"
#include "MantidAPI/Workspace.h"

void export_WorkspaceProperty()
{
using Mantid::API::Workspace;
EXPORT_WORKSPACE_PROPERTY(Workspace, "WorkspaceProperty");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from testhelpers import run_algorithm, can_be_instantiated

from mantid.api import (MatrixWorkspace, WorkspaceProperty_Workspace, Workspace,
from mantid.api import (MatrixWorkspace, WorkspaceProperty, Workspace,
ExperimentInfo, AnalysisDataService)
from mantid.geometry import Detector

Expand Down Expand Up @@ -83,7 +83,7 @@ def test_spectrum_retrieval(self):
self.assertEquals(expected[i], ids[i])

def test_that_a_histogram_workspace_is_returned_as_a_MatrixWorkspace_from_a_property(self):
self.assertEquals(type(self._test_ws_prop), WorkspaceProperty_Workspace)
self.assertEquals(type(self._test_ws_prop), WorkspaceProperty)
# Is Workspace in the hierarchy of the value
self.assertTrue(isinstance(self._test_ws, Workspace))
# Have got a MatrixWorkspace back and not just the generic interface
Expand Down

0 comments on commit a10c5f0

Please sign in to comment.