Skip to content

Commit

Permalink
Add deprecated_function/deprecated_member child classes
Browse files Browse the repository at this point in the history
+ shortcut for different default messages
  • Loading branch information
ManifoldFR committed May 2, 2024
1 parent b75fb0d commit 95454e9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
37 changes: 28 additions & 9 deletions include/eigenpy/deprecation-policy.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//
// Copyright (C) 2020 INRIA
// Copyright (C) 2024 LAAS-CNRS, INRIA
//
#ifndef __eigenpy_deprecation_hpp__
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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__
13 changes: 11 additions & 2 deletions unittest/deprecation_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<>());
}
7 changes: 6 additions & 1 deletion unittest/python/test_deprecation_policy.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 95454e9

Please sign in to comment.