Back to index

lightning-sunbird  0.9+nobinonly
nsIGenericFactory.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Benjamin Smedberg <benjamin@smedbergs.us>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef nsIGenericFactory_h___
00040 #define nsIGenericFactory_h___
00041 
00042 #include "nsIFactory.h"
00043 #include "nsIModule.h"
00044 #include "nsIClassInfo.h"
00045 
00046 class nsIFile;
00047 class nsIComponentManager;
00048 
00049 // {3bc97f01-ccdf-11d2-bab8-b548654461fc}
00050 #define NS_GENERICFACTORY_CID                                                 \
00051   { 0x3bc97f01, 0xccdf, 0x11d2,                                               \
00052     { 0xba, 0xb8, 0xb5, 0x48, 0x65, 0x44, 0x61, 0xfc } }
00053 
00054 // {3bc97f00-ccdf-11d2-bab8-b548654461fc}
00055 #define NS_IGENERICFACTORY_IID                                                \
00056   { 0x3bc97f00, 0xccdf, 0x11d2,                                               \
00057     { 0xba, 0xb8, 0xb5, 0x48, 0x65, 0x44, 0x61, 0xfc } }
00058 
00059 #define NS_GENERICFACTORY_CONTRACTID "@mozilla.org/generic-factory;1"
00060 #define NS_GENERICFACTORY_CLASSNAME "Generic Factory"
00061 
00062 struct nsModuleComponentInfo; // forward declaration
00063 
00068 class nsIGenericFactory : public nsIFactory {
00069 public:
00070     NS_DEFINE_STATIC_IID_ACCESSOR(NS_IGENERICFACTORY_IID)
00071     
00072     NS_IMETHOD SetComponentInfo(const nsModuleComponentInfo *info) = 0;
00073     NS_IMETHOD GetComponentInfo(const nsModuleComponentInfo **infop) = 0;
00074 };
00075 
00076 NS_COM_GLUE nsresult
00077 NS_NewGenericFactory(nsIGenericFactory **result,
00078                      const nsModuleComponentInfo *info);
00079 
00080 
00103 typedef NS_CALLBACK(NSConstructorProcPtr)(nsISupports *aOuter, 
00104                                           REFNSIID aIID,
00105                                           void **aResult);
00106 
00131 typedef NS_CALLBACK(NSRegisterSelfProcPtr)(nsIComponentManager *aCompMgr,
00132                                            nsIFile *aPath,
00133                                            const char *aLoaderStr,
00134                                            const char *aType,
00135                                            const nsModuleComponentInfo *aInfo);
00136 
00158 typedef NS_CALLBACK(NSUnregisterSelfProcPtr)(nsIComponentManager *aCompMgr,
00159                                              nsIFile *aPath,
00160                                              const char *aLoaderStr,
00161                                              const nsModuleComponentInfo *aInfo);
00162 
00169 typedef NS_CALLBACK(NSFactoryDestructorProcPtr)(void);
00170 
00171 
00186 typedef NS_CALLBACK(NSGetInterfacesProcPtr)(PRUint32 *countp,
00187                                             nsIID* **array);
00188 
00205 typedef NS_CALLBACK(NSGetLanguageHelperProcPtr)(PRUint32 language,
00206                                                 nsISupports **helper);
00207 
00231 struct nsModuleComponentInfo {
00232     const char*                                 mDescription;
00233     nsCID                                       mCID;
00234     const char*                                 mContractID;
00235     NSConstructorProcPtr                        mConstructor;
00236     NSRegisterSelfProcPtr                       mRegisterSelfProc;
00237     NSUnregisterSelfProcPtr                     mUnregisterSelfProc;
00238     NSFactoryDestructorProcPtr                  mFactoryDestructor;
00239     NSGetInterfacesProcPtr                      mGetInterfacesProc;
00240     NSGetLanguageHelperProcPtr                  mGetLanguageHelperProc;
00241     nsIClassInfo **                             mClassInfoGlobal;
00242     PRUint32                                    mFlags;
00243 };
00244 
00245 
00259 typedef nsresult (PR_CALLBACK *nsModuleConstructorProc) (nsIModule *self);
00260 
00261 
00269 typedef void (PR_CALLBACK *nsModuleDestructorProc) (nsIModule *self);
00270 
00287 struct nsModuleInfo {
00288     PRUint32                mVersion;
00289     const char*             mModuleName;
00290     const nsModuleComponentInfo *mComponents;
00291     PRUint32                mCount;
00292     nsModuleConstructorProc mCtor;
00293     nsModuleDestructorProc  mDtor;
00294 };
00295 
00301 #define NS_MODULEINFO_VERSION 0x00015000UL // 1.5
00302 
00307 NS_COM_GLUE nsresult
00308 NS_NewGenericModule2(nsModuleInfo const *info, nsIModule* *result);
00309 
00313 NS_COM_GLUE nsresult
00314 NS_NewGenericModule(const char* moduleName,
00315                     PRUint32 componentCount,
00316                     nsModuleComponentInfo* components,
00317                     nsModuleDestructorProc dtor,
00318                     nsIModule* *result);
00319 
00320 #if defined(XPCOM_TRANSLATE_NSGM_ENTRY_POINT)
00321 #  define NSGETMODULE_ENTRY_POINT(_name)  NS_VISIBILITY_HIDDEN nsresult _name##_NSGetModule
00322 #else
00323 #  define NSGETMODULE_ENTRY_POINT(_name)  extern "C" NS_EXPORT nsresult NSGetModule
00324 #endif
00325 
00332 #define NS_IMPL_NSGETMODULE(_name, _components)                               \
00333     NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, nsnull, nsnull)
00334 
00335 #define NS_IMPL_NSGETMODULE_WITH_CTOR(_name, _components, _ctor)              \
00336     NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, _ctor, nsnull)
00337 
00338 #define NS_IMPL_NSGETMODULE_WITH_DTOR(_name, _components, _dtor)              \
00339     NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, nsnull, _dtor)
00340 
00341 #define NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, _ctor, _dtor)  \
00342 static nsModuleInfo const kModuleInfo = {                                     \
00343     NS_MODULEINFO_VERSION,                                                    \
00344     (#_name),                                                                 \
00345     (_components),                                                            \
00346     (sizeof(_components) / sizeof(_components[0])),                           \
00347     (_ctor),                                                                  \
00348     (_dtor)                                                                   \
00349 };                                                                            \
00350 NSGETMODULE_ENTRY_POINT(_name)                                                \
00351 (nsIComponentManager *servMgr,                                                \
00352             nsIFile* location,                                                \
00353             nsIModule** result)                                               \
00354 {                                                                             \
00355     return NS_NewGenericModule2(&kModuleInfo, result);                        \
00356 }
00357 
00359 
00360 #define NS_GENERIC_FACTORY_CONSTRUCTOR(_InstanceClass)                        \
00361 static NS_IMETHODIMP                                                          \
00362 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID,               \
00363                             void **aResult)                                   \
00364 {                                                                             \
00365     nsresult rv;                                                              \
00366                                                                               \
00367     _InstanceClass * inst;                                                    \
00368                                                                               \
00369     *aResult = NULL;                                                          \
00370     if (NULL != aOuter) {                                                     \
00371         rv = NS_ERROR_NO_AGGREGATION;                                         \
00372         return rv;                                                            \
00373     }                                                                         \
00374                                                                               \
00375     NS_NEWXPCOM(inst, _InstanceClass);                                        \
00376     if (NULL == inst) {                                                       \
00377         rv = NS_ERROR_OUT_OF_MEMORY;                                          \
00378         return rv;                                                            \
00379     }                                                                         \
00380     NS_ADDREF(inst);                                                          \
00381     rv = inst->QueryInterface(aIID, aResult);                                 \
00382     NS_RELEASE(inst);                                                         \
00383                                                                               \
00384     return rv;                                                                \
00385 }                                                                             \
00386 
00387 
00388 #define NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod)      \
00389 static NS_IMETHODIMP                                                          \
00390 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID,               \
00391                             void **aResult)                                   \
00392 {                                                                             \
00393     nsresult rv;                                                              \
00394                                                                               \
00395     _InstanceClass * inst;                                                    \
00396                                                                               \
00397     *aResult = NULL;                                                          \
00398     if (NULL != aOuter) {                                                     \
00399         rv = NS_ERROR_NO_AGGREGATION;                                         \
00400         return rv;                                                            \
00401     }                                                                         \
00402                                                                               \
00403     NS_NEWXPCOM(inst, _InstanceClass);                                        \
00404     if (NULL == inst) {                                                       \
00405         rv = NS_ERROR_OUT_OF_MEMORY;                                          \
00406         return rv;                                                            \
00407     }                                                                         \
00408     NS_ADDREF(inst);                                                          \
00409     rv = inst->_InitMethod();                                                 \
00410     if(NS_SUCCEEDED(rv)) {                                                    \
00411         rv = inst->QueryInterface(aIID, aResult);                             \
00412     }                                                                         \
00413     NS_RELEASE(inst);                                                         \
00414                                                                               \
00415     return rv;                                                                \
00416 }                                                                             \
00417 
00418 // 'Constructor' that uses an existing getter function that gets a singleton.
00419 // NOTE: assumes that getter does an AddRef - so additional AddRef is not done.
00420 #define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(_InstanceClass, _GetterProc) \
00421 static NS_IMETHODIMP                                                          \
00422 _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID,               \
00423                             void **aResult)                                   \
00424 {                                                                             \
00425     nsresult rv;                                                              \
00426                                                                               \
00427     _InstanceClass * inst;                                                    \
00428                                                                               \
00429     *aResult = NULL;                                                          \
00430     if (NULL != aOuter) {                                                     \
00431         rv = NS_ERROR_NO_AGGREGATION;                                         \
00432         return rv;                                                            \
00433     }                                                                         \
00434                                                                               \
00435     inst = _GetterProc();                                                     \
00436     if (NULL == inst) {                                                       \
00437         rv = NS_ERROR_OUT_OF_MEMORY;                                          \
00438         return rv;                                                            \
00439     }                                                                         \
00440     /* NS_ADDREF(inst); */                                                    \
00441     rv = inst->QueryInterface(aIID, aResult);                                 \
00442     NS_RELEASE(inst);                                                         \
00443                                                                               \
00444     return rv;                                                                \
00445 }                                                                             \
00446 
00447 #endif /* nsIGenericFactory_h___ */