iterators.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. #include <google/protobuf/descriptor.h>
  3. namespace NProtoBuf {
  4. class TFieldsIterator {
  5. public:
  6. explicit TFieldsIterator(const NProtoBuf::Descriptor* descriptor, int position = 0)
  7. : Descriptor(descriptor)
  8. , Position(position)
  9. { }
  10. TFieldsIterator& operator++() {
  11. ++Position;
  12. return *this;
  13. }
  14. TFieldsIterator& operator++(int) {
  15. auto& ret = *this;
  16. ++*this;
  17. return ret;
  18. }
  19. const NProtoBuf::FieldDescriptor* operator*() const {
  20. return Descriptor->field(Position);
  21. }
  22. bool operator== (const TFieldsIterator& other) const {
  23. return Position == other.Position && Descriptor == other.Descriptor;
  24. }
  25. bool operator!= (const TFieldsIterator& other) const {
  26. return !(*this == other);
  27. }
  28. private:
  29. const NProtoBuf::Descriptor* Descriptor = nullptr;
  30. int Position = 0;
  31. };
  32. }
  33. // Namespaces required by `range-based for` ADL:
  34. namespace google {
  35. namespace protobuf {
  36. NProtoBuf::TFieldsIterator begin(const NProtoBuf::Descriptor& descriptor) {
  37. return NProtoBuf::TFieldsIterator(&descriptor);
  38. }
  39. NProtoBuf::TFieldsIterator end(const NProtoBuf::Descriptor& descriptor) {
  40. return NProtoBuf::TFieldsIterator(&descriptor, descriptor.field_count());
  41. }
  42. }
  43. }