From a3f0af934e387ba259919f0b72b42120eef3bc5c Mon Sep 17 00:00:00 2001 From: weidai Date: Fri, 6 Dec 2002 22:02:46 +0000 Subject: add script-driven testing git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@24 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- factory.h | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 factory.h (limited to 'factory.h') diff --git a/factory.h b/factory.h new file mode 100644 index 0000000..85d6631 --- /dev/null +++ b/factory.h @@ -0,0 +1,90 @@ +#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: + ~ObjectFactoryRegistry() + { + for (Map::iterator i = m_map.begin(); i != m_map.end(); ++i) + { + delete i->second; + i->second = NULL; + } + } + + void RegisterFactory(const char *name, ObjectFactory *factory) + { + m_map[name] = factory; + } + + const ObjectFactory * GetFactory(const char *name) const + { + Map::const_iterator i = m_map.find(name); + return i == m_map.end() ? NULL : i->second; + } + + AbstractClass *CreateObject(const char *name) const + { + const ObjectFactory *factory = GetFactory(name); + return factory ? factory->CreateObject() : NULL; + } + + static ObjectFactoryRegistry & Registry() + { + static ObjectFactoryRegistry s_registry; + return s_registry; + } + +private: + typedef std::map *> Map; + Map m_map; +}; + +template +void RegisterDefaultFactoryFor(const char *name, AbstractClass *Dummy1=NULL, ConcreteClass *Dummy2=NULL) +{ + ObjectFactoryRegistry::Registry().RegisterFactory(name, new DefaultObjectFactory); +} + +template +void RegisterPublicKeyCryptoSystemDefaultFactories(const char *name, SchemeClass *dummy=NULL) +{ + RegisterDefaultFactoryFor(name); + RegisterDefaultFactoryFor(name); +} + +template +void RegisterSignatureSchemeDefaultFactories(const char *name, SchemeClass *dummy=NULL) +{ + RegisterDefaultFactoryFor(name); + RegisterDefaultFactoryFor(name); +} + +NAMESPACE_END + +#endif -- cgit v1.2.1