Skip to content

Commit

Permalink
Remove another macro in favour of a templated static class
Browse files Browse the repository at this point in the history
Refs #8996
  • Loading branch information
martyngigg committed Feb 17, 2014
1 parent 77a48d6 commit ad5b4f4
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 39 deletions.
Expand Up @@ -21,7 +21,7 @@
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidPythonInterface/kernel/PropertyWithValue.h"
#include "MantidPythonInterface/kernel/PropertyWithValueExporter.h"
#include "MantidAPI/WorkspaceProperty.h"
#include <boost/python/register_ptr_to_python.hpp>
#include <boost/python/make_constructor.hpp>
Expand Down Expand Up @@ -96,7 +96,9 @@ namespace Mantid
}

/**
* Defines the necessary exports for a WorkspaceProperty<WorkspaceType>
* Defines the necessary exports for a WorkspaceProperty<WorkspaceType>. This includes a
* PropertyWithValue<WorkspaceType_sptr> whose name is formed by appending "PropertyWithValue"
* to the given class name
* @param pythonClassName :: The name of the class in python
*/
static void define(const char * pythonClassName)
Expand All @@ -105,7 +107,8 @@ namespace Mantid
using Mantid::API::IWorkspaceProperty;
using Mantid::Kernel::PropertyWithValue;

EXPORT_PROP_W_VALUE(WorkspaceType_sptr, WorkspaceType);
std::string basePropName = std::string(pythonClassName) + "PropertyWithValue";
PropertyWithValueExporter<WorkspaceType_sptr>::define(basePropName.c_str());
register_ptr_to_python<TypedWorkspaceProperty*>();

class_<TypedWorkspaceProperty, bases<PropertyWithValue<WorkspaceType_sptr>, IWorkspaceProperty>,
Expand Down
Expand Up @@ -30,15 +30,30 @@
#include <boost/python/bases.hpp>
#include <boost/python/return_value_policy.hpp>

/**
* Define a macro to export PropertyWithValue template types
*/
#define EXPORT_PROP_W_VALUE(type,suffix) \
boost::python::class_<Mantid::Kernel::PropertyWithValue< type >, \
boost::python::bases<Mantid::Kernel::Property>, boost::noncopyable>("PropertyWithValue_"#suffix, boost::python::no_init) \
.add_property("value", \
make_function(&Mantid::Kernel::PropertyWithValue<type>::operator(),\
boost::python::return_value_policy<Mantid::PythonInterface::Policies::ToSharedPtrWithDowncast>())) \
;
namespace Mantid
{
namespace PythonInterface
{
/**
* A helper struct to export PropertyWithValue<> types to Python.
*/
template<typename HeldType>
struct PropertyWithValueExporter
{
static void define(const char * pythonClassName)
{
using namespace boost::python;
using namespace Mantid::Kernel;

class_<PropertyWithValue<HeldType>, bases<Property>, boost::noncopyable>(pythonClassName, no_init)
.add_property("value",
make_function(&PropertyWithValue<HeldType>::operator(),
return_value_policy<Policies::ToSharedPtrWithDowncast>()))
;
}

};
}
}

#endif /* MANTID_PYTHONINTERFACE_PROPERTY_HPP_ */
Expand Up @@ -83,7 +83,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
${HEADER_DIR}/api/WorkspacePropertyExporter.h
)

