ext-cryptopp/factory.h

116 lines
3.3 KiB
C
Raw Normal View History

2002-12-06 22:02:46 +00:00
#ifndef CRYPTOPP_OBJFACT_H
#define CRYPTOPP_OBJFACT_H
#include "cryptlib.h"
#include <map>
NAMESPACE_BEGIN(CryptoPP)
//! _
2002-12-06 22:02:46 +00:00
template <class AbstractClass>
class ObjectFactory
{
public:
virtual AbstractClass * CreateObject() const =0;
};
//! _
2002-12-06 22:02:46 +00:00
template <class AbstractClass, class ConcreteClass>
class DefaultObjectFactory : public ObjectFactory<AbstractClass>
{
public:
AbstractClass * CreateObject() const
{
return new ConcreteClass;
}
};
//! _
2003-03-26 21:50:44 +00:00
template <class AbstractClass, int instance=0>
2002-12-06 22:02:46 +00:00
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) {}
};
2002-12-06 22:02:46 +00:00
~ObjectFactoryRegistry()
{
2003-03-20 01:24:12 +00:00
for (CPP_TYPENAME Map::iterator i = m_map.begin(); i != m_map.end(); ++i)
2002-12-06 22:02:46 +00:00
{
2003-07-19 03:47:20 +00:00
delete (ObjectFactory<AbstractClass> *)i->second;
2002-12-06 22:02:46 +00:00
i->second = NULL;
}
}
void RegisterFactory(const std::string &name, ObjectFactory<AbstractClass> *factory)
2002-12-06 22:02:46 +00:00
{
m_map[name] = factory;
}
const ObjectFactory<AbstractClass> * GetFactory(const char *name) const
{
2003-03-20 01:24:12 +00:00
CPP_TYPENAME Map::const_iterator i = m_map.find(name);
2003-07-19 03:47:20 +00:00
return i == m_map.end() ? NULL : (ObjectFactory<AbstractClass> *)i->second;
2002-12-06 22:02:46 +00:00
}
AbstractClass *CreateObject(const char *name) const
{
const ObjectFactory<AbstractClass> *factory = GetFactory(name);
if (!factory)
throw FactoryNotFound(name);
return factory->CreateObject();
2002-12-06 22:02:46 +00:00
}
2003-03-20 03:03:11 +00:00
// VC60 workaround: use "..." to prevent this function from being inlined
2003-03-26 21:50:44 +00:00
static ObjectFactoryRegistry<AbstractClass, instance> & Registry(...);
2002-12-06 22:02:46 +00:00
private:
2003-07-19 03:47:20 +00:00
// use void * instead of ObjectFactory<AbstractClass> * to save code size
typedef std::map<std::string, void *> Map;
2002-12-06 22:02:46 +00:00
Map m_map;
};
2003-03-26 21:50:44 +00:00
template <class AbstractClass, int instance>
ObjectFactoryRegistry<AbstractClass, instance> & ObjectFactoryRegistry<AbstractClass, instance>::Registry(...)
2003-03-20 03:03:11 +00:00
{
2003-03-26 21:50:44 +00:00
static ObjectFactoryRegistry<AbstractClass, instance> s_registry;
2003-03-20 03:03:11 +00:00
return s_registry;
}
2003-03-26 21:50:44 +00:00
template <class AbstractClass, class ConcreteClass, int instance = 0>
struct RegisterDefaultFactoryFor {
RegisterDefaultFactoryFor(const char *name=NULL)
2002-12-06 22:02:46 +00:00
{
ObjectFactoryRegistry<AbstractClass, instance>::Registry().
RegisterFactory(name ? name : ConcreteClass::StaticAlgorithmName(), new DefaultObjectFactory<AbstractClass, ConcreteClass>);
2003-03-26 21:50:44 +00:00
}};
2002-12-06 22:02:46 +00:00
template <class SchemeClass>
void RegisterAsymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
2002-12-06 22:02:46 +00:00
{
2003-03-26 21:50:44 +00:00
RegisterDefaultFactoryFor<PK_Encryptor, CPP_TYPENAME SchemeClass::Encryptor>((const char *)name);
RegisterDefaultFactoryFor<PK_Decryptor, CPP_TYPENAME SchemeClass::Decryptor>((const char *)name);
2002-12-06 22:02:46 +00:00
}
template <class SchemeClass>
void RegisterSignatureSchemeDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
2002-12-06 22:02:46 +00:00
{
2003-03-26 21:50:44 +00:00
RegisterDefaultFactoryFor<PK_Signer, CPP_TYPENAME SchemeClass::Signer>((const char *)name);
RegisterDefaultFactoryFor<PK_Verifier, CPP_TYPENAME SchemeClass::Verifier>((const char *)name);
}
template <class SchemeClass>
void RegisterSymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
2003-03-26 21:50:44 +00:00
{
RegisterDefaultFactoryFor<SymmetricCipher, CPP_TYPENAME SchemeClass::Encryption, ENCRYPTION>((const char *)name);
RegisterDefaultFactoryFor<SymmetricCipher, CPP_TYPENAME SchemeClass::Decryption, DECRYPTION>((const char *)name);
2002-12-06 22:02:46 +00:00
}
NAMESPACE_END
#endif