Share via


Microsoft Information Protection SDK - 보호 SDK 관찰자

보호 SDK에는 세 가지 관찰자 클래스가 포함되어 있습니다. 관찰자 멤버는 가상이며 비동기 작업에 대한 콜백을 처리하도록 재정의할 수 있습니다.

비동기 작업이 완료되면 OnXxx() 결과에 해당하는 멤버 함수가 호출됩니다. 예는 OnLoadSuccess(), OnLoadFailure()OnAddEngineSuccess() 에 대한 것입니다 mip::ProtectionProfile::Observer.

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

ProtectionProfile 관찰자 구현

다음 예제에서는 .에서 mip::ProtectionProfile::Observer파생된 클래스 ProtectionProfileObserverImpl 를 만들었습니다. 멤버 함수는 샘플 전체에서 사용되는 promise/future 패턴을 사용하도록 재정의되었습니다.

ProtectionProfileObserverImpl 클래스 선언

헤더에서 파생된 다음 각 멤버 함수를 재정의하여 mip::ProtectionProfile::Observer정의ProtectionProfileObserverImpl합니다.

//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 OnAddEngineError(const exception_ptr& error, const shared_ptr<void>& context) override;
};

ProtectionProfileObserverImpl 구현

구현 자체에서 각 관찰자 멤버 함수에 대해 수행할 작업을 정의하기만 하면 됩니다.

각 멤버는 두 개의 매개 변수를 허용합니다. 첫 번째는 함수에서 처리하는 클래스에 대한 공유 포인터입니다. ProtectionObserver::OnLoadSuccess를 수신할 것으로 예상mip::ProtectionEngine합니다.mip::ProtectionProtectionProtectionObserver::OnAddEngineSuccess

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

마지막으로 개체를 설정하고 promise->set_value() 전달하여 미래를 준비합니다 mip::ProtectionProtection .

//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::OnAddEngineError(
  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 . 아래 예에서는 mip::ProtectionProfile::Settings 생성자에서 사용되는 사용자 지정 ProtectionProfileObserverImpl을 보여줍니다.

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 OnCreateProtectionHandlerError(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::OnCreateProtectionHandlerError(
  const exception_ptr& error,
  const shared_ptr<void>& context) {
  auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get());
  createProtectionHandlerPromise->set_exception(error);
};