/***************************************************************************
*                                                                          *
*  Copyright (C) Golaem S.A.  All Rights Reserved.                         *
*                                                                          *
***************************************************************************/

#pragma once

#include "glmSDKAPI.h"
#include "glmAbstractBehavior.h"
#include <glmPrototypeFactory.h>
#include <glmTypesManager.h>
#include <typeinfo>

namespace glm
{
    class QtGraphicalNode;

    class GLM_SDK_API BehaviorCreator
    {
    public:
        virtual ~BehaviorCreator()
        {
        }
        virtual AbstractBehavior* operator()() const
        {
            return NULL;
        }
    };

    template <class T>
    class BehaviorCreatorT : public BehaviorCreator
    {
    public:
        // creates and returns a new instance of type T
        virtual AbstractBehavior* operator()() const override
        {
            T* myNewT = GLM_NEW(T, myNewT, getClassMemoryStatId());
            return myNewT;
        }
    };

    typedef Singleton<PrototypeFactory<size_t, BehaviorCreator>> behaviorFactorySingleton;

    template <class T>
    void registerBehaviorCreator()
    {
        behaviorFactorySingleton::getInstance().registerCreator<BehaviorCreatorT<T>>(typeid(T).hash_code());
        // register the type in the types manager
        getTypesManagerSingleton().registerType<T>();
    }

    template <class T>
    void unregisterBehaviorCreator()
    {
        behaviorFactorySingleton::getInstance().unregisterCreator<BehaviorCreatorT<T>>(typeid(T).hash_code());
        // register the type in the types manager
        getTypesManagerSingleton().unregisterType<T>();
    }

} // namespace glm

template class GLM_SDK_API glm::Singleton<glm::PrototypeFactory<size_t, glm::BehaviorCreator>>;
