Leaked source code of windows server 2003
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.

137 lines
3.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // Factory.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file declares the classes Factory and FactoryCache.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/05/1998 Original version.
  16. // 04/16/1998 Removed FactoryCache::theCache.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #ifndef _FACTORY_H_
  20. #define _FACTORY_H_
  21. #include <guard.h>
  22. #include <nocopy.h>
  23. #include <set>
  24. ///////////////////////////////////////////////////////////////////////////////
  25. //
  26. // CLASS
  27. //
  28. // Factory
  29. //
  30. // DESCRIPTION
  31. //
  32. // This class facilitates storing a (ProgID, IClassFactory) tuple in a
  33. // collection. It is primarily for use by the FactoryCache, but it is
  34. // suitable for stand-alone use.
  35. //
  36. ///////////////////////////////////////////////////////////////////////////////
  37. class Factory
  38. {
  39. public:
  40. // Constructors.
  41. Factory(PCWSTR progID, IClassFactory* classFactory);
  42. Factory(const Factory& f);
  43. // Assignment operator.
  44. Factory& operator=(const Factory& f);
  45. // Destructor.
  46. ~Factory() throw ();
  47. // Create an object with the factory.
  48. void createInstance(IUnknown* pUnkOuter,
  49. REFIID riid,
  50. void** ppvObject) const
  51. {
  52. using _com_util::CheckError;
  53. CheckError(factory->CreateInstance(pUnkOuter, riid, ppvObject));
  54. }
  55. // Returns the factory for the class. Caller is responsible for releasing.
  56. IClassFactory* getFactory(IClassFactory** f) const throw ()
  57. {
  58. factory->AddRef();
  59. return factory;
  60. }
  61. // Returns the prog ID for the class.
  62. PCWSTR getProgID() const throw ()
  63. { return name; }
  64. /////////
  65. // Comparison operators to allow factories to be stored in collections.
  66. /////////
  67. bool operator<(const Factory& f) const throw ()
  68. { return wcscmp(name, f.name) < 0; }
  69. bool operator==(const Factory& f) const throw ()
  70. { return wcscmp(name, f.name) == 0; }
  71. protected:
  72. PWSTR name; // The ProgID of the factory.
  73. mutable IClassFactory* factory; // The class factory.
  74. };
  75. ///////////////////////////////////////////////////////////////////////////////
  76. //
  77. // CLASS
  78. //
  79. // FactoryCache
  80. //
  81. // DESCRIPTION
  82. //
  83. // This class maintains a cache of Class Factory objects. It is intended
  84. // for situations where large numbers of various object must be created
  85. // dynamically. Generally, there should be only one cache per process.
  86. //
  87. // The class also has the concept of a default ProgID prefix. When an
  88. // object is created. The cache first checks for "defaultPrefix.ProgID".
  89. // If this fails, it then tries "ProgID".
  90. //
  91. ///////////////////////////////////////////////////////////////////////////////
  92. class FactoryCache
  93. : NonCopyable, Guardable
  94. {
  95. public:
  96. FactoryCache(PCWSTR defaultPrefix = NULL);
  97. ~FactoryCache() throw ();
  98. void clear() throw ();
  99. void createInstance(PCWSTR progID,
  100. IUnknown* pUnkOuter,
  101. REFIID riid,
  102. void** ppvObject);
  103. // Returns the prefix for the cache. May be null.
  104. PCWSTR getPrefix() const throw ()
  105. { return prefix; }
  106. protected:
  107. // Converts a progID to a class ID using the algorithm described above.
  108. void CLSIDFromProgID(PCWSTR progID, LPCLSID pclsid) const;
  109. std::set<Factory> factories; // The cache of class factories.
  110. DWORD prefixLen; // The length of the prefix in characters.
  111. PWSTR prefix; // The default prefix (may be NULL).
  112. };
  113. //////////
  114. // The global factory cache.
  115. //////////
  116. extern FactoryCache theFactoryCache;
  117. #endif // _FACTORY_H_