walk.h 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. #pragma once
  2. #include "simple_reflection.h"
  3. #include <google/protobuf/message.h>
  4. #include <google/protobuf/descriptor.h>
  5. #include <functional>
  6. namespace NProtoBuf {
  7. // Apply @onField processor to each field in @msg (even empty)
  8. // Do not walk deeper the field if the field is an empty message
  9. // Returned bool defines if we should walk down deeper to current node children (true), or not (false)
  10. void WalkReflection(Message& msg,
  11. std::function<bool(Message&, const FieldDescriptor*)> onField);
  12. void WalkReflection(const Message& msg,
  13. std::function<bool(const Message&, const FieldDescriptor*)> onField);
  14. template <typename TOnField>
  15. inline void WalkReflection(Message& msg, TOnField& onField) { // is used when TOnField is a callable class instance
  16. WalkReflection(msg, std::function<bool(Message&, const FieldDescriptor*)>(std::ref(onField)));
  17. }
  18. template <typename TOnField>
  19. inline void WalkReflection(const Message& msg, TOnField& onField) {
  20. WalkReflection(msg, std::function<bool(const Message&, const FieldDescriptor*)>(std::ref(onField)));
  21. }
  22. // Apply @onField processor to each descriptor of a field
  23. // Walk every field including nested messages. Avoid cyclic fields pointing to themselves
  24. // Returned bool defines if we should walk down deeper to current node children (true), or not (false)
  25. void WalkSchema(const Descriptor* descriptor,
  26. std::function<bool(const FieldDescriptor*)> onField);
  27. }