Crypto++  5.6.5
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 
13 NAMESPACE_BEGIN(CryptoPP)
14 
15 //! \class ObjectFactory
16 //! \brief Object factory interface for registering objects
17 //! \tparam AbstractClass Base class interface of the object
18 template <class AbstractClass>
20 {
21 public:
22  virtual ~ObjectFactory () {}
23  virtual AbstractClass * CreateObject() const =0;
24 };
25 
26 //! \class DefaultObjectFactory
27 //! \brief Object factory for registering objects
28 //! \tparam AbstractClass Base class interface of the object
29 //! \tparam ConcreteClass Class object
30 template <class AbstractClass, class ConcreteClass>
31 class DefaultObjectFactory : public ObjectFactory<AbstractClass>
32 {
33 public:
34  AbstractClass * CreateObject() const
35  {
36  return new ConcreteClass;
37  }
38 };
39 
40 //! \class ObjectFactoryRegistry
41 //! \brief Object factory registry
42 //! \tparam AbstractClass Base class interface of the object
43 //! \tparam instance unique identifier
44 template <class AbstractClass, int instance=0>
46 {
47 public:
48  class FactoryNotFound : public Exception
49  {
50  public:
51  FactoryNotFound(const char *name) : Exception(OTHER_ERROR, std::string("ObjectFactoryRegistry: could not find factory for algorithm ") + name) {}
52  };
53 
55  {
56  for (typename Map::iterator i = m_map.begin(); i != m_map.end(); ++i)
57  {
58  delete (ObjectFactory<AbstractClass> *)i->second;
59  i->second = NULLPTR;
60  }
61  }
62 
63  void RegisterFactory(const std::string &name, ObjectFactory<AbstractClass> *factory)
64  {
65  m_map[name] = factory;
66  }
67 
68  const ObjectFactory<AbstractClass> * GetFactory(const char *name) const
69  {
70  typename Map::const_iterator i = m_map.find(name);
71  return i == m_map.end() ? NULLPTR : (ObjectFactory<AbstractClass> *)i->second;
72  }
73 
74  AbstractClass *CreateObject(const char *name) const
75  {
76  const ObjectFactory<AbstractClass> *factory = GetFactory(name);
77  if (!factory)
78  throw FactoryNotFound(name);
79  return factory->CreateObject();
80  }
81 
82  // Return a vector containing the factory names. This is easier than returning an iterator.
83  // from Andrew Pitonyak
84  std::vector<std::string> GetFactoryNames() const
85  {
86  std::vector<std::string> names;
87  typename Map::const_iterator iter;
88  for (iter = m_map.begin(); iter != m_map.end(); ++iter)
89  names.push_back(iter->first);
90  return names;
91  }
92 
93  CRYPTOPP_NOINLINE static ObjectFactoryRegistry<AbstractClass, instance> & Registry(CRYPTOPP_NOINLINE_DOTDOTDOT);
94 
95 private:
96  // use void * instead of ObjectFactory<AbstractClass> * to save code size
97  typedef std::map<std::string, void *> Map;
98  Map m_map;
99 };
100 
101 template <class AbstractClass, int instance>
103 {
105  return s_registry;
106 }
107 
108 //! \class RegisterDefaultFactoryFor
109 //! \brief Object factory registry helper
110 //! \tparam AbstractClass Base class interface of the object
111 //! \tparam ConcreteClass Class object
112 //! \tparam instance unique identifier
113 template <class AbstractClass, class ConcreteClass, int instance = 0>
115 {
116  RegisterDefaultFactoryFor(const char *name=NULLPTR)
117  {
118  // BCB2006 workaround
119  std::string n = name ? std::string(name) : std::string(ConcreteClass::StaticAlgorithmName());
122  }
123 };
124 
125 //! \fn RegisterAsymmetricCipherDefaultFactories
126 //! \brief Register asymmetric ciphers
127 //! \tparam SchemeClass interface of the object under a scheme
128 //! \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
129 //! signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
130 //! symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
131 //! authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
132 template <class SchemeClass>
133 void RegisterAsymmetricCipherDefaultFactories(const char *name=NULLPTR)
134 {
137 }
138 
139 //! \fn RegisterSignatureSchemeDefaultFactories
140 //! \brief Register signature schemes
141 //! \tparam SchemeClass interface of the object under a scheme
142 //! \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
143 //! signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
144 //! symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
145 //! authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
146 template <class SchemeClass>
147 void RegisterSignatureSchemeDefaultFactories(const char *name=NULLPTR)
148 {
151 }
152 
153 //! \fn RegisterSymmetricCipherDefaultFactories
154 //! \brief Register symmetric ciphers
155 //! \tparam SchemeClass interface of the object under a scheme
156 //! \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
157 //! signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
158 //! symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
159 //! authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
160 template <class SchemeClass>
161 void RegisterSymmetricCipherDefaultFactories(const char *name=NULLPTR)
162 {
165 }
166 
167 //! \fn RegisterAuthenticatedSymmetricCipherDefaultFactories
168 //! \brief Register authenticated symmetric ciphers
169 //! \tparam SchemeClass interface of the object under a scheme
170 //! \details Schemes include asymmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
171 //! signature schemes (registers <tt>SchemeClass::Signer</tt> and <tt>SchemeClass::Verifier</tt>),
172 //! symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>),
173 //! authenticated symmetric ciphers (registers <tt>SchemeClass::Encryptor</tt> and <tt>SchemeClass::Decryptor</tt>), etc.
174 template <class SchemeClass>
175 void RegisterAuthenticatedSymmetricCipherDefaultFactories(const char *name=NULLPTR)
176 {
179 }
180 
181 NAMESPACE_END
182 
183 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:150
Utility functions for the Crypto++ library.
Abstract base classes that provide a uniform interface to this library.
Object factory for registering objects.
Definition: factory.h:31
Object factory registry helper.
Definition: factory.h:114
Object factory interface for registering objects.
Definition: factory.h:19
Crypto++ library namespace.
Object factory registry.
Definition: factory.h:45