《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.gifstaticconstnsModuleComponentInfocomponents[]=
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{"PrettyClassName",
InBlock.gifCID,
InBlock.gifCONTRACT_ID,
InBlock.gifConstructor
ExpandedSubBlockEnd.gif}
,
InBlock.gifdot.gif.
ExpandedBlockEnd.gif}

None.gif

工厂宏:

None.gifNS_GENERIC_FACTORY_CONSTRUCTOR(ConcreteClass)

常用实现宏

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

声明宏:

None.gifclassmyEnumerator:publicnsISimpleEnumerator
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.gifContractedBlock.gifdot.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
returnNS_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
returnNS_OK;
ExpandedBlockEnd.gif}

None.gif

示例:

None.gif#include"nsIGenericFactory.h"
None.gif
#defineSAMPLE_CID/
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{0x777f7150,0x4a2b,0x4301,/
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{0xad,0x10,0x5e,0xab,0x25,0xb3,0x22,0xaa}}

None.gif
classSample:publicnsISupports
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:innewerversionsofGecko(1.3orlater)
InBlock.gif
//youdon’thavetodothis:
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
staticconstnsModuleComponentInfocomponents[]=
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{"PrettyClassName",
InBlock.gifSAMPLE_CID,
InBlock.gif
"@company.com/sample"
InBlock.gifSampleConstructor
ExpandedSubBlockEnd.gif}

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

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页