Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CURA-10914] Add ability to register multiple modify plugins #2123

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions include/plugins/slotproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SlotProxy
{
Default default_process{};
using value_type = PluginProxy<SlotID, SlotVersion, Stub, ValidatorTp, RequestTp, ResponseTp>;
std::optional<value_type> plugin_{ std::nullopt };
std::vector<value_type> plugins_;

public:
static constexpr plugins::v0::SlotID slot_id{ SlotID };
Expand All @@ -53,14 +53,15 @@ class SlotProxy
SlotProxy() noexcept = default;

/**
* @brief Constructs a SlotProxy object with a plugin.
*
* Constructs a SlotProxy object and initializes the plugin using the provided gRPC channel.
*
* @brief Adds a plugin to this proxy.
* @param name The fully-qualified name of the plugin
* @param version The full verison of the plugin
* @param channel A shared pointer to the gRPC channel for communication with the plugin.
*/
SlotProxy(const std::string& name, const std::string& version, std::shared_ptr<grpc::Channel> channel)
: plugin_{ value_type{ name, version, channel } } {};
void addPlugin(const std::string& name, const std::string& version, std::shared_ptr<grpc::Channel> channel)
{
plugins_.emplace_back(name, version, channel);
}

/**
* @brief Executes the plugin operation.
Expand All @@ -75,18 +76,25 @@ class SlotProxy
*/
constexpr auto generate(auto&&... args)
{
if (plugin_.has_value())
if (! plugins_.empty())
{
return plugin_.value().generate(std::forward<decltype(args)>(args)...);
return plugins_.front().generate(std::forward<decltype(args)>(args)...);
}
return std::invoke(default_process, std::forward<decltype(args)>(args)...);
}

constexpr auto modify(auto& original_value, auto&&... args)
{
if (plugin_.has_value())
if (! plugins_.empty())
{
return plugin_.value().modify(original_value, std::forward<decltype(args)>(args)...);
auto modified_value = original_value;

for (value_type& plugin : plugins_)
{
modified_value = plugin.modify(modified_value, std::forward<decltype(args)>(args)...);
}

return modified_value;
}
if constexpr (sizeof...(args) == 0)
{
Expand All @@ -98,9 +106,9 @@ class SlotProxy
template<v0::SlotID S>
void broadcast(auto&&... args)
{
if (plugin_.has_value())
for (value_type& plugin : plugins_)
{
plugin_.value().template broadcast<S>(std::forward<decltype(args)>(args)...);
plugin.template broadcast<S>(std::forward<decltype(args)>(args)...);
}
}
};
Expand Down
3 changes: 1 addition & 2 deletions include/plugins/slots.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ class Registry<Typelist<T, Types...>, Unit> : public Registry<Typelist<Types...>
{
if (slot_id == T::slot_id)
{
using Tp = typename Unit<T>::value_type;
value_.proxy = Tp{ name, version, std::forward<decltype(channel)>(channel) };
value_.proxy.addPlugin(name, version, std::forward<decltype(channel)>(channel));
return;
}
Base::connect(slot_id, name, version, std::forward<decltype(channel)>(channel));
Expand Down
Loading