《XPCOM组件开发》笔记(四)

本文详细介绍了XPCOM中的通用模块宏及其使用方法,包括实现接口、构造和析构宏等,并通过示例代码展示了如何创建XPCOM组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通用型XPCOM模块宏
作用
NS_IMPL_NSGETMODULE(name, components)
实现IModule接口
NS_IMPL_NSGETMODULE_WITH_CTOR(name, components, ctor)
同上,但允许指定模块创建时调用一个特定函数
NS_IMPL_NSGETMODULE_WITH_DTOR
(name, components, dtor)
同第一个,但允许指定模块销毁时调用一个特定函数
NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR
(name, components, ctor, dtor)
结合2,3
模块实现宏:
      nsModuleComponentInfo结构体包含4个域:1)给人读的类名称 2)类ID(CID) 3)契约ID 4)用于给定对象的构造者
static const nsModuleComponentInfo components[] =
{
{ "Pretty Class Name",
CID,
CONTRACT_ID,
Constructor
},
.
}

工厂宏:
NS_GENERIC_FACTORY_CONSTRUCTOR(ConcreteClass)
常用实现宏
NS_IMPL_ISUPPORTS1(classname, interface1)
NS_IMPL_ISUPPORTSn(classname, interface1, …, interfacen)

声明宏:
class myEnumerator : public nsISimpleEnumerator
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSISIMPLEENUMERATOR
myEnumerator();
virtual ~myEnumerator() {}
};

NS_IMPL_ISUPPORTSn
为指定类实现nsISupports,此类有n个接口
NS_DECL_ISUPPORTS
声明ISuppotrs接口的方法,包含mRefCnt
NS_INIT_ISUPPORTS
初始化mRefCnt为0
NS_GET_IID
返回给定接口名称的IID
 
智能指针
{
nsCOMPtr<nsISupports> value;
object->method(getter_AddRefs(value));
if (!value) return;

if (NS_FAILED(error))
return;

}

SomeClass::Get(nsISupports** aResult)
{
if (! aResult)
return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsISupports> value;
object->method(getter_AddRefs(value));
*aResult = value.get();
NS_IF_ADDREF(*aResult);
return NS_OK;
}

示例:
#include "nsIGenericFactory.h"
#define SAMPLE_CID \
{ 0x777f7150, 0x4a2b, 0x4301, \
{ 0xad, 0x10, 0x5e, 0xab, 0x25, 0xb3, 0x22, 0xaa}}
class Sample: public nsISupports 
{
public:
Sample();
virtual ~Sample();
NS_DECL_ISUPPORTS
};
Sample::Sample()
{
// note: in newer versions of Gecko (1.3 or later)
// you don’t have to do this:
NS_INIT_ISUPPORTS();
}
Sample::~Sample()
{
}
NS_IMPL_ISUPPORTS(Sample, nsISupports);
NS_GENERIC_FACTORY_CONSTRUCTOR(Sample);

static const nsModuleComponentInfo components[] =
{
{ "Pretty Class Name",
SAMPLE_CID,
"@company.com/sample"
SampleConstructor
}
};
NS_IMPL_NSGETMODULE(nsSampleModule, components)



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/06/02/1212338.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值