VirtualBox

Changeset 27607 in vbox for trunk/include


Ignore:
Timestamp:
Mar 22, 2010 6:13:07 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
59148
Message:

Main: remove templates for 'weak' com pointers which do nothing anyway

File:
1 edited

Legend:

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

    r23223 r27607  
    7373    void LogRef(const char *pcszFormat, ...);
    7474}
    75 
    76 /**
    77  *  Strong referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>.
    78  */
    79 template <class C>
    80 class ComStrongRef
    81 {
    82 protected:
    83 
    84     static void addref(C *p)
    85     {
    86         p->AddRef();
    87     }
    88     static void release(C *p)
    89     {
    90         p->Release();
    91     }
    92 };
    93 
    94 /**
    95  *  Weak referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>.
    96  */
    97 template <class C>
    98 class ComWeakRef
    99 {
    100 protected:
    101 
    102     static void addref(C * /* p */) {}
    103     static void release(C * /* p */) {}
    104 };
    10575
    10676/**
     
    169139 *  Base template for smart COM pointers. Not intended to be used directly.
    170140 */
    171 template <class C, template <class> class RefOps = ComStrongRef>
    172 class ComPtrBase : protected RefOps <C>
     141template <class C>
     142class ComPtrBase
    173143{
    174144public:
     
    190160protected:
    191161
    192     ComPtrBase () : p (NULL) {}
    193     ComPtrBase (const ComPtrBase &that) : p (that.p) { addref(); }
    194     ComPtrBase (C *that_p) : p (that_p) { addref(); }
    195 
    196     ~ComPtrBase() { release(); }
    197 
    198     ComPtrBase &operator= (const ComPtrBase &that)
    199     {
    200         safe_assign (that.p);
    201         return *this;
    202     }
    203 
    204     ComPtrBase &operator= (C *that_p)
    205     {
    206         safe_assign (that_p);
     162    ComPtrBase()
     163        : p(NULL)
     164    {}
     165
     166    ComPtrBase(const ComPtrBase &that)
     167        : p(that.p)
     168    {
     169        addref();
     170    }
     171
     172    ComPtrBase(C *that_p)
     173        : p(that_p)
     174    {
     175        addref();
     176    }
     177
     178    ~ComPtrBase()
     179    {
     180        release();
     181    }
     182
     183    ComPtrBase &operator=(const ComPtrBase &that)
     184    {
     185        safe_assign(that.p);
     186        return *this;
     187    }
     188
     189    ComPtrBase &operator=(C *that_p)
     190    {
     191        safe_assign(that_p);
    207192        return *this;
    208193    }
     
    221206    }
    222207
    223     bool operator! () const { return isNull(); }
    224 
    225     bool operator< (C* that_p) const { return p < that_p; }
    226     bool operator== (C* that_p) const { return p == that_p; }
     208    bool operator!() const { return isNull(); }
     209
     210    bool operator<(C* that_p) const { return p < that_p; }
     211    bool operator==(C* that_p) const { return p == that_p; }
    227212
    228213    template <class I>
    229     bool equalsTo (I *aThat) const
    230     {
    231         return ComPtrEquals (p, aThat);
     214    bool equalsTo(I *aThat) const
     215    {
     216        return ComPtrEquals(p, aThat);
    232217    }
    233218
    234219    template <class OC>
    235     bool equalsTo (const ComPtrBase <OC> &oc) const
    236     {
    237         return equalsTo ((OC *) oc);
     220    bool equalsTo(const ComPtrBase <OC> &oc) const
     221    {
     222        return equalsTo((OC*)oc);
    238223    }
    239224
    240225    /** Intended to pass instances as in parameters to interface methods */
    241     operator C* () const { return p; }
     226    operator C*() const { return p; }
    242227
    243228    /**
     
    245230     *  pointer).
    246231     */
    247     NoAddRefRelease <C> *operator-> () const
    248     {
    249         AssertMsg (p, ("Managed pointer must not be null\n"));
    250         return (NoAddRefRelease <C> *) p;
     232    NoAddRefRelease<C>* operator->() const
     233    {
     234        AssertMsg(p, ("Managed pointer must not be null\n"));
     235        return (NoAddRefRelease<C>*)p;
    251236    }
    252237
    253238    template <class I>
    254     HRESULT queryInterfaceTo (I **pp) const
     239    HRESULT queryInterfaceTo(I **pp) const
    255240    {
    256241        if (pp)
     
    258243            if (p)
    259244            {
    260                 return p->QueryInterface (COM_IIDOF (I), (void **) pp);
     245                return p->QueryInterface(COM_IIDOF(I), (void**)pp);
    261246            }
    262247            else
     
    282267    {
    283268        if (p)
    284             RefOps <C>::addref (p);
     269            p->AddRef();
    285270    }
    286271
     
    288273    {
    289274        if (p)
    290             RefOps <C>::release (p);
     275            p->Release();
    291276    }
    292277
     
    295280        /* be aware of self-assignment */
    296281        if (that_p)
    297             RefOps <C>::addref (that_p);
     282            that_p->AddRef();
    298283        release();
    299284        p = that_p;
     
    309294 *  @param I    COM interface class
    310295 */
    311 template <class I, template <class> class RefOps = ComStrongRef>
    312 class ComPtr : public ComPtrBase <I, RefOps>
    313 {
    314     typedef ComPtrBase <I, RefOps> Base;
     296template <class I>
     297class ComPtr : public ComPtrBase<I>
     298{
     299    typedef ComPtrBase<I> Base;
    315300
    316301public:
    317302
    318     ComPtr () : Base() {}
    319     ComPtr (const ComPtr &that) : Base (that) {}
    320     ComPtr &operator= (const ComPtr &that)
     303    ComPtr() : Base() {}
     304    ComPtr(const ComPtr &that) : Base(that) {}
     305    ComPtr& operator=(const ComPtr &that)
    321306    {
    322307        Base::operator= (that);
     
    325310
    326311    template <class OI>
    327     ComPtr (OI *that_p) : Base () { operator= (that_p); }
     312    ComPtr(OI *that_p) : Base() { operator=(that_p); }
    328313
    329314    /* specialization for I */
    330     ComPtr (I *that_p) : Base (that_p) {}
     315    ComPtr(I *that_p) : Base(that_p) {}
    331316
    332317    template <class OC>
    333     ComPtr (const ComPtr <OC, RefOps> &oc) : Base () { operator= ((OC *) oc); }
     318    ComPtr(const ComPtr<OC> &oc) : Base() { operator=((OC*)oc); }
    334319
    335320    template <class OI>
    336     ComPtr &operator= (OI *that_p)
     321    ComPtr &operator=(OI *that_p)
    337322    {
    338323        if (that_p)
    339             that_p->QueryInterface (COM_IIDOF (I), (void **) Base::asOutParam());
     324            that_p->QueryInterface(COM_IIDOF(I), (void**)Base::asOutParam());
    340325        else
    341326            Base::setNull();
     
    346331    ComPtr &operator=(I *that_p)
    347332    {
    348         Base::operator= (that_p);
     333        Base::operator=(that_p);
    349334        return *this;
    350335    }
    351336
    352337    template <class OC>
    353     ComPtr &operator= (const ComPtr <OC, RefOps> &oc)
    354     {
    355         return operator= ((OC *) oc);
     338    ComPtr &operator=(const ComPtr<OC> &oc)
     339    {
     340        return operator=((OC*)oc);
    356341    }
    357342
     
    365350        I *obj = NULL;
    366351#if !defined (VBOX_WITH_XPCOM)
    367         rc = CoCreateInstance (clsid, NULL, CLSCTX_INPROC_SERVER, _ATL_IIDOF (I),
    368                                (void **) &obj);
     352        rc = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, _ATL_IIDOF(I),
     353                              (void**)&obj);
    369354#else /* !defined (VBOX_WITH_XPCOM) */
    370         nsCOMPtr <nsIComponentManager> manager;
    371         rc = NS_GetComponentManager (getter_AddRefs (manager));
    372         if (SUCCEEDED (rc))
    373             rc = manager->CreateInstance (clsid, nsnull, NS_GET_IID (I),
    374                                           (void **) &obj);
     355        nsCOMPtr<nsIComponentManager> manager;
     356        rc = NS_GetComponentManager(getter_AddRefs(manager));
     357        if (SUCCEEDED(rc))
     358            rc = manager->CreateInstance(clsid, nsnull, NS_GET_IID(I),
     359                                         (void **) &obj);
    375360#endif /* !defined (VBOX_WITH_XPCOM) */
    376361        *this = obj;
    377         if (SUCCEEDED (rc))
     362        if (SUCCEEDED(rc))
    378363            obj->Release();
    379364        return rc;
     
    389374     *  method is fully equivalent to #createInprocObject() for now.
    390375     */
    391     HRESULT createLocalObject (const CLSID &clsid)
     376    HRESULT createLocalObject(const CLSID &clsid)
    392377    {
    393378#if !defined (VBOX_WITH_XPCOM)
    394379        HRESULT rc;
    395380        I *obj = NULL;
    396         rc = CoCreateInstance (clsid, NULL, CLSCTX_LOCAL_SERVER, _ATL_IIDOF (I),
    397                                (void **) &obj);
     381        rc = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, _ATL_IIDOF(I),
     382                              (void**)&obj);
    398383        *this = obj;
    399         if (SUCCEEDED (rc))
     384        if (SUCCEEDED(rc))
    400385            obj->Release();
    401386        return rc;
    402387#else /* !defined (VBOX_WITH_XPCOM) */
    403         return createInprocObject (clsid);
     388        return createInprocObject(clsid);
    404389#endif /* !defined (VBOX_WITH_XPCOM) */
    405390    }
     
    412397     *  @param serverName   Name of the server to create an object within.
    413398     */
    414     HRESULT createObjectOnServer (const CLSID &clsid, const char *serverName)
     399    HRESULT createObjectOnServer(const CLSID &clsid, const char *serverName)
    415400    {
    416401        HRESULT rc;
    417402        I *obj = NULL;
    418         nsCOMPtr <ipcIService> ipcServ = do_GetService (IPC_SERVICE_CONTRACTID, &rc);
    419         if (SUCCEEDED (rc))
     403        nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc);
     404        if (SUCCEEDED(rc))
    420405        {
    421406            PRUint32 serverID = 0;
    422             rc = ipcServ->ResolveClientName (serverName, &serverID);
     407            rc = ipcServ->ResolveClientName(serverName, &serverID);
    423408            if (SUCCEEDED (rc))
    424409            {
    425                 nsCOMPtr <ipcIDConnectService> dconServ =
    426                     do_GetService (IPC_DCONNECTSERVICE_CONTRACTID, &rc);
    427                 if (SUCCEEDED (rc))
    428                     rc = dconServ->CreateInstance (serverID, clsid, NS_GET_IID (I),
    429                                                    (void **) &obj);
     410                nsCOMPtr<ipcIDConnectService> dconServ = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc);
     411                if (SUCCEEDED(rc))
     412                    rc = dconServ->CreateInstance(serverID, clsid, NS_GET_IID(I),
     413                                                  (void**)&obj);
    430414            }
    431415        }
    432416        *this = obj;
    433         if (SUCCEEDED (rc))
     417        if (SUCCEEDED(rc))
    434418            obj->Release();
    435419        return rc;
     
    443427 *  another interface pointer disregarding its type.
    444428 */
    445 template <template <class> class RefOps>
    446 class ComPtr <IUnknown, RefOps> : public ComPtrBase <IUnknown, RefOps>
    447 {
    448     typedef ComPtrBase <IUnknown, RefOps> Base;
     429template<>
     430class ComPtr<IUnknown> : public ComPtrBase<IUnknown>
     431{
     432    typedef ComPtrBase<IUnknown> Base;
    449433
    450434public:
    451435
    452     ComPtr () : Base() {}
    453     ComPtr (const ComPtr &that) : Base (that) {}
    454     ComPtr &operator= (const ComPtr &that)
    455     {
    456         Base::operator= (that);
     436    ComPtr() : Base() {}
     437    ComPtr(const ComPtr &that) : Base (that) {}
     438    ComPtr& operator=(const ComPtr &that)
     439    {
     440        Base::operator=(that);
    457441        return *this;
    458442    }
    459443
    460444    template <class OI>
    461     ComPtr (OI *that_p) : Base () { operator= (that_p); }
     445    ComPtr(OI *that_p) : Base() { operator=(that_p); }
    462446
    463447    template <class OC>
    464     ComPtr (const ComPtr <OC, RefOps> &oc) : Base () { operator= ((OC *) oc); }
     448    ComPtr(const ComPtr<OC> &oc) : Base() { operator=((OC*)oc); }
    465449
    466450    template <class OI>
    467     ComPtr &operator= (OI *that_p)
     451    ComPtr &operator=(OI *that_p)
    468452    {
    469453        if (that_p)
    470             that_p->QueryInterface (COM_IIDOF (IUnknown), (void **) Base::asOutParam());
     454            that_p->QueryInterface(COM_IIDOF(IUnknown), (void**)Base::asOutParam());
    471455        else
    472456            Base::setNull();
     
    475459
    476460    template <class OC>
    477     ComPtr &operator= (const ComPtr <OC, RefOps> &oc)
    478     {
    479         return operator= ((OC *) oc);
     461    ComPtr &operator=(const ComPtr<OC> &oc)
     462    {
     463        return operator=((OC*)oc);
    480464    }
    481465};
     
    489473 *  @param C    class that implements some COM interface
    490474 */
    491 template <class C, template <class> class RefOps = ComStrongRef>
    492 class ComObjPtr : public ComPtrBase <C, RefOps>
    493 {
    494     typedef ComPtrBase <C, RefOps> Base;
     475template <class C>
     476class ComObjPtr : public ComPtrBase<C>
     477{
     478    typedef ComPtrBase<C> Base;
    495479
    496480public:
    497481
    498     ComObjPtr () : Base() {}
    499     ComObjPtr (const ComObjPtr &that) : Base (that) {}
    500     ComObjPtr (C *that_p) : Base (that_p) {}
    501 
    502     ComObjPtr &operator= (const ComObjPtr &that)
    503     {
    504         Base::operator= (that);
    505         return *this;
    506     }
    507 
    508     ComObjPtr &operator= (C *that_p)
    509     {
    510         Base::operator= (that_p);
     482    ComObjPtr() : Base() {}
     483    ComObjPtr(const ComObjPtr &that) : Base(that) {}
     484    ComObjPtr(C *that_p) : Base(that_p) {}
     485
     486    ComObjPtr& operator=(const ComObjPtr &that)
     487    {
     488        Base::operator=(that);
     489        return *this;
     490    }
     491
     492    ComObjPtr& operator=(C *that_p)
     493    {
     494        Base::operator=(that_p);
    511495        return *this;
    512496    }
     
    530514#if !defined (VBOX_WITH_XPCOM)
    531515#   ifdef VBOX_COM_OUTOFPROC_MODULE
    532         CComObjectNoLock <C> *obj = new CComObjectNoLock <C>();
     516        CComObjectNoLock<C> *obj = new CComObjectNoLock<C>();
    533517        if (obj)
    534518        {
     
    540524            rc = E_OUTOFMEMORY;
    541525#   else
    542         CComObject <C> *obj = NULL;
    543         rc = CComObject <C>::CreateInstance (&obj);
     526        CComObject<C> *obj = NULL;
     527        rc = CComObject<C>::CreateInstance(&obj);
    544528#   endif
    545529#else /* !defined (VBOX_WITH_XPCOM) */
    546         CComObject <C> *obj = new CComObject <C>();
     530        CComObject<C> *obj = new CComObject<C>();
    547531        if (obj)
    548532            rc = obj->FinalConstruct();
Note: See TracChangeset for help on using the changeset viewer.

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