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