Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
SDK Perlindungan berisi tiga kelas pengamat. Anggota observer bersifat virtual dan dapat dioverride untuk menangani callback dari operasi asinkron.
-
Profil Perlindungan:
mip::ProtectionProfile::Observer -
Mesin Perlindungan:
mip::ProtectionEngine::Observer -
Penangan Perlindungan:
mip::ProtectionHandler::Observer
Ketika operasi asinkron selesai, OnXxx() fungsi anggota yang sesuai dengan hasil dipanggil. Contohnya adalah OnLoadSuccess(), OnLoadFailure(), dan OnAddEngineSuccess() untuk mip::ProtectionProfile::Observer.
Contoh di bawah ini menunjukkan pola janji/masa depan, yang juga digunakan oleh sampel SDK, dan dapat diperluas untuk menerapkan perilaku panggilan balik yang diinginkan.
Implementasi Pengamat ProtectionProfile
Dalam contoh berikut, kami telah membuat kelas, ProtectionProfileObserverImpl yang berasal dari mip::ProtectionProfile::Observer. Fungsi anggota telah dioverride untuk menggunakan pola promise/future yang digunakan di seluruh contoh.
Deklarasi Kelas ProtectionProfileObserverImpl
Di header, kita menentukan ProtectionProfileObserverImpl, berasal dari mip::ProtectionProfile::Observer, lalu mengambil alih setiap fungsi anggota.
//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;
};
Implementasi ProtectionProfileObserverImpl
Dalam implementasi itu sendiri, kami hanya mendefinisikan tindakan yang harus diambil untuk setiap fungsi anggota pengamat.
Setiap anggota menerima dua parameter. Yang pertama adalah pointer bersama yang merujuk ke kelas yang kita tangani dalam fungsi ini.
ProtectionObserver::OnLoadSuccess akan mengharapkan untuk menerima mip::ProtectionProtection, ProtectionObserver::OnAddEngineSuccess akan mengharapkan mip::ProtectionEngine.
Yang kedua adalah penunjuk bersama ke konteks. Dalam implementasi kami, konteks adalah referensi ke std::promise, yang diteruskan sebagai shared_ptr<void>. Baris pertama fungsi melemparkan ini ke std::promise, lalu disimpan dalam objek yang disebut promise.
Akhirnya, future disiapkan dengan mengatur promise->set_value() dan meneruskan objek 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);
};
Ketika kita membuat instans kelas SDK atau menggunakan fungsi yang melakukan operasi asinkron, kita akan meneruskan implementasi pengamat ke konstruktor pengaturan atau fungsi asinkron itu sendiri. Saat membuat mip::ProtectionProfile::Settings instans objek, konstruktor mengambil mip::ProtectionProfile::Observer sebagai salah satu parameter. Contoh di bawah ini menunjukkan ProtectionProfileObserverImpl kustom kami, yang digunakan dalam konstruktor mip::ProtectionProfile::Settings.
Implementasi Pengamat ProtectionHandler
Mirip dengan observer perlindungan, mip::ProtectionHandler mengimplementasikan kelas mip::ProtectionHandler::Observer untuk menangani notifikasi kejadian asinkron selama operasi perlindungan. Implementasinya mirip dengan yang dirinci di atas.
ProtectionHandlerObserverImpl didefinisikan sebagian di bawah ini. Implementasi lengkap dapat ditemukan di GitHub repositori sampel kami.
Deklarasi Kelas 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 Implementasi Sebagian
Sampel ini hanyalah dua fungsi pertama, tetapi fungsi yang tersisa menggunakan pola yang mirip dengan ini dan untuk 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);
};