mirror of
https://github.com/qpdf/qpdf.git
synced 2024-12-23 11:28:56 +00:00
77 lines
1.7 KiB
C++
77 lines
1.7 KiB
C++
|
#include <qpdf/QPDFCryptoProvider.hh>
|
||
|
#include <stdexcept>
|
||
|
|
||
|
#include <qpdf/QPDFCrypto_native.hh>
|
||
|
|
||
|
std::shared_ptr<QPDFCryptoImpl>
|
||
|
QPDFCryptoProvider::getImpl()
|
||
|
{
|
||
|
QPDFCryptoProvider& p = getInstance();
|
||
|
if (p.m->default_provider.empty())
|
||
|
{
|
||
|
throw std::logic_error(
|
||
|
"QPDFCryptoProvider::getImpl called with no default provider.");
|
||
|
}
|
||
|
return p.getImpl_internal(p.m->default_provider);
|
||
|
}
|
||
|
|
||
|
std::shared_ptr<QPDFCryptoImpl>
|
||
|
QPDFCryptoProvider::getImpl(std::string const& name)
|
||
|
{
|
||
|
return getInstance().getImpl_internal(name);
|
||
|
}
|
||
|
|
||
|
template<typename T>
|
||
|
void
|
||
|
QPDFCryptoProvider::registerImpl(std::string const& name)
|
||
|
{
|
||
|
getInstance().registerImpl_internal<T>(name);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
QPDFCryptoProvider::setDefaultProvider(std::string const& name)
|
||
|
{
|
||
|
getInstance().setDefaultProvider_internal(name);
|
||
|
}
|
||
|
|
||
|
QPDFCryptoProvider::QPDFCryptoProvider() :
|
||
|
m(std::make_shared<Members>())
|
||
|
{
|
||
|
registerImpl_internal<QPDFCrypto_native>("native");
|
||
|
setDefaultProvider_internal("native");
|
||
|
}
|
||
|
|
||
|
QPDFCryptoProvider&
|
||
|
QPDFCryptoProvider::getInstance()
|
||
|
{
|
||
|
static QPDFCryptoProvider instance;
|
||
|
return instance;
|
||
|
}
|
||
|
|
||
|
std::shared_ptr<QPDFCryptoImpl>
|
||
|
QPDFCryptoProvider::getImpl_internal(std::string const& name) const
|
||
|
{
|
||
|
auto iter = this->m->providers.find(name);
|
||
|
if (iter == this->m->providers.end())
|
||
|
{
|
||
|
throw std::logic_error(
|
||
|
"QPDFCryptoProvider requested unknown implementation \"" +
|
||
|
name + "\"");
|
||
|
}
|
||
|
return this->m->providers[name]();
|
||
|
}
|
||
|
|
||
|
template<typename T>
|
||
|
void
|
||
|
QPDFCryptoProvider::registerImpl_internal(std::string const& name)
|
||
|
{
|
||
|
this->m->providers[name] = std::make_shared<T>;
|
||
|
|
||
|
}
|
||
|
|
||
|
void
|
||
|
QPDFCryptoProvider::setDefaultProvider_internal(std::string const& name)
|
||
|
{
|
||
|
this->m->default_provider = name;
|
||
|
}
|