path.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. #include <google/protobuf/descriptor.h>
  3. #include <google/protobuf/message.h>
  4. #include <util/generic/vector.h>
  5. namespace NProtoBuf {
  6. class TFieldPath {
  7. public:
  8. TFieldPath();
  9. TFieldPath(const Descriptor* msgType, const TStringBuf& path); // throws exception if path doesn't exist
  10. TFieldPath(const TVector<const FieldDescriptor*>& path);
  11. TFieldPath(const TFieldPath&) = default;
  12. TFieldPath& operator=(const TFieldPath&) = default;
  13. bool InitUnsafe(const Descriptor* msgType, const TStringBuf path); // noexcept
  14. void Init(const Descriptor* msgType, const TStringBuf& path); // throws
  15. const TVector<const FieldDescriptor*>& Fields() const {
  16. return Path;
  17. }
  18. void AddField(const FieldDescriptor* field) {
  19. Path.push_back(field);
  20. }
  21. const Descriptor* ParentType() const {
  22. return Empty() ? nullptr : Path.front()->containing_type();
  23. }
  24. const FieldDescriptor* FieldDescr() const {
  25. return Empty() ? nullptr : Path.back();
  26. }
  27. bool Empty() const {
  28. return Path.empty();
  29. }
  30. explicit operator bool() const {
  31. return !Empty();
  32. }
  33. bool operator!() const {
  34. return Empty();
  35. }
  36. private:
  37. TVector<const FieldDescriptor*> Path;
  38. };
  39. }