Crypto++  5.6.4
Free C++ class library of cryptographic schemes
factory.h
1 #ifndef CRYPTOPP_OBJFACT_H
2 #define CRYPTOPP_OBJFACT_H
3 
4 #include "cryptlib.h"
5 #include "misc.h"
6 #include "stdcpp.h"
7 
8 NAMESPACE_BEGIN(CryptoPP)
9 
10 //! _
11 template <class AbstractClass>
13 {
14 public:
15  virtual ~ObjectFactory () {}
16  virtual AbstractClass * CreateObject() const =0;
17 };
18 
19 //! _
20 template <class AbstractClass, class ConcreteClass>
21 class DefaultObjectFactory : public ObjectFactory<AbstractClass>
22 {
23 public:
24  AbstractClass * CreateObject() const
25  {
26  return new ConcreteClass;
27  }
28 };
29 
30 //! _
31 template <class AbstractClass, int instance=0>
33 {
34 public:
35  class FactoryNotFound : public Exception
36  {
37  public:
38  FactoryNotFound(const char *name) : Exception(OTHER_ERROR, std::string("ObjectFactoryRegistry: could not find factory for algorithm ") + name) {}
39  };
40 
42  {
43  for (CPP_TYPENAME Map::iterator i = m_map.begin(); i != m_map.end(); ++i)
44  {
45  delete (ObjectFactory<AbstractClass> *)i->second;
46  i->second = NULL;
47  }
48  }
49 
50  void RegisterFactory(const std::string &name, ObjectFactory<AbstractClass> *factory)
51  {
52  m_map[name] = factory;
53  }
54 
55  const ObjectFactory<AbstractClass> * GetFactory(const char *name) const
56  {
57  CPP_TYPENAME Map::const_iterator i = m_map.find(name);
58  return i == m_map.end() ? NULL : (ObjectFactory<AbstractClass> *)i->second;
59  }
60 
61  AbstractClass *CreateObject(const char *name) const
62  {
63  const ObjectFactory<AbstractClass> *factory = GetFactory(name);
64  if (!factory)
65  throw FactoryNotFound(name);
66  return factory->CreateObject();
67  }
68 
69  // Return a vector containing the factory names. This is easier than returning an iterator.
70  // from Andrew Pitonyak
71  std::vector<std::string> GetFactoryNames() const
72  {
73  std::vector<std::string> names;
74  CPP_TYPENAME Map::const_iterator iter;
75  for (iter = m_map.begin(); iter != m_map.end(); ++iter)
76  names.push_back(iter->first);
77  return names;
78  }
79 
80  CRYPTOPP_NOINLINE static ObjectFactoryRegistry<AbstractClass, instance> & Registry(CRYPTOPP_NOINLINE_DOTDOTDOT);
81 
82 private:
83  // use void * instead of ObjectFactory<AbstractClass> * to save code size
84  typedef std::map<std::string, void *> Map;
85  Map m_map;
86 };
87 
88 template <class AbstractClass, int instance>
90 {
92  return s_registry;
93 }
94 
95 template <class AbstractClass, class ConcreteClass, int instance = 0>
97 RegisterDefaultFactoryFor(const char *name=NULL)
98 {
99  // BCB2006 workaround
100  std::string n = name ? std::string(name) : std::string(ConcreteClass::StaticAlgorithmName());
103 }};
104 
105 template <class SchemeClass>
106 void RegisterAsymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
107 {
108  CRYPTOPP_UNUSED(dummy);
111 }
112 
113 template <class SchemeClass>
114 void RegisterSignatureSchemeDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
115 {
116  CRYPTOPP_UNUSED(dummy);
119 }
120 
121 template <class SchemeClass>
122 void RegisterSymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
123 {
124  CRYPTOPP_UNUSED(dummy);
127 }
128 
129 template <class SchemeClass>
130 void RegisterAuthenticatedSymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
131 {
132  CRYPTOPP_UNUSED(dummy);
135 }
136 
137 NAMESPACE_END
138 
139 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:144
Utility functions for the Crypto++ library.
Exception(ErrorType errorType, const std::string &s)
Construct a new Exception.
Definition: cryptlib.h:167
Abstract base classes that provide a uniform interface to this library.
Some other error occurred not belonging to other categories.
Definition: cryptlib.h:163
Crypto++ library namespace.