Is it usful to write a class like "Do_When_Return" so I can make it easy to delete some objects when

I

i.c.code

maybe like:

#include "boost/function.hpp"

template <typename T>
class Do_When_Return
{
private:
Do_When_Return(const Do_When_Return&);
Do_When_Return();

Do_When_Return& operator=(const Do_When_Return&);

public:
Do_When_Return(boost::function<void (T)> f, T* p)
:func_(f)
,obj_(p)
{
}

~Do_When_Return()
{
if ( func_ && obj_ )
{
func_(*obj_);
}
}

private:
boost::function<void (T)> func_;
T* obj_;
};


so I can use it like this in some Windows GDI operations,

void test()
{
HDC hdc =GetDC(NULL);

Do_When_Return<HDC> auto_release_hdc(&ReleaseDC, &hdc);

//do something which use hdc
}
 
O

Old Wolf

( Is it usful to write a class like "Do_When_Return" so I can
make it easy to delete some objects when I exit from a code block? )
Yes

void test()
{
HDC hdc =GetDC(NULL);

Do_When_Return<HDC> auto_release_hdc(&ReleaseDC, &hdc);

//do something which use hdc
}

Another option is something like:

struct auto_hdc
{
auto_hdc(): hdc( GetDC(0) ) {}
~auto_hdc() { ReleaseDC( hdc ); }
operator HDC() const { return hdc; }
private:
HDC hdc;
};

void test()
{
auto_hdc hdc;
// do something with hdc
}
 
V

Victor Bazarov

maybe like:

#include "boost/function.hpp"

template <typename T>
class Do_When_Return
{
private:
Do_When_Return(const Do_When_Return&);
Do_When_Return();

Do_When_Return& operator=(const Do_When_Return&);

public:
Do_When_Return(boost::function<void (T)> f, T* p)
:func_(f)
,obj_(p)
{
}

~Do_When_Return()
{
if ( func_ && obj_ )
{
func_(*obj_);
}
}

private:
boost::function<void (T)> func_;
T* obj_;
};


so I can use it like this in some Windows GDI operations,

void test()
{
HDC hdc =GetDC(NULL);

Do_When_Return<HDC> auto_release_hdc(&ReleaseDC, &hdc);

//do something which use hdc
}

It is useful. But 'ReleaseDC' does not take one argument, IIRC,
so you need to use binders somehow.

V
 
A

Alf P. Steinbach

* (e-mail address removed):
maybe like:

#include "boost/function.hpp"

template <typename T>
class Do_When_Return
{
private:
Do_When_Return(const Do_When_Return&);
Do_When_Return();

Do_When_Return& operator=(const Do_When_Return&);

public:
Do_When_Return(boost::function<void (T)> f, T* p)
:func_(f)
,obj_(p)
{
}

~Do_When_Return()
{
if ( func_ && obj_ )
{
func_(*obj_);
}
}

private:
boost::function<void (T)> func_;
T* obj_;
};


so I can use it like this in some Windows GDI operations,

void test()
{
HDC hdc =GetDC(NULL);

Do_When_Return<HDC> auto_release_hdc(&ReleaseDC, &hdc);

//do something which use hdc
}

boost::function introduces some overhead, namely dynamic allocation.

If you don't want the overhead, consider instead using ScopeGuard, which
handles this without any dynamic allocation.

Or, if you don't want to depend on ScopeGuard, write your own little
no-dynamic-allocation class.
 
H

Heinz Ozwirk

so I can use it like this in some Windows GDI operations,

void test()
{
HDC hdc =GetDC(NULL);

Do_When_Return<HDC> auto_release_hdc(&ReleaseDC, &hdc);

//do something which use hdc
}

I would even go a step further and use such a class instead of hdc or whatever resource, that has to be released later, so I could write

void test()
{
AutoRelease<HDC> dc = GetDC(0);
// do something with dc
}

Of cause AutoRelease would have to know how to release a DC or whatever it controls, but that information can be supplied as a (default) template parameter like the traits for std::basic_string.

Heinz
 
B

Ben Pope

> Is it usful to write a class like "Do_When_Return" so I can make
> it easy to delete some objects when I exit from a code block?

That sounds like you're describing RAII, google it.

The basic concept is to manage a resource using the lifetime of an
object; in the constructor you acquire the resource, in the destructor
you release it.

It's not just useful, it's fundamental for exception safe code.

Ben Pope
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top