proto_holder.h 859 B

12345678910111213141516171819202122232425262728293031
  1. #pragma once
  2. #include <google/protobuf/arena.h>
  3. #include <util/generic/ptr.h>
  4. #include <type_traits>
  5. namespace NYql::NPureCalc {
  6. class TProtoDestroyer {
  7. public:
  8. template <typename T>
  9. static inline void Destroy(T* t) noexcept {
  10. if (t->GetArena() == nullptr) {
  11. CheckedDelete(t);
  12. }
  13. }
  14. };
  15. template <typename TProto>
  16. concept IsProtoMessage = std::is_base_of_v<NProtoBuf::Message, TProto>;
  17. template <IsProtoMessage TProto>
  18. using TProtoHolder = THolder<TProto, TProtoDestroyer>;
  19. template <IsProtoMessage TProto, typename... TArgs>
  20. TProtoHolder<TProto> MakeProtoHolder(NProtoBuf::Arena* arena, TArgs&&... args) {
  21. auto* ptr = NProtoBuf::Arena::CreateMessage<TProto>(arena, std::forward<TArgs>(args)...);
  22. return TProtoHolder<TProto>(ptr);
  23. }
  24. }