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

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

非同期操作が完了すると、結果に対応する OnXxx() メンバー関数が呼び出されます。 例としては、mip::FileProfile::Observerに対する OnLoadSuccess()OnLoadFailure()OnAddEngineSuccess() です。

以下の例は、Promise/future パターンを示しています。これは SDK サンプルでも使用されており、目的のコールバック動作を実装するために拡張できます。

ファイルプロファイルオブザーバーの実装

次の例では、mip::FileProfile::Observer から派生したクラス ProfileObserverを作成しました。 メンバー関数は、サンプル全体で使用される future/promise パターンを使用するようにオーバーライドされています。

: 以下のサンプルは部分的にのみ実装されており、mip::FileEngine 関連するオブザーバーのオーバーライドは含まれていません。

profile_observer.h

ヘッダーでは、mip::FileProfile::Observerから派生した ProfileObserver を定義し、各メンバー関数をオーバーライドします。

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::OnLoadSuccessmip::FileProfile を受け取ることを期待します。 ProfileObserver::OnAddEngineSuccessmip::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::Observers クラスを実装します。 実装は上記で詳しく説明したものと似ています。 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