#ifndef CRYPTOPP_OBJFACT_H #define CRYPTOPP_OBJFACT_H #include "cryptlib.h" #include "misc.h" #include #include #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 NAMESPACE_BEGIN(CryptoPP) //! _ template class ObjectFactory { public: virtual ~ObjectFactory () {} 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(); } // Return a vector containing the factory names. This is easier than returning an iterator. // from Andrew Pitonyak std::vector GetFactoryNames() const { std::vector 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 & 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); } template void RegisterAuthenticatedSymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL) { RegisterDefaultFactoryFor((const char *)name); RegisterDefaultFactoryFor((const char *)name); } NAMESPACE_END #if GCC_DIAGNOSTIC_AWARE # pragma GCC diagnostic push #endif #endif