Boost logo

Boost :

From: Jim Patterson (jimp_at_[hidden])
Date: 2001-11-05 21:49:53


On Mon, 05 Nov 2001 15:11:17 -0800
Sean Parent <sparent_at_[hidden]> wrote:

> This goes full circle to my original question. Asked another way - is there
> a way to make it easy to declare a deleter function object (or any function
> object) from a pointer to a function that is statically bound to the pointer
> so it can have a default constructor? This wasn't intended to be any
> criticism or analysis of shared_ptr, it was just pointed out to me to look
> at how it is done there - but it isn't done there.
>
> The issue boils down to deriving the type of a value in a class template -
>
> That is, is there any way to create a template that looks something like
> this (or could be used as one would imagine this could be):
>
> template <Type Value>
> class foo {...};
>
> Where both Type and Value are template parameters. The best I've been able
> to do is:
>
> template <typename Type, Type Value>
> class foo {...};
>

Sean.

I'm not sure if I really grok what your real problem is, but then I'm
pretty new to Boost and the STL style of generic programming anyway.

I have recently written a template class to manage "C" style handles
for use in a C++ programs that use "C" libraries. An example of its
use is:

#include "C_HandleManager.hpp"

#include <iostream>
#include <cstdlib>

using namespace std;

int
main( int, char** )
{
    cout << "starting..." << flush;
    {
        C_HandleManager<void*,void,free> memory( malloc(1000) );
        if ( NULL == memory )
        {
            cerr << "\nUnable to allocate memory!" << endl;
            memory.no_deallocate_reset();
            return 1;
        }

        memset( memory, 0, 1000 );
    } // memory is destroyed here (and free is called on the pointer)

    cout << "\ndone!" << endl;
}

The class looks like:
template<typename T, // Type of the handle
         typename R, // Return type of the dealloc function
         R (*DEALLOC)(T)> // Function to dealloc the handle
class C_HandleManager;

The purpose of the class is to provide shared_ptr like semantics to
a handle to a "C" library resource. It could be malloc'ed memory, or
a FILE*, or any given resource handle. It tracks the ref count and calls
the supplied dealloc routine when the count is zero. The complexity of
the template is usually hidden behind a typedef.

Is this the kind of thing your talking about? Is there any interest in
this for Boost in general?

Jim Patterson


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