XLL+ Class Library (7.0)

Specifying a smart pointer type


When a pointer to an object is stored in the object cache, it is wrapped in a smart pointer. This ensures that when the object is no longer needed, it will automatically be deleted.

By default, ple::simple_shared_ptr<T> is used as the smart pointer. This type is included as part of the XLL+ run-time libraries, in simple_shared_ptr.h.

Other smart pointer types

If the objects that you wish to represent as handles in Excel are already wrapped in smart pointers, you will need to use the same type of smart pointer within the object cache. This will ensure that ownership of the object is shared between the object cache and the library that created the object, and thus the object will not be deleted until is no longer needed by both the object cache and the originating library.

To specify a specific smart pointer type you should define the macro RTDHANDLES_PTR_CLASS before the inclusion of rtdhandles.h. For example, to use the boost shared_ptr class, the following lines are used:

#include <boost\shared_ptr.hpp>
#define RTDHANDLES_PTR_CLASS boost::shared_ptr
#include <extensions\rtdhandles.h>

You can then use the smart pointer type as an argument to the global function CreateHandleInCache, e.g.:

CXlOper* CachedThing_Create_Impl(CXlOper& xloResult, const CXlStringArg& Name, 
    long Value)
    // End of generated code 
    boost::shared_ptr<CachedThing> thing(new CachedThing(Name, Value, CXlDate::Now()));
    xloResult = psl::CreateHandleInCache(thing);     
    return xloResult.Ret();

More realistically, you might use code like the following to put a shared pointer in the object cache, and return a handle:

boost::shared_ptr<YieldCurve> yc = CurveFactory::CreateYieldCurve(
    ccy, dates, rates);
xloResult = psl::CreateHandleInCache(yc);
return xloResult.Ret();

For more details see the BoostHandles sample project.

Next: Sharing object handles between XLLs >>