Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / xpcom / components / ModuleUtils.h
blobbf5fa2143072e8ad66dd402ba1795691bcb497df
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Benjamin Smedberg <benjamin@smedbergs.us>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef mozilla_GenericModule_h
40 #define mozilla_GenericModule_h
42 #include "mozilla/Module.h"
44 #define NS_GENERIC_FACTORY_CONSTRUCTOR(_InstanceClass) \
45 static nsresult \
46 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
47 void **aResult) \
48 { \
49 nsresult rv; \
51 _InstanceClass * inst; \
53 *aResult = NULL; \
54 if (NULL != aOuter) { \
55 rv = NS_ERROR_NO_AGGREGATION; \
56 return rv; \
57 } \
59 inst = new _InstanceClass(); \
60 if (NULL == inst) { \
61 rv = NS_ERROR_OUT_OF_MEMORY; \
62 return rv; \
63 } \
64 NS_ADDREF(inst); \
65 rv = inst->QueryInterface(aIID, aResult); \
66 NS_RELEASE(inst); \
68 return rv; \
71 #define NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod) \
72 static nsresult \
73 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
74 void **aResult) \
75 { \
76 nsresult rv; \
78 _InstanceClass * inst; \
80 *aResult = NULL; \
81 if (NULL != aOuter) { \
82 rv = NS_ERROR_NO_AGGREGATION; \
83 return rv; \
84 } \
86 inst = new _InstanceClass(); \
87 if (NULL == inst) { \
88 rv = NS_ERROR_OUT_OF_MEMORY; \
89 return rv; \
90 } \
91 NS_ADDREF(inst); \
92 rv = inst->_InitMethod(); \
93 if(NS_SUCCEEDED(rv)) { \
94 rv = inst->QueryInterface(aIID, aResult); \
95 } \
96 NS_RELEASE(inst); \
98 return rv; \
101 // 'Constructor' that uses an existing getter function that gets a singleton.
102 // NOTE: assumes that getter does an AddRef - so additional AddRef is not done.
103 #define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(_InstanceClass, _GetterProc) \
104 static nsresult \
105 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
106 void **aResult) \
108 nsresult rv; \
110 _InstanceClass * inst; \
112 *aResult = NULL; \
113 if (NULL != aOuter) { \
114 rv = NS_ERROR_NO_AGGREGATION; \
115 return rv; \
118 inst = already_AddRefed<_InstanceClass>(_GetterProc()).get(); \
119 if (NULL == inst) { \
120 rv = NS_ERROR_OUT_OF_MEMORY; \
121 return rv; \
123 /* NS_ADDREF(inst); */ \
124 rv = inst->QueryInterface(aIID, aResult); \
125 NS_RELEASE(inst); \
127 return rv; \
130 #ifndef MOZILLA_INTERNAL_API
132 #include "nsIModule.h"
133 #include "nsISupportsUtils.h"
135 namespace mozilla {
137 class GenericModule : public nsIModule
139 public:
140 GenericModule(const mozilla::Module* aData)
141 : mData(aData)
145 NS_DECL_ISUPPORTS
146 NS_DECL_NSIMODULE
148 private:
149 const mozilla::Module* mData;
152 } // namespace mozilla
154 #define NS_IMPL_MOZILLA192_NSGETMODULE(module) \
155 extern "C" NS_EXPORT nsresult \
156 NSGetModule(nsIComponentManager* aCompMgr, \
157 nsIFile* aLocation, \
158 nsIModule** aResult) \
160 *aResult = new mozilla::GenericModule(module); \
161 NS_ADDREF(*aResult); \
162 return NS_OK; \
165 #endif
167 #endif // mozilla_GenericModule_h