Read QT5.7 source code (VII) Qmetaobjectprivate

Source: Internet
Author: User
Tags class definition int size relative

Qmetaobjectprivate provides the management of an array of uint types in a Qmetaobject class member, which is a complex structure, so qmetaobjectprivate provides the definition and operation functions of the structure to facilitate its management.



Added ClassInfo method Emum property after MOC generated Qmetaobject.data section


Declaration of the class:

Class Widget:public Qwidget
{
    q_object
    //q_declare_private (Widget) public
:

    q_classinfo (" Widgetinfo "," Infotest ")
    q_property (QString prop1 READ prop1)
    enum Selectionbehavior {
        selectitems,
        Selectrows,
        selectcolumns
    };
    Q_enum (Selectionbehavior)
    q_flags (selectionbehavior)

    q_invokable void Methodtest (QString &,int);
Public:
    Widget (Qwidget *parent = 0);
    ~widget ();
Protected slots:
    void Testslot (QString &str);
Signals:
    void TestSignal1 (QString &str);
    void TestSignal2 (QString &str,int i);
    void TestSignal3 ();
};




Two arrays generated by the corresponding MOC


static const qt_meta_stringdata_widget_t Qt_meta_stringdata_widget = {{qt_moc_literal (0, 0, 6),//"Widget" qt_moc_l Iteral (1, 7, ten),//"Widgetinfo" Qt_moc_literal (2, 8),//"Infotest" Qt_moc_literal (3,, one),//"TestSignal1" qt_m Oc_literal (4, 0),//"" Qt_moc_literal (5, 8),//"qstring&" qt_moc_literal (6,, 3),//"str" qt_moc_literal ( 7, TestSignal2, one),//"Qt_moc_literal" (8, 1),//"I" qt_moc_literal (9,, one),//"TestSignal3" qt_moc_literal (1 0, 8),//"Testslot" qt_moc_literal (one, ten),//"Methodtest" qt_moc_literal (5),//"Prop1" qt_moc_literal ( (Selectionbehavior),//"qt_moc_literal" (+, 123, one),//"Selectitems" qt_moc_literal (all, 135, ten),//"Select Rows "Qt_moc_literal (146, +)//" Selectcolumns "}," Widget\0widgetinfo\0infotest\0testsignal1\0 "" \0qstr Ing&\0str\0testsignal2\0i\0testsignal3\0 "" Testslot\0methodtest\0prop1\0selectionbehavior\0 "" SelectItems\
0selectrows\0selectcolumns "};       #undef qt_moc_literal static const UINT Qt_meta_data_widget[] = {//Content:7,//revision 0, ClassName 1, +,//ClassInfo 5,,//methods 1,,//Properties 1, A,//E Nums/sets 0, 0,//constructors 0,//flags 3,//Signalcount//Classinfo:key, Val
       UE 1, 2,//Signals:name, argc, parameters, tag, flags 3, 1,, 4, 0x06/* public */,  7, 2, 4, 0x06/* public */, 9, 0,, 4, 0x06/* public */,//Slots:name, argc, parameters,    tag, flags, 1, 4, 0x09/* Protected */,//Methods:name, argc, parameters, tag, flags 11, 2, 4, 0x02/* public */,//Signals:parameters qmetatype::void, 0x80000000 | 5, 6, qmetatype::void, 0x80000000 | 5, Qmetatype::int, 6, 8, qmetatype::void,//Slots:parameters qmetatype::void, 0x80000000 | 5, 6,//MethOds:parameters Qmetatype::void, 0x80000000 | 5, Qmetatype::int, 4, 4,//Properties:name, type, flags, qmetatype::qstring, 0x00095001,//Enums:na Me, flags, count, data, 0x1, 3, Data:key,//enum, value, uint (widget::selectitems), 15 , UINT (widget::selectrows), +, uint (widget::selectcolumns), 0//EOD};



1, +,//ClassInfo
5, +,//methods
1, +,//Properties
1, enums/sets,//


The front is the quantity, followed by the relative offset of the data.

Qmetaobjectprivate class Definition:

struct Qmetaobjectprivate {enum {outputrevision = 7};//used by MOC, Qmetaobjectbuilder and Qdbus int Revisio  N    Version number int className;   Class name index int classinfocount, classinfodata;         The number of ClassInfo, and the index, need to have a macro definition will be generated by MOC int methodcount, methoddata;          function number and index, need to have macro definition will be generated by MOC int propertycount, propertydata;       Property quantity and index, need to have macro definition to be generated by MOC int enumeratorcount, enumeratordata; Enumeration member number and index, need to have macro definition will be generated by MOC int constructorcount, constructordata; Since revision 2 constructor number and index, need to have a macro definition will be generated by MOC int flags; Since revision 3 the number of flags int signalcount;  Since revision 4 signal count static inline const qmetaobjectprivate *get (const qmetaobject *metaobject) {return Reinterpret_cast<const qmetaobjectprivate*> (Metaobject->d.data); Manage the static int originalclone (const qmetaobject *obj, int local_) by converting the memory structure of the Qmetaobject data point to the Qmetaobjectprivate pointer

    Method_index); Static Qbytearray decodemethodsignature (const CHAr *signature, Qargumenttypearray &types); static int indexofsignalrelative (const qmetaobject **baseobject, const Qbytearray &AM
    p;name, int argc, const qargumenttype *types); static int indexofslotrelative (const qmetaobject **m, const qbytearray &name, int a
    RGC, const qargumenttype *types); static int indexofsignal (const qmetaobject *m, const qbytearray &name, int argc, const QA
    Rgumenttype *types); static int Indexofslot (const qmetaobject *m, const qbytearray &name, int argc, const Qargum
    Enttype *types); static int Indexofmethod (const qmetaobject *m, const qbytearray &name, int argc, const QA
    Rgumenttype *types); static int indexofconstructor (const qmetaobject *m, const qbyteArray &name, int argc, const qargumenttype *types);
    Q_core_export Static Qmetamethod signal (const qmetaobject *m, int signal_index);
    Q_core_export static int Signaloffset (const qmetaobject *m);
    Q_core_export static int Absolutesignalcount (const qmetaobject *m);
    Q_core_export static int Signalindex (const qmetamethod &m); static bool Checkconnectargs (int signalargc, const qargumenttype *signaltypes, int method
    ARGC, const qargumenttype *methodtypes); static bool Checkconnectargs (const qmetamethodprivate *signal, const qmetamethodprivate *

    method);

Static qlist<qbytearray> parametertypenamesfromsignature (const char *signature);
    #ifndef qt_no_qobject//defined in qobject.cpp enum Disconnecttype {disconnectall, disconnectone}; static void Memberindexes (const qobject *obj, const qmetamethod &member, int *Signalindex, int *methodindex); Static qobjectprivate::connection *connect (const qobject *sender, int signal_index, const qmetaobj ECT *smeta, const qobject *receiver, int method_index_relative, const QMET
    Aobject *rmeta = 0, int type = 0, int *types = 0);
                           static bool Disconnect (const qobject *sender, int signal_index, const qmetaobject *smeta, Const Qobject *receiver, int method_index, void **slot, Disconnecttype =
    Disconnectall); Static inline bool Disconnecthelper (qobjectprivate::connection *c, const qobject * Receiver, int method_index, void **slot, Qmutex *sendermutex, Disconnecttype = Dis
Connectall);
 #endif};






1, static int originalclone (const qmetaobject *obj, int local_method_index); If the given function index is cloned, return the original index


int Qmetaobjectprivate::originalclone (const qmetaobject *mobj, int local_method_index)
{
    Q_assert (Local_ Method_index < Get (mobj)->methodcount);
    int handle = Get (mobj)->methoddata + 5 * LOCAL_METHOD_INDEX;    Handle equals the field offset of the function   each function segment 5 int size while
    (Mobj->d.data[handle + 4] & methodcloned) {
        Q_assert (Local_ Method_index > 0);
        Handle-= 5;
        local_method_index--;                       Traverse back to find the clone of the flags for 0x20
    }
    return local_method_index;
}



2, Qbytearray qmetaobjectprivate::d ecodemethodsignature (const char *signature,qargumenttypearray &types);

Parses the signal function string, populates the parameter into the Qargumenttypearray array, and returns the Qbytearray structure of the Fill function name


Qbytearray qmetaobjectprivate::d ecodemethodsignature (
        const char *signature, Qargumenttypearray &types)
{
    Q_assert (signature! = 0);
    const char *lparens = STRCHR (signature, ' (');
    if (!lparens)
        return Qbytearray ();
    const char *rparens = STRRCHR (lparens + 1, ') ');
    if (!rparens | | * (RPARENS+1))
        return Qbytearray ();
    int namelength = lparens-signature;
    Argumenttypesfromstring (Lparens + 1, rparens, types);    Fills the characters in the function parameter () with the array
    return Qbytearray::fromrawdata (signature, namelength);   Returns the function name block directly as Qbytearray data. Do not copy
}

The struct referenced here Qargumenttype a class that identifies a function parameter

Class Qargumenttype
{public
:
    qargumenttype (int type)
        : _type (Type)
    {}
    Qargumenttype ( Const Qbytearray &name)
        : _type (Qmetatypetypeinternal (Name.constdata ())), _name (name)
    {}
    Qargumenttype ()
        : _type (0)
    {} ..... ...

.. .............
.............................. Skip
private:
    int _type;
    Qbytearray _name;
};


typedef qvarlengtharray<qargumenttype, 10> Qargumenttypearray; Supports up to 10 parameters



Argumenttypesfromstring parse a comma-delimited list of arguments, populate qargumenttypes and add to the Qargumenttypearray array

Parses a string of comma-separated types into qargumenttypes.
No Normalization of the type names is performed.
static void argumenttypesfromstring (const char *STR, const char *end,
                                    qargumenttypearray &types)
{
    q_ ASSERT (str <= end);
    while (str! = end) {
        if (!types.isempty ()))
            ++str;//Skip comma
        const char *begin = str;
        int level = 0;
        while (str! = end && (Level > 0 | | *str! = ', ')) {
            if (*str = = ' < ')
                ++level;
            else if (*str = = ' > ')
                --level;
            ++str;
        }
        Types + = Qargumenttype (Qbytearray (begin, Str-begin));}
}



