Microsoft Information Protection SDK - ファイル SDK オブザーバー

File SDK には、2 つのオブザーバー クラスが含まれています。 イベント コールバックを処理するために、仮想オブザーバー メンバーをオーバーライドできます。

非同期操作が完了すると、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

実装では、各オブザーバー メンバー関数に対して実行するアクションを定義します。

各メンバーは 2 つのパラメータを受け入れます。 1 つ目は、関数が処理するクラスへの共有ポインターです。 ProfileObserver::OnLoadSuccess は mip::FileProfile を受け取ることを想定しています。 ProfileObserver::OnAddEngineSuccess は mip::FileEngineを想定しています。

2 番目は、contextへの共有ポインタです。 この実装では、コンテキストは std::promiseへの参照であり、 std::shared_ptr<void>として参照渡しされます。 関数の最初の行は、この値を 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 オブジェクトをインスタンス化すると、コンストラクターはパラメーターの 1 つとして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

このサンプルには最初の 2 つの関数のみが含まれていますが、残りの関数はこれらの関数と同様のパターンを使用し、 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

次のステップ