Mantid
Loading...
Searching...
No Matches
RemoveConst.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/python/detail/prefix.hpp>
10#include <boost/python/to_python_value.hpp>
11
12#include <boost/mpl/and.hpp>
13
14#include <memory>
15
16#include <type_traits>
17
33namespace Mantid {
34namespace PythonInterface {
35namespace Policies {
36// Utility code to help out
37namespace {
38//-----------------------------------------------------------------------
39// MPL helper structs
40//-----------------------------------------------------------------------
43template <typename T> struct IsConstSharedPtr : std::false_type {};
44
47template <typename T> struct IsConstSharedPtr<std::shared_ptr<const T>> : std::true_type {};
48
49//-----------------------------------------------------------------------
50// Polciy implementations
51//-----------------------------------------------------------------------
52
53// The entry point for the policy is in the struct RemoveConst below. It does
54// a check as to whether the return type is valid, if so it forwards the
55// call to this struct
56template <typename ConstPtrType> struct RemoveConstImpl {
57 // Remove the pointer type to leave value type
58 using ValueType = typename std::remove_pointer<ConstPtrType>::type;
59 // Remove constness
60 using NonConstValueType = typename std::remove_const<ValueType>::type;
61
62 inline PyObject *operator()(const ConstPtrType &p) const {
63 using namespace boost::python;
64 return to_python_value<NonConstValueType *>()(const_cast<NonConstValueType *>(p));
65 }
66
67 inline PyTypeObject const *get_pytype() const {
68 using namespace boost::python;
69 return converter::registered<NonConstValueType *>::converters.to_python_target_type();
70 }
71};
72
73// Error handler for raw pointer types. If return type is wrong then user sees
74// the name of this
75// class in the output, which hopefully gives a clue as to what is going on
76template <typename T> struct RemoveConst_Requires_Pointer_Return_Value {};
77
78// The entry point for the policy is in the struct RemoveConstSharedPtr below.
79// It does
80// a check as to whether the return type is valid, if so it forwards the
81// call to this struct
82template <typename ConstSharedPtr> struct RemoveConstSharedPtrImpl {
83 using ConstElementType = typename ConstSharedPtr::element_type;
84 using NonConstElementType = typename std::remove_const<ConstElementType>::type;
85 using NonConstSharedPtr = typename std::shared_ptr<NonConstElementType>;
86
87 inline PyObject *operator()(const ConstSharedPtr &p) const {
88 using namespace boost::python;
89 return to_python_value<NonConstSharedPtr>()(std::const_pointer_cast<NonConstElementType>(p));
90 }
91
92 inline PyTypeObject const *get_pytype() const {
93 using namespace boost::python::converter;
94 return registered<NonConstSharedPtr>::converters.to_python_target_type();
95 }
96};
97
98// Error handler for shared pointer types. If return type is wrong then user
99// sees the name of this
100// class in the output, which hopefully gives a clue as to what is going on
101template <typename T> struct RemoveConstSharedPtr_Requires_SharedPtr_Const_T_Pointer_Return_Value {};
102
103} // namespace
104
109 template <class T> struct apply {
110 // Deduce if type is correct for policy, needs to be a "T*"
111 using type = typename boost::mpl::if_c<std::is_pointer<T>::value, RemoveConstImpl<T>,
112 RemoveConst_Requires_Pointer_Return_Value<T>>::type;
113 };
114};
115
120 template <class T> struct apply {
121 // Deduce if type is correct for policy, needs to be a
122 // "std::shared_ptr<T>"
123 using type =
124 typename boost::mpl::if_c<IsConstSharedPtr<T>::value, RemoveConstSharedPtrImpl<T>,
125 RemoveConstSharedPtr_Requires_SharedPtr_Const_T_Pointer_Return_Value<T>>::type;
126 };
127};
128
129} // namespace Policies
130} // namespace PythonInterface
131} // namespace Mantid
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
typename boost::mpl::if_c< IsConstSharedPtr< T >::value, RemoveConstSharedPtrImpl< T >, RemoveConstSharedPtr_Requires_SharedPtr_Const_T_Pointer_Return_Value< T > >::type type
Definition: RemoveConst.h:125
Implements the RemoveConstSharedPtr policy.
Definition: RemoveConst.h:119
typename boost::mpl::if_c< std::is_pointer< T >::value, RemoveConstImpl< T >, RemoveConst_Requires_Pointer_Return_Value< T > >::type type
Definition: RemoveConst.h:112
Implements the RemoveConst policy.
Definition: RemoveConst.h:108