我使用一些 using
语句和 unique_ptr
来处理 OpenSSL,如 在另一个问题中建议.否则,代码会变得非常丑陋,而且我不太喜欢 goto 语句.
I use some using
statements and unique_ptr
to work with OpenSSL, as suggested in another question. Without, code becomes really ugly and I am not so much a fan of goto statements.
到目前为止,我已经尽可能地更改了我的代码.以下是我使用的示例:
So far I have changed my code as far as possible. Here are examples, what I use:
using BIO_ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>;
using X509_ptr = std::unique_ptr<X509, decltype(&::X509_free)>;
using EVP_PKEY_ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
using PKCS7_ptr = std::unique_ptr<PKCS7, decltype(&::PKCS7_free)>;
...
BIO_ptr tbio(BIO_new_file(some_filename, "r"), ::BIO_free);
现在我需要 STACK_OF(X509)
,但我不知道,unique_ptr
是否也可以.我正在寻找类似于下面的内容,但这不起作用.
Now I have the need of a STACK_OF(X509)
and I do not know, if this is also possible with unique_ptr
. I am looking for something similar to below, but this is not working.
using STACK_OF_X509_ptr = std::unique_ptr<STACK_OF(X509), decltype(&::sk_X509_free)>;
我也尝试过 Functor:
I also tried the Functor:
struct StackX509Deleter {
void operator()(STACK_OF(X509) *ptr) {
sk_X509_free(ptr);
}
};
using STACK_OF_X509_ptr = std::unique_ptr<STACK_OF(X509), StackX509Deleter>;
STACK_OF_X509_ptr chain(loadIntermediate(cert.string()));
编译器接受这个并且应用程序运行.只是一个问题:在上面显示的其他 unique_ptrs
中,我总是指定了第二个参数,所以我打赌我遗漏了一些东西:
The compiler accepts this and the application runs. Just one question: In other unique_ptrs
as shown above, I always had specified a second argument, so I bet I am missing something:
STACK_OF_X509_ptr chain(loadIntermediate(cert.string()), ??????);
我如何使用 C++ unique_ptr
和 OpenSSL 的 STACK_OF(X509)*
?
How do I use C++ unique_ptr
and OpenSSL's STACK_OF(X509)*
?
我定义了一个正则函数:
I defined a regular function:
void stackOfX509Deleter(STACK_OF(X509) *ptr) {
sk_X509_free(ptr);
}
然后我在我的代码中使用它:
Then I use it in my code:
using STACK_OF_X509_ptr = std::unique_ptr<STACK_OF(X509),
decltype(&stackOfX509Deleter)>;
STACK_OF_X509_ptr chain(loadIntermediate(cert.string()),
stackOfX509Deleter);
这篇关于unique_ptr 和 OpenSSL 的 STACK_OF(X509)*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!