3, static int indexofsignalrelative (const qmetaobject **baseobject,
Const Qbytearray &name, int argc,
Const Qargumenttype *types);


Returns the index value of the signal relative to the array of this class of signals


int qmetaobjectprivate::indexofsignalrelative (const qmetaobject **baseobject,  Const Qbytearray &name, int argc, const qargumenttype   *types) {int i = indexofmethodrelative<methodsignal> (baseobject, name, argc, types);
    Call the global template function indexofmethodrelative to get #ifndef qt_no_debug const Qmetaobject *m = *baseobject; if (i >= 0 && m && m->d.superdata) {int conflict = Indexofmethod (M->d.superdata, name, a
        RGC, types);
            if (conflict >= 0) {Qmetamethod Conflictmethod = m->d.superdata->method (conflict); Qwarning ("Qmetaobject::indexofsignal:signal%s from%s redefined in%s", Conflictmethod.methodsignat    Ure (). Constdata (), Objectclassname (M->d.superdata), Objectclassname (m));
If the parent class defines the same signal, the output warning}} #endif return i; }

Template<int methodtype> static inline int indexofmethodrelative (const qmetaobject **baseobject, Const Qbytearray &name, int argc, const qargumenttype *ty PES) {for (const qmetaobject *m = *baseobject; m; m = m->d.superdata) {Q_assert (Priv (m->d.data)->re
        Vision >= 7); int i = (Methodtype = = methodsignal)?  (Priv (M->d.data)->signalcount-1): (Priv (M->d.data)->methodcount-1); The index that returns that function is determined by the template parameter  methodtype.
                        signal or const int end = (Methodtype = = Methodslot) slot or member function ?

        (Priv (M->d.data)->signalcount): 0;
            for (; I >= end;-I.) {int handle = PRIV (m->d.data)->methoddata + 5*i; if (Methodmatch (m, handle, name, argc, types)) {Methodmatch matches the number of class name function name function parameters *baseobje
        ct = m;        return i;
}}} return-1; }
Priv (X) is an inline function that converts qmetaobject::d ata from uint* to qmetaobjectprivate type pointer


