|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef mozilla_GenericModule_h |
|
7 #define mozilla_GenericModule_h |
|
8 |
|
9 #include "mozilla/Attributes.h" |
|
10 #include "mozilla/Module.h" |
|
11 |
|
12 #define NS_GENERIC_FACTORY_CONSTRUCTOR(_InstanceClass) \ |
|
13 static nsresult \ |
|
14 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \ |
|
15 void **aResult) \ |
|
16 { \ |
|
17 nsresult rv; \ |
|
18 \ |
|
19 _InstanceClass * inst; \ |
|
20 \ |
|
21 *aResult = nullptr; \ |
|
22 if (nullptr != aOuter) { \ |
|
23 rv = NS_ERROR_NO_AGGREGATION; \ |
|
24 return rv; \ |
|
25 } \ |
|
26 \ |
|
27 inst = new _InstanceClass(); \ |
|
28 if (nullptr == inst) { \ |
|
29 rv = NS_ERROR_OUT_OF_MEMORY; \ |
|
30 return rv; \ |
|
31 } \ |
|
32 NS_ADDREF(inst); \ |
|
33 rv = inst->QueryInterface(aIID, aResult); \ |
|
34 NS_RELEASE(inst); \ |
|
35 \ |
|
36 return rv; \ |
|
37 } |
|
38 |
|
39 #define NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod) \ |
|
40 static nsresult \ |
|
41 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \ |
|
42 void **aResult) \ |
|
43 { \ |
|
44 nsresult rv; \ |
|
45 \ |
|
46 _InstanceClass * inst; \ |
|
47 \ |
|
48 *aResult = nullptr; \ |
|
49 if (nullptr != aOuter) { \ |
|
50 rv = NS_ERROR_NO_AGGREGATION; \ |
|
51 return rv; \ |
|
52 } \ |
|
53 \ |
|
54 inst = new _InstanceClass(); \ |
|
55 if (nullptr == inst) { \ |
|
56 rv = NS_ERROR_OUT_OF_MEMORY; \ |
|
57 return rv; \ |
|
58 } \ |
|
59 NS_ADDREF(inst); \ |
|
60 rv = inst->_InitMethod(); \ |
|
61 if(NS_SUCCEEDED(rv)) { \ |
|
62 rv = inst->QueryInterface(aIID, aResult); \ |
|
63 } \ |
|
64 NS_RELEASE(inst); \ |
|
65 \ |
|
66 return rv; \ |
|
67 } |
|
68 |
|
69 // 'Constructor' that uses an existing getter function that gets a singleton. |
|
70 // NOTE: assumes that getter does an AddRef - so additional AddRef is not done. |
|
71 #define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(_InstanceClass, _GetterProc) \ |
|
72 static nsresult \ |
|
73 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \ |
|
74 void **aResult) \ |
|
75 { \ |
|
76 nsresult rv; \ |
|
77 \ |
|
78 _InstanceClass * inst; \ |
|
79 \ |
|
80 *aResult = nullptr; \ |
|
81 if (nullptr != aOuter) { \ |
|
82 rv = NS_ERROR_NO_AGGREGATION; \ |
|
83 return rv; \ |
|
84 } \ |
|
85 \ |
|
86 inst = already_AddRefed<_InstanceClass>(_GetterProc()).take(); \ |
|
87 if (nullptr == inst) { \ |
|
88 rv = NS_ERROR_OUT_OF_MEMORY; \ |
|
89 return rv; \ |
|
90 } \ |
|
91 /* NS_ADDREF(inst); */ \ |
|
92 rv = inst->QueryInterface(aIID, aResult); \ |
|
93 NS_RELEASE(inst); \ |
|
94 \ |
|
95 return rv; \ |
|
96 } |
|
97 |
|
98 #ifndef MOZILLA_INTERNAL_API |
|
99 |
|
100 #include "nsIModule.h" |
|
101 #include "nsISupportsUtils.h" |
|
102 |
|
103 namespace mozilla { |
|
104 |
|
105 class GenericModule MOZ_FINAL : public nsIModule |
|
106 { |
|
107 public: |
|
108 GenericModule(const mozilla::Module* aData) |
|
109 : mData(aData) |
|
110 { |
|
111 } |
|
112 |
|
113 NS_DECL_THREADSAFE_ISUPPORTS |
|
114 NS_DECL_NSIMODULE |
|
115 |
|
116 private: |
|
117 const mozilla::Module* mData; |
|
118 }; |
|
119 |
|
120 } // namespace mozilla |
|
121 |
|
122 #define NS_IMPL_MOZILLA192_NSGETMODULE(module) \ |
|
123 extern "C" NS_EXPORT nsresult \ |
|
124 NSGetModule(nsIComponentManager* aCompMgr, \ |
|
125 nsIFile* aLocation, \ |
|
126 nsIModule** aResult) \ |
|
127 { \ |
|
128 *aResult = new mozilla::GenericModule(module); \ |
|
129 NS_ADDREF(*aResult); \ |
|
130 return NS_OK; \ |
|
131 } |
|
132 |
|
133 #endif |
|
134 |
|
135 #endif // mozilla_GenericModule_h |