aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/openssl/holders/holder.h
blob: c2a26ce4312de66d833d4c1bddbe184e5ca97b26 (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
32
#pragma once

#include <util/generic/yexception.h>

namespace NOpenSSL {

template <typename TType, auto Create, auto Destroy, class... Args>
class THolder {
public:
    inline THolder(Args... args) {
        Ptr = Create(args...);
        if (!Ptr) {
            throw std::bad_alloc();
        }
    }

    THolder(const THolder&) = delete;
    THolder& operator=(const THolder&) = delete;

    inline ~THolder() noexcept {
        Destroy(Ptr);
    }

    inline operator TType* () noexcept {
        return Ptr;
    }

private:
    TType* Ptr;
};

}