diff --git a/include/eigenpy/deprecation-policy.hpp b/include/eigenpy/deprecation-policy.hpp index c1635ea1703557841a115d70599ecf1b6f725e25..f763065f211ad1d76fcccdf53debf2856bef20e9 100644 --- a/include/eigenpy/deprecation-policy.hpp +++ b/include/eigenpy/deprecation-policy.hpp @@ -1,4 +1,5 @@ // +// Copyright (C) 2020 INRIA // Copyright (C) 2024 LAAS-CNRS, INRIA // #ifndef __eigenpy_deprecation_hpp__ @@ -23,11 +24,6 @@ constexpr PyObject *deprecationTypeToPyObj(DeprecationType dep) { } // namespace detail -constexpr char defaultDeprecationMessage[] = - "This function or attribute has been marked as deprecated, and will be " - "removed in the " - "future."; - /// @brief A Boost.Python call policy which triggers a Python warning on /// precall. template <DeprecationType deprecation_type = DeprecationType::DEPRECATION, @@ -36,11 +32,10 @@ struct deprecation_warning_policy : BasePolicy { using result_converter = typename BasePolicy::result_converter; using argument_package = typename BasePolicy::argument_package; - deprecation_warning_policy( - const std::string &warning_msg = defaultDeprecationMessage) + deprecation_warning_policy(const std::string &warning_msg) : BasePolicy(), m_what(warning_msg) {} - const std::string what() const { return m_what; } + std::string what() const { return m_what; } const BasePolicy *derived() const { return static_cast<const BasePolicy *>(this); @@ -53,10 +48,34 @@ struct deprecation_warning_policy : BasePolicy { return derived()->precall(args); } - private: + protected: const std::string m_what; }; +template <DeprecationType deprecation_type = DeprecationType::DEPRECATION, + class BasePolicy = bp::default_call_policies> +struct deprecated_function + : deprecation_warning_policy<deprecation_type, BasePolicy> { + static constexpr char defaultMsg[] = + "This function has been marked as deprecated, and will be " + "removed in the future."; + + deprecated_function(const std::string &msg = defaultMsg) + : deprecation_warning_policy<deprecation_type, BasePolicy>(msg) {} +}; + +template <DeprecationType deprecation_type = DeprecationType::DEPRECATION, + class BasePolicy = bp::default_call_policies> +struct deprecated_member + : deprecation_warning_policy<deprecation_type, BasePolicy> { + static constexpr char defaultMsg[] = + "This attribute or method has been marked as deprecated, and will be " + "removed in the future."; + + deprecated_member(const std::string &msg = defaultMsg) + : deprecation_warning_policy<deprecation_type, BasePolicy>(msg) {} +}; + } // namespace eigenpy #endif // ifndef __eigenpy_deprecation_hpp__ diff --git a/unittest/deprecation_policy.cpp b/unittest/deprecation_policy.cpp index d06d4954fff9804228b42495f64bc4579b64ed85..c819f458b5032b13da57e58c548bdcccb0d49c41 100644 --- a/unittest/deprecation_policy.cpp +++ b/unittest/deprecation_policy.cpp @@ -16,9 +16,18 @@ void some_future_deprecated_function() { << std::endl; } +class X { + public: + void deprecated_member_function() {} +}; + BOOST_PYTHON_MODULE(deprecation_policy) { bp::def("some_deprecated_function", some_deprecated_function, - eigenpy::deprecation_warning_policy<DeprecationType::DEPRECATION>()); + eigenpy::deprecated_function<DeprecationType::DEPRECATION>()); bp::def("some_future_deprecated_function", some_future_deprecated_function, - eigenpy::deprecation_warning_policy<DeprecationType::FUTURE>()); + eigenpy::deprecated_function<DeprecationType::FUTURE>()); + + bp::class_<X>("X", bp::init<>(bp::args("self"))) + .def("deprecated_member_function", &X::deprecated_member_function, + eigenpy::deprecated_member<>()); } diff --git a/unittest/python/test_deprecation_policy.py b/unittest/python/test_deprecation_policy.py index b47550c7536f828e40d764e4f4f7a383fff2d701..46bc922c649ac46fc6e85f190cea1bf0a8966a05 100644 --- a/unittest/python/test_deprecation_policy.py +++ b/unittest/python/test_deprecation_policy.py @@ -1,4 +1,9 @@ -from deprecation_policy import some_deprecated_function, some_future_deprecated_function +from deprecation_policy import ( + X, + some_deprecated_function, + some_future_deprecated_function, +) some_deprecated_function() some_future_deprecated_function() +X().deprecated_member_function()