ext-cryptopp/factory.h

184 lines
7.5 KiB
C
Raw Normal View History

// factory.h - originally written and placed in the public domain by Wei Dai
//! \file factory.h
//! \brief Classes and functions for registering and locating library objects
2015-11-05 06:59:46 +00:00
#ifndef CRYPTOPP_OBJFACT_H
#define CRYPTOPP_OBJFACT_H
#include "cryptlib.h"
#include "misc.h"
#include "stdcpp.h"
NAMESPACE_BEGIN(CryptoPP)
//! \class ObjectFactory
//! \brief Object factory interface for registering objects
//! \tparam AbstractClass Base class interface of the object
2015-11-05 06:59:46 +00:00
template <class AbstractClass>
class ObjectFactory
{
public:
virtual ~ObjectFactory () {}
virtual AbstractClass * CreateObject() const =0;
};
//! \class DefaultObjectFactory
//! \brief Object factory for registering objects
//! \tparam AbstractClass Base class interface of the object
//! \tparam ConcreteClass Class object
2015-11-05 06:59:46 +00:00
template <class AbstractClass, class ConcreteClass>
class DefaultObjectFactory : public ObjectFactory<AbstractClass>
{
public:
AbstractClass * CreateObject() const
{
return new ConcreteClass;
}
};
//! \class ObjectFactoryRegistry
//! \brief Object factory registry
//! \tparam AbstractClass Base class interface of the object
//! \tparam instance unique identifier
2015-11-05 06:59:46 +00:00
template <class AbstractClass, int instance=0>
class ObjectFactoryRegistry
{
public:
class FactoryNotFound : public Exception
{
public:
FactoryNotFound(const char *name) : Exception(OTHER_ERROR, std::string("ObjectFactoryRegistry: could not find factory for algorithm ") + name) {}
};
~ObjectFactoryRegistry()
{
for (typename Map::iterator i = m_map.begin(); i != m_map.end(); ++i)
2015-11-05 06:59:46 +00:00
{
delete (ObjectFactory<AbstractClass> *)i->second;
2017-03-01 11:10:06 +00:00
i->second = NULLPTR;
2015-11-05 06:59:46 +00:00
}
}
void RegisterFactory(const std::string &name, ObjectFactory<AbstractClass> *factory)
{
m_map[name] = factory;
}
const ObjectFactory<AbstractClass> * GetFactory(const char *name) const
{
typename Map::const_iterator i = m_map.find(name);
2017-03-01 11:10:06 +00:00
return i == m_map.end() ? NULLPTR : (ObjectFactory<AbstractClass> *)i->second;
2015-11-05 06:59:46 +00:00
}
AbstractClass *CreateObject(const char *name) const
{
const ObjectFactory<AbstractClass> *factory = GetFactory(name);
if (!factory)
throw FactoryNotFound(name);
return factory->CreateObject();
}
// Return a vector containing the factory names. This is easier than returning an iterator.
// from Andrew Pitonyak
std::vector<std::string> GetFactoryNames() const
{
std::vector<std::string> names;
typename Map::const_iterator iter;
2015-11-05 06:59:46 +00:00
for (iter = m_map.begin(); iter != m_map.end(); ++iter)
names.push_back(iter->first);
return names;
}
CRYPTOPP_NOINLINE static ObjectFactoryRegistry<AbstractClass, instance> & Registry(CRYPTOPP_NOINLINE_DOTDOTDOT);
private:
// use void * instead of ObjectFactory<AbstractClass> * to save code size
typedef std::map<std::string, void *> Map;
Map m_map;
};
template <class AbstractClass, int instance>
ObjectFactoryRegistry<AbstractClass, instance> & ObjectFactoryRegistry<AbstractClass, instance>::Registry(CRYPTOPP_NOINLINE_DOTDOTDOT)
{
static ObjectFactoryRegistry<AbstractClass, instance> s_registry;
return s_registry;
}
//! \class RegisterDefaultFactoryFor
//! \brief Object factory registry helper
//! \tparam AbstractClass Base class interface of the object
//! \tparam ConcreteClass Class object
//! \tparam instance unique identifier
2015-11-05 06:59:46 +00:00
template <class AbstractClass, class ConcreteClass, int instance = 0>
struct RegisterDefaultFactoryFor
2015-11-05 06:59:46 +00:00
{
2017-03-01 11:10:06 +00:00
RegisterDefaultFactoryFor(const char *name=NULLPTR)
{
// BCB2006 workaround
std::string n = name ? std::string(name) : std::string(ConcreteClass::StaticAlgorithmName());
ObjectFactoryRegistry<AbstractClass, instance>::Registry().
2015-11-05 06:59:46 +00:00
RegisterFactory(n, new DefaultObjectFactory<AbstractClass, ConcreteClass>);
}
};
2015-11-05 06:59:46 +00:00
//! \fn RegisterAsymmetricCipherDefaultFactories
//! \brief Register asymmetric ciphers
//! \tparam SchemeClass interface of the object under a scheme
//! \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
//! signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
//! symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
//! authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
2015-11-05 06:59:46 +00:00
template <class SchemeClass>
2017-03-01 11:10:06 +00:00
void RegisterAsymmetricCipherDefaultFactories(const char *name=NULLPTR)
2015-11-05 06:59:46 +00:00
{
RegisterDefaultFactoryFor<PK_Encryptor, typename SchemeClass::Encryptor>((const char *)name);
RegisterDefaultFactoryFor<PK_Decryptor, typename SchemeClass::Decryptor>((const char *)name);
2015-11-05 06:59:46 +00:00
}
//! \fn RegisterSignatureSchemeDefaultFactories
//! \brief Register signature schemes
//! \tparam SchemeClass interface of the object under a scheme
//! \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
//! signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
//! symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
//! authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
2015-11-05 06:59:46 +00:00
template <class SchemeClass>
2017-03-01 11:10:06 +00:00
void RegisterSignatureSchemeDefaultFactories(const char *name=NULLPTR)
2015-11-05 06:59:46 +00:00
{
RegisterDefaultFactoryFor<PK_Signer, typename SchemeClass::Signer>((const char *)name);
RegisterDefaultFactoryFor<PK_Verifier, typename SchemeClass::Verifier>((const char *)name);
2015-11-05 06:59:46 +00:00
}
//! \fn RegisterSymmetricCipherDefaultFactories
//! \brief Register symmetric ciphers
//! \tparam SchemeClass interface of the object under a scheme
//! \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
//! signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
//! symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
//! authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
2015-11-05 06:59:46 +00:00
template <class SchemeClass>
2017-03-01 11:10:06 +00:00
void RegisterSymmetricCipherDefaultFactories(const char *name=NULLPTR)
2015-11-05 06:59:46 +00:00
{
RegisterDefaultFactoryFor<SymmetricCipher, typename SchemeClass::Encryption, ENCRYPTION>((const char *)name);
RegisterDefaultFactoryFor<SymmetricCipher, typename SchemeClass::Decryption, DECRYPTION>((const char *)name);
2015-11-05 06:59:46 +00:00
}
//! \fn RegisterAuthenticatedSymmetricCipherDefaultFactories
//! \brief Register authenticated symmetric ciphers
//! \tparam SchemeClass interface of the object under a scheme
//! \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
//! signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
//! symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
//! authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
2015-11-05 06:59:46 +00:00
template <class SchemeClass>
2017-03-01 11:10:06 +00:00
void RegisterAuthenticatedSymmetricCipherDefaultFactories(const char *name=NULLPTR)
2015-11-05 06:59:46 +00:00
{
RegisterDefaultFactoryFor<AuthenticatedSymmetricCipher, typename SchemeClass::Encryption, ENCRYPTION>((const char *)name);
RegisterDefaultFactoryFor<AuthenticatedSymmetricCipher, typename SchemeClass::Decryption, DECRYPTION>((const char *)name);
2015-11-05 06:59:46 +00:00
}
NAMESPACE_END
#endif