Mantid
Loading...
Searching...
No Matches
AsType.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2012 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include <boost/mpl/and.hpp>
10#include <boost/python/detail/prefix.hpp>
11#include <boost/python/to_python_value.hpp>
12#include <type_traits>
13
18namespace Mantid {
19namespace PythonInterface {
20namespace Policies {
21// Utility code to help out
22namespace {
23
24//-----------------------------------------------------------------------
25// Polciy implementation
26//-----------------------------------------------------------------------
27
28// The entry point for the policy is in the struct AsType below. It does
29// a check as to whether the return type is valid, if so it forwards the
30// call to this struct
31template <typename ReturnType, typename InputType> struct AsTypeImpl {
32
33 inline PyObject *operator()(const InputType &p) const {
34 using namespace boost::python;
35 return to_python_value<ReturnType>()(ReturnType(p));
36 }
37
38 inline PyTypeObject const *get_pytype() const {
39 using namespace boost::python;
40 return converter::registered<ReturnType>::converters.to_python_target_type();
41 }
42};
43
44// Error handler for shared pointer types. If return type is wrong then user
45// sees the name of this
46// class in the output, which hopefully gives a clue as to what is going on
47template <typename T> struct AsType_Requires_New_Type_Automatically_Convertible_To_Original {};
48
49} // namespace
50
54template <class ReturnType> struct AsType {
55 template <class InputType> struct apply {
56 // Deduce if type is correct for policy, needs to be convertible to
57 // ReturnType
58 using type =
59 typename boost::mpl::if_c<std::is_convertible<InputType, ReturnType>::value, AsTypeImpl<ReturnType, InputType>,
60 AsType_Requires_New_Type_Automatically_Convertible_To_Original<InputType>>::type;
61 };
62};
63
64} // namespace Policies
65} // namespace PythonInterface
66} // namespace Mantid
Helper class which provides the Collimation Length for SANS instruments.
typename boost::mpl::if_c< std::is_convertible< InputType, ReturnType >::value, AsTypeImpl< ReturnType, InputType >, AsType_Requires_New_Type_Automatically_Convertible_To_Original< InputType > >::type type
Definition: AsType.h:60
Implements the AsType policy.
Definition: AsType.h:54