Program Listing for File EntityWatchdogFactory.h

Return to documentation for file (axr.sdk/utils/EntityWatchdogFactory.h)

// Copyright (C) AcceleratXR, Inc. All rights reserved.
//
// Author: Jean-Philippe Steinmetz <info@acceleratxr.com>
#pragma once

#include "EntityWatchdog.h"
#include "../Object.h"
#include "../net/IApiClient.h"
#include "../net/IWebSocket.h"
#include "../Configuration.h"

namespace axr {
namespace sdk {
namespace utils {

class EntityWatchdogFactory
{
public:
    EntityWatchdogFactory(std::shared_ptr<Configuration> config, std::shared_ptr<net::IApiClient> apiClient);

    ~EntityWatchdogFactory();

    template<class T>
    std::shared_ptr<EntityWatchdog<T>> GetEntityWatchdog()
    {
        utility::string_t className = T::ClassName();
        if (this->utilities.find(className) != this->utilities.cend())
        {
            return std::static_pointer_cast<EntityWatchdog<T>>(this->utilities.at(className));
        }
        else
        {
            std::shared_ptr<EntityWatchdog<T>> utility = std::make_shared<EntityWatchdog<T>>(config, apiClient);
            this->utilities.insert_or_assign(className, std::static_pointer_cast<IEntityWatchdog>(utility));
            utility->Start();
            return utility;
        }
    }

    bool NotifyEntityUpdate(Object& message);

    void SetPushSocket(std::shared_ptr<net::IWebSocket> socket);

private:
    std::shared_ptr<Configuration> config;
    std::shared_ptr<net::IApiClient> apiClient;
    std::map<utility::string_t, std::shared_ptr<IEntityWatchdog>> utilities;
};

} // namespace utils
} // namespace sdk
} // namespace axr