Codelite代码分析之 Singleton Pattern Template实现及应用

Hoffman-singleton-graph CodeLite中实现了一个简单的模板并有10个模块使用了它,通过源码我们可以看到它并没有考虑多线程安全和垃圾回收等。

的作用是保证一个类仅有一个实例,并提供一个访问它的全局访问点。Singleton模式其实是对全局静态变量的一个取代策略,上面提到的Singleton模式的两个作用在C++中是通过如下的机制实现的:1)仅有一个实例,提供一个类的静态成员变量,大家知道类的静态成员变量对于一个类的所有对象而言是惟一的 2)提供一个访问它的全局访问点,也就是提供对应的访问这个静态成员变量的静态成员函数,对类的所有对象而言也是惟一的.在C++中,可以直接使用类域进行访问而不必初始化一个类的对象。

在Codelite中也应用到了这个模式实现如下:

<class T> class Singleton
{
    static T* ms_instance;
public:
    /**
     * Static method to access the only pointer of this instance.
     * \return a pointer to the only instance of this
     */
   static T* Get();

    /**
     * Release resources.
     */
    static void Free();

protected:
    /**
     * Default constructor.
     */
    Singleton();

    /**
     * Destructor.
     */
    virtual ~Singleton();
};
<class T> T* Singleton<T>::ms_instance = 0;

<class T> Singleton<T>::Singleton()
{
}

<class T> Singleton<T>::~Singleton()
{
}

<class T> T* Singleton<T>::Get()
{
    if(!ms_instance)
        ms_instance = new T();
    return ms_instance;
}

<class T> void Singleton<T>::Free()
{
    if( ms_instance )
    {
        delete ms_instance;
        ms_instance = 0;
    }
}

Codelite中一共有如下十个模块使用了 :

Buildmanager.h (codelite-1.0.2822\plugin):typedef Singleton<BuildManager> BuildManagerST;
Build_settings_config.h (codelite-1.0.2822\plugin):typedef Singleton<BuildSettingsConfig> BuildSettingsConfigST;
Cscopedbbuilderthread.h (codelite-1.0.2822\cscope):typedef Singleton< CscopeDbBuilderThread > CScopeThreadST;
Ctags_manager.h (codelite-1.0.2822\codelite):typedef Singleton<TagsManager> TagsManagerST;
Editor_config.h (codelite-1.0.2822\plugin):typedef Singleton<EditorConfig> EditorConfigST;
Language.h (codelite-1.0.2822\codelite):typedef Singleton<Language> LanguageST;
Manager.h (codelite-1.0.2822\liteeditor):typedef Singleton<Manager> ManagerST;
Parse_thread.h (codelite-1.0.2822\codelite):typedef Singleton<ParseThread> ParseThreadST;
Search_thread.h (codelite-1.0.2822\plugin):typedef Singleton<SearchThread> SearchThreadST;
Workspace.h (codelite-1.0.2822\plugin):typedef Singleton<Workspace> WorkspaceST;

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

Related posts:

  1. Singleton模式
  2. CodeLite Plugin Internal(1) – IPlugin interface
  3. CodeLite代码分析之 – Create New Project流程
  4. AVL树Source Code
  5. CodeLite Initializaion analysis - 初始化流程分析(1)
  6. Code::Blocks中Plugin的实现原理
  7. CodeLite Plugin Internal(2)-Load: 插件加载分析
  8. CodeLite IDE介绍
  9. CodeBlocks下SDL编译成功实例

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Contact us

Admin: Bryan Wu