Crypto++ 8.9
Free C++ class library of cryptographic schemes
factory.h
Go to the documentation of this file.
1// factory.h - originally written and placed in the public domain by Wei Dai
2
3/// \file factory.h
4/// \brief Classes and functions for registering and locating library objects
5
6#ifndef CRYPTOPP_OBJFACT_H
7#define CRYPTOPP_OBJFACT_H
8
9#include "cryptlib.h"
10#include "misc.h"
11#include "stdcpp.h"
12
13NAMESPACE_BEGIN(CryptoPP)
14
15/// \brief Object factory interface for registering objects
16/// \tparam AbstractClass Base class interface of the object
17template <class AbstractClass>
19{
20public:
21 virtual ~ObjectFactory () {}
22 virtual AbstractClass * CreateObject() const =0;
23};
24
25/// \brief Object factory for registering objects
26/// \tparam AbstractClass Base class interface of the object
27/// \tparam ConcreteClass Class object
28template <class AbstractClass, class ConcreteClass>
29class DefaultObjectFactory : public ObjectFactory<AbstractClass>
30{
31public:
32 AbstractClass * CreateObject() const
33 {
34 return new ConcreteClass;
35 }
36};
37
38/// \brief Object factory registry
39/// \tparam AbstractClass Base class interface of the object
40/// \tparam instance unique identifier
41template <class AbstractClass, int instance=0>
43{
44public:
46 {
47 public:
48 FactoryNotFound(const char *name) : Exception(OTHER_ERROR, std::string("ObjectFactoryRegistry: could not find factory for algorithm ") + name) {}
49 };
50
52 {
53 for (typename Map::iterator i = m_map.begin(); i != m_map.end(); ++i)
54 {
55 delete (ObjectFactory<AbstractClass> *)i->second;
56 i->second = NULLPTR;
57 }
58 }
59
60 void RegisterFactory(const std::string &name, ObjectFactory<AbstractClass> *factory)
61 {
62 m_map[name] = factory;
63 }
64
65 const ObjectFactory<AbstractClass> * GetFactory(const char *name) const
66 {
67 typename Map::const_iterator i = m_map.find(name);
68 return i == m_map.end() ? NULLPTR : (ObjectFactory<AbstractClass> *)i->second;
69 }
70
71 AbstractClass *CreateObject(const char *name) const
72 {
73 const ObjectFactory<AbstractClass> *factory = GetFactory(name);
74 if (!factory)
75 throw FactoryNotFound(name);
76 return factory->CreateObject();
77 }
78
79 // Return a vector containing the factory names. This is easier than returning an iterator.
80 // from Andrew Pitonyak
81 std::vector<std::string> GetFactoryNames() const
82 {
83 std::vector<std::string> names;
84 typename Map::const_iterator iter;
85 for (iter = m_map.begin(); iter != m_map.end(); ++iter)
86 names.push_back(iter->first);
87 return names;
88 }
89
90 CRYPTOPP_NOINLINE static ObjectFactoryRegistry<AbstractClass, instance> & Registry(CRYPTOPP_NOINLINE_DOTDOTDOT);
91
92private:
93 // use void * instead of ObjectFactory<AbstractClass> * to save code size
94 typedef std::map<std::string, void *> Map;
95 Map m_map;
96};
97
98template <class AbstractClass, int instance>
100{
102 return s_registry;
103}
104
105/// \brief Object factory registry helper
106/// \tparam AbstractClass Base class interface of the object
107/// \tparam ConcreteClass Class object
108/// \tparam instance unique identifier
109template <class AbstractClass, class ConcreteClass, int instance = 0>
111{
112 RegisterDefaultFactoryFor(const char *name=NULLPTR)
113 {
114 // BCB2006 workaround
115 std::string n = name ? std::string(name) : std::string(ConcreteClass::StaticAlgorithmName());
118 }
119};
120
121/// \fn RegisterAsymmetricCipherDefaultFactories
122/// \brief Register asymmetric ciphers
123/// \tparam SchemeClass interface of the object under a scheme
124/// \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
125/// signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
126/// symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
127/// authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
128template <class SchemeClass>
129void RegisterAsymmetricCipherDefaultFactories(const char *name=NULLPTR)
130{
133}
134
135/// \fn RegisterSignatureSchemeDefaultFactories
136/// \brief Register signature schemes
137/// \tparam SchemeClass interface of the object under a scheme
138/// \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
139/// signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
140/// symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
141/// authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
142template <class SchemeClass>
143void RegisterSignatureSchemeDefaultFactories(const char *name=NULLPTR)
144{
147}
148
149/// \fn RegisterSymmetricCipherDefaultFactories
150/// \brief Register symmetric ciphers
151/// \tparam SchemeClass interface of the object under a scheme
152/// \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
153/// signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
154/// symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
155/// authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
156template <class SchemeClass>
157void RegisterSymmetricCipherDefaultFactories(const char *name=NULLPTR)
158{
161}
162
163/// \fn RegisterAuthenticatedSymmetricCipherDefaultFactories
164/// \brief Register authenticated symmetric ciphers
165/// \tparam SchemeClass interface of the object under a scheme
166/// \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
167/// signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
168/// symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
169/// authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
170template <class SchemeClass>
171void RegisterAuthenticatedSymmetricCipherDefaultFactories(const char *name=NULLPTR)
172{
175}
176
177NAMESPACE_END
178
179#endif
Object factory for registering objects.
Definition factory.h:30
Base class for all exceptions thrown by the library.
Definition cryptlib.h:164
Exception(ErrorType errorType, const std::string &s)
Construct a new Exception.
Definition cryptlib.h:188
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Definition cryptlib.h:182
Object factory interface for registering objects.
Definition factory.h:19
Object factory registry.
Definition factory.h:43
Abstract base classes that provide a uniform interface to this library.
Utility functions for the Crypto++ library.
Crypto++ library namespace.
Common C++ header files.
Object factory registry helper.
Definition factory.h:111