Microsoft Information Protection SDK - 정책 SDK 관찰자

정책 SDK에는 하나의 관찰자 클래스가 포함됩니다. 비동기 작업에 대한 콜백을 처리하도록 가상 관찰자 멤버를 재정의합니다.

비동기 작업이 완료되면 SDK는 결과에 해당하는 멤버 함수를 호출 OnXxx() 합니다. 예시들은 OnLoadSuccess(), OnLoadFailure(), OnAddEngineSuccess() 및 mip::PolicyProfile::Observer에 대한 것입니다.

다음 예제에서는 promise/future 패턴을 보여 줍니다. 또한 SDK 샘플은 이 패턴을 사용하며 원하는 콜백 동작을 구현하도록 확장할 수 있습니다.

프로필 관찰자 구현

다음 예제에서는 .에서 mip::PolicyProfile::Observer파생되는 클래스ProfileObserver를 만듭니다. 멤버 함수는 샘플 전체에서 사용되는 future/promise 패턴을 사용하도록 기본 클래스를 재정의합니다.

Note

다음 샘플은 관찰자 클래스의 일부만 구현하며 관련 관찰자에 대한 재정의를 mip::PolicyEngine 포함하지 않습니다.

profile_observer.h

헤더는 ProfileObserver를 정의하고, 이를 mip::PolicyProfile::Observer에서 파생시킨 다음 각 멤버 함수를 재정의합니다.

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

구현은 각 관찰자 멤버 함수에 대해 수행할 작업을 정의합니다.

각 멤버는 두 개의 매개 변수를 허용합니다. 첫 번째는 함수가 처리하는 클래스에 대한 공유 포인터입니다. ProfileObserver::OnLoadSuccess은 mip::PolicyProfile를 수신할 것으로 예상됩니다. ProfileObserver::OnAddEngineSuccess에는 mip::PolicyEngine이 필요합니다.

두 번째는 컨텍스트에 대한 공유 포인터입니다. 이 구현에서 컨텍스트는 std::promise로 전달되는 shared_ptr<void>에 대한 참조입니다. 함수의 첫 번째 줄은 이 값을 std::promise로 캐스팅한 다음, promise라는 개체에 저장합니다.

마지막으로, 이 코드는 promise->set_value()를 설정하고 mip::PolicyProfile 개체를 전달하여 향후에 대비할 수 있게 합니다.

#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

비동기 작업을 수행하는 경우 관찰자 구현을 설정 생성자 또는 비동기 함수 자체에 전달합니다.

다음 단계