set ( PY_FILES
Expand Down
@@ -1,13 +1,14 @@
#include "MantidAPI/AlgorithmProperty.h"
#include "MantidAPI/IAlgorithm.h"
#include "MantidPythonInterface/kernel/PropertyWithValue.h"
#include "MantidPythonInterface/kernel/PropertyWithValueExporter.h"
#include <boost/python/class.hpp>
#include <boost/python/make_constructor.hpp>

using Mantid::API::AlgorithmProperty;
using Mantid::API::IAlgorithm;
using Mantid::Kernel::IValidator;
using Mantid::Kernel::PropertyWithValue;
using Mantid::PythonInterface::PropertyWithValueExporter;
using namespace boost::python;

namespace
Expand Down Expand Up @@ -42,7 +43,7 @@ void export_AlgorithmProperty()
// AlgorithmProperty has base PropertyWithValue<boost::shared_ptr<IAlgorithm>>
// which must be exported
typedef boost::shared_ptr<IAlgorithm> HeldType;
EXPORT_PROP_W_VALUE(HeldType, IAlgorithm);
PropertyWithValueExporter<HeldType>::define("AlgorithmPropertyWithValue");

class_<AlgorithmProperty, bases<PropertyWithValue<HeldType>>, boost::noncopyable>("AlgorithmProperty", no_init)
.def(init<const std::string &>(args("name")))
Expand Down
@@ -1,18 +1,20 @@
#include "MantidAPI/FunctionProperty.h"
#include "MantidPythonInterface/kernel/PropertyWithValue.h"
#include "MantidPythonInterface/kernel/PropertyWithValueExporter.h"
#include <boost/python/class.hpp>

using Mantid::API::FunctionProperty;
using Mantid::API::IFunction;
using Mantid::Kernel::PropertyWithValue;
using Mantid::PythonInterface::PropertyWithValueExporter;
using namespace boost::python;

void export_FunctionProperty()
{
// FuncitonProperty has base PropertyWithValue<boost::shared_ptr<IFunction>>
// which must be exported
typedef boost::shared_ptr<IFunction> HeldType;
EXPORT_PROP_W_VALUE(HeldType, IFunction);
PropertyWithValueExporter<HeldType>::define("FunctionPropertyWithValue");


class_<FunctionProperty, bases<PropertyWithValue<HeldType>>, boost::noncopyable>("FunctionProperty", no_init)
.def(init<const std::string &>(arg("name"), "Constructs a FunctionProperty with the given name"))
Expand Down
@@ -1,11 +1,12 @@
#include "MantidAPI/MultipleFileProperty.h"
#include "MantidPythonInterface/kernel/PropertyWithValue.h"
#include "MantidPythonInterface/kernel/PropertyWithValueExporter.h"
#include <boost/python/class.hpp>
#include <boost/python/list.hpp>
#include <boost/python/str.hpp>

using Mantid::API::MultipleFileProperty;
using Mantid::Kernel::PropertyWithValue;
using Mantid::PythonInterface::PropertyWithValueExporter;
using namespace boost::python;

namespace
Expand Down Expand Up @@ -57,7 +58,7 @@ namespace
void export_MultipleFileProperty()
{
typedef PropertyWithValue<HeldType> BaseClass;
EXPORT_PROP_W_VALUE(HeldType,vec_vec_str);
PropertyWithValueExporter<HeldType>::define("VectorVectorStringPropertyWithValue");

class_<MultipleFileProperty, bases<BaseClass>, boost::noncopyable>("MultipleFileProperty", no_init)
// Override the base class one to do something more appropriate
Expand Down
Expand Up @@ -88,7 +88,7 @@ set ( INC_FILES
${HEADER_DIR}/kernel/Registry/TypeRegistry.h
${HEADER_DIR}/kernel/Registry/DowncastRegistry.h
${HEADER_DIR}/kernel/IsNone.h
${HEADER_DIR}/kernel/PropertyWithValue.h
${HEADER_DIR}/kernel/PropertyWithValueExporter.h
${HEADER_DIR}/kernel/PythonObjectInstantiator.h
${HEADER_DIR}/kernel/StlExportDefinitions.h
${HEADER_DIR}/kernel/TypedValidatorExportMacro.h
Expand Down
@@ -1,25 +1,38 @@
#include "MantidPythonInterface/kernel/PropertyWithValue.h"
#include "MantidPythonInterface/kernel/PropertyWithValueExporter.h"

using Mantid::PythonInterface::PropertyWithValueExporter;

void export_BasicPropertyWithValueTypes()
{
#define EXPORT_BASIC_INTEGER_TYPE(ctype, export_name)\
EXPORT_PROP_W_VALUE(ctype, export_name);\
EXPORT_PROP_W_VALUE(std::vector<ctype>, export_name);\
EXPORT_PROP_W_VALUE(unsigned ctype, unsigned ## _export_name);\
EXPORT_PROP_W_VALUE(std::vector<unsigned ctype>, vector_ ## export_name);
// cut down copy-and-paste code
#define EXPORT_PROP(CType, ExportName) \
PropertyWithValueExporter<CType>::define(ExportName);

#define EXPORT_BASIC_TYPE(ctype, export_name)\
EXPORT_PROP_W_VALUE(ctype, export_name);\
EXPORT_PROP_W_VALUE(std::vector<ctype>, vector_ ## export_name);
//ints & vectors
EXPORT_PROP(int, "IntPropertyWithValue");
EXPORT_PROP(std::vector<int>, "VectorIntPropertyWithValue");
EXPORT_PROP(unsigned int, "UIntPropertyWithValue");
EXPORT_PROP(std::vector<unsigned int>, "VectorUIntPropertyWithValue");
// longs & vectors
EXPORT_PROP(long, "LongPropertyWithValue");
EXPORT_PROP(std::vector<long>, "VectorLongPropertyWithValue");
EXPORT_PROP(unsigned long, "ULongPropertyWithValue");
EXPORT_PROP(std::vector<unsigned long>, "VectorULongPropertyWithValue");
// long long long longs & vectors
EXPORT_PROP(long long, "LongLongPropertyWithValue");
EXPORT_PROP(std::vector<long long>, "VectorLongLongPropertyWithValue");
EXPORT_PROP(unsigned long long, "ULongLongPropertyWithValue");
EXPORT_PROP(std::vector<unsigned long long>, "VectorULongLongPropertyWithValue");
// double
EXPORT_PROP(double, "FloatPropertyWithValue");
EXPORT_PROP(std::vector<double>, "VectorFloatPropertyWithValue");
// boolean
EXPORT_PROP(bool, "BoolPropertyWithValue");
EXPORT_PROP(std::vector<bool>, "VectorBoolPropertyWithValue");
// std::string
EXPORT_PROP(std::string, "StringPropertyWithValue");
EXPORT_PROP(std::vector<std::string>, "VectorStringPropertyWithValue");

// -- PropertyWithValueTypes
EXPORT_BASIC_INTEGER_TYPE(int, int);
EXPORT_BASIC_INTEGER_TYPE(long, long);
EXPORT_BASIC_INTEGER_TYPE(long long, long_long);

EXPORT_BASIC_TYPE(double, double);
EXPORT_BASIC_TYPE(bool, bool);
EXPORT_BASIC_TYPE(std::string, string);
#undef EXPORT_PROP
}


0 comments on commit ad5b4f4

Please sign in to comment.