Static inline q_decl_unused const qmetaobjectprivate *PRIV (const uint* data)
{return reinterpret_cast<const qmetaobjectprivate*> (data);}


4. Indexofslotrelative returns the index of the slot in its group

int qmetaobjectprivate::indexofslotrelative (const qmetaobject **m,
                                            const qbytearray &name, int argc,
                                            Const Qargumenttype *types)
{
    return indexofmethodrelative<methodslot> (M, name, argc, types);
}


5, Indexofsignal Indexofslot Indexofmethod returns the signal Slot member function Index These indexes are the entire (inherited and private) large array of indexes including their grandfather classes


Indexofconstructor

int qmetaobjectprivate::indexofsignal (const qmetaobject *m, const Qbytearray &name,   int argc, const qargumenttype *types) {int i = indexofsignalrelative (&m, name, argc, types);    Returns the index in its own array if (I >= 0) i + = M->methodoffset ();
Returns the amount of the inherited function that is placed in the Grandfather class Staticmetaobject return i;
                                    } int Qmetaobjectprivate::indexofslot (const qmetaobject *m, const Qbytearray &name,
    int argc, const qargumenttype *types) {int i = indexofslotrelative (&m, name, argc, types);
    if (i >= 0) i + = M->methodoffset ();
return i;
                                      } int Qmetaobjectprivate::indexofmethod (const qmetaobject *m, const Qbytearray &name,
    int argc, const qargumenttype *types) {int i = indexofmethodrelative<0> (&m, name, argc, types);
    if (i >= 0) i + = M->methodoffset ();
return i; } int Qmetaobjectprivate::indexofconstructor (const QMetaObject *m, const qbytearray &name, int argc, const qargumenttype *types ) {for (int i = PRIV (m->d.data)->constructorcount-1; i >= 0;-i) {int handle = PRIV (m->d.data)-
        >constructordata + 5*i;
    if (Methodmatch (m, handle, name, argc, types)) return i;
} return-1; }


