muduo之Singleton

         muduo中的單例模式

Singleton.h

// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)

#ifndef MUDUO_BASE_SINGLETON_H
#define MUDUO_BASE_SINGLETON_H

#include "muduo/base/noncopyable.h"

#include <assert.h>
#include <pthread.h>
#include <stdlib.h> // atexit

namespace muduo
{

namespace detail
{
// This doesn't detect inherited member functions!
// http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functions
template<typename T>
struct has_no_destroy
{
  //decltype選擇並返回操作數的數據類型
  template <typename C> static char test(decltype(&C::no_destroy));
  template <typename C> static int32_t test(...);
  const static bool value = sizeof(test<T>(0)) == 1;//判斷如果是類的話,是否有no_destroy方法
};
}  // namespace detail




template<typename T>    
class Singleton : noncopyable
{
 public:
  Singleton() = delete;
  ~Singleton() = delete;

  static T& instance()				//static,保證可以通過類作用域運算符進行調用
  {
  	//pthread_once()函數,在多線程中,保證某個函數只被執行一次。<pthread.h>
    pthread_once(&ponce_, &Singleton::init);	
    assert(value_ != NULL);
    return *value_;
  }

 private:
  static void init()
  {
    value_ = new T();   //根據傳入的類型進行new
    if (!detail::has_no_destroy<T>::value)//當參數是類且沒有"no_destroy"方法纔會註冊atexit的destroy
    {
    	//註冊一個函數,在程序終止時執行
      ::atexit(destroy);//登記atexit時調用的銷燬函數,防止內存泄漏
    }
  }

  static void destroy()
  {
    ////用typedef定義了一個數組類型,數組的大小不能爲-1,利用這個方法,如果是不完全類型,編譯階段就會發現錯誤
    ////定義一個char數組類型 T_MUST_BE_COMPELET_TYPE :char[-1]---如果T只聲明沒有定義-不完全類型 ; char[1]--T是完全類型,即有定義,delete操作,可以調用析構函數,沒定義就沒有析構函數,delete就不會調用析構函數了
    typedef char T_must_be_complete_type[sizeof(T) == 0 ? -1 : 1];//要銷燬這個類型,這個類型必須是完全類型
    T_must_be_complete_type dummy; (void) dummy;

    delete value_;
    value_ = NULL;
  }

 private:
  //pthread_once參數
  static pthread_once_t ponce_;
  static T*             value_;//指向一個實例
};




//static需要在類外進行初始化
template<typename T>
	
//創建一個全局的Singleton<T>::ponce_變量,體現static的作用
pthread_once_t Singleton<T>::ponce_ = PTHREAD_ONCE_INIT;  //ponce_初始化

template<typename T>
T* Singleton<T>::value_ = NULL;

}  // namespace muduo

#endif  // MUDUO_BASE_SINGLETON_H

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章