Boost logo

Boost :

From: Pavel Vozenilek (pavel_vozenilek_at_[hidden])
Date: 2005-02-18 19:54:10


Hello Ion,

> T * ptr = shmem_segment.create<T>("ObjectName")[size]
> /*proxy returned here, calls operator() of the object*/
> (arg1, arg2, ...argN);
>

When reading your question (no answer for it, at least now)
it come to my mind:

- maybe there should be smart pointer for shmem and version
of create() which returns smart pointer. It would result in
greater safety of the library.

Something as:

// helper class, returned by segment::create(),
// not to be used as is, only for conversion
// to T* or to smart pointer
template<typename T>
class shmem_smart_pointer_proxy
{
   shmem_smart_pointer_proxy(T* p_, segment* seg_) p(p_), seg(seg_) {}
public:
   ~shmem_smart_pointer_proxy() { if (p) seg->destroy(p); }
   operator T*() { T* aux = p; p = 0; return aux; } // assigned to raw
pointer
private:
   T* p;
    segment* seg;
};

// the smart pointer
template<typename T>
class shmem_smart_pointer
{
public:
     shmem_smart_pointer(const shmem_smart_pointer_proxy& aux) {
        p = aux.p;
       aux.p = 0;
       seg = aux.seg;
     }
     T* operator->() { ...}
     ~shmem_smart_pointer() { if (p) seg->destroy(p); }
private:
   T* p;
   segment* seg;
};

shmem_smart_pointer_proxy,T> = shmem_segment.create<T>("ObjectName")[size]
          (arg1, arg2, ...argN);

It could be then used:

T* t = segment.create(...); // no smarteness used
shmem_smart_pointer ptr = segment_create(...); // smart ptr used
(void)segment.create(...); // no leak happens

but not as:
segment.create(...)->do_something(); // this would probably be error anyway

/Pavel


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