共用方式為


Microsoft 資訊保護 SDK - 檔案 SDK 觀察工具

檔案 SDK 包含兩個觀察者類別。 觀察者成員是虛擬的,可以覆寫來處理事件回呼函數。

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

這些範例示範 promise/future 模式,SDK 範例也會使用此模式,並可擴充以實作所需的回呼行為。

檔案設定檔觀察者實作

在下列範例中,我們建立了衍生自 mip::FileProfile::Observer的類別ProfileObserver。 已經重寫成員函式,使其在整個範例中採用未來/承諾(future/promise)設計模式。

注意:這些範例僅部分完成,且不包含對相關觀察者的覆寫。

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

第二個是指向上下文的共享指標。 在我們的實作中,對 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 Observer 實作

與個人檔案觀察者類似,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

此範例只是前兩個函式,但其餘函式會使用與這些以及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