Commit fe1df9ed authored by Pascal Palenda's avatar Pascal Palenda
Browse files

Add worker interface - WIP

parent 5dbf0e6d
#ifndef INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_ROOM_ACOUSTICS_WORKER_INTERFACE
#define INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_ROOM_ACOUSTICS_WORKER_INTERFACE
// std includes
#include <memory>
#include <functional>
// API includes
#include <ITA/simulation_scheduler/definitions.h>
// ITA includes
#include <ITA/simulation_scheduler/update_scene.h>
// Vista include
#include <VistaAspects/VistaPropertyList.h>
namespace ITA
{
namespace simulation_scheduler
{
namespace room_acoustics
{
class CScheduler;
///
/// \brief Interface for worker.
///
/// A worker is used by the scheduler to simulate an update.
/// When an update is passed to a worker, it is imitatively simulated.
/// The simulation is blocking.
/// When the simulation is finished, the result is passed back to the scheduler.
///
/// A worker is created by CWorkerFactory.
/// For this, a function like this is necessary:
/// \code{.cpp}
/// ///
/// /// \brief Factory function for a worker.
/// /// \remark This function should to be static when overwritten.
/// ///
/// static std::unique_ptr<IWorkerInterface> createWorker ( const IWorkerInterface::WorkerConfig& pConfig, CScheduler* pParent );
/// \endcode
///
class ITA_SIMULATION_SCHEDULER_API IWorkerInterface
{
public:
///
/// \brief Configuration class for Worker.
/// \todo This class might be only used to hold other worker configs. See if we should keep this.
///
struct ITA_SIMULATION_SCHEDULER_API WorkerConfig : public VistaPropertyList
{
int i = 5;
};
virtual ~IWorkerInterface ( ) = default;
///
/// \brief Checks if the worker is busy.
/// \return true if the worker is busy, else false.
///
virtual bool isBusy ( ) = 0;
///
/// \brief Post a new update to the worker.
///
/// After a new Update is posted to a worker, the update gets simulated immediately.
/// \note This method takes ownership of the update.
/// \param pUpdateMessage the new update for the scheduler.
///
virtual void postUpdate ( std::unique_ptr<CUpdateScene> pUpdateMessage ) = 0;
///
/// \brief Reset the worker.
///
virtual void reset ( ) = 0;
protected:
///
/// \brief Pointer to the parent scheduler.
///
/// Via this pointer the result of the simulation is passed to the parent scheduler.
///
CScheduler* m_pParentScheduler = nullptr;
};
///
/// \brief Factory class for Worker.
///
/// This class implements an extensible factory for worker.
/// After a type of worker is registered with its IWorkerInterface::createWorker, it can be created using this factory.
///
class ITA_SIMULATION_SCHEDULER_API CWorkerFactory
{
public:
///
/// \brief The function signature for a create function.
///
typedef std::function<std::unique_ptr<IWorkerInterface> ( const IWorkerInterface::WorkerConfig&, CScheduler* )> CreateCallback;
///
/// \brief Register a type of worker to the factory.
/// \param type name of the worker type to be registered.
/// \param createFunction the corresponding create function.
///
static void registerWorker ( const std::string& type, CreateCallback createFunction );
///
/// \brief Unregister a type of worker from the factory.
/// \param type name of the worker type to be removed.
///
static void unregisterWorker ( const std::string& type );
///
/// \brief Factory method.
/// \param pConfig the configuration for the worker.
/// \param pParent the parent scheduler.
/// \return a pointer to the created worker.
///
static std::unique_ptr<IWorkerInterface> createWorker(const IWorkerInterface::WorkerConfig& pConfig,
CScheduler* pParent);
private:
///
/// \brief Map containing all creation functions.
///
static std::map<std::string, CreateCallback> m_mWorkers;
};
} // namespace room_acoustics
} // namespace simulation_scheduler
} // namespace ITA
#endif // INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_ROOM_ACOUSTICS_WORKER_INTERFACE
\ No newline at end of file
#include <ITA\simulation_scheduler\room_acoustics\worker_interface.h>
#include <ITA/simulation_scheduler/audibility_filter/audibility_filter_interface.h>
namespace ITA
{
namespace simulation_scheduler
{
namespace room_acoustics
{
std::map<std::string, CWorkerFactory::CreateCallback> CWorkerFactory::m_mWorkers;
void CWorkerFactory::registerWorker ( const std::string& type, CreateCallback createFunction )
{
m_mWorkers [type] = createFunction;
}
void CWorkerFactory::unregisterWorker ( const std::string& type )
{
m_mWorkers.erase ( type );
}
std::unique_ptr<IWorkerInterface> CWorkerFactory::createWorker ( const IWorkerInterface::WorkerConfig& pConfig, CScheduler* pParent )
{
auto it = m_mWorkers.find ( pConfig.begin ( )->first );
if ( it != m_mWorkers.end ( ) )
{
// call the creation callback to construct this derived type
return it->second ( pConfig, pParent );
}
return nullptr;
}
} // namespace audibility_filter
} // namespace simulation_scheduler
} // namespace ITA
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment