保护 SDK 包含三个观察程序类。 你可以替代虚拟观察程序成员来处理异步操作的回调。
-
保护概要:
mip::ProtectionProfile::Observer -
保护引擎:
mip::ProtectionEngine::Observer -
保护处理程序:
mip::ProtectionHandler::Observer
异步操作完成后,SDK 将调用 OnXxx() 与结果相对应的成员函数。 例如,对于 OnLoadSuccess(),示例包括 OnLoadFailure()、OnAddEngineSuccess() 和 mip::ProtectionProfile::Observer。
以下示例演示了 promise/future 模式。 SDK 示例还使用此模式,并且可以扩展它以实现所需的回调行为。
ProtectionProfile 观察程序实现
以下示例创建派生自mip::ProtectionProfile::Observer的类ProtectionProfileObserverImpl。 成员函数会替代基类,以便在整个示例中使用承诺/未来模式。
ProtectionProfileObserverImpl 类声明
该头文件定义了 ProtectionProfileObserverImpl,使其派生自 mip::ProtectionProfile::Observer,然后重写了各个成员函数。
//ProtectionProfileObserverImpl.h
class ProtectionProfileObserverImpl final : public mip::ProtectionProfile::Observer {
public:
ProtectionProfileObserverImpl() { }
void OnLoadSuccess(const shared_ptr<mip::ProtectionProfile>& profile, const shared_ptr<void>& context) override;
void OnLoadFailure(const exception_ptr& error, const shared_ptr<void>& context) override;
void OnAddEngineSuccess(const shared_ptr<mip::ProtectionEngine>& engine, const shared_ptr<void>& context) override;
void OnAddEngineFailure(const exception_ptr& error, const shared_ptr<void>& context) override;
};
ProtectionProfileObserverImpl 实现
该实现定义要对每个观察者成员函数执行的操作。
每个成员可接受两个参数。 第一个是指向函数处理的类的共享指针。
ProtectionObserver::OnLoadSuccess 预期会收到一个 mip::ProtectionProfile。
ProtectionObserver::OnAddEngineSuccess 需要 mip::ProtectionEngine.
第二个是指向上下文的共享指针。 在此实现中,上下文是对shared_ptr<void>的引用,并作为std::promise传入。 函数的第一行将此值强制转换为 std::promise,然后将其存储在名为 promise 的对象中。
最后,代码通过设置 promise->set_value() 和传入 mip::ProtectionProfile 对象使未来准备就绪。
//protection_observers.cpp
void ProtectionProfileObserverImpl::OnLoadSuccess(
const shared_ptr<mip::ProtectionProfile>& profile,
const shared_ptr<void>& context) {
auto loadPromise = static_cast<promise<shared_ptr<mip::ProtectionProfile>>*>(context.get());
loadPromise->set_value(profile);
};
void ProtectionProfileObserverImpl::OnLoadFailure(const exception_ptr& error, const shared_ptr<void>& context) {
auto loadPromise = static_cast<promise<shared_ptr<mip::ProtectionProfile>>*>(context.get());
loadPromise->set_exception(error);
};
void ProtectionProfileObserverImpl::OnAddEngineSuccess(
const shared_ptr<mip::ProtectionEngine>& engine,
const shared_ptr<void>& context) {
auto addEnginePromise = static_cast<promise<shared_ptr<mip::ProtectionEngine>>*>(context.get());
addEnginePromise->set_value(engine);
};
void ProtectionProfileObserverImpl::OnAddEngineFailure(
const exception_ptr& error,
const shared_ptr<void>& context) {
auto addEnginePromise = static_cast<promise<shared_ptr<mip::ProtectionEngine>>*>(context.get());
addEnginePromise->set_exception(error);
};
实例化任何 SDK 类或使用执行异步操作的函数时,请将观察程序实现传递给设置构造函数或异步函数本身。 实例化 mip::ProtectionProfile::Settings 对象时,构造函数接受 mip::ProtectionProfile::Observer 为其中一个参数。 下面的示例展示了在 ProtectionProfileObserverImpl 构造函数中使用的自定义 mip::ProtectionProfile::Settings。
ProtectionHandler 观察程序实现
与保护观察者类似,mip::ProtectionHandler 将会实现一个 mip::ProtectionHandler::Observer 类,用于在保护操作期间处理异步事件通知。 该实现类似于前面的实现。 下面的示例部分定义 ProtectionHandlerObserverImpl。 完整实现位于GitHub示例存储库中。
ProtectionHandlerObserverImpl 类声明
//protection_observers.h
class ProtectionHandlerObserverImpl final : public mip::ProtectionHandler::Observer {
public:
ProtectionHandlerObserverImpl() { }
void OnCreateProtectionHandlerSuccess(const shared_ptr<mip::ProtectionHandler>& protectionHandler, const shared_ptr<void>& context) override;
void OnCreateProtectionHandlerFailure(const exception_ptr& error, const shared_ptr<void>& context) override;
};
ProtectionHandlerObserverImpl 的部分实现
此示例仅包括前两个函数,但其余函数使用与这些函数和函数 ProtectionObserver类似的模式。
//protection_observers.cpp
void ProtectionHandlerObserverImpl::OnCreateProtectionHandlerSuccess(
const shared_ptr<mip::ProtectionHandler>& protectionHandler,
const shared_ptr<void>& context) {
auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get());
createProtectionHandlerPromise->set_value(protectionHandler);
};
void ProtectionHandlerObserverImpl::OnCreateProtectionHandlerFailure(
const exception_ptr& error,
const shared_ptr<void>& context) {
auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get());
createProtectionHandlerPromise->set_exception(error);
};
后续步骤
- 浏览 GitHub 上的 MIP 保护 SDK C++ 示例。