aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/openssl/holders/holder.h
blob: 72a6e28a168de2470ceb3c786230701c9a4b7b4d (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;
};

}