Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.1 KiB

  1. #ifndef CRYPTOPP_OBJFACT_H
  2. #define CRYPTOPP_OBJFACT_H
  3. #include "cryptlib.h"
  4. #include <map>
  5. #include <vector>
  6. NAMESPACE_BEGIN(CryptoPP)
  7. //! _
  8. template <class AbstractClass>
  9. class ObjectFactory
  10. {
  11. public:
  12. virtual AbstractClass * CreateObject() const =0;
  13. };
  14. //! _
  15. template <class AbstractClass, class ConcreteClass>
  16. class DefaultObjectFactory : public ObjectFactory<AbstractClass>
  17. {
  18. public:
  19. AbstractClass * CreateObject() const
  20. {
  21. return new ConcreteClass;
  22. }
  23. };
  24. //! _
  25. template <class AbstractClass, int instance=0>
  26. class ObjectFactoryRegistry
  27. {
  28. public:
  29. class FactoryNotFound : public Exception
  30. {
  31. public:
  32. FactoryNotFound(const char *name) : Exception(OTHER_ERROR, std::string("ObjectFactoryRegistry: could not find factory for algorithm ") + name) {}
  33. };
  34. ~ObjectFactoryRegistry()
  35. {
  36. for (CPP_TYPENAME Map::iterator i = m_map.begin(); i != m_map.end(); ++i)
  37. {
  38. delete (ObjectFactory<AbstractClass> *)i->second;
  39. i->second = NULL;
  40. }
  41. }
  42. void RegisterFactory(const std::string &name, ObjectFactory<AbstractClass> *factory)
  43. {
  44. m_map[name] = factory;
  45. }
  46. const ObjectFactory<AbstractClass> * GetFactory(const char *name) const
  47. {
  48. CPP_TYPENAME Map::const_iterator i = m_map.find(name);
  49. return i == m_map.end() ? NULL : (ObjectFactory<AbstractClass> *)i->second;
  50. }
  51. AbstractClass *CreateObject(const char *name) const
  52. {
  53. const ObjectFactory<AbstractClass> *factory = GetFactory(name);
  54. if (!factory)
  55. throw FactoryNotFound(name);
  56. return factory->CreateObject();
  57. }
  58. // Return a vector containing the factory names. This is easier than returning an iterator.
  59. // from Andrew Pitonyak
  60. std::vector<std::string> GetFactoryNames() const
  61. {
  62. std::vector<std::string> names;
  63. CPP_TYPENAME Map::const_iterator iter;
  64. for (iter = m_map.begin(); iter != m_map.end(); ++iter)
  65. names.push_back(iter->first);
  66. return names;
  67. }
  68. CRYPTOPP_NOINLINE static ObjectFactoryRegistry<AbstractClass, instance> & Registry(CRYPTOPP_NOINLINE_DOTDOTDOT);
  69. private:
  70. // use void * instead of ObjectFactory<AbstractClass> * to save code size
  71. typedef std::map<std::string, void *> Map;
  72. Map m_map;
  73. };
  74. template <class AbstractClass, int instance>
  75. ObjectFactoryRegistry<AbstractClass, instance> & ObjectFactoryRegistry<AbstractClass, instance>::Registry(CRYPTOPP_NOINLINE_DOTDOTDOT)
  76. {
  77. static ObjectFactoryRegistry<AbstractClass, instance> s_registry;
  78. return s_registry;
  79. }
  80. template <class AbstractClass, class ConcreteClass, int instance = 0>
  81. struct RegisterDefaultFactoryFor {
  82. RegisterDefaultFactoryFor(const char *name=NULL)
  83. {
  84. // BCB2006 workaround
  85. std::string n = name ? std::string(name) : std::string(ConcreteClass::StaticAlgorithmName());
  86. ObjectFactoryRegistry<AbstractClass, instance>::Registry().
  87. RegisterFactory(n, new DefaultObjectFactory<AbstractClass, ConcreteClass>);
  88. }};
  89. template <class SchemeClass>
  90. void RegisterAsymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
  91. {
  92. RegisterDefaultFactoryFor<PK_Encryptor, CPP_TYPENAME SchemeClass::Encryptor>((const char *)name);
  93. RegisterDefaultFactoryFor<PK_Decryptor, CPP_TYPENAME SchemeClass::Decryptor>((const char *)name);
  94. }
  95. template <class SchemeClass>
  96. void RegisterSignatureSchemeDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
  97. {
  98. RegisterDefaultFactoryFor<PK_Signer, CPP_TYPENAME SchemeClass::Signer>((const char *)name);
  99. RegisterDefaultFactoryFor<PK_Verifier, CPP_TYPENAME SchemeClass::Verifier>((const char *)name);
  100. }
  101. template <class SchemeClass>
  102. void RegisterSymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
  103. {
  104. RegisterDefaultFactoryFor<SymmetricCipher, CPP_TYPENAME SchemeClass::Encryption, ENCRYPTION>((const char *)name);
  105. RegisterDefaultFactoryFor<SymmetricCipher, CPP_TYPENAME SchemeClass::Decryption, DECRYPTION>((const char *)name);
  106. }
  107. template <class SchemeClass>
  108. void RegisterAuthenticatedSymmetricCipherDefaultFactories(const char *name=NULL, SchemeClass *dummy=NULL)
  109. {
  110. RegisterDefaultFactoryFor<AuthenticatedSymmetricCipher, CPP_TYPENAME SchemeClass::Encryption, ENCRYPTION>((const char *)name);
  111. RegisterDefaultFactoryFor<AuthenticatedSymmetricCipher, CPP_TYPENAME SchemeClass::Decryption, DECRYPTION>((const char *)name);
  112. }
  113. NAMESPACE_END
  114. #endif