共用方式為


Microsoft 資訊保護 SDK - 保護 SDK 觀察者

保護 SDK 包含三個觀察者類別。 觀察者成員是虛擬的,可以覆寫以處理非同步作業的回呼。

非同步作業完成時, OnXxx() 會呼叫對應至結果的成員函式。 範例包括 OnLoadSuccess()OnLoadFailure()OnAddEngineSuccess()mip::ProtectionProfile::Observer

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

ProtectionProfile Observer 實作

在下列範例中,我們已建立衍生自 mip::ProtectionProfile::Observer 的類別 ProtectionProfileObserverImpl 。 已覆寫成員函式,以使用整個範例中使用的 promise/future 模式。

ProtectionProfileObserverImpl 類別宣告

在 標頭中,我們會定義 ProtectionProfileObserverImpl 衍生自 mip::ProtectionProfile::Observer ,然後覆寫每個成員函式。

//ProtectionProfileObserverImpl.h
class ProtectionProfileObserverImpl final : public mip::ProtectionProfile::Observer {
public:
  ProtectionProfileObserverImpl() { }
  void OnLoadSuccess(const shared_ptr<mip::ProtectionProfile>& profile, const shared_ptr<void>& context) override;
  void OnLoadFailure(const exception_ptr& error, const shared_ptr<void>& context) override;
  void OnAddEngineSuccess(const shared_ptr<mip::ProtectionEngine>& engine, const shared_ptr<void>& context) override;
  void OnAddEngineError(const exception_ptr& error, const shared_ptr<void>& context) override;
};

ProtectionProfileObserverImpl 實作

在實作本身中,我們只會定義要針對每個觀察者成員函式採取的動作。

每個成員都接受兩個參數。 第一個是我們在 函式中處理之類別的共用指標。 ProtectionObserver::OnLoadSuccess 預期會收到 mip::ProtectionProtectionProtectionObserver::OnAddEngineSuccess 預期會是 mip::ProtectionEngine

第二個是內容的 共用指標 。 在我們的實作中,內容是 傳遞為 shared_ptr<void> 的 參考 std::promise 。 函式的第一行會將這個 std::promise 轉換成 ,然後儲存在名為 promise 的物件中。

最後,藉由設定 promise->set_value() 和 傳入 mip::ProtectionProtection 物件,即可備妥未來。

//protection_observers.cpp

void ProtectionProfileObserverImpl::OnLoadSuccess(
  const shared_ptr<mip::ProtectionProfile>& profile,
  const shared_ptr<void>& context) {
  auto loadPromise = static_cast<promise<shared_ptr<mip::ProtectionProfile>>*>(context.get());
  loadPromise->set_value(profile);
};

void ProtectionProfileObserverImpl::OnLoadFailure(const exception_ptr& error, const shared_ptr<void>& context) {
  auto loadPromise = static_cast<promise<shared_ptr<mip::ProtectionProfile>>*>(context.get());
  loadPromise->set_exception(error);
};

void ProtectionProfileObserverImpl::OnAddEngineSuccess(
  const shared_ptr<mip::ProtectionEngine>& engine,
  const shared_ptr<void>& context) {
  auto addEnginePromise = static_cast<promise<shared_ptr<mip::ProtectionEngine>>*>(context.get());
  addEnginePromise->set_value(engine);
};

void ProtectionProfileObserverImpl::OnAddEngineError(
  const exception_ptr& error,
  const shared_ptr<void>& context) {
  auto addEnginePromise = static_cast<promise<shared_ptr<mip::ProtectionEngine>>*>(context.get());
  addEnginePromise->set_exception(error);
};

當我們具現化任何 SDK 類別或使用執行非同步作業的函式時,我們會將觀察者實作傳遞至設定建構函式或非同步函式本身。 具現化 mip::ProtectionProfile::Settings 物件時,建構函式會接受 mip::ProtectionProfile::Observer 做為其中一個參數。 下列範例顯示我們在建構函式中使用的 mip::ProtectionProfile::Settings 自訂 ProtectionProfileObserverImpl

ProtectionHandler Observer 實作

與保護觀察者類似, mip::ProtectionHandler 實作 類別 mip::ProtectionHandler::Observer ,以在保護作業期間處理非同步事件通知。 實作類似于上述詳細實作。 ProtectionHandlerObserverImpl 部分定義于下方。 您可以在 GitHub 範例存放庫中 找到完整的實作。

ProtectionHandlerObserverImpl 類別宣告

//protection_observers.h

class ProtectionHandlerObserverImpl final : public mip::ProtectionHandler::Observer {
public:
  ProtectionHandlerObserverImpl() { }
  void OnCreateProtectionHandlerSuccess(const shared_ptr<mip::ProtectionHandler>& protectionHandler, const shared_ptr<void>& context) override;
  void OnCreateProtectionHandlerError(const exception_ptr& error, const shared_ptr<void>& context) override;
};

ProtectionHandlerObserverImpl 部分實作

此範例只是前兩個函式,但其餘函式會使用這些 和 的 ProtectionObserver 類似模式。

//protection_observers.cpp

void ProtectionHandlerObserverImpl::OnCreateProtectionHandlerSuccess(
  const shared_ptr<mip::ProtectionHandler>& protectionHandler,
  const shared_ptr<void>& context) {
  auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get());
  createProtectionHandlerPromise->set_value(protectionHandler);
};

void ProtectionHandlerObserverImpl::OnCreateProtectionHandlerError(
  const exception_ptr& error,
  const shared_ptr<void>& context) {
  auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get());
  createProtectionHandlerPromise->set_exception(error);
};