Microsoft 資訊保護 SDK - 檔案 SDK 的觀察者

檔案 SDK 包含兩個觀察者類別。 你可以覆寫虛擬觀察員成員來處理事件回撥。

當非同步操作完成時,SDK 會呼叫 OnXxx() 對應結果的成員函式。 範例包括OnLoadSuccess()OnLoadFailure()OnAddEngineSuccess()用於mip::FileProfile::Observer

這些例子展示了承諾與未來模式。 SDK 範例也使用這個模式,你可以擴展它來實現想要的回調行為。

檔案設定檔觀察器實作

以下範例會產生一個類別 ,該類別 ProfileObserver由 衍生 mip::FileProfile::Observer出。 這些成員函式會覆寫基底類別中的對應函式,以使用整個範例中一貫採用的 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::FileProfileProfileObserver::OnAddEngineSuccess 需要 mip::FileEngine

第二個是指向上下文的共享指標。 在此實作中,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 物件時,建構子會接受 mip::FileProfile::Observer 作為其中一個參數。 此範例展示了在 ProfileObserver 建構函式中使用的自訂 mip::FileProfile::Settings

FileHandler 的觀察者實作

類似於 profile observer, 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

下一步