Android frameworks Singleton

这篇博客探讨了在Android框架中Singleton模式的应用,尤其是关于模板特化和静态成员变量初始化的问题。博主指出,静态成员变量的特化可能导致编译器将其视为函数声明而非构造函数初始化,并建议了一种可能的解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * 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();

至于使用特化定义,还是使用普通的定义区别,我现在真的说不好。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值