32 utils::function_ref<
void(google::protobuf::Message&,
const google::protobuf::FieldDescriptor&)>;
85 void Compile(
const google::protobuf::Descriptor* descriptor);
88 void Compile(
const DescriptorList& descriptors);
108 using Dependencies = std::unordered_map<
109 const google::protobuf::Descriptor*,
110 std::unordered_set<
const google::protobuf::FieldDescriptor*>>;
113 using DescriptorSet = std::unordered_set<
const google::protobuf::Descriptor*>;
116 using FieldDescriptorSet = std::unordered_set<
const google::protobuf::FieldDescriptor*>;
119 const Dependencies& GetFieldsWithSelectedChildren(
utils::impl::InternalTag)
const {
120 return fields_with_selected_children_;
124 const Dependencies& GetReverseEdges(
utils::impl::InternalTag)
const {
return reverse_edges_; }
127 const DescriptorSet& GetPropagated(
utils::impl::InternalTag)
const {
return propagated_; }
130 const DescriptorSet& GetCompiled(
utils::impl::InternalTag)
const {
return compiled_; }
133 explicit VisitorCompiler(
LockBehavior lock_behavior) : lock_behavior_(lock_behavior) {}
136 ~VisitorCompiler() =
default;
139 std::shared_lock<std::shared_mutex> LockRead();
142 std::unique_lock<std::shared_mutex> LockWrite();
144 const Dependencies& GetFieldsWithSelectedChildren()
const {
return fields_with_selected_children_; }
148 virtual void CompileOne(
const google::protobuf::Descriptor& descriptor) = 0;
151 virtual bool IsSelected(
const google::protobuf::Descriptor&)
const = 0;
155 DescriptorSet GetFullSubtrees(
const DescriptorList& descriptors)
const;
158 void PropagateSelected(
const google::protobuf::Descriptor* descriptor);
160 std::shared_mutex mutex_;
163 Dependencies fields_with_selected_children_;
164 Dependencies reverse_edges_;
165 DescriptorSet propagated_;
166 DescriptorSet compiled_;
178 void Visit(google::protobuf::Message& message, Callback callback) {
180 Compile(message.GetDescriptor());
182 std::shared_lock read_lock = LockRead();
178 void Visit(google::protobuf::Message& message, Callback callback) {
…}
192 Compile(message.GetDescriptor());
194 constexpr int kMaxRecursionLimit = 100;
195 std::shared_lock read_lock = LockRead();
196 VisitRecursiveImpl(message, callback, kMaxRecursionLimit);
203 ~BaseVisitor() =
default;
206 virtual void DoVisit(google::protobuf::Message& message, Callback callback)
const = 0;
210 void VisitRecursiveImpl(google::protobuf::Message& message, Callback callback,
int recursion_limit) {
211 UINVARIANT(recursion_limit > 0,
"Recursion limit reached while traversing protobuf Message.");
217 const auto it = GetFieldsWithSelectedChildren().find(message.GetDescriptor());
218 if (it == GetFieldsWithSelectedChildren().end())
return;
220 const FieldDescriptorSet& fields = it->second;
221 for (
const google::protobuf::FieldDescriptor* field : fields) {
223 VisitNestedMessage(message, *field, [&](google::protobuf::Message& msg) {
224 VisitRecursiveImpl(msg, callback, recursion_limit - 1);
244class FieldsVisitor
final :
public BaseVisitor<FieldVisitCallback> {
246 using Selector =
utils::function_ref<
bool(
const google::protobuf::FieldDescriptor& field)>;
270 const Dependencies& GetSelectedFields(
utils::impl::InternalTag)
const {
return selected_fields_; }
274 void CompileOne(
const google::protobuf::Descriptor& descriptor)
override;
276 bool IsSelected(
const google::protobuf::Descriptor& descriptor)
const override {
277 return selected_fields_.find(&descriptor) != selected_fields_.end();
280 void DoVisit(google::protobuf::Message& message, FieldVisitCallback callback)
const override;
282 Dependencies selected_fields_;
283 const Selector selector_;
297class MessagesVisitor
final :
public BaseVisitor<MessageVisitCallback> {
299 using Selector =
utils::function_ref<
bool(
const google::protobuf::Descriptor& descriptor)>;
323 const DescriptorSet& GetSelectedMessages(
utils::impl::InternalTag)
const {
return selected_messages_; }
327 void CompileOne(
const google::protobuf::Descriptor& descriptor)
override;
329 bool IsSelected(
const google::protobuf::Descriptor& descriptor)
const override {
330 return selected_messages_.find(&descriptor) != selected_messages_.end();
333 void DoVisit(google::protobuf::Message& message, MessageVisitCallback callback)
const override;
335 DescriptorSet selected_messages_;
336 const Selector selector_;