Skip to content

Commit

Permalink
Refs #4399. Add protection for non std::vector return types
Browse files Browse the repository at this point in the history
Ensures the policy can only be used with functions returning references
to std::vector.
  • Loading branch information
martyngigg committed Feb 17, 2012
1 parent 2a18e58 commit 0357d86
Showing 1 changed file with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
*/
#include "MantidKernel/System.h"
#include "MantidPythonInterface/kernel/NumpyConverters.h"
//#include <boost/mpl/if.hpp>
//#include <boost/mpl/or.hpp>
//#include <boost/type_traits/is_convertible.hpp>

#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/mpl/and.hpp>
#include <vector>

namespace Mantid
Expand Down Expand Up @@ -109,6 +110,25 @@ namespace Mantid
//-----------------------------------------------------------------------
// return_value_policy
//-----------------------------------------------------------------------
namespace
{
/// MPL struct to figure out if a type is a std::vector
/// The general one inherits from boost::false_type
template<typename T>
struct is_std_vector : boost::false_type
{};

/// Specialization for std::vector types to inherit from
/// boost::true_type
template<typename T>
struct is_std_vector<std::vector<T> > : boost::true_type
{};

template<typename T>
struct VectorToNumpy_Requires_StdVector_Return_Type
{};

}
/**
* Implements a return value policy that
* returns a numpy array from a std::vector
Expand All @@ -120,10 +140,18 @@ namespace Mantid
template<typename ConversionPolicy>
struct VectorToNumpy
{
// The boost::python framework calls return_value_policy::apply<T>::type
template <class T>
struct apply
{
typedef ConvertVectorToNDArray<T, ConversionPolicy> type;
// Typedef that removes and const or reference qualifiers from the type
typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type non_const_type;
// MPL compile-time check that T is a reference to a std::vector
typedef typename boost::mpl::if_c<
boost::mpl::and_<boost::is_reference<T>, is_std_vector<non_const_type> >::value
, ConvertVectorToNDArray<T, ConversionPolicy>
, VectorToNumpy_Requires_StdVector_Return_Type<T>
>::type type;
};
};

Expand Down

0 comments on commit 0357d86

Please sign in to comment.