The ATL component simulates multiple interface implementations and internal class implementation multiple interfaces

Source: Internet
Author: User

For components generated by ATL, it is easy to implement multiple interfaces (whether through methods that inherit multiple interfaces or through internal classes). How can this problem be achieved, the key is to manage the life cycle and creation of components, and support for multi-interface queries by components.

The following figure shows the simulated classes that implement multiple interface components for multiple inheritance and internal classes:

 

The source code is as follows:

# Include "stdafx. H"
# Include "wtypes. H"
# Include "objbase. H"
# Include "initguid. H"
// {84296355-694e-4869-84a9-cae4e91f96f1}
Define_guid (iid_ix,
0x84296355, 0x694e, 0x4869, 0x84, 0xa9, 0xca, 0xe4, 0xe9, 0x1f, 0x96, 0xf1 );

// {08c05339-ae03-4265-9a1a-0fdb05b7a289}
Define_guid (iid_iy,
0x8c05339, 0xae03, 0x4255, 0x9a, 0x1a, 0xf, 0xdb, 0x5, 0xb7, 0xa2, 0x89 );

// {AC1164FA-9496-4003-88FB-10CF6160888E}
Define_guid (iid_ideal,
0xac1164fa, 0x9496, 0x4003, 0x88, 0xfb, 0x10, 0xcf, 0x61, 0x60, 0x88, 0x8e );

// {6a854410-f80c-4e05-a42f-ef9031448e2a}
Define_guid (iid_ydeal,
0x6a854410, 0xf80c, 0x4e05, 0xa4, 0x2f, 0xef, 0x90, 0x31, 0x44, 0x8e, 0x2a );

 

// The following is part of the template code

Inline bool cequalguid (IID * rguid1, IID * rguid2)
{
Return (
(Plong) & rguid1) [0] = (plong) & rguid2) [0] &
(Plong) & rguid1) [1] = (plong) & rguid2) [1] &
(Plong) & rguid1) [2] = (plong) & rguid2) [2] &
(Plong) & rguid1) [3] = (plong) & rguid2) [3]);
}

// ## Modelid = 474bbb28029f
Template <class T>
Class cxx
{
// ## Modelid = 474bbb2802a1
T * P;
Public:
// ## Modelid = 474bbb2802af
Cxx ()
{
P = NULL;
}
// ## Modelid = 474bbb2802b0
Cxx (T * GMM)
{
P = GMM;
P-> addref ();

}
// ## Modelid = 474bbb2802bf
~ Cxx (){
If (P)
P-> release ();
}

// ## Modelid = 474bbb2802c0
T * operator-> (void ){
If (P)
Return P;
Else
Return NULL;
}
};

 

// The template class ends.
//////////////////////////////////////// //////////////////////////////////
// ## Modelid = 474bbb2802ce
Class IU
{
Public:
// ## Modelid = 474bbb2802cf
Virtual ulong addref () = 0;
// ## Modelid = 474bbb2802df
Virtual ulong release () = 0;
// ## Modelid = 474bbb2802e1
Virtual ulong QueryInterface (IID * IID, void ** pthis) = 0;
};

