From 83d054ac1252382b3da301e9fa84811160c8766d Mon Sep 17 00:00:00 2001 From: ManifoldFR <wilson.jallet@polytechnique.org> Date: Thu, 2 May 2024 17:35:58 +0200 Subject: [PATCH] Add deprecated_function/deprecated_member child classes + shortcut for different default messages Co-authored-by: Justin Carpentier <justin.carpentier@inria.fr> --- include/eigenpy/deprecation-policy.hpp | 37 ++++++++++++++++------ unittest/deprecation_policy.cpp | 13 ++++++-- unittest/python/test_deprecation_policy.py | 7 +++- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/include/eigenpy/deprecation-policy.hpp b/include/eigenpy/deprecation-policy.hpp index c1635ea1..f763065f 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 d06d4954..c819f458 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 b47550c7..46bc922c 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() -- GitLab