Fabcoin Core  0.16.2
P2P Digital Currency
factory.h
Go to the documentation of this file.
1 // factory.h - written and placed in the public domain by Wei Dai
2 
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 
14 
15 template <class AbstractClass>
20 {
21 public:
22  virtual ~ObjectFactory () {}
23  virtual AbstractClass * CreateObject() const =0;
24 };
25 
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 
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 = NULL;
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() ? NULL : (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 
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 
113 template <class AbstractClass, class ConcreteClass, int instance = 0>
115 {
116  RegisterDefaultFactoryFor(const char *name=NULL)
117  {
118  // BCB2006 workaround
119  std::string n = name ? std::string(name) : std::string(ConcreteClass::StaticAlgorithmName());
122  }
123 };
124 
132 template <class SchemeClass>
134 {
137 }
138 
146 template <class SchemeClass>
148 {
151 }
152 
160 template <class SchemeClass>
162 {
165 }
166 
174 template <class SchemeClass>
176 {
179 }
180 
182 
183 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
Utility functions for the Crypto++ library.
FactoryNotFound(const char *name)
Definition: factory.h:51
void RegisterAuthenticatedSymmetricCipherDefaultFactories(const char *name=NULL)
Definition: factory.h:175
std::vector< std::string > GetFactoryNames() const
Definition: factory.h:84
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
static CRYPTOPP_NOINLINE ObjectFactoryRegistry< AbstractClass, instance > & Registry(CRYPTOPP_NOINLINE_DOTDOTDOT)
Definition: factory.h:102
Abstract base classes that provide a uniform interface to this library.
void RegisterAsymmetricCipherDefaultFactories(const char *name=NULL)
Definition: factory.h:133
std::hash for asio::adress
Definition: Common.h:323
Object factory for registering objects.
Definition: factory.h:31
virtual AbstractClass * CreateObject() const =0
const ObjectFactory< AbstractClass > * GetFactory(const char *name) const
Definition: factory.h:68
void RegisterSignatureSchemeDefaultFactories(const char *name=NULL)
Definition: factory.h:147
void RegisterSymmetricCipherDefaultFactories(const char *name=NULL)
Definition: factory.h:161
const char * name
Definition: rest.cpp:36
void RegisterFactory(const std::string &name, ObjectFactory< AbstractClass > *factory)
Definition: factory.h:63
#define CRYPTOPP_NOINLINE_DOTDOTDOT
Definition: config.h:532
Object factory registry helper.
Definition: factory.h:114
Object factory interface for registering objects.
Definition: factory.h:19
#define CRYPTOPP_NOINLINE
Definition: config.h:533
AbstractClass * CreateObject(const char *name) const
Definition: factory.h:74
virtual ~ObjectFactory()
Definition: factory.h:22
AbstractClass * CreateObject() const
Definition: factory.h:34
#define NAMESPACE_END
Definition: config.h:201
Object factory registry.
Definition: factory.h:45
RegisterDefaultFactoryFor(const char *name=NULL)
Definition: factory.h:116
std::map< std::string, void * > Map
Definition: factory.h:97