#pragma once #include "app/AppContext.h" #include "app/AppManifest.h" #include "Bundle.h" #include "Mutex.h" #include #include namespace tt::app { typedef enum { StateInitial, // App is being activated in loader StateStarted, // App is in memory StateShowing, // App view is created StateHiding, // App view is destroyed StateStopped // App is not in memory } State; struct ResultHolder { Result result; std::shared_ptr resultData; explicit ResultHolder(Result result) : result(result), resultData(nullptr) {} ResultHolder(Result result, std::shared_ptr resultData) : result(result), resultData(std::move(resultData)) {} }; /** * Thread-safe app instance. */ class AppInstance : public AppContext { private: Mutex mutex = Mutex(Mutex::TypeNormal); const AppManifest& manifest; State state = StateInitial; Flags flags = { .showStatusbar = true }; /** @brief Optional parameters to start the app with * When these are stored in the app struct, the struct takes ownership. * Do not mutate after app creation. */ std::shared_ptr _Nullable parameters; /** @brief @brief Contextual data related to the running app's instance * The app can attach its data to this. * The lifecycle is determined by the on_start and on_stop methods in the AppManifest. * These manifest methods can optionally allocate/free data that is attached here. */ std::shared_ptr _Nullable data; std::unique_ptr _Nullable resultHolder; public: explicit AppInstance(const AppManifest& manifest) : manifest(manifest) {} AppInstance(const AppManifest& manifest, std::shared_ptr parameters) : manifest(manifest), parameters(std::move(parameters)) {} ~AppInstance() override = default; void setState(State state); State getState() const; const AppManifest& getManifest() const override; Flags getFlags() const override; void setFlags(Flags flags); Flags& mutableFlags() { return flags; } // TODO: locking mechanism std::shared_ptr _Nullable getData() const override; void setData(std::shared_ptr data) override; std::shared_ptr getParameters() const override; void setResult(Result result) override; void setResult(Result result, std::shared_ptr bundle) override; bool hasResult() const override; std::unique_ptr& getResult() { return resultHolder; } }; } // namespace