File SDK には、2 つのオブザーバー クラスが含まれています。 オブザーバー メンバーは仮想であり、イベント コールバックを処理するためにオーバーライドできます。
非同期操作が完了すると、結果に対応する OnXxx() メンバー関数が呼び出されます。 例としては、OnLoadSuccess()に対する OnLoadFailure()、OnAddEngineSuccess()、mip::FileProfile::Observer です。
これらの例では、PROMISE/future パターンを示します。これは SDK サンプルでも使用され、目的のコールバック動作を実装するために拡張できます。
ファイルプロファイルオブザーバーの実装
次の例では、mip::FileProfile::Observerから派生したクラスProfileObserverを作成しました。 メンバー関数は、サンプル全体で使用される future/promise パターンを使用するようにオーバーライドされています。
注: サンプルは部分的にのみ実装され、 mip::FileEngine 関連オブザーバーのオーバーライドは含まれません。
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
実装自体では、各オブザーバー メンバー関数に対して実行するアクションを定義します。
各メンバーは 2 つのパラメータを受け入れます。 1 つ目は、関数で処理しているクラスへの共有ポインタです。 ProfileObserver::OnLoadSuccess は、mip::FileProfile を受け取れるものと予期します。 ProfileObserver::OnAddEngineSuccess は mip::FileEngineを期待します。
2 番目は、contextへの共有ポインタです。 この実装では、コンテキストは 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オブジェクトをインスタンス化するとき、コンストラクターはパラメーターの 1 つとして mip::FileProfile::Observer を受け取ります。 この例では、mip::FileProfile::Settings コンストラクターで使用されるカスタム ProfileObserverを示します。
FileHandler オブザーバーの実装
プロファイル オブザーバーと同様に、 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
このサンプルは最初の 2 つの関数だけですが、残りの関数はこれらと 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