//////////////////////////////////////// //////////////////////////////////
// Multiple inheritance classes of the template
// Create a class
// ## Modelid = 474bbb2802fd
Template <class base>
Class coobject: public Base
{
// ## Modelid = 474bbb28030d
Ulong m_cbref;
Public:
// ## Modelid = 474bbb28030e
Virtual ulong addref ()
{
M_cbref ++;
Return m_cbref;
}
// ## Modelid = 474bbb280310
Virtual ulong release ()
{
M_cbref --;
If (m_cbref = 0)
Delete this;
Return m_cbref;
}
// ## Modelid = 474bbb28031c
Virtual ulong QueryInterface (IID * IID, void ** ppvoid)
{
Cc_map * pentry = getentry ();
Int dd;
IID * guid;
IU * P = NULL;
While (pentry-> IID! = NULL)
{
// Guid = pentry-> IID;
If (memcmp (void *) pentry-> IID, (void *) IID, sizeof (guid) = 0)
// If (cequalguid (IID *) pentry-> IID, IID ))
// If (pentry-> IID = IID)
{
P = (IU *) (INT) This + pentry-> m_dwoffset );
P-> addref ();

Break;
}
Pentry ++;
}
If (P)
* Ppvoid = P;
Return 0;
}
// ## Modelid = 474bbb280320
Coobject ()
{
M_cbref = 0;
}
// Static ulong createinstance (refiid riid, void ** ppvoid)
//{
// * Ppvoid = new base ();
// Return 0;
//}
};
// ## Modelid = 474bbb28032c
Template <class base>
Class coxcreate
{
Public:
// ## Modelid = 474bbb28033d
Static ulong createinstance (IID * IID, void ** ppvoid)
{
Base * P = new base ();
/* P-> addref ();*/
P-> QueryInterface (IID, ppvoid );
// If (IID = iid_ideal)
// * Ppvoid = P;
// Else if (IID = iid_iydeal)
// * Ppvoid = P;
Return 0;
}
};
// ## Modelid = 474bbb28034b
Struct cc_map
{
// ## Modelid = 474bbb28035c
Const IID * IID;
// ## Modelid = 474bbb280360
DWORD m_dwoffset;
};
# Define declare_cc_xx (x) Public :/
Typedef coxcreate <coobject <x> xobj;

# Define catchiid (x) IID _ # x
# Define catchclass (x) m_p # x
# Define offsetof (base, x) (DWORD) (static_cast <base *> (x *) 8)-8)
# Define offsetofin (S, m) (size_t) & (S *) 0)-> m)
# Define begin_cc_inter ()/
Public :/
Virtual ulong addref () = 0 ;/
Virtual ulong release () = 0 ;/
Virtual ulong QueryInterface (IID * IID, void ** ppvoid) = 0;

# Define begin_dd_inter (X )/
Typedef X _ ccclass ;/
Public :/
Cc_map * getentry (){/
Static struct cc_map entry [] = {

# Define end_dd_inter ()/
{Null, 0 }}; return entry ;}

# Define dd_inter (XX )/
{& Catchiid (XX), offsetof (XX, _ ccclass )},

# Define ddi_inter (XX, YY )/
{& Catchiid (XX), offsetofin (_ ccclass, catchclass (yy ))},

