VirtualBox

Changeset 6440 in vbox for trunk/include/VBox/com


Ignore:
Timestamp:
Jan 22, 2008 1:35:10 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
27415
Message:

Main: Better ComPtrBase::equalsTo() specialization for IUnknown (allows to avoid unnecessary QueryInterface calls which is sensitive for IPC).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/com/ptr.h

    r5999 r6440  
    6262{
    6363protected:
     64
    6465    static void addref (C *p) { p->AddRef(); }
    6566    static void release (C *p) { p->Release(); }
     
    7374{
    7475protected:
     76
    7577    static void addref (C *p) {}
    7678    static void release (C *p) {}
     
    7880
    7981/**
     82 *  Equality operations for the ComPtrBase template.
     83 */
     84template <class C>
     85class ComPtrEqOps
     86{
     87protected:
     88
     89    template <class I>
     90    static bool equals (C *aThis, I *aThat)
     91    {
     92        IUnknown *thatUnk = NULL, *thisUnk = NULL;
     93        if (aThat)
     94            aThat->QueryInterface (COM_IIDOF (IUnknown), (void **) &thatUnk);
     95        if (aThis)
     96            aThis->QueryInterface (COM_IIDOF (IUnknown), (void **) &thisUnk);
     97        bool equal = thisUnk == thatUnk;
     98        if (thisUnk)
     99            thisUnk->Release();
     100        if (thatUnk)
     101            thatUnk->Release();
     102        return equal;
     103    }
     104
     105    /* specialization for IUnknown */
     106    template<>
     107    static bool equals <IUnknown> (C *aThis, IUnknown *aThat)
     108    {
     109        IUnknown *thisUnk = NULL;
     110        if (aThis)
     111            aThis->QueryInterface (COM_IIDOF (IUnknown), (void **) &thisUnk);
     112        bool equal = thisUnk == aThat;
     113        if (thisUnk)
     114            thisUnk->Release();
     115        return equal;
     116    }
     117};
     118
     119/** Specialization for IUnknown */
     120template<>
     121class ComPtrEqOps <IUnknown>
     122{
     123protected:
     124
     125    template <class I>
     126    static bool equals (IUnknown *aThis, I *aThat)
     127    {
     128        IUnknown *thatUnk = NULL;
     129        if (aThat)
     130            aThat->QueryInterface (COM_IIDOF (IUnknown), (void **) &thatUnk);
     131        bool equal = aThis == thatUnk;
     132        if (thatUnk)
     133            thatUnk->Release();
     134        return equal;
     135    }
     136
     137    /* specialization for IUnknown */
     138    template<>
     139    static bool equals <IUnknown> (IUnknown *aThis, IUnknown *aThat)
     140    {
     141        return aThis == aThat;
     142    }
     143};
     144
     145/**
    80146 *  Base template for smart COM pointers. Not intended to be used directly.
    81147 */
    82148template <class C, template <class> class RefOps = ComStrongRef>
    83 class ComPtrBase : protected RefOps <C>
     149class ComPtrBase : protected RefOps <C>, protected ComPtrEqOps <C>
    84150{
    85151public:
    86152
    87     // a special template to disable AddRef()/Release()
     153    /* special template to disable AddRef()/Release() */
    88154    template <class I>
    89     class NoAddRefRelease : public I {
     155    class NoAddRefRelease : public I
     156    {
    90157        private:
    91158#if !defined (VBOX_WITH_XPCOM)
     
    106173    ~ComPtrBase() { release(); }
    107174
    108     ComPtrBase &operator= (const ComPtrBase &that) {
     175    ComPtrBase &operator= (const ComPtrBase &that)
     176    {
    109177        safe_assign (that.p);
    110178        return *this;
    111179    }
    112     ComPtrBase &operator= (C *that_p) {
     180
     181    ComPtrBase &operator= (C *that_p)
     182    {
    113183        safe_assign (that_p);
    114184        return *this;
     
    117187public:
    118188
    119     void setNull() {
     189    void setNull()
     190    {
    120191        release();
    121192        p = NULL;
    122193    }
    123194
    124     bool isNull() const {
     195    bool isNull() const
     196    {
    125197        return (p == NULL);
    126198    }
     199
    127200    bool operator! () const { return isNull(); }
    128201
     
    131204
    132205    template <class I>
    133     bool equalsTo (I *i) const {
    134         IUnknown *this_unk = NULL, *that_unk = NULL;
    135         if (i)
    136             i->QueryInterface (COM_IIDOF (IUnknown), (void**) &that_unk);
    137         if (p)
    138             p->QueryInterface (COM_IIDOF (IUnknown), (void**) &this_unk);
    139         bool equal = this_unk == that_unk;
    140         if (that_unk)
    141             that_unk->Release();
    142         if (this_unk)
    143             this_unk->Release();
    144         return equal;
     206    bool equalsTo (I *aThat) const
     207    {
     208        return equals (p, aThat);
    145209    }
    146210
    147211    template <class OC>
    148     bool equalsTo (const ComPtrBase <OC> &oc) const {
     212    bool equalsTo (const ComPtrBase <OC> &oc) const
     213    {
    149214        return equalsTo ((OC *) oc);
    150215    }
     
    157222     *  pointer).
    158223     */
    159     NoAddRefRelease <C> *operator-> () const {
     224    NoAddRefRelease <C> *operator-> () const
     225    {
    160226        AssertMsg (p, ("Managed pointer must not be null\n"));
    161227        return (NoAddRefRelease <C> *) p;
     
    163229
    164230    template <class I>
    165     HRESULT queryInterfaceTo (I **pp) const {
    166         if (pp) {
    167             if (p) {
    168                 return p->QueryInterface (COM_IIDOF (I), (void**) pp);
    169             } else {
     231    HRESULT queryInterfaceTo (I **pp) const
     232    {
     233        if (pp)
     234        {
     235            if (p)
     236            {
     237                return p->QueryInterface (COM_IIDOF (I), (void **) pp);
     238            }
     239            else
     240            {
    170241                *pp = NULL;
    171242                return S_OK;
    172243            }
    173         } else {
    174             return E_INVALIDARG;
    175244        }
     245
     246        return E_INVALIDARG;
    176247    }
    177248
    178249    /** Intended to pass instances as out parameters to interface methods */
    179     C **asOutParam() {
     250    C **asOutParam()
     251    {
    180252        setNull();
    181253        return &p;
     
    184256private:
    185257
    186     void addref() {
     258    void addref()
     259    {
    187260        if (p)
    188261            RefOps <C>::addref (p);
    189262    }
    190     void release() {
     263
     264    void release()
     265    {
    191266        if (p)
    192267            RefOps <C>::release (p);
    193268    }
    194269
    195     void safe_assign (C *that_p) {
    196         // be aware of self-assignment
     270    void safe_assign (C *that_p)
     271    {
     272        /* be aware of self-assignment */
    197273        if (that_p)
    198274            RefOps <C>::addref (that_p);
     
    219295    ComPtr () : Base() {}
    220296    ComPtr (const ComPtr &that) : Base (that) {}
    221     ComPtr &operator= (const ComPtr &that) {
     297    ComPtr &operator= (const ComPtr &that)
     298    {
    222299        Base::operator= (that);
    223300        return *this;
     
    226303    template <class OI>
    227304    ComPtr (OI *that_p) : Base () { operator= (that_p); }
    228     // specialization for I
     305
     306    /* specialization for I */
    229307    ComPtr (I *that_p) : Base (that_p) {}
    230308
     
    233311
    234312    template <class OI>
    235     ComPtr &operator= (OI *that_p) {
     313    ComPtr &operator= (OI *that_p)
     314    {
    236315        if (that_p)
    237316            that_p->QueryInterface (COM_IIDOF (I), (void **) Base::asOutParam());
     
    240319        return *this;
    241320    }
    242     // specialization for I
    243     ComPtr &operator= (I *that_p) {
     321
     322    /* specialization for I */
     323    ComPtr &operator=(I *that_p)
     324    {
    244325        Base::operator= (that_p);
    245326        return *this;
     
    247328
    248329    template <class OC>
    249     ComPtr &operator= (const ComPtr <OC, RefOps> &oc) {
     330    ComPtr &operator= (const ComPtr <OC, RefOps> &oc)
     331    {
    250332        return operator= ((OC *) oc);
    251333    }
     
    316398            PRUint32 serverID = 0;
    317399            rc = ipcServ->ResolveClientName (serverName, &serverID);
    318             if (SUCCEEDED (rc)) {
     400            if (SUCCEEDED (rc))
     401            {
    319402                nsCOMPtr <ipcIDConnectService> dconServ =
    320403                    do_GetService (IPC_DCONNECTSERVICE_CONTRACTID, &rc);
     
    346429    ComPtr () : Base() {}
    347430    ComPtr (const ComPtr &that) : Base (that) {}
    348     ComPtr &operator= (const ComPtr &that) {
     431    ComPtr &operator= (const ComPtr &that)
     432    {
    349433        Base::operator= (that);
    350434        return *this;
     
    358442
    359443    template <class OI>
    360     ComPtr &operator= (OI *that_p) {
     444    ComPtr &operator= (OI *that_p)
     445    {
    361446        if (that_p)
    362447            that_p->QueryInterface (COM_IIDOF (IUnknown), (void **) Base::asOutParam());
     
    367452
    368453    template <class OC>
    369     ComPtr &operator= (const ComPtr <OC, RefOps> &oc) {
     454    ComPtr &operator= (const ComPtr <OC, RefOps> &oc)
     455    {
    370456        return operator= ((OC *) oc);
    371457    }
     
    390476    ComObjPtr (const ComObjPtr &that) : Base (that) {}
    391477    ComObjPtr (C *that_p) : Base (that_p) {}
    392     ComObjPtr &operator= (const ComObjPtr &that) {
     478
     479    ComObjPtr &operator= (const ComObjPtr &that)
     480    {
    393481        Base::operator= (that);
    394482        return *this;
    395483    }
    396     ComObjPtr &operator= (C *that_p) {
     484
     485    ComObjPtr &operator= (C *that_p)
     486    {
    397487        Base::operator= (that_p);
    398488        return *this;
     
    412502     *  does otherwise.
    413503     */
    414     HRESULT createObject() {
     504    HRESULT createObject()
     505    {
    415506        HRESULT rc;
    416507#if !defined (VBOX_WITH_XPCOM)
    417508#   ifdef VBOX_COM_OUTOFPROC_MODULE
    418509        CComObjectNoLock <C> *obj = new CComObjectNoLock <C>();
    419         if (obj) {
     510        if (obj)
     511        {
    420512            obj->InternalFinalConstructAddRef();
    421513            rc = obj->FinalConstruct();
    422514            obj->InternalFinalConstructRelease();
    423         } else {
     515        }
     516        else
    424517            rc = E_OUTOFMEMORY;
    425         }
    426518#   else
    427519        CComObject <C> *obj = NULL;
     
    430522#else /* !defined (VBOX_WITH_XPCOM) */
    431523        CComObject <C> *obj = new CComObject <C>();
    432         if (obj) {
     524        if (obj)
    433525            rc = obj->FinalConstruct();
    434         } else {
     526        else
    435527            rc = E_OUTOFMEMORY;
    436         }
    437528#endif /* !defined (VBOX_WITH_XPCOM) */
    438529        *this = obj;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette