Home > Support > SmartPointerMethods

Methods of HnxGC Smart Pointers

You can use '.' operator to access methods of HnxGC smart pointers and use '->" operator to access methods of the object that the smart pointer points to. For example, in the following codes, smart pointer 'pObj' points to an object. `pObj._GetNativePtr()' accesses the method of HnxGC smart pointer, while `pObj->xxx()' accesses the method of the object it points to.

#include "hnxGC.h"

class CFoo {
public:
    void SetFoo();
};

int main()
{
    gcptr<CFoo> pObj = gcnew CFoo;

    CFoo * pRawObj = pObj._GetNativePtr();

    pObj->SetFoo(); // the same as: pRawObj->SetFoo();

    return 0;
}
Below are descriptions of methods of HnxGC smart pointers.

_GetNativePtr

T * _GetNativePtr() const throw();

Return the native type of pointer (the traditional C/C++ type pointer) from the smart pointer.

Applies To: CWeakPtr<>, CReturnPtr<>, CLockedPtr<>, CMemberPtr<>, CReferencePtr<>, gcptr<>::s / g / sg / gs / m / mm / ret / out / in / inout / weak / flex

Duplicate

CWeakPtr<T> Duplicate() const throw();

Return a duplicated reference to the same object that the smart pointer points to. The reference in the original smart pointer does not change.

Applies To: CLockedPtr<>, CMemberPtr<>, CReferencePtr<>, gcptr<>::s / g / sg / gs / m / mm / out / in / inout / flex

Export

CReturnPtr<T> Export() throw();

Move the reference out of the smart pointer, and return it to the caller of the method. The value of the smart pointer is cleared (reset to nullptr).

Applies To: CLockedPtr<>, CMemberPtr<>, CReferencePtr<>, gcptr<>::s / g / sg / gs / m / mm / out / in / inout / flex

Attach

CWeakPtr<T> Attach(uintptr_t p) throw()

Clear original reference in the smart pointer and assume a new reference derived from the specified integer, which must be a return value from the `Detach' method as described below.

Example:

gcptr<CFoo> p = gcnew CFoo;
uintptr_t handle = p.Detach();
...   // object is alive even there is no reference to it.

p.Attach(handle);   // transfer `handle' back to reference in `p'.
// object might be collected when it become garbage

Note: The Pauseless Benchmark testing program is also an example of using "Attach/Detach" methods to provide atomic pointer operation between multiple threads. You can view the source code to get more ideas about "Attach/Detach" mechanism.

Applies To: CLockedPtr<>, CMemberPtr<>, CReferencePtr<>, gcptr<>::s / g / sg / gs / m / mm / out / in / inout / flex

Detach

uintptr_t Detach() throw();

Return an integer representing a reference to the object that the smart pointer points to. The value of the smart pointer is cleared (reset to nullptr).

Remarks: (1) The object is still virtually holded by the returned integer, programmers should 'attach' the integer to a HnxGC smart pointer at last, or else the object will never be collected and cause a leak of resources. (2) One detached integer value can only be attached to a smart pointer *once*. Multiple attaching a detached integer value will confuse the HnxGC system and cause unpredictable result.

Applies To: CLockedPtr<>, CMemberPtr<>, CReferencePtr<>, gcptr<>::s / g / sg / gs / m / mm / out / in / inout / flex

Attach (COM)

CWeakPtr<T> Attach(T * pIntf) throw()
CWeakPtr<T> AttachQI(IUnknown * pIUnknown) throw()

Clear original reference in the smart pointer and assume a new reference derived from the specified pointer, which must be an COM interface pointer, i.e. an pointer to interface derived from IUnknown.

AttachQI will invoke QueryInterface() method on the specified IUnknown interface to retrieves an interface of `T' by expression __uuidof(T).

Example:

gcptr<IXMLDOMDocument> pXMLDom;

HRESULT hr;
IXMLDOMDocument * ppv = 0;
hr = ::CoCreateInstance(
            __uuidof(DOMDocument30),        // uuid of class
            NULL, CLSCTX_INPROC_SERVER,
            __uuidof(T),                    // uuid of interface
            (void **)&ppv);
if (hr == S_OK) {
    pXMLDom.Attach(ppv);
}

Note: HnxGC also provides CoCreateInstance() method for some smart pointers. It is more convenient for the above case, see below CoCreateInstance() method for details.

Applies To: CLockedPtr<>, CMemberPtr<>, CReferencePtr<>, gcptr<>::s / g / sg / gs / m / mm / out / in / inout / flex

CoCreateInstance

HRESULT CoCreateInstance(
    IN REFCLSID rclsid,
    IN LPUNKNOWN pUnkOuter = NULL,
    IN DWORD dwClsContext = CLSCTX_INPROC_SERVER) throw()

Call the COM global function ::CoCreateInstance to create an object of the class associated with the specified CLSID, and return the `T' interface of the object to the smart pointer.

Remarks: If ::CoCreateInstance() fails, then the smart pointer is cleared (reset to nullptr).

Example:

gcptr<IXMLDOMDocument> pXMLDom;

HRESULT hr = pXMLDom.CoCreateInstance(__uuidof(DOMDocument30));
if (hr == S_OK) {
   ...
}

Applies To: CLockedPtr<>, CMemberPtr<>, CReferencePtr<>, gcptr<>::s / g / sg / gs / m / mm / out / in / inout / flex

QueryInterface

HRESULT QueryInterface(IN LPUNKNOWN pUnknown) throw();

Clear original reference in the smart pointer and assume a new reference derived from calling QueryInterface(__uuidof(T)) on the specified COM interface `pUnknown'.

Remarks: If QueryInterface() fails, then the smart pointer is cleared (reset to nullptr).

Applies To: CLockedPtr<>, CMemberPtr<>, CReferencePtr<>, gcptr<>::s / g / sg / gs / m / mm / out / in / inout / flex

GetQIRef

[void ** | T **] GetQIRef() throw();

Return the address to a COM interface pointer. It is designed for your convenience to allow a client (native) function to assign a valid COM interface to a HnxGC smart pointer.

Remarks: If the client function does not update the pointer with a non-null pointer, the value of HnxGC smart pointer does not change. So, in some cases, you may want to reset the HnxGC smart pointer before calling this method.

Example:

gcptr<IXMLDOMDocument> pXMLDom;

HRESULT hr;
hr = ::CoCreateInstance(
            __uuidof(DOMDocument30),
            NULL, CLSCTX_INPROC_SERVER,
            __uuidof(T),
            pXMLDom.GetQIRef());
if (hr == S_OK) {
   ...
}

Applies To: CLockedPtr<>, CMemberPtr<>, CReferencePtr<>, gcptr<>::s / g / sg / gs / m / mm / out / in / inout / flex

Home | Download | Terms of Use | Privacy Statement | Contact Us
Copyright@ 2008 hnxgc.harnixworld.com, All rights reserved.