aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/purecalc/io_specs/protobuf_raw/proto_holder.h
blob: 7d4d843bfcfef7ad7433af53f45df37e14056935 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#pragma once

#include <google/protobuf/arena.h>

#include <util/generic/ptr.h>

#include <type_traits>

namespace NYql::NPureCalc {
    class TProtoDestroyer {
    public:
        template <typename T>
        static inline void Destroy(T* t) noexcept {
            if (t->GetArena() == nullptr) {
                CheckedDelete(t);
            }
        }
    };

    template <typename TProto>
    concept IsProtoMessage = std::is_base_of_v<NProtoBuf::Message, TProto>;

    template <IsProtoMessage TProto>
    using TProtoHolder = THolder<TProto, TProtoDestroyer>;

    template <IsProtoMessage TProto, typename... TArgs>
    TProtoHolder<TProto> MakeProtoHolder(NProtoBuf::Arena* arena, TArgs&&... args) {
        auto* ptr = NProtoBuf::Arena::CreateMessage<TProto>(arena, std::forward<TArgs>(args)...);
        return TProtoHolder<TProto>(ptr);
    }
}