diff options
| author | coteeq <[email protected]> | 2025-03-19 18:20:36 +0300 |
|---|---|---|
| committer | coteeq <[email protected]> | 2025-03-19 19:07:06 +0300 |
| commit | d37d28aa60058e6dee72591247b17d938da557b7 (patch) | |
| tree | b72b55c6d575aae1aff4ad542d6d3afdb7a72688 | |
| parent | 3804b785cc4f57c5589d36672e3f116782e53892 (diff) | |
Allow passing update arguments by value
commit_hash:21ec3f9f2dcd2a944074c3b83196c6e1b61ef64f
| -rw-r--r-- | yt/yt/core/ytree/yson_struct_update-inl.h | 56 | ||||
| -rw-r--r-- | yt/yt/core/ytree/yson_struct_update.h | 4 |
2 files changed, 54 insertions, 6 deletions
diff --git a/yt/yt/core/ytree/yson_struct_update-inl.h b/yt/yt/core/ytree/yson_struct_update-inl.h index 7405e1078a3..25ebc3652dc 100644 --- a/yt/yt/core/ytree/yson_struct_update-inl.h +++ b/yt/yt/core/ytree/yson_struct_update-inl.h @@ -48,6 +48,22 @@ struct TUnwrapYsonStructIntrusivePtr<TIntrusivePtr<T>> //////////////////////////////////////////////////////////////////////////////// +template <class TValue, class... Args> +TCallback<void(const TValue&, const TValue&)> WrapUserCallback(TCallback<void(Args...)> callback) +{ + return BIND_NO_PROPAGATE([callback = std::move(callback)] (const TValue& oldValue, const TValue& newValue) { + if constexpr (sizeof...(Args) == 2) { + callback(oldValue, newValue); + } else if constexpr (sizeof...(Args) == 1) { + callback(newValue); + } else { + static_assert(TDependentFalse<Args...>, "Wrong number of arguments"); + } + }); +} + +//////////////////////////////////////////////////////////////////////////////// + template <class TValue> TFieldConfigurator<TValue>& TFieldConfigurator<TValue>::Validator(TCallback<void(const TValue&, const TValue&)> validator) { @@ -57,12 +73,26 @@ TFieldConfigurator<TValue>& TFieldConfigurator<TValue>::Validator(TCallback<void } template <class TValue> +TFieldConfigurator<TValue>& TFieldConfigurator<TValue>::Validator(TCallback<void(TValue, TValue)> validator) +{ + VerifyEmptyValidator(); + Validator_ = WrapUserCallback<TValue>(std::move(validator)); + return *this; +} + +template <class TValue> TFieldConfigurator<TValue>& TFieldConfigurator<TValue>::Validator(TCallback<void(const TValue&)> validator) { VerifyEmptyValidator(); - Validator_ = BIND_NO_PROPAGATE([validator = std::move(validator)] (const TValue& /*oldValue*/, const TValue& newValue) { - validator(std::move(newValue)); - }); + Validator_ = WrapUserCallback<TValue>(std::move(validator)); + return *this; +} + +template <class TValue> +TFieldConfigurator<TValue>& TFieldConfigurator<TValue>::Validator(TCallback<void(TValue)> validator) +{ + VerifyEmptyValidator(); + Validator_ = WrapUserCallback<TValue>(std::move(validator)); return *this; } @@ -75,12 +105,26 @@ TFieldConfigurator<TValue>& TFieldConfigurator<TValue>::Updater(TCallback<void(c } template <class TValue> +TFieldConfigurator<TValue>& TFieldConfigurator<TValue>::Updater(TCallback<void(TValue, TValue)> updater) +{ + VerifyEmptyUpdater(); + Updater_ = WrapUserCallback<TValue>(std::move(updater)); + return *this; +} + +template <class TValue> TFieldConfigurator<TValue>& TFieldConfigurator<TValue>::Updater(TCallback<void(const TValue&)> updater) { VerifyEmptyUpdater(); - Updater_ = BIND_NO_PROPAGATE([updater = std::move(updater)] (const TValue& /*oldValue*/, const TValue& newValue) { - updater(std::move(newValue)); - }); + Updater_ = WrapUserCallback<TValue>(std::move(updater)); + return *this; +} + +template <class TValue> +TFieldConfigurator<TValue>& TFieldConfigurator<TValue>::Updater(TCallback<void(TValue)> updater) +{ + VerifyEmptyUpdater(); + Updater_ = WrapUserCallback<TValue>(std::move(updater)); return *this; } diff --git a/yt/yt/core/ytree/yson_struct_update.h b/yt/yt/core/ytree/yson_struct_update.h index fde7fc94cf3..393e104b913 100644 --- a/yt/yt/core/ytree/yson_struct_update.h +++ b/yt/yt/core/ytree/yson_struct_update.h @@ -49,15 +49,19 @@ class TFieldConfigurator public: // Registers validator that accepts old and new values as arguments. TFieldConfigurator& Validator(TCallback<void(const TValue&, const TValue&)> validator); + TFieldConfigurator& Validator(TCallback<void(TValue, TValue)> validator); // Registers validator that accepts only new value as an argument. TFieldConfigurator& Validator(TCallback<void(const TValue&)> validator); + TFieldConfigurator& Validator(TCallback<void(TValue)> validator); // Registers updater that accepts old and new values as arguments. TFieldConfigurator& Updater(TCallback<void(const TValue&, const TValue&)> updater); + TFieldConfigurator& Updater(TCallback<void(TValue, TValue)> updater); // Registers updater that accepts only new value as an argument. TFieldConfigurator& Updater(TCallback<void(const TValue&)> updater); + TFieldConfigurator& Updater(TCallback<void(TValue)> updater); // Registers nested YsonStruct to be updated recursively. template <CYsonStructDerived TUnwrappedValue> |