# Define method_ee (x) virtual ulong x
# Define begin_ee_inter (x, base )/
Public :/
Class X: public base/
{/
Method_ee (addref ());/
Method_ee (release ());/
Method_ee (QueryInterface (IID *, void **));
# Define end_ee_inter (x )};/
X catchclass (X );

 

// ## Modelid = 474bbb28036b
Template <class tbase>
Class create
{
Public:
// ## Modelid = 474bbb280399
Typedef tbase _ tclass;
 
// ## Modelid = 474bbb28037b
Static ulong createinstance (IID * IID, void ** ppvoid)
{
/* _ Tclass * P ;*/
// P = new _ tclass ();
// * Ppvoid = P;
// P-> addref ();
Tbase: xobj: createinstance (IID, ppvoid );
// P = (_ tclass *) (* ppvoid );
// P-> addref ();
Return 0;
}

};

// ## Modelid = 474bbb2803a9
Class ideal: Public IU
{
Public:
// ## Modelid = 474bbb2803ab
Virtual ulong H1 () = 0;
};
// ## Modelid = 474bbb2803b9
Class ydeal: Public IU
{
Public:
// ## Modelid = 474bbb2803c9
Virtual ulong HH () = 0;
};
// ## Modelid = 474bbb2803d8
Class cxdeal:/* Public refcount ,*/
Public create <cxdeal>,
Public ideal
 
{
Public:
// ## Modelid = 474bbb2900ec
Cxdeal (){}
// ## Modelid = 474bbb2900ed
Virtual ulong H1 ()
{
Printf ("hello ");
Return 0;
}
Declare_cc_xx (cxdeal );
// ## Modelid = 474bbb2900ef
Begin_cc_inter ();

Begin_dd_inter (cxdeal)
Dd_inter (ideal)
End_dd_inter ()
};

// ## Modelid = 474bbb2900fa
Class cydeal:/* Public refcount ,*/
Public create <cydeal>,
Public ydeal
{
Public:
// ## Modelid = 474bbb29011b
Cydeal (){}
// ## Modelid = 474bbb29011c
Virtual ulong HH (){
Printf ("hello ");
Return 0;
}
Declare_cc_xx (cydeal );
// ## Modelid = 474bbb29011e
Begin_cc_inter ();

Begin_dd_inter (cydeal)
Dd_inter (ydeal)
End_dd_inter ()
};

// Support for multiple inheritance
// ## Modelid = 474bbb290128
Class cxydeal:/* Public refcount ,*/
Public create <cxydeal>,
Public ydeal,
Public ideal
{
Public:
// ## Modelid = 474bbb29014b
Cxydeal (){}
// ## Modelid = 474bbb29014c
Virtual ulong HH (){
Printf ("hello ");
Return 0;
}
// ## Modelid = 474bbb29014e
Virtual ulong H1 ()
{
Printf ("hello ");
Return 0;
}
Declare_cc_xx (cxydeal );
// ## Modelid = 474bbb290150
Begin_cc_inter ();

Begin_dd_inter (cxydeal)
Dd_inter (ideal)
Dd_inter (ydeal)
End_dd_inter ()
};

// Internal class support
// ## Modelid = 474bbb290157
Class cixydeal: Public create <cixydeal>
{
// ## Modelid = 474bbb290178
Declare_cc_xx (cxydeal );

// ## Modelid = 474bbb29017c
Begin_cc_inter ();

// Internal Interface Support
Begin_ee_inter (ideal, ideal)
Method_ee (H1 )();
End_ee_inter (ideal)
Begin_ee_inter (ydeal, ydeal)
Method_ee (HH )();
End_ee_inter (ydeal)

Begin_dd_inter (cixydeal)
Ddi_inter (ideal, ideal)
Ddi_inter (ydeal, ydeal)
End_dd_inter ()
};
//
Ulong cixydeal: Ideal: H1 ()
{
Printf ("hello ");
Return 0;
}
Ulong cixydeal: Ideal: addref ()
{
Return this-> addref ();
Return 0;
}
Ulong cixydeal: Ideal: release ()
{
Return this-> release ();
Return 0;
}
Ulong cixydeal: Ideal: QueryInterface (IID * IID, void ** pthis)
{
Return this-> QueryInterface (IID, pthis );
Return 0;
}

Ulong cixydeal: ydeal: hh ()
{
Printf ("hello ");
Return 0;
}
Ulong cixydeal: ydeal: addref ()
{
Return this-> addref ();
Return 0;
}
Ulong cixydeal: ydeal: release ()
{
Return this-> release ();
Return 0;
}
Ulong cixydeal: ydeal: QueryInterface (IID * IID, void ** pthis)
{
Return this-> QueryInterface (IID, pthis );
Return 0;
}
Void test3 ()
{
Guid;
Cxdeal * P;
Guid = iid_ideal;
Cxdeal: createinstance (& guid, (void **) & P );
P-> H1 ();
P-> release ();
Cxx <cxdeal> PP;
Guid = iid_ideal;
Cxdeal: createinstance (& guid, (void **) & PP );
PP-> H1 ();

Cxx <cydeal> py;
Guid = iid_ydeal;
Cydeal: createinstance (& guid, (void **) & Py );
Py-> hh ();

Cxx <ideal> PI;
Guid = iid_ideal;
Cxydeal: createinstance (& guid, (void **) & PI );
Pi-> H1 ();
Cxx <ydeal> PPy;
Guid = iid_ydeal;
Pi-> QueryInterface (& guid, (void **) & PPy );
PPy-> hh ();

Guid = iid_ideal;
Cxx <ideal> II;
Cixydeal: createinstance (& guid, (void **) & II );
II-> H1 ();
Cxx <ydeal> Iy;
Guid = iid_ydeal;
II-> QueryInterface (& guid, (void **) & Iy );
Iy-> hh ();
}
// Multi-inheritance class structure of the template
//////////////////////////////////////// //////////////////////////////////
Int main (INT argc, char * argv [])
{
// Multi-inheritance, inherited from the template
Test3 ();

Return 0;
}

 

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.