#ifndef _JFXMAP_SINGLETON_H_
#define _JFXMAP_SINGLETON_H_

namespace jf {

template <typename inClassType>
struct singleton_default_new {
    static inClassType* Create() { return new inClassType; }
};

template <typename inClassType, typename inNewFunctorType = singleton_default_new<inClassType>>
class Singleton {
   public:
    typedef inClassType classType;
    typedef inNewFunctorType newFunctorType;

    static classType* GetSingleton() {
        if (nullptr == m_pInstance) {
            // std::lock_guard<std::mutex> lock(m_mutex);
            if (nullptr == m_pInstance) {
                m_pInstance = inNewFunctorType::Create();
            }
        }
        return m_pInstance;
    }

    ~Singleton() = default;

    classType* operator->() { return m_pInstance; }

    Singleton() = default;

    Singleton(classType* in) { m_pInstance = in; }
    // singleton(const singleton &) = delete;
    // singleton &operator=(const singleton &) = delete;

   private:
    static inClassType* m_pInstance;
    // static std::mutex m_mutex;
};

template <typename inClassType, typename inNewFunctorType>
inClassType* Singleton<inClassType, inNewFunctorType>::m_pInstance = nullptr;
// template <typename inClassType, typename inNewFunctorType>
// std::mutex Singleton<inClassType, inNewFunctorType>::m_mutex;

}  // namespace jf

#endif
