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

通用型XPCOM模块宏

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

作用

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)

结合23

模块实现宏:

      nsModuleComponentInfo结构体包含4个域:1)给人读的类名称 2)类ID(CID) 3)契约ID 4)用于给定对象的构造者

None.gif static   const  nsModuleComponentInfo components[]  =
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif"Pretty Class Name",
InBlock.gifCID,
InBlock.gifCONTRACT_ID,
InBlock.gifConstructor
ExpandedSubBlockEnd.gif}
,
InBlock.gifdot.gif.
ExpandedBlockEnd.gif}

None.gif

工厂宏:

None.gif NS_GENERIC_FACTORY_CONSTRUCTOR(ConcreteClass)

常用实现宏

None.gif NS_IMPL_ISUPPORTS1(classname, interface1)
None.gifNS_IMPL_ISUPPORTSn(classname, interface1, …, interfacen)
None.gif

声明宏:

None.gif class  myEnumerator :  public  nsISimpleEnumerator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gifNS_DECL_ISUPPORTS
InBlock.gifNS_DECL_NSISIMPLEENUMERATOR
InBlock.gifmyEnumerator();
ExpandedSubBlockStart.gifContractedSubBlock.gif
virtual ~myEnumerator() dot.gif{}
ExpandedBlockEnd.gif}
;
None.gif

NS_IMPL_ISUPPORTSn

为指定类实现nsISupports,此类有n个接口

NS_DECL_ISUPPORTS

声明ISuppotrs接口的方法,包含mRefCnt

NS_INIT_ISUPPORTS

初始化mRefCnt0

NS_GET_IID

返回给定接口名称的IID

 

智能指针

ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
InBlock.gifnsCOMPtr
<nsISupports> value;
InBlock.gif
object->method(getter_AddRefs(value));
InBlock.gif
if (!value) return;
InBlock.gifdot.gif
InBlock.gif
if (NS_FAILED(error))
InBlock.gif
return;
InBlock.gifdot.gif
ExpandedBlockEnd.gif}

None.gif
None.gifSomeClass::Get(nsISupports
**  aResult)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if (! aResult)
InBlock.gif
return NS_ERROR_NULL_POINTER;
InBlock.gifnsCOMPtr
<nsISupports> value;
InBlock.gif
object->method(getter_AddRefs(value));
InBlock.gif
*aResult = value.get();
InBlock.gifNS_IF_ADDREF(
*aResult);
InBlock.gif
return NS_OK;
ExpandedBlockEnd.gif}

None.gif

示例:

None.gif #include  " nsIGenericFactory.h "
None.gif
#define  SAMPLE_CID \
ExpandedBlockStart.gifContractedBlock.gif
dot.gif 0x777f71500x4a2b0x4301, \
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif0xad0x100x5e0xab0x250xb30x220xaa}}

None.gif
class  Sample:  public  nsISupports 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gifSample();
InBlock.gif
virtual ~Sample();
InBlock.gifNS_DECL_ISUPPORTS
ExpandedBlockEnd.gif}
;
None.gifSample::Sample()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
// note: in newer versions of Gecko (1.3 or later)
InBlock.gif
// you don’t have to do this:
InBlock.gif
NS_INIT_ISUPPORTS();
ExpandedBlockEnd.gif}

None.gifSample::
~ Sample()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

None.gifNS_IMPL_ISUPPORTS(Sample, nsISupports);
None.gifNS_GENERIC_FACTORY_CONSTRUCTOR(Sample);
None.gif
None.gif
static   const  nsModuleComponentInfo components[]  =
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif"Pretty Class Name",
InBlock.gifSAMPLE_CID,
InBlock.gif
"@company.com/sample"
InBlock.gifSampleConstructor
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}
;
None.gifNS_IMPL_NSGETMODULE(nsSampleModule, components)
None.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值