#include <grpc/support/port_platform.h>
#include "src/core/lib/surface/channel_init.h"
#include <algorithm>
namespace grpc_core {
void ChannelInit::Builder::RegisterStage(grpc_channel_stack_type type,
int priority, Stage stage) {
slots_[type].emplace_back(std::move(stage), priority);
}
ChannelInit ChannelInit::Builder::Build() {
ChannelInit result;
for (int i = 0; i < GRPC_NUM_CHANNEL_STACK_TYPES; i++) {
auto& slots = slots_[i];
std::stable_sort(
slots.begin(), slots.end(),
[](const Slot& a, const Slot& b) { return a.priority < b.priority; });
auto& result_slots = result.slots_[i];
result_slots.reserve(slots.size());
for (auto& slot : slots) {
result_slots.emplace_back(std::move(slot.stage));
}
}
return result;
}
bool ChannelInit::CreateStack(ChannelStackBuilder* builder) const {
for (const auto& stage : slots_[builder->channel_stack_type()]) {
if (!stage(builder)) return false;
}
return true;
}
}