Skip to content

Commit

Permalink
[filter] support input control optional api
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisCarouge committed Feb 13, 2024
1 parent 5ddcfe8 commit 1fd7350
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/fcarouge/internal/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct std::formatter<
format_context.advance_to(
format_to(format_context.out(), R"({{"f": {}, )", filter.f()));

if constexpr (requires { filter.g(); }) {
if constexpr (fcarouge::internal::has_input_control_method<kalman>) {
format_context.advance_to(
format_to(format_context.out(), R"("g": {}, )", filter.g()));
}
Expand Down
27 changes: 15 additions & 12 deletions include/fcarouge/internal/kalman.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,29 @@ kalman<State, Output, Input, UpdateTypes, PredictionTypes>::h(
template <typename State, typename Output, typename Input, typename UpdateTypes,
typename PredictionTypes>
[[nodiscard("The returned control transition matrix G is unexpectedly "
"discarded.")]] inline constexpr auto
"discarded.")]] inline constexpr const auto &
kalman<State, Output, Input, UpdateTypes, PredictionTypes>::g() const
-> const input_control &requires(requires { filter.g; }) {
return filter.g;
}
requires(has_input_control<implementation>)
{
return filter.g;
}

template <typename State, typename Output, typename Input, typename UpdateTypes,
typename PredictionTypes>
[[nodiscard("The returned control transition matrix G is unexpectedly "
"discarded.")]] inline constexpr auto kalman<State, Output, Input,
UpdateTypes,
PredictionTypes>::g()
-> input_control &requires(requires { filter.g; }) { return filter.g; }
"discarded.")]] inline constexpr auto &
kalman<State, Output, Input, UpdateTypes, PredictionTypes>::g()
requires(has_input_control<implementation>)
{
return filter.g;
}

template <typename State, typename Output, typename Input, typename UpdateTypes,
typename PredictionTypes>
inline constexpr void kalman<State, Output, Input, UpdateTypes,
PredictionTypes>::g(const auto &value,
const auto &...values)
requires(requires { filter.g; })
inline constexpr void
kalman<State, Output, Input, UpdateTypes, PredictionTypes>::g(
const auto &value, const auto &...values)
requires(has_input_control<implementation>)
{
using transition_control_function = decltype(filter.transition_control_g);
filter.transition_control_g =
Expand Down
11 changes: 11 additions & 0 deletions include/fcarouge/internal/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ concept has_input_method = requires(Filter filter) { filter.u(); };
template <typename Filter>
concept has_input = has_input_member<Filter> || has_input_method<Filter>;

template <typename Filter>
concept has_input_control_member = requires(Filter filter) { filter.g; };

template <typename Filter>
concept has_input_control_method = requires(Filter filter) { filter.g(); };

//! @todo Shorten when MSVC has better if-constexpr-requires support.
template <typename Filter>
concept has_input_control =
has_input_control_member<Filter> || has_input_control_method<Filter>;

struct empty {
inline constexpr explicit empty([[maybe_unused]] auto &&...any) noexcept {
// Constructs from anything for all initializations compatibility.
Expand Down
9 changes: 5 additions & 4 deletions include/fcarouge/kalman.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,10 @@ class kalman final {
//! @return The control transition matrix G.
//!
//! @complexity Constant.
inline constexpr auto g() const
-> const input_control &requires(requires { filter.g; });
inline constexpr auto g() -> input_control &requires(requires { filter.g; });
inline constexpr const auto &g() const
requires(has_input_control<implementation>);
inline constexpr auto &g()
requires(has_input_control<implementation>);

//! @brief Sets the control transition matrix G.
//!
Expand All @@ -506,7 +507,7 @@ class kalman final {
//!
//! @complexity Constant.
inline constexpr void g(const auto &value, const auto &...values)
requires(requires { filter.g; });
requires(has_input_control<implementation>);

//! @brief Returns the gain matrix K.
//!
Expand Down
7 changes: 7 additions & 0 deletions include/fcarouge/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ concept eigen = internal::eigen<Type>;
//! member and `u()` input method.
template <typename Filter>
concept has_input = internal::has_input<Filter>;

//! @brief Filter input control support concept.
//!
//! @details The filter supports the input control related functionality:
//! `input_control` type member and `g()` input method.
template <typename Filter>
concept has_input_control = internal::has_input_control<Filter>;
//! @}

//! @name Types
Expand Down

0 comments on commit 1fd7350

Please sign in to comment.