Boost logo

Boost :

From: Vesa Karvonen (vesa.karvonen_at_[hidden])
Date: 2001-06-15 09:27:48


----- Original Message -----
From: "Igor Smirnov" <Igor.Smirnov_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, June 13, 2001 22:03
Subject: [boost] Smart pointers and OOP

> There is a lot of different variants of pointers. It is impossible and
> senseless to include all of them.

I think that it is possible to create a practically useful feature model
of resource management that includes millions of variations of "smart
pointers" and other resource management primitives (e.g. mutex locks,
scope guards, etc...).

> The smart pointers of Andrei Alexandrescu may be called twice smart. It
> seems that at appropriate parameters they can simulate ownership, but
> perhaps they can never simulate the reference to alien object.
Moreover,
> the code is too large. At its permanent use the compilation time will
be
> extremal. Too many possibilities can confuse programmer and are not
really
> necessary.

Andrei's smart_pointer is basically just the ICCL (Implementation
Components Configuration Language). It has also mainly been designed for
pointers rather than resources in general. I think that using an ICCL
directly becomes totally impractical when the number of configurations
grows significantly.

In my resource model implementation, the user uses a configuration DSL to
describe the desired concrete resource implementation. The resource DSL
parser and language codify domain knowledge of the resource concepts they
can describe. This way the user only needs to give a small subset of the
complete description and the library computes meaningful defaults for the
rest. The parser also checks that the description is valid. The
description is then passed to the resource generator, which creates a
concrete resource managing handle using the ICCL.

For example, to create an auto_ptr<int> equivalent, the user could write
the following (the syntax below is not the final, but should be close
enough):

    typedef
      resource::generator
      < resource::type< int > // the order doesn't matter
      , resource::adt< resource::adt_pointer<> >
      , resource::releaser< resource::apply_delete<> >
      , resource::ownership< resource::ownership_single<> >
>::type auto_ptr_int;

Actually, the above description could be shortened to:

    typedef
      resource::generator
      < resource::type< int >
>::type auto_ptr_int;

Because the other parameters would be computed as defaults anyway. This
can hopefully be streamlined considerably in C++ 0x using template
typedefs.

It is also possible to describe many other kinds of resources. For
instance, a COM smart pointer can be created as follows:

    typedef
      resource::generator
      < resource::type< IDirect3DTexture8 >
      , resource::releaser< resource::apply_Release<> >
      , resource::copier< resource::apply_AddRef<> >
      , resource::address_of< resource::address_of_assign_null<> >
      , resource::morphology_checker< check_QueryInterface >
>::type texture_ptr;

(The copier and address_of above are defaults anyway and could be removed.
These defaults are used whenever the releaser is apply_Release, which
implies a COM object. The implied ownership model is intrinsically
reference counted.)

Other examples would be resources that are not pointers:

    typedef
      resource::generator
      < resource::type< HWND >
      , resource::adt< resource::adt_nullable<> >
      , resource::releaser< apply_DestroyWindow >
>::type HWND_handle;

Or:

    typedef
      resource::generator
      < resource::type< DWORD >
      , resource::adt< resource::adt_nullable< DWORD, 0xFFFFFFFF > >
      , resource::releaser< apply_IDirect3DDevice8_DeletePixelShader >
      , resource::ownership< resource::ownership_multi<> >
      , resource::address_of<>
>::type pixel_shader_handle;


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk