Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
The Policy SDK contains one observer class. Observer members are virtual and should be overridden to handle callbacks for asynchronous operations.
Al termine di un'operazione asincrona, viene chiamata la OnXxx() funzione membro corrispondente al risultato. Gli esempi sono OnLoadSuccess(), OnLoadFailure()e OnAddEngineSuccess() per mip::Profile::Observer.
Gli esempi seguenti illustrano il modello promise/future, che viene usato anche dagli esempi dell'SDK e possono essere estesi per implementare il comportamento di callback desiderato.
Profile Observer Implementation
Nell'esempio seguente è stata creata una classe derivata ProfileObserver da mip::Profile::Observer. The member functions have been overridden to use the future/promise pattern used throughout the samples.
Note: The below samples are only partially implemented and do not include overrides for the mip::ProfileEngine related observers.
profile_observer.h
Nell'intestazione si definisce ProfileObserver, derivando da mip::Profile::Observer, quindi si esegue l'override di ognuna delle funzioni membro.
class ProfileObserver final : public mip::Profile::Observer {
public:
ProfileObserver() { }
void OnLoadSuccess(const std::shared_ptr<mip::Profile>& 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
In the implementation itself, we define an action to take for each observer member function.
Ogni membro accetta due parametri. The first is a shared pointer to the class handled by the function.
ProfileObserver::OnLoadSuccess would expect to receive a mip::Profile.
ProfileObserver::OnAddEngineSuccess would expect mip::ProfileEngine.
The second is a shared pointer to the context. Nell'implementazione, il contesto è un riferimento a un std::promise, passato come shared_ptr<void>. La prima riga della funzione esegue il cast di questo oggetto in std::promise, quindi archiviato in un oggetto denominato promise.
Finally, the future is made ready by setting the promise->set_value() and passing in the mip::Profile object.
#include "profile_observer.h"
#include <future>
//Called when Profile is successfully loaded
void ProfileObserver::OnLoadSuccess(const std::shared_ptr<mip::Profile>& profile, const std::shared_ptr<void>& context) {
//cast context to promise
auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::Profile>>>(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::Profile>>>(context);
promise->set_exception(error);
}
//TODO: Implement remaining observer members
When performing any asynchronous operation, the observer implementation is passed to the settings constructor or async function itself.