2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsIClassInfoImpl_h__
|
|
|
|
#define nsIClassInfoImpl_h__
|
|
|
|
|
2013-08-14 00:00:52 -07:00
|
|
|
#include "mozilla/Alignment.h"
|
2014-04-24 02:21:00 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/MacroArgs.h"
|
|
|
|
#include "mozilla/MacroForEach.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIClassInfo.h"
|
2010-06-10 11:11:11 -07:00
|
|
|
#include "nsISupportsImpl.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-11 17:16:41 -07:00
|
|
|
#include <new>
|
2010-07-01 09:11:42 -07:00
|
|
|
|
2011-05-20 09:37:32 -07:00
|
|
|
/**
|
|
|
|
* This header file provides macros which help you make your class implement
|
|
|
|
* nsIClassInfo. Implementing nsIClassInfo is particularly helpful if you have
|
|
|
|
* a C++ class which implements multiple interfaces and which you access from
|
|
|
|
* JavaScript. If that class implements nsIClassInfo, the JavaScript code
|
|
|
|
* won't have to call QueryInterface on instances of the class; all methods
|
|
|
|
* from all interfaces returned by GetInterfaces() will be available
|
|
|
|
* automagically.
|
|
|
|
*
|
|
|
|
* Here's all you need to do. Given a class
|
|
|
|
*
|
|
|
|
* class nsFooBar : public nsIFoo, public nsIBar { };
|
|
|
|
*
|
|
|
|
* you should already have the following nsISupports implementation in its cpp
|
|
|
|
* file:
|
|
|
|
*
|
2014-04-27 00:06:00 -07:00
|
|
|
* NS_IMPL_ISUPPORTS(nsFooBar, nsIFoo, nsIBar).
|
2011-05-20 09:37:32 -07:00
|
|
|
*
|
|
|
|
* Change this to
|
|
|
|
*
|
2013-10-10 13:41:39 -07:00
|
|
|
* NS_IMPL_CLASSINFO(nsFooBar, nullptr, 0, NS_FOOBAR_CID)
|
2014-04-27 00:06:00 -07:00
|
|
|
* NS_IMPL_ISUPPORTS_CI(nsFooBar, nsIFoo, nsIBar)
|
2011-05-20 09:37:32 -07:00
|
|
|
*
|
|
|
|
* If nsFooBar is threadsafe, change the 0 above to nsIClassInfo::THREADSAFE.
|
|
|
|
* If it's a singleton, use nsIClassInfo::SINGLETON. The full list of flags is
|
|
|
|
* in nsIClassInfo.idl.
|
|
|
|
*
|
2013-10-10 13:41:39 -07:00
|
|
|
* The nullptr parameter is there so you can pass a function for converting
|
|
|
|
* from an XPCOM object to a scriptable helper. Unless you're doing
|
|
|
|
* specialized JS work, you can probably leave this as nullptr.
|
2011-05-20 09:37:32 -07:00
|
|
|
*
|
2014-04-27 00:06:00 -07:00
|
|
|
* This file also defines the NS_IMPL_QUERY_INTERFACE_CI macro, which you can
|
|
|
|
* use to replace NS_IMPL_QUERY_INTERFACE, if you use that instead of
|
|
|
|
* NS_IMPL_ISUPPORTS.
|
2011-05-20 09:37:32 -07:00
|
|
|
*
|
|
|
|
* That's it! The rest is gory details.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Notice that nsFooBar didn't need to inherit from nsIClassInfo in order to
|
|
|
|
* "implement" it. However, after adding these macros to nsFooBar, you you can
|
|
|
|
* QueryInterface an instance of nsFooBar to nsIClassInfo. How can this be?
|
|
|
|
*
|
2014-04-27 00:06:00 -07:00
|
|
|
* The answer lies in the NS_IMPL_ISUPPORTS_CI macro. It modifies nsFooBar's
|
2011-05-20 09:37:32 -07:00
|
|
|
* QueryInterface implementation such that, if we ask to QI to nsIClassInfo, it
|
|
|
|
* returns a singleton object associated with the class. (That singleton is
|
|
|
|
* defined by NS_IMPL_CLASSINFO.) So all nsFooBar instances will return the
|
|
|
|
* same object when QI'ed to nsIClassInfo. (You can see this in
|
|
|
|
* NS_IMPL_QUERY_CLASSINFO below.)
|
|
|
|
*
|
|
|
|
* This hack breaks XPCOM's rules, since if you take an instance of nsFooBar,
|
|
|
|
* QI it to nsIClassInfo, and then try to QI to nsIFoo, that will fail. On the
|
|
|
|
* upside, implementing nsIClassInfo doesn't add a vtable pointer to instances
|
|
|
|
* of your class.
|
|
|
|
*
|
|
|
|
* In principal, you can also implement nsIClassInfo by inheriting from the
|
|
|
|
* interface. But some code expects that when it QI's an object to
|
|
|
|
* nsIClassInfo, it gets back a singleton which isn't attached to any
|
|
|
|
* particular object. If a class were to implement nsIClassInfo through
|
|
|
|
* inheritance, that code might QI to nsIClassInfo and keep the resulting
|
|
|
|
* object alive, thinking it was only keeping alive the classinfo singleton,
|
|
|
|
* but in fact keeping a whole instance of the class alive. See, e.g., bug
|
|
|
|
* 658632.
|
|
|
|
*
|
|
|
|
* Unless you specifically need to have a different nsIClassInfo instance for
|
|
|
|
* each instance of your class, you should probably just implement nsIClassInfo
|
|
|
|
* as a singleton.
|
|
|
|
*/
|
|
|
|
|
2010-07-02 11:23:41 -07:00
|
|
|
class NS_COM_GLUE GenericClassInfo : public nsIClassInfo
|
2010-06-10 11:11:11 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct ClassInfoData
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
typedef NS_CALLBACK(GetInterfacesProc)(uint32_t* countp,
|
2012-07-06 13:14:07 -07:00
|
|
|
nsIID*** array);
|
2012-08-22 08:56:38 -07:00
|
|
|
typedef NS_CALLBACK(GetLanguageHelperProc)(uint32_t language,
|
2010-06-10 11:11:11 -07:00
|
|
|
nsISupports** helper);
|
|
|
|
|
|
|
|
GetInterfacesProc getinterfaces;
|
|
|
|
GetLanguageHelperProc getlanguagehelper;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags;
|
2010-06-22 09:59:57 -07:00
|
|
|
nsCID cid;
|
2010-06-10 11:11:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSICLASSINFO
|
|
|
|
|
|
|
|
GenericClassInfo(const ClassInfoData* data)
|
|
|
|
: mData(data)
|
|
|
|
{ }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
private:
|
|
|
|
const ClassInfoData* mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NS_CLASSINFO_NAME(_class) g##_class##_classInfoGlobal
|
|
|
|
#define NS_CI_INTERFACE_GETTER_NAME(_class) _class##_GetInterfacesHelper
|
2007-03-22 10:30:00 -07:00
|
|
|
#define NS_DECL_CI_INTERFACE_GETTER(_class) \
|
2008-12-11 12:13:52 -08:00
|
|
|
extern NS_IMETHODIMP NS_CI_INTERFACE_GETTER_NAME(_class) \
|
2012-08-22 08:56:38 -07:00
|
|
|
(uint32_t *, nsIID ***);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-22 09:59:57 -07:00
|
|
|
#define NS_IMPL_CLASSINFO(_class, _getlanguagehelper, _flags, _cid) \
|
2010-06-10 11:11:11 -07:00
|
|
|
NS_DECL_CI_INTERFACE_GETTER(_class) \
|
|
|
|
static const GenericClassInfo::ClassInfoData k##_class##ClassInfoData = { \
|
|
|
|
NS_CI_INTERFACE_GETTER_NAME(_class), \
|
|
|
|
_getlanguagehelper, \
|
2012-07-13 05:33:25 -07:00
|
|
|
_flags | nsIClassInfo::SINGLETON_CLASSINFO, \
|
2010-06-22 09:59:57 -07:00
|
|
|
_cid, \
|
2010-06-10 11:11:11 -07:00
|
|
|
}; \
|
2013-07-29 16:57:28 -07:00
|
|
|
mozilla::AlignedStorage2<GenericClassInfo> k##_class##ClassInfoDataPlace; \
|
2013-10-10 13:41:39 -07:00
|
|
|
nsIClassInfo* NS_CLASSINFO_NAME(_class) = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-01 09:11:42 -07:00
|
|
|
#define NS_IMPL_QUERY_CLASSINFO(_class) \
|
|
|
|
if ( aIID.Equals(NS_GET_IID(nsIClassInfo)) ) { \
|
|
|
|
if (!NS_CLASSINFO_NAME(_class)) \
|
2013-07-29 16:57:28 -07:00
|
|
|
NS_CLASSINFO_NAME(_class) = new (k##_class##ClassInfoDataPlace.addr()) \
|
2010-07-01 09:11:42 -07:00
|
|
|
GenericClassInfo(&k##_class##ClassInfoData); \
|
|
|
|
foundInterface = NS_CLASSINFO_NAME(_class); \
|
2007-03-22 10:30:00 -07:00
|
|
|
} else
|
|
|
|
|
|
|
|
#define NS_CLASSINFO_HELPER_BEGIN(_class, _c) \
|
|
|
|
NS_IMETHODIMP \
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_CI_INTERFACE_GETTER_NAME(_class)(uint32_t *count, nsIID ***array) \
|
2007-03-22 10:30:00 -07:00
|
|
|
{ \
|
|
|
|
*count = _c; \
|
2014-04-24 02:21:00 -07:00
|
|
|
*array = (nsIID **)nsMemory::Alloc(sizeof (nsIID *) * _c); \
|
|
|
|
uint32_t i = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-24 02:21:00 -07:00
|
|
|
#define NS_CLASSINFO_HELPER_ENTRY(_interface) \
|
|
|
|
(*array)[i++] = (nsIID*)nsMemory::Clone(&NS_GET_IID(_interface), \
|
2007-03-22 10:30:00 -07:00
|
|
|
sizeof(nsIID));
|
|
|
|
|
|
|
|
#define NS_CLASSINFO_HELPER_END \
|
2014-04-24 02:21:00 -07:00
|
|
|
MOZ_ASSERT(i == *count, "Incorrent number of entries"); \
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK; \
|
|
|
|
}
|
|
|
|
|
2014-04-24 02:21:00 -07:00
|
|
|
#define NS_IMPL_CI_INTERFACE_GETTER(aClass, ...) \
|
|
|
|
MOZ_STATIC_ASSERT_VALID_ARG_COUNT(__VA_ARGS__); \
|
|
|
|
NS_CLASSINFO_HELPER_BEGIN(aClass, \
|
|
|
|
MOZ_PASTE_PREFIX_AND_ARG_COUNT(/* No prefix */, \
|
|
|
|
__VA_ARGS__)) \
|
|
|
|
MOZ_FOR_EACH(NS_CLASSINFO_HELPER_ENTRY, (), (__VA_ARGS__)) \
|
|
|
|
NS_CLASSINFO_HELPER_END
|
|
|
|
|
|
|
|
#define NS_IMPL_QUERY_INTERFACE_CI(aClass, ...) \
|
|
|
|
MOZ_STATIC_ASSERT_VALID_ARG_COUNT(__VA_ARGS__); \
|
|
|
|
NS_INTERFACE_MAP_BEGIN(aClass) \
|
|
|
|
MOZ_FOR_EACH(NS_INTERFACE_MAP_ENTRY, (), (__VA_ARGS__)) \
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, MOZ_ARG_1(__VA_ARGS__)) \
|
|
|
|
NS_IMPL_QUERY_CLASSINFO(aClass) \
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-24 02:21:00 -07:00
|
|
|
#define NS_IMPL_ISUPPORTS_CI(aClass, ...) \
|
|
|
|
NS_IMPL_ADDREF(aClass) \
|
|
|
|
NS_IMPL_RELEASE(aClass) \
|
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(aClass, __VA_ARGS__) \
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(aClass, __VA_ARGS__)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif // nsIClassInfoImpl_h__
|