إشعار
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تسجيل الدخول أو تغيير الدلائل.
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تغيير الدلائل.
The Policy SDK contains one observer class. Override virtual observer members to handle callbacks for asynchronous operations.
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::PolicyProfile::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.
Profile observer implementation
The following example creates a class, ProfileObserver, that derives from mip::PolicyProfile::Observer. The member functions override the base class to use the future/promise pattern used throughout the samples.
Note
The following samples implement only part of the observer class and don't include overrides for the mip::PolicyEngine related observers.
profile_observer.h
The header defines ProfileObserver, derives it from mip::PolicyProfile::Observer, then overrides each of the member functions.
class ProfileObserver final : public mip::PolicyProfile::Observer {
public:
ProfileObserver() { }
void OnLoadSuccess(const std::shared_ptr<mip::PolicyProfile>& profile, const std::shared_ptr<void>& context) override;
void OnLoadFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) override;
//TODO: Implement remaining members
};
profile_observer.cpp
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. ProfileObserver::OnLoadSuccess expects to receive a mip::PolicyProfile. ProfileObserver::OnAddEngineSuccess expects mip::PolicyEngine.
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::PolicyProfile object.
#include "profile_observer.h"
#include <future>
//Called when Profile is successfully loaded
void ProfileObserver::OnLoadSuccess(const std::shared_ptr<mip::PolicyProfile>& profile, const std::shared_ptr<void>& context) {
//cast context to promise
auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::PolicyProfile>>>(context);
//set promise value to profile
promise->set_value(profile);
}
//Called when Profile fails to load
void ProfileObserver::OnLoadFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) {
auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::PolicyProfile>>>(context);
promise->set_exception(error);
}
//TODO: Implement remaining observer members
When you perform any asynchronous operation, pass the observer implementation to the settings constructor or async function itself.
Next steps
- Explore the MIP Policy SDK C++ sample on GitHub.