带有重置选项的基本单例课

发布于 2025-01-24 10:05:50 字数 1813 浏览 2 评论 0原文

我正在尝试使用a 单线环境中的CRTP实现基类单

#include <type_traits>
#include <atomic>
#include <cassert>

//Single threaded singleton.
//T is default constructable.
template<typename T>
class singleton
{
    static bool m_empty;

public:
    virtual ~singleton()
    {
        this->reset();
    }

    //YOU will end up with nullptr if I reset: YES THAT IS WHAT I WANT, YOU SHOULDN'T HAVE THE POINTER AT FIRST PLACE.
    //Calls should be done this way: singleton::instance()->do_stuff(); Never hold the instance ++ Single threaded.

    static T* instance()
    {
        static T* pme = new T();
        if (pme)
            return pme;
        m_empty = false;
        pme = new T();
        return pme;
    };

    static bool reset()
    {
        if(m_empty)
            return false;

        T* pme = instance();
        m_empty = true;
        delete pme;
        return true;
    };

protected:
    singleton() = default;
};

顿研究员:

class derive : public singleton<derive>
{
public:
    derive() = default;

    double get() const
    {
        return m_example;
    }
private:
    double m_example;
};

Q1 。可以吗? (请参阅上面的限制。我知道这在多线程环境中以及是否持有实例无效)。

Q2 。这样做时我会遇到错误:

int main(int argc, char* argv[])
{
    const double d = derive::instance()->get();
};


>main.cpp
1>main.obj : error LNK2001: unresolved external symbol "private: static bool singleton<class derive>::m_empty" (?m_empty@?$singleton@0_NA)
1>..\build\bin\Debug\windows\x86_64\tests\tests.exe : fatal error LNK1120: 1 unresolved externals

您能帮我吗?

Q3 。我不需要这个,但是有什么方法可以在多线程Env中使用CRTP class Singleton拥有一个基础?

I am trying to implement a base class singleton using CRTP in a single threaded environnement where the instance is not held by the user, code is below:

#include <type_traits>
#include <atomic>
#include <cassert>

//Single threaded singleton.
//T is default constructable.
template<typename T>
class singleton
{
    static bool m_empty;

public:
    virtual ~singleton()
    {
        this->reset();
    }

    //YOU will end up with nullptr if I reset: YES THAT IS WHAT I WANT, YOU SHOULDN'T HAVE THE POINTER AT FIRST PLACE.
    //Calls should be done this way: singleton::instance()->do_stuff(); Never hold the instance ++ Single threaded.

    static T* instance()
    {
        static T* pme = new T();
        if (pme)
            return pme;
        m_empty = false;
        pme = new T();
        return pme;
    };

    static bool reset()
    {
        if(m_empty)
            return false;

        T* pme = instance();
        m_empty = true;
        delete pme;
        return true;
    };

protected:
    singleton() = default;
};

I can use the class as fellow:

class derive : public singleton<derive>
{
public:
    derive() = default;

    double get() const
    {
        return m_example;
    }
private:
    double m_example;
};

Q1. Is this ok please? (Please see the restriction above. I know this doesn't work in multithreaded environnement and if the instance is held).

Q2. I get error when doing this:

int main(int argc, char* argv[])
{
    const double d = derive::instance()->get();
};


>main.cpp
1>main.obj : error LNK2001: unresolved external symbol "private: static bool singleton<class derive>::m_empty" (?m_empty@?$singleton@0_NA)
1>..\build\bin\Debug\windows\x86_64\tests\tests.exe : fatal error LNK1120: 1 unresolved externals

Could you please help me?

Q3. I don't need this but is there any way to have a base using CRTP class singleton in a multithreaded env?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2025-01-31 10:05:50

singleton_interface是管理单例并调用实例的接口(我拥有此类不是用户)。最终实施:

class singleton_interface;

//Single threaded singleton.
//T is default constructable.
template<typename T>
class singleton
{   
    friend singleton_interface;
   
    static T* instance()
    {
        static T* pme = new T();
        if (pme)
            return pme;
        pme = new T();
        return pme;
    };

    static bool reset()
    {
        T* pme = instance();
        if(!pme)
            return false;

        delete pme;
        pme = nullptr;
        return true;
    };

protected:
    singleton() = default;

    virtual ~singleton()
    {
        this->reset();
    };
};

派生的类应该做:

class derived: public singleton<derived>
{
    friend singleton<singleton<derived>>;
    //....
};

谢谢所有!我会选择这个。

singleton_interface is the interface managing the singletons and calls the instances (I own this class not the user). Final implementation:

class singleton_interface;

//Single threaded singleton.
//T is default constructable.
template<typename T>
class singleton
{   
    friend singleton_interface;
   
    static T* instance()
    {
        static T* pme = new T();
        if (pme)
            return pme;
        pme = new T();
        return pme;
    };

    static bool reset()
    {
        T* pme = instance();
        if(!pme)
            return false;

        delete pme;
        pme = nullptr;
        return true;
    };

protected:
    singleton() = default;

    virtual ~singleton()
    {
        this->reset();
    };
};

derived classes should do:

class derived: public singleton<derived>
{
    friend singleton<singleton<derived>>;
    //....
};

Thanks all! I will go with this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文