보호 SDK에는 세 가지 관찰자 클래스가 포함되어 있습니다. 비동기 작업에 대한 콜백을 처리하도록 가상 관찰자 멤버를 재정의할 수 있습니다.
-
보호 프로필:
mip::ProtectionProfile::Observer -
보호 엔진:
mip::ProtectionEngine::Observer -
보호 처리기:
mip::ProtectionHandler::Observer
비동기 작업이 완료되면 SDK는 결과에 해당하는 멤버 함수를 호출 OnXxx() 합니다. 예는 OnLoadSuccess(), OnLoadFailure()및 OnAddEngineSuccess() 에 대한 것입니다 mip::ProtectionProfile::Observer.
다음 예제에서는 promise/future 패턴을 보여 줍니다. 또한 SDK 샘플은 이 패턴을 사용하며 원하는 콜백 동작을 구현하도록 확장할 수 있습니다.
ProtectionProfile 관찰자 구현
다음 예제에서는 .에서 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 OnAddEngineFailure(const exception_ptr& error, const shared_ptr<void>& context) override;
};
ProtectionProfileObserverImpl 구현
구현은 각 관찰자 멤버 함수에 대해 수행할 작업을 정의합니다.
각 멤버는 두 개의 매개 변수를 허용합니다. 첫 번째는 함수가 처리하는 클래스에 대한 공유 포인터입니다.
ProtectionObserver::OnLoadSuccess은 mip::ProtectionProfile를 수신할 것으로 예상됩니다.
ProtectionObserver::OnAddEngineSuccess에는 mip::ProtectionEngine이 필요합니다.
두 번째는 컨텍스트에 대한 공유 포인터입니다. 이 구현에서 컨텍스트는 std::promise로 전달되는 shared_ptr<void>에 대한 참조입니다. 함수의 첫 번째 줄은 이 값을 std::promise로 캐스팅한 다음, promise라는 개체에 저장합니다.
마지막으로, 이 코드는 promise->set_value()를 설정하고 mip::ProtectionProfile 개체를 전달하여 향후에 대비할 수 있게 합니다.
//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::OnAddEngineFailure(
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 관찰자 구현
보호 관찰자와 비슷하게, 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 OnCreateProtectionHandlerFailure(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::OnCreateProtectionHandlerFailure(
const exception_ptr& error,
const shared_ptr<void>& context) {
auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get());
createProtectionHandlerPromise->set_exception(error);
};
다음 단계
- GitHub MIP Protection SDK C++ 샘플을 살펴보세요.