Qt:解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb)

Qt:解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb)
From:  http://zhgw01.blog.163.com/blog/static/1041481220105253491586/
根据 src/corelib/global.h
 template <typename T>
class QGlobalStatic
{
public:
    T *pointer;
    inline QGlobalStatic(T *p) : pointer(p) { }
    inline ~QGlobalStatic() { pointer = 0; }
};

#define Q_GLOBAL_STATIC(TYPE, NAME)                              \
    static TYPE *NAME()                                          \
    {                                                            \
        static TYPE this_##NAME;                                 \
        static QGlobalStatic<TYPE > global_##NAME(&this_##NAME); \
        return global_##NAME.pointer;                            \
    }

   

上面的展开成
 static  QFontDatabasePrivate *privateDb()
{
    static QFontDatabasePrivate this_privateDb;  // 注意第一次引用的时候会开始进行初始化, 以后不再生成
   static QGLobalStatic<QFontDatabasePrivate> global_privateDb(&this_privateDb); //对上面生成的对象的指针进行包装, 同上,只有第一次才会生成

  return global_privateDb;
}

QFontDatabase的构造函数中会调用createDatabase (里面调用了initializeDb), initializeDb里调用了privateDb(),一个这个时候是第一次调用, 会生成QFontDatabasePrivate对象

QFontDatabasePrivate中的QDataStream *stream成员才是真正处理qte的字体

你可能感兴趣的:(Qt:解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb))