Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
objectfactory.h
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef _OBJECTFACTORY_H
18 #define _OBJECTFACTORY_H
19 
20 
21 
22 
23 
24 template <typename T>
26 {
27 public:
28 
29  template <typename X>
30  static HRESULT CreateInstanceNoInit(X** ppInstance)
31  {
32  T* pT = NULL;
33 
34  if (ppInstance == NULL)
35  {
36  return E_INVALIDARG;
37  }
38 
39  pT = new T();
40  if (pT == NULL)
41  {
42  return E_OUTOFMEMORY;
43  }
44 
45  pT->AddRef();
46  *ppInstance = pT;
47  return S_OK;
48  }
49 
50 
51  template <typename I>
52  static HRESULT CreateInstance(I** ppI)
53  {
54  T* pInstance = NULL;
55  HRESULT hr = S_OK;
56 
57  ChkIf(ppI == NULL, E_NOINTERFACE);
58  Chk(CreateInstanceNoInit(&pInstance));
59  Chk(pInstance->Initialize());
60  *ppI = pInstance;
61  pInstance = NULL;
62  Cleanup:
63  // Cleanup
64  if (pInstance)
65  {
66  pInstance->Release();
67  pInstance = NULL;
68  }
69  return hr;
70  }
71 
72  template <typename A, typename I>
73  static HRESULT CreateInstance(A paramA, I** ppI)
74  {
75  T* pInstance = NULL;
76  HRESULT hr = S_OK;
77 
78  ChkIf(ppI == NULL, E_NOINTERFACE);
79  Chk(CreateInstanceNoInit(&pInstance));
80  Chk(pInstance->Initialize(paramA));
81  *ppI = pInstance;
82  pInstance = NULL;
83  Cleanup:
84  // Cleanup
85  if (pInstance)
86  {
87  pInstance->Release();
88  pInstance = NULL;
89  }
90  return hr;
91  }
92 
93  template <typename A, typename B, typename I>
94  static HRESULT CreateInstance(A paramA, B paramB, I** ppI)
95  {
96  T* pInstance = NULL;
97  HRESULT hr = S_OK;
98 
99  ChkIf(ppI == NULL, E_NOINTERFACE);
100  Chk(CreateInstanceNoInit(&pInstance));
101  Chk(pInstance->Initialize(paramA, paramB));
102  *ppI = pInstance;
103  pInstance = NULL;
104  Cleanup:
105  // Cleanup
106  if (pInstance)
107  {
108  pInstance->Release();
109  pInstance = NULL;
110  }
111  return hr;
112  }
113 
114 };
115 
116 
117 
118 #endif /* _OBJECTFACTORY_H */
119 
#define S_OK
Definition: hresult.h:46
static HRESULT CreateInstanceNoInit(X **ppInstance)
Definition: objectfactory.h:30
#define Chk(expr)
Definition: chkmacros.h:53
#define ChkIf(expr, hrerror)
Definition: chkmacros.h:63
#define E_NOINTERFACE
Definition: hresult.h:52
int32_t HRESULT
Definition: hresult.h:22
static HRESULT CreateInstance(A paramA, I **ppI)
Definition: objectfactory.h:73
#define E_INVALIDARG
Definition: hresult.h:51
#define E_OUTOFMEMORY
Definition: hresult.h:50
static HRESULT CreateInstance(A paramA, B paramB, I **ppI)
Definition: objectfactory.h:94
static HRESULT CreateInstance(I **ppI)
Definition: objectfactory.h:52