Skip to content

Commit

Permalink
config/property: template set_value to allow conversion to T
Browse files Browse the repository at this point in the history
a properly typed set_value(std::constructible_from<T, U> U) helps when T
is std::optional<V> and V can be constructed from U.

for example, property<optional<ms>>.set_value(1h).

before this change, this would throw a bad_any_cast.

derived classes needs to pull this in via using property::set_value, and
will receive a std::any via the overriding set_value(std::any), like
before.

retention_property needed to be adjusted for this (not clear if it was
buggy before)

(cherry picked from commit aa768ef)
  • Loading branch information
andijcr authored and Michal Maslanka committed Feb 19, 2024
1 parent 69cde13 commit b6c6477
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/v/config/bounded_property.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,12 @@ class bounded_property : public property<T> {
, _bounds(bounds)
, _example(generate_example()) {}

using property<T>::set_value;

void set_value(std::any v) override {
property<T>::update_value(std::any_cast<T>(std::move(v)));
}

bool set_value(YAML::Node n) override {
auto val = std::move(n.as<T>());
return clamp_and_update(val);
Expand Down
15 changes: 14 additions & 1 deletion src/v/config/property.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ class property : public base_property {
update_value(std::any_cast<T>(std::move(v)));
}

template<typename U>
requires std::constructible_from<T, U>
void set_value(U&& v) {
// needs to go through virtual inheritance chain, since this class is
// not final
set_value(std::make_any<T>(std::forward<U>(v)));
}

bool set_value(YAML::Node n) override {
return update_value(std::move(n.as<T>()));
}
Expand Down Expand Up @@ -865,9 +873,14 @@ class retention_duration_property final
: public property<std::optional<std::chrono::milliseconds>> {
public:
using property::property;
using property::set_value;

void set_value(std::any v) final {
update_value(std::any_cast<std::chrono::milliseconds>(std::move(v)));
update_value(
std::any_cast<std::optional<std::chrono::milliseconds>>(std::move(v))
.value_or(-1ms));
}

bool set_value(YAML::Node n) final {
return update_value(n.as<std::chrono::milliseconds>());
}
Expand Down

0 comments on commit b6c6477

Please sign in to comment.