ext-cryptopp/factory.h

149 lines
4.4 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 "misc.h"
2002-12-06 22:02:46 +00:00
#include <map>
2007-08-14 00:55:43 +00:00
#include <vector>
2002-12-06 22:02:46 +00:00
#if GCC_DIAGNOSTIC_AWARE
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-value"
# pragma GCC diagnostic ignored "-Wunused-variable"
# pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
2002-12-06 22:02:46 +00:00
NAMESPACE_BEGIN(CryptoPP)
//! _
2002-12-06 22:02:46 +00:00
template <class AbstractClass>
class ObjectFactory
{
public:
2011-10-12 06:11:16 +00:00
virtual ~ObjectFactory () {}
2002-12-06 22:02:46 +00:00
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
}
// 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;
CPP_TYPENAME Map::const_iterator iter;
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);
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(CRYPTOPP_NOINLINE_DOTDOTDOT)
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
{
2006-12-14 11:41:39 +00:00
// BCB2006 workaround
std::string n = name ? std::string(name) : std::string(ConcreteClass::StaticAlgorithmName());
ObjectFactoryRegistry<AbstractClass, instance>::Registry().
2006-12-14 11:41:39 +00:00
RegisterFactory(n, 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
}
template <class SchemeClass>
void RegisterAuthenticatedSymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
{
RegisterDefaultFactoryFor<AuthenticatedSymmetricCipher, CPP_TYPENAME SchemeClass::Encryption, ENCRYPTION>((const char *)name);
RegisterDefaultFactoryFor<AuthenticatedSymmetricCipher, CPP_TYPENAME SchemeClass::Decryption, DECRYPTION>((const char *)name);
}
2002-12-06 22:02:46 +00:00
NAMESPACE_END
#if GCC_DIAGNOSTIC_AWARE
# pragma GCC diagnostic push
#endif
2002-12-06 22:02:46 +00:00
#endif