/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_UTILS_SINGLETON_H
#define ANDROID_UTILS_SINGLETON_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/threads.h>
#include <cutils/compiler.h>
namespace android {
// ---------------------------------------------------------------------------
template <typename TYPE>
class ANDROID_API Singleton
{
public:
static TYPE& getInstance() {
Mutex::Autolock _l(sLock);
TYPE* instance = sInstance;
if (instance == 0) {
instance = new TYPE();
sInstance = instance;
}
return *instance;
}
static bool hasInstance() {
Mutex::Autolock _l(sLock);
return sInstance != 0;
}
protected:
~Singleton() { };
Singleton() { };
private:
//禁止复制构造函数和赋值运算符函数,禁止类外部和内部以及友元调用 declare private,not define
Singleton(const Singleton&);
Singleton& operator = (const Singleton&);
static Mutex sLock;
static TYPE* sInstance;
};
/*
* use ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) in your implementation file
* (eg: <TYPE>.cpp) to create the static instance of Singleton<>'s attributes,
* and avoid to have a copy of them in each compilation units Singleton<TYPE>
* is used.
*
* NOTE: we use a version of Mutex ctor that takes a parameter, because
* for some unknown reason using the default ctor doesn't emit the variable!
* 注意:我们使用Mutex类带一个参数的构造函数,是因为一些未知原因可能导致使用默认构造函数无法生成变量。
* Google工程师也有妥协的时候啊,开来我们也不要太过执着,淡定,这个注释说明其意图,使读者明白其道理,并体现了Google工程师的坦诚。
*/
//想要使用Singleton,需要在自定义类型的实现文件中包含此宏,用以初始化类模版static变量,并显示实例化类模版
#define ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) \
template<> Mutex Singleton< TYPE >::sLock(Mutex::PRIVATE); \
template<> TYPE* Singleton< TYPE >::sInstance(0); \
template class Singleton< TYPE >;
// ---------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_UTILS_SINGLETON_H
时隔一年整,我今天终于弄明白困扰谷歌工程师的疑惑啦,首先,根据模板语法,template<>是对静态成员变量的特化。
在此情况下,如果使用默认的初始化方式
template<> Mutex Singleton< TYPE >::sLock
编译器会认为上行代码是一个非定义的声明。并且我们不能这样做
template<> Mutex Singleton< TYPE >::sLock();
因为编译器会认为只是一个函数声明,而不是调用默认构造函数进行初始化。
如果为了从简单的角度出发(),使用如下方法应该一样好
template<typename T> T Singleton<T>::mutex();
至于使用特化定义,还是使用普通的定义区别,我现在真的说不好。