إشعار
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تسجيل الدخول أو تغيير الدلائل.
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تغيير الدلائل.
The Protection SDK contains three observer classes. You can override virtual observer members to handle callbacks for asynchronous operations.
- Protection Profile:
mip::ProtectionProfile::Observer - Protection Engine:
mip::ProtectionEngine::Observer - Protection Handler:
mip::ProtectionHandler::Observer
When an asynchronous operation completes, the SDK calls the OnXxx() member function that corresponds to the result. Examples are OnLoadSuccess(), OnLoadFailure(), and OnAddEngineSuccess() for mip::ProtectionProfile::Observer.
The following examples demonstrate the promise/future pattern. The SDK samples also use this pattern, and you can extend it to implement the desired callback behavior.
ProtectionProfile observer implementation
The following example creates a class, ProtectionProfileObserverImpl, that derives from mip::ProtectionProfile::Observer. The member functions override the base class to use the promise/future pattern used throughout the samples.
ProtectionProfileObserverImpl class declaration
The header defines ProtectionProfileObserverImpl, derives it from mip::ProtectionProfile::Observer, then overrides each of the member functions.
//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 implementation
The implementation defines an action to take for each observer member function.
Each member accepts two parameters. The first is a shared pointer to the class that the function handles. ProtectionObserver::OnLoadSuccess expects to receive a mip::ProtectionProfile. ProtectionObserver::OnAddEngineSuccess expects mip::ProtectionEngine.
The second is a shared pointer to the context. In this implementation, the context is a reference to a std::promise, passed in as shared_ptr<void>. The first line of the function casts this value to std::promise, then stores it in an object called promise.
Finally, the code makes the future ready by setting promise->set_value() and passing in the mip::ProtectionProfile object.
//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);
};
When you instantiate any SDK class or use a function that performs asynchronous operations, pass the observer implementation to the settings constructor or async function itself. When you instantiate the mip::ProtectionProfile::Settings object, the constructor accepts mip::ProtectionProfile::Observer as one of the parameters. The following example shows the custom ProtectionProfileObserverImpl, used in a mip::ProtectionProfile::Settings constructor.
ProtectionHandler observer implementation
Similar to the Protection observer, mip::ProtectionHandler implements a mip::ProtectionHandler::Observer class for handling asynchronous event notifications during protection operations. The implementation is similar to the preceding implementation. The following example partially defines ProtectionHandlerObserverImpl. The full implementation is in the GitHub sample repository.
ProtectionHandlerObserverImpl class declaration
//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 partial implementation
This sample includes only the first two functions, but the remaining functions use a similar pattern to these functions and to 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);
};
Next steps
- Explore the MIP Protection SDK C++ sample on GitHub.