|
| 1 | +// |
| 2 | +// Copyright (C) 2020 INRIA |
| 3 | +// Copyright (C) 2024 LAAS-CNRS, INRIA |
| 4 | +// |
| 5 | +#ifndef __eigenpy_deprecation_hpp__ |
| 6 | +#define __eigenpy_deprecation_hpp__ |
| 7 | + |
| 8 | +#include "eigenpy/fwd.hpp" |
| 9 | + |
| 10 | +namespace eigenpy { |
| 11 | + |
| 12 | +enum class DeprecationType { DEPRECATION, FUTURE }; |
| 13 | + |
| 14 | +namespace detail { |
| 15 | + |
| 16 | +constexpr PyObject *deprecationTypeToPyObj(DeprecationType dep) { |
| 17 | + switch (dep) { |
| 18 | + case DeprecationType::DEPRECATION: |
| 19 | + return PyExc_DeprecationWarning; |
| 20 | + case DeprecationType::FUTURE: |
| 21 | + return PyExc_FutureWarning; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +} // namespace detail |
| 26 | + |
| 27 | +/// @brief A Boost.Python call policy which triggers a Python warning on |
| 28 | +/// precall. |
| 29 | +template <DeprecationType deprecation_type = DeprecationType::DEPRECATION, |
| 30 | + class BasePolicy = bp::default_call_policies> |
| 31 | +struct deprecation_warning_policy : BasePolicy { |
| 32 | + using result_converter = typename BasePolicy::result_converter; |
| 33 | + using argument_package = typename BasePolicy::argument_package; |
| 34 | + |
| 35 | + deprecation_warning_policy(const std::string &warning_msg) |
| 36 | + : BasePolicy(), m_what(warning_msg) {} |
| 37 | + |
| 38 | + std::string what() const { return m_what; } |
| 39 | + |
| 40 | + const BasePolicy *derived() const { |
| 41 | + return static_cast<const BasePolicy *>(this); |
| 42 | + } |
| 43 | + |
| 44 | + template <class ArgPackage> |
| 45 | + bool precall(const ArgPackage &args) const { |
| 46 | + PyErr_WarnEx(detail::deprecationTypeToPyObj(deprecation_type), |
| 47 | + m_what.c_str(), 1); |
| 48 | + return derived()->precall(args); |
| 49 | + } |
| 50 | + |
| 51 | + protected: |
| 52 | + const std::string m_what; |
| 53 | +}; |
| 54 | + |
| 55 | +template <DeprecationType deprecation_type = DeprecationType::DEPRECATION, |
| 56 | + class BasePolicy = bp::default_call_policies> |
| 57 | +struct deprecated_function |
| 58 | + : deprecation_warning_policy<deprecation_type, BasePolicy> { |
| 59 | + deprecated_function(const std::string &msg = |
| 60 | + "This function has been marked as deprecated, and " |
| 61 | + "will be removed in the future.") |
| 62 | + : deprecation_warning_policy<deprecation_type, BasePolicy>(msg) {} |
| 63 | +}; |
| 64 | + |
| 65 | +template <DeprecationType deprecation_type = DeprecationType::DEPRECATION, |
| 66 | + class BasePolicy = bp::default_call_policies> |
| 67 | +struct deprecated_member |
| 68 | + : deprecation_warning_policy<deprecation_type, BasePolicy> { |
| 69 | + deprecated_member(const std::string &msg = |
| 70 | + "This attribute or method has been marked as " |
| 71 | + "deprecated, and will be removed in the future.") |
| 72 | + : deprecation_warning_policy<deprecation_type, BasePolicy>(msg) {} |
| 73 | +}; |
| 74 | + |
| 75 | +} // namespace eigenpy |
| 76 | + |
| 77 | +#endif // ifndef __eigenpy_deprecation_hpp__ |
0 commit comments