#ifndef CRYPTOPP_OBJFACT_H #define CRYPTOPP_OBJFACT_H #include "cryptlib.h" #include NAMESPACE_BEGIN(CryptoPP) //! _ template class ObjectFactory { public: virtual AbstractClass * CreateObject() const =0; }; //! _ template class DefaultObjectFactory : public ObjectFactory { public: AbstractClass * CreateObject() const { return new ConcreteClass; } }; //! _ template 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 (CPP_TYPENAME Map::iterator i = m_map.begin(); i != m_map.end(); ++i) { delete (ObjectFactory *)i->second; i->second = NULL; } } void RegisterFactory(const std::string &name, ObjectFactory *factory) { m_map[name] = factory; } const ObjectFactory * GetFactory(const char *name) const { CPP_TYPENAME Map::const_iterator i = m_map.find(name); return i == m_map.end() ? NULL : (ObjectFactory *)i->second; } AbstractClass *CreateObject(const char *name) const { const ObjectFactory *factory = GetFactory(name); if (!factory) throw FactoryNotFound(name); return factory->CreateObject(); } CRYPTOPP_NOINLINE static ObjectFactoryRegistry & Registry(CRYPTOPP_NOINLINE_DOTDOTDOT); private: // use void * instead of ObjectFactory * to save code size typedef std::map Map; Map m_map; }; template ObjectFactoryRegistry & ObjectFactoryRegistry::Registry(CRYPTOPP_NOINLINE_DOTDOTDOT) { static ObjectFactoryRegistry s_registry; return s_registry; } template struct RegisterDefaultFactoryFor { RegisterDefaultFactoryFor(const char *name=NULL) { // BCB2006 workaround std::string n = name ? std::string(name) : std::string(ConcreteClass::StaticAlgorithmName()); ObjectFactoryRegistry::Registry(). RegisterFactory(n, new DefaultObjectFactory); }}; template void RegisterAsymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL) { RegisterDefaultFactoryFor((const char *)name); RegisterDefaultFactoryFor((const char *)name); } template void RegisterSignatureSchemeDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL) { RegisterDefaultFactoryFor((const char *)name); RegisterDefaultFactoryFor((const char *)name); } template void RegisterSymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL) { RegisterDefaultFactoryFor((const char *)name); RegisterDefaultFactoryFor((const char *)name); } NAMESPACE_END #endif