| 1 | // Licensed to the .NET Foundation under one or more agreements. |
|---|---|
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | |
| 6 | #ifndef _FACTORY_H_ |
| 7 | #define _FACTORY_H_ |
| 8 | |
| 9 | template<typename PRODUCT> |
| 10 | class Factory |
| 11 | { |
| 12 | public: |
| 13 | virtual PRODUCT* Create() = 0; |
| 14 | virtual ~Factory() {} |
| 15 | }; |
| 16 | |
| 17 | template<typename PRODUCT, DWORD MAX_FACTORY_PRODUCT = 64> |
| 18 | class InlineFactory : public Factory<PRODUCT> |
| 19 | { |
| 20 | public: |
| 21 | InlineFactory() : m_next(NULL), m_cProduct(0) { WRAPPER_NO_CONTRACT; } |
| 22 | ~InlineFactory() { WRAPPER_NO_CONTRACT; if (m_next) delete m_next; } |
| 23 | PRODUCT* Create(); |
| 24 | |
| 25 | private: |
| 26 | InlineFactory* GetNext() |
| 27 | { |
| 28 | CONTRACTL { |
| 29 | NOTHROW; |
| 30 | GC_NOTRIGGER; |
| 31 | } CONTRACTL_END; |
| 32 | |
| 33 | if (m_next == NULL) |
| 34 | { |
| 35 | m_next = new (nothrow) InlineFactory<PRODUCT, MAX_FACTORY_PRODUCT>(); |
| 36 | } |
| 37 | |
| 38 | return m_next; |
| 39 | } |
| 40 | |
| 41 | InlineFactory* m_next; |
| 42 | PRODUCT m_product[MAX_FACTORY_PRODUCT]; |
| 43 | INT32 m_cProduct; |
| 44 | }; |
| 45 | |
| 46 | #include "factory.inl" |
| 47 | |
| 48 | #endif |
| 49 | |
| 50 |