Delen via


Microsoft Information Protection SDK - File SDK Observers

The File SDK contains two observer classes. Observer members are virtual and can be overridden to handle event callbacks.

Wanneer een asynchrone bewerking is voltooid, wordt de OnXxx() lidfunctie die overeenkomt met het resultaat aangeroepen. Voorbeelden zijn OnLoadSuccess(), OnLoadFailure()en OnAddEngineSuccess() voor mip::FileProfile::Observer.

These examples demonstrate the promise/future pattern, which is also used by the SDK samples, and can be extended to implement the desired callback behavior.

File Profile Observer Implementation

In the following example, we created a class, ProfileObserver that is derived from mip::FileProfile::Observer. The member functions have been overridden to use the future/promise pattern used throughout the samples.

Note: The samples are only partially implemented and do not include overrides for the mip::FileEngine related observers.

profile_observer.h

In de koptekst definiëren we ProfileObserver, afgeleid van mip::FileProfile::Observer, en overschrijven we vervolgens elk van de lidfuncties.

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

In de implementatie zelf definiëren we een actie die moet worden uitgevoerd voor elke waarnemersfunctie.

Elk lid accepteert twee parameters. De eerste is een gedeelde aanwijzer naar de klasse die we in de functie verwerken. ProfileObserver::OnLoadSuccess would expect to receive a mip::FileProfile. ProfileObserver::OnAddEngineSuccess zou verwachten mip::FileEngine.

De tweede is een gedeelde aanwijzer naar de context. In our implementation, the context is a reference to a std::promise, passed by reference as std::shared_ptr<void>. De eerste regel van de functie cast dit naar std::promise, waarna het wordt opgeslagen in een object genaamd promise.

Ten slotte wordt de toekomst gereed gemaakt door promise->set_value() in te stellen en het mip::FileProfile-object door te geven.

#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.

When we instantiate any SDK class or use a function that performs asynchronous operations, we pass the observer implementation to the settings constructor or async function itself. Bij het instantiëren van het mip::FileProfile::Settings object accepteert de constructor mip::FileProfile::Observer als een van de parameters. This example shows our custom ProfileObserver, used in a mip::FileProfile::Settings constructor.

FileHandler Observer Implementation

Similar to the profile observer, the mip::FileHandler implements a mip::FileHandler::Observers class for handling asynchronous event notifications during file operations. De implementatie is vergelijkbaar met die hierboven beschreven. FileHandlerObserver is gedeeltelijk gedefinieerd hieronder.

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

Dit voorbeeld toont slechts de eerste twee functies, maar de resterende functies gebruiken een vergelijkbaar patroon als deze en 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