6, Qbytearray qmetaobjectprivate::d ecodemethodsignature Pass a string of signal function, fill the parameter into Qargumenttypearray, Populates the function name with Qbytearray and returns



Qbytearray qmetaobjectprivate::d ecodemethodsignature (
        const char *signature, Qargumenttypearray &types)
{
    Q_assert (signature! = 0);
    const char *lparens = STRCHR (signature, ' (');
    if (!lparens)
        return Qbytearray ();
    const char *rparens = STRRCHR (lparens + 1, ') ');
    if (!rparens | | * (RPARENS+1))
        return Qbytearray ();
    int namelength = lparens-signature;
    Argumenttypesfromstring (Lparens + 1, rparens, types);
    Return Qbytearray::fromrawdata (signature, namelength);
}


7, Qmetamethod qmetaobjectprivate::signal The index of the transmitted signal returns the Qmetamethod object marked with the Signal function


Qmetamethod qmetaobjectprivate::signal (const qmetaobject *m, int signal_index)
{
    qmetamethod result;
    if (Signal_index < 0)
        return result;
    Q_assert (M! = 0);
    int i = Signal_index;
    I-= Signaloffset (m);
    if (I < 0 && m->d.superdata)
        return signal (M->d.superdata, signal_index);

    if (i >= 0 && i < priv (m->d.data)->signalcount) {
        result.mobj = m;
        Result.handle = Priv (m->d.data)->methoddata + 5*i;
    }
    return result;
}


8, int qmetaobjectprivate::absolutesignalcount (const qmetaobject *m) Returns the number of signals including the signal of its base class

int Qmetaobjectprivate::absolutesignalcount (const qmetaobject *m)
{
    Q_assert (M! = 0);
    int n = priv (m->d.data)->signalcount;
    for (M = m->d.superdata; m; m = m->d.superdata)
        n + = Priv (m->d.data)->signalcount;
    return n;
}


9, bool Qmetaobjectprivate::checkconnectargs (int signalargc, const qargumenttype *signaltypes,
int METHODARGC, const qargumenttype *methodtypes)


BOOL Qmetaobjectprivate::checkconnectargs (const qmetamethodprivate *signal,
Const Qmetamethodprivate *method)


Whether the detection signal matches the function parameter to be linked



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.