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

Update ObservableProxy::pipe to support any number of operators #387

Merged
merged 3 commits into from
Sep 25, 2023
Merged
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
75 changes: 11 additions & 64 deletions python/mrc/_pymrc/src/subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ PySubscription ObservableProxy::subscribe(PyObjectObservable* self, PyObjectSubs
return self->subscribe(subscriber);
}

template <typename... OpsT>
PyObjectObservable pipe_ops(const PyObjectObservable* self, OpsT&&... ops)
{
return (*self | ... | ops);
}

PyObjectObservable ObservableProxy::pipe(const PyObjectObservable* self, py::args args)
{
std::vector<PyObjectOperateFn> operators;
Expand All @@ -150,66 +144,19 @@ PyObjectObservable ObservableProxy::pipe(const PyObjectObservable* self, py::arg
operators.emplace_back(op.get_operate_fn());
}

switch (operators.size())
if (operators.empty())
{
throw std::runtime_error("pipe() must be given at least one argument");
}

auto result = *self | operators[0];

for (auto i = 1; i < operators.size(); i++)
{
case 1:
return pipe_ops(self, operators[0]);
case 2:
return pipe_ops(self, operators[0], operators[1]);
case 3:
return pipe_ops(self, operators[0], operators[1], operators[2]);
case 4:
return pipe_ops(self, operators[0], operators[1], operators[2], operators[3]);
case 5:
return pipe_ops(self, operators[0], operators[1], operators[2], operators[3], operators[4]);
case 6:
return pipe_ops(self, operators[0], operators[1], operators[2], operators[3], operators[4], operators[5]);
case 7:
return pipe_ops(self,
operators[0],
operators[1],
operators[2],
operators[3],
operators[4],
operators[5],
operators[6]);
case 8:
return pipe_ops(self,
operators[0],
operators[1],
operators[2],
operators[3],
operators[4],
operators[5],
operators[6],
operators[7]);
case 9:
return pipe_ops(self,
operators[0],
operators[1],
operators[2],
operators[3],
operators[4],
operators[5],
operators[6],
operators[7],
operators[8]);
case 10:
return pipe_ops(self,
operators[0],
operators[1],
operators[2],
operators[3],
operators[4],
operators[5],
operators[6],
operators[7],
operators[8],
operators[9]);
default:
// Not supported error
throw std::runtime_error("pipe() only supports up 10 arguments. Please use another pipe() to use more");
result = result | operators[i];
}

return result;
}

} // namespace mrc::pymrc