xpcom/components/ModuleUtils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/components/ModuleUtils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_GenericModule_h
    1.10 +#define mozilla_GenericModule_h
    1.11 +
    1.12 +#include "mozilla/Attributes.h"
    1.13 +#include "mozilla/Module.h"
    1.14 +
    1.15 +#define NS_GENERIC_FACTORY_CONSTRUCTOR(_InstanceClass)                        \
    1.16 +static nsresult                                                               \
    1.17 +_InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID,               \
    1.18 +                            void **aResult)                                   \
    1.19 +{                                                                             \
    1.20 +    nsresult rv;                                                              \
    1.21 +                                                                              \
    1.22 +    _InstanceClass * inst;                                                    \
    1.23 +                                                                              \
    1.24 +    *aResult = nullptr;                                                       \
    1.25 +    if (nullptr != aOuter) {                                                  \
    1.26 +        rv = NS_ERROR_NO_AGGREGATION;                                         \
    1.27 +        return rv;                                                            \
    1.28 +    }                                                                         \
    1.29 +                                                                              \
    1.30 +    inst = new _InstanceClass();                                              \
    1.31 +    if (nullptr == inst) {                                                    \
    1.32 +        rv = NS_ERROR_OUT_OF_MEMORY;                                          \
    1.33 +        return rv;                                                            \
    1.34 +    }                                                                         \
    1.35 +    NS_ADDREF(inst);                                                          \
    1.36 +    rv = inst->QueryInterface(aIID, aResult);                                 \
    1.37 +    NS_RELEASE(inst);                                                         \
    1.38 +                                                                              \
    1.39 +    return rv;                                                                \
    1.40 +}
    1.41 +
    1.42 +#define NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod)      \
    1.43 +static nsresult                                                               \
    1.44 +_InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID,               \
    1.45 +                            void **aResult)                                   \
    1.46 +{                                                                             \
    1.47 +    nsresult rv;                                                              \
    1.48 +                                                                              \
    1.49 +    _InstanceClass * inst;                                                    \
    1.50 +                                                                              \
    1.51 +    *aResult = nullptr;                                                       \
    1.52 +    if (nullptr != aOuter) {                                                  \
    1.53 +        rv = NS_ERROR_NO_AGGREGATION;                                         \
    1.54 +        return rv;                                                            \
    1.55 +    }                                                                         \
    1.56 +                                                                              \
    1.57 +    inst = new _InstanceClass();                                              \
    1.58 +    if (nullptr == inst) {                                                    \
    1.59 +        rv = NS_ERROR_OUT_OF_MEMORY;                                          \
    1.60 +        return rv;                                                            \
    1.61 +    }                                                                         \
    1.62 +    NS_ADDREF(inst);                                                          \
    1.63 +    rv = inst->_InitMethod();                                                 \
    1.64 +    if(NS_SUCCEEDED(rv)) {                                                    \
    1.65 +        rv = inst->QueryInterface(aIID, aResult);                             \
    1.66 +    }                                                                         \
    1.67 +    NS_RELEASE(inst);                                                         \
    1.68 +                                                                              \
    1.69 +    return rv;                                                                \
    1.70 +}
    1.71 +
    1.72 +// 'Constructor' that uses an existing getter function that gets a singleton.
    1.73 +// NOTE: assumes that getter does an AddRef - so additional AddRef is not done.
    1.74 +#define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(_InstanceClass, _GetterProc) \
    1.75 +static nsresult                                                               \
    1.76 +_InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID,               \
    1.77 +                            void **aResult)                                   \
    1.78 +{                                                                             \
    1.79 +    nsresult rv;                                                              \
    1.80 +                                                                              \
    1.81 +    _InstanceClass * inst;                                                    \
    1.82 +                                                                              \
    1.83 +    *aResult = nullptr;                                                       \
    1.84 +    if (nullptr != aOuter) {                                                  \
    1.85 +        rv = NS_ERROR_NO_AGGREGATION;                                         \
    1.86 +        return rv;                                                            \
    1.87 +    }                                                                         \
    1.88 +                                                                              \
    1.89 +    inst = already_AddRefed<_InstanceClass>(_GetterProc()).take();   \
    1.90 +    if (nullptr == inst) {                                                    \
    1.91 +        rv = NS_ERROR_OUT_OF_MEMORY;                                          \
    1.92 +        return rv;                                                            \
    1.93 +    }                                                                         \
    1.94 +    /* NS_ADDREF(inst); */                                                    \
    1.95 +    rv = inst->QueryInterface(aIID, aResult);                                 \
    1.96 +    NS_RELEASE(inst);                                                         \
    1.97 +                                                                              \
    1.98 +    return rv;                                                                \
    1.99 +}
   1.100 +
   1.101 +#ifndef MOZILLA_INTERNAL_API
   1.102 +
   1.103 +#include "nsIModule.h"
   1.104 +#include "nsISupportsUtils.h"
   1.105 +
   1.106 +namespace mozilla {
   1.107 +
   1.108 +class GenericModule MOZ_FINAL : public nsIModule
   1.109 +{
   1.110 +public:
   1.111 +    GenericModule(const mozilla::Module* aData)
   1.112 +        : mData(aData)
   1.113 +    {
   1.114 +    }
   1.115 +
   1.116 +    NS_DECL_THREADSAFE_ISUPPORTS
   1.117 +    NS_DECL_NSIMODULE
   1.118 +
   1.119 +private:
   1.120 +    const mozilla::Module* mData;
   1.121 +};
   1.122 +
   1.123 +} // namespace mozilla
   1.124 +
   1.125 +#define NS_IMPL_MOZILLA192_NSGETMODULE(module)     \
   1.126 +extern "C" NS_EXPORT nsresult                      \
   1.127 +NSGetModule(nsIComponentManager* aCompMgr,         \
   1.128 +            nsIFile* aLocation,                    \
   1.129 +            nsIModule** aResult)                   \
   1.130 +{                                                  \
   1.131 +    *aResult = new mozilla::GenericModule(module); \
   1.132 +    NS_ADDREF(*aResult);                           \
   1.133 +    return NS_OK;                                  \
   1.134 +}
   1.135 +
   1.136 +#endif
   1.137 +
   1.138 +#endif // mozilla_GenericModule_h

mercurial