这两天看关于NNAPI的文档,发现有下面的代码:
typedef struct ANeuralNetworksModel ANeuralNetworksModel;
文档是这样写的:
ANeuralNetworksModel is an opaque type that contains a description of the mathematical operations that constitute the model.
最开始对不透明类型(opaque type)不太理解,并且代码中也搜不到对ANeuralNetworksModel的具体定义。 后来看了源代码才知道这是用不透明类型来实现抽象的方式。
ANeuralNetworksModel *model;
ANeuralNetworksModel_create(&model);
int ANeuralNetworksModel_create(ANeuralNetworksModel** model) {
initVLogMask();
if (!model) {
LOG(ERROR) << "ANeuralNetworksModel_create passed a nullptr";
return ANEURALNETWORKS_UNEXPECTED_NULL;
}
ModelBuilder* m = new ModelBuilder();
if (m == nullptr) {
*model = nullptr;
return ANEURALNETWORKS_OUT_OF_MEMORY;
}
*model = reinterpret_cast<ANeuralNetworksModel*>(m);
return ANEURALNETWORKS_NO_ERROR;
}
上面的代码中,model实现了对具体类型的隐藏,代码中对ANeuralNetworksModel的任何使用都会首先转换成ModelBuilder,也就是说外部看到的是ANeuralNetworksModel,内部使用的是ModelBuilder,并且外部不知道内部任何信息。
参考: https://blog.csdn.net/clannad_wawa/article/details/40922097