1.Suites类
在illustrator里,有许多不同的功能模块,这些模块在插件开发中如果需要使用,就必须先加载,或者初始化,获取其接口。这时必然使用到Suites这个类。头文件代码如下:
struct ImportSuite
{
const char *name;
int version;
void *suite;//指针变量
};
const ASInt32 kStartOptionalSuites = 'OPTS';
const ASInt32 kEndAllSuites = 0;
// The fAcquiredCount field is not necessarily maintained across unload/reloads;
// in fact it probably isn't. Whenever we first create a Suites object after
// being loaded or reloaded, it's important to call InitializeRefCount on it.
class Suites
{
public:
Suites();
~Suites();
ASErr Error() { return fError; }
void InitializeRefCount() { fAcquiredCount = 1; }
void acquire_Optional_Suites();
private:
static ASUInt16 fAcquiredCount;
ASErr fError;
ASUInt16 fOptionalSuitesTotalCount;
ASUInt16 fRequiredSuitesCount;
ASUInt16 fOptionalSuitesAcquiredCount;
ASErr AcquireSuites();//获取模块组件
ASErr ReleaseSuites(); //释放模块组件
ASErr AcquireSuite(ImportSuite *suite);
ASErr ReleaseSuite(ImportSuite *suite);
};
初始化函数,代码如下:
Suites::Suites()
: fOptionalSuitesTotalCount(0),
fOptionalSuitesAcquiredCount(0),
fRequiredSuitesCount(0)
{
fError = AcquireSuites();//请求加载模块
ASUInt16 i = 0;
while( (gImportSuites[i++].name != nil))
{ ++fRequiredSuitesCount; } // Skip all required suites.
if(gImportSuites[i - 1].version == kStartOptionalSuites)//计算额外模块
{
while(gImportSuites[i++].name != nil)
{
++fOptionalSuitesTotalCount;
}
}
if(kNoErr == fError)
acquire_Optional_Suites();
}
单个模块的请求加载,代码如下:
ASErr Suites::AcquireSuite(ImportSuite *suite)
{
ASErr result = kNoErr;
if (suite->suite != nil) {
result = sSPBasic->AcquireSuite(suite->name,
suite->version,
(const void **)suite->suite);
}
return result;
}
单个模块的释放卸载,代码如下:
ASErr Suites::ReleaseSuite(ImportSuite *suite)
{
ASErr result = kNoErr;
if (suite->suite != nil) {
void **s = (void **) suite->suite;
if (*s != nil) {
result = sSPBasic->ReleaseSuite(suite->name, suite->version);
*s = nil;
}
}
return result;
}
多个模块的请求加载,代码如下:
ASErr Suites::AcquireSuites()
{
ASErr result = kNoErr;
ASUInt16 i;
if (fAcquiredCount == 0)
{
//基础模块
for (i = 0; i < sizeof(gBasicSuites)/sizeof(gBasicSuites[0]); ++i)
{
result = AcquireSuite(gBasicSuites+i);
if (result)
goto error;
}
//指定模块
for (i = 0; gImportSuites[i].name != nil; ++i)
{
result = AcquireSuite(gImportSuites+i);
if (result)
goto error;
}
}
fAcquiredCount++;
error:
return result;
}
多个模块的释放卸载,代码如下:
ASErr Suites::ReleaseSuites()
{
ASErr result = kNoErr;
ASUInt16 i;
fAcquiredCount--;
if (fAcquiredCount == 0)
{
//基础模块
for (i = 0; i < sizeof(gBasicSuites)/sizeof(gBasicSuites[0]); ++i)
{
result = ReleaseSuite(gBasicSuites+i);
if ( result )
goto error;
}
//指定模块
for (i = 0; gImportSuites[i].name != nil; ++i)
{
result = ReleaseSuite(gImportSuites+i);
if (result)
goto error;
}
if(gImportSuites[i++].version == kStartOptionalSuites)
{
for (; fOptionalSuitesAcquiredCount && (gImportSuites[i].name != nil); ++i)
{
result = ReleaseSuite(gImportSuites+i);
if (result)
goto error;
else
--fOptionalSuitesAcquiredCount;
}
}
}
error:
return result;
}
请求可选模块,代码如下:
void Suites::acquire_Optional_Suites()
{
if(fOptionalSuitesAcquiredCount != fOptionalSuitesTotalCount)
{
ImportSuite* optionalSuites = gImportSuites + fRequiredSuitesCount + 1;
for(ASUInt16 i = 0; (i < fOptionalSuitesTotalCount) && optionalSuites->suite; ++i)
{
int** suitePtr = static_cast<int**>(optionalSuites->suite);
if( *suitePtr == nil)
{
if( kNoErr == AcquireSuite(optionalSuites))
++fOptionalSuitesAcquiredCount;
}
++optionalSuites;
}
}
}
该类的主要功能就是illustrator内部模块的加载和卸载。
2.作者答疑
如有疑问,请留言。
提示: 作者知了-联系方式1
提示: 作者知了-联系方式2