Microsoft Information Protection SDK - 파일 SDK 관찰자

파일 SDK에는 두 개의 관찰자 클래스가 포함되어 있습니다. 이벤트 콜백을 처리하도록 가상 관찰자 멤버를 재정의할 수 있습니다.

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

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

파일 프로필 관찰자 구현

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

Note

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

profile_observer.h

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

class ProfileObserver final : public mip::FileProfile::Observer {
public:
ProfileObserver() { }
  void OnLoadSuccess(const std::shared_ptr<mip::FileProfile>& profile, const std::shared_ptr<void>& context) override;
  void OnLoadFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) override;
  //TODO: Implement mip::FileEngine related observers.
};

profile_observer.cpp

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

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

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

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

#include "profile_observer.h"
#include <future>

//Called when FileProfile is successfully loaded
void ProfileObserver::OnLoadSuccess(const std::shared_ptr<mip::FileProfile>& profile, const std::shared_ptr<void>& context) {
  //cast context to promise
  auto promise = 
  std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileProfile>>>(context);
  //set promise value to profile
  promise->set_value(profile);
}

//Called when FileProfile 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::FileProfile>>>(context);
  promise->set_exception(error);
}

//TODO: Implement mip::FileEngine related observers.

SDK 클래스를 인스턴스화하거나 비동기 작업을 수행하는 함수를 사용하는 경우 관찰자 구현을 설정 생성자 또는 비동기 함수 자체에 전달합니다. mip::FileProfile::Settings 개체를 인스턴스화할 때 생성자는 매개 변수 중 하나로 mip::FileProfile::Observer를 받습니다. 이 예제에서는 mip::FileProfile::Settings 생성자에서 사용되는 사용자 지정 ProfileObserver을 보여줍니다.

FileHandler 관찰자 구현

프로필 관찰자와 마찬가지로 mip::FileHandler는 파일 작업 중 비동기 이벤트 알림을 처리하기 위한 mip::FileHandler::Observer 클래스를 구현합니다. 구현은 이전 구현과 유사합니다. 다음 예제에서는 부분적으로 정의 합니다.FileHandlerObserver

file_handler_observer.h

#include "mip/file/file_handler.h"

class FileHandlerObserver final : public mip::FileHandler::Observer {
public:
  void OnCreateFileHandlerSuccess(
      const std::shared_ptr<mip::FileHandler>& fileHandler,
      const std::shared_ptr<void>& context) override;

  void OnCreateFileHandlerFailure(
      const std::exception_ptr& error,
      const std::shared_ptr<void>& context) override;

  //TODO: override remaining member functions inherited from mip::FileHandler::Observer
};

file_handler_observer.cpp

이 샘플에는 처음 두 함수만 포함되지만 나머지 함수는 이러한 함수와 ProfileObserver유사한 패턴을 사용합니다.

#include "file_handler_observer.h"

void FileHandlerObserver::OnCreateFileHandlerSuccess(const std::shared_ptr<mip::FileHandler>& fileHandler, const std::shared_ptr<void>& context) {
    auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileHandler>>>(context);
    promise->set_value(fileHandler);
}

void FileHandlerObserver::OnCreateFileHandlerFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) {
    auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileHandler>>>(context);
    promise->set_exception(error);
}

//TODO: override remaining member functions inherited from mip::FileHandler::Observer

다음 단계