Discussion:
like experimental::sope_exit, but more advanced
(too old to reply)
Bonita Montero
2024-07-02 09:44:29 UTC
Permalink
For some time I use sth. like experimental::scope_exit, but sth.
more advanced. My class is called invoke_on_destruct and multiple
of these scope-guards can be concatenated. So if you have multiple
data struc- tures which are changed you can revert all changes with
multiple individual scope-guards.
After you changed everything you do sth. like a commit by disabling
the last scope-guard. The enabled state is propagated on destruction
to the depending scope-guards so you only have to disable the last
and all depening scope-guards are disabled also.

#pragma once
#include <utility>

template<typename Fn>
struct invoke_on_destruct;

struct iod_base
{
private:
template<typename Fn>
friend struct invoke_on_destruct;
bool m_enabled;
iod_base *m_next;
iod_base( iod_base *next ) :
m_enabled( true ),
m_next( next )
{
}
void disable()
{
m_enabled = false;
}
};

template<typename Fn>
struct invoke_on_destruct final : public iod_base
{
private:
Fn m_fn;
public:
invoke_on_destruct( Fn &&fn, iod_base *next = nullptr )
requires requires( Fn fn ) { { fn() }; } :
iod_base( next ),
m_fn( std::forward<Fn>( fn ) )
{
}
~invoke_on_destruct()
{
bool enabled = m_enabled;
if( m_next )
m_next->m_enabled = enabled;
if( enabled )
m_fn();
}
void operator ()()
{
m_enabled = false;
m_fn();
}
using iod_base::disable;
};
Chris M. Thomasson
2024-07-02 22:16:06 UTC
Permalink
Post by Bonita Montero
For some time I use sth. like experimental::scope_exit, but sth.
more advanced. My class is called invoke_on_destruct and multiple
of these scope-guards can be concatenated. So if you have multiple
data struc- tures which are changed you can revert all changes with
multiple individual scope-guards.
After you changed everything you do sth. like a commit by disabling
the last scope-guard. The enabled state is propagated on destruction
to the depending scope-guards so you only have to disable the last
and all depening scope-guards are disabled also.
[...]

It actually does remind me of the original scope guard in a sense.
Bonita Montero
2024-07-02 22:59:47 UTC
Permalink
Post by Chris M. Thomasson
Post by Bonita Montero
For some time I use sth. like experimental::scope_exit, but sth.
more advanced. My class is called invoke_on_destruct and multiple
of these scope-guards can be concatenated. So if you have multiple
data struc- tures which are changed you can revert all changes with
multiple individual scope-guards.
After you changed everything you do sth. like a commit by disabling
the last scope-guard. The enabled state is propagated on destruction
to the depending scope-guards so you only have to disable the last
and all depening scope-guards are disabled also.
[...]
It actually does remind me of the original scope guard in a sense.
What's the "original" scope guard ?
The only semi-official version is experimental::scope_exit.
Paavo Helde
2024-07-03 07:47:29 UTC
Permalink
Post by Bonita Montero
Post by Chris M. Thomasson
Post by Bonita Montero
For some time I use sth. like experimental::scope_exit, but sth.
more advanced. My class is called invoke_on_destruct and multiple
of these scope-guards can be concatenated. So if you have multiple
data struc- tures which are changed you can revert all changes with
multiple individual scope-guards.
After you changed everything you do sth. like a commit by disabling
the last scope-guard. The enabled state is propagated on destruction
to the depending scope-guards so you only have to disable the last
and all depening scope-guards are disabled also.
[...]
It actually does remind me of the original scope guard in a sense.
What's the "original" scope guard ?
The only semi-official version is experimental::scope_exit.
I guess Chris means ScopeGuard by Petru Marginean and Andrei Alexandrescu:

"https://web.archive.org/web/20121009071337/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758"
(page 1)

"https://web.archive.org/web/20190317153736/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=2"
(page 2)

"https://web.archive.org/web/20190317164052/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=3"
(page 3)
Bonita Montero
2024-07-03 08:07:21 UTC
Permalink
Post by Bonita Montero
Post by Chris M. Thomasson
Post by Bonita Montero
For some time I use sth. like experimental::scope_exit, but sth.
more advanced. My class is called invoke_on_destruct and multiple
of these scope-guards can be concatenated. So if you have multiple
data struc- tures which are changed you can revert all changes with
multiple individual scope-guards.
After you changed everything you do sth. like a commit by disabling
the last scope-guard. The enabled state is propagated on destruction
to the depending scope-guards so you only have to disable the last
and all depening scope-guards are disabled also.
[...]
It actually does remind me of the original scope guard in a sense.
What's the "original" scope guard ?
The only semi-official version is experimental::scope_exit.
"https://web.archive.org/web/20121009071337/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758" (page 1)
"https://web.archive.org/web/20190317153736/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=2" (page 2)
"https://web.archive.org/web/20190317164052/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=3" (page 3)
This ancient scope-guards don't make sense for me because scope
-guards become convenient with the introduction of lambdas first.
Chris M. Thomasson
2024-07-31 04:20:24 UTC
Permalink
Post by Bonita Montero
Post by Chris M. Thomasson
Post by Bonita Montero
For some time I use sth. like experimental::scope_exit, but sth.
more advanced. My class is called invoke_on_destruct and multiple
of these scope-guards can be concatenated. So if you have multiple
data struc- tures which are changed you can revert all changes with
multiple individual scope-guards.
After you changed everything you do sth. like a commit by disabling
the last scope-guard. The enabled state is propagated on destruction
to the depending scope-guards so you only have to disable the last
and all depening scope-guards are disabled also.
[...]
It actually does remind me of the original scope guard in a sense.
What's the "original" scope guard ?
The only semi-official version is experimental::scope_exit.
"https://web.archive.org/web/20121009071337/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758" (page 1)
"https://web.archive.org/web/20190317153736/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=2" (page 2)
"https://web.archive.org/web/20190317164052/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=3" (page 3)
Bingo!
Bonita Montero
2024-07-31 05:36:37 UTC
Permalink
Post by Chris M. Thomasson
Post by Paavo Helde
Post by Bonita Montero
Post by Chris M. Thomasson
Post by Bonita Montero
For some time I use sth. like experimental::scope_exit, but sth.
more advanced. My class is called invoke_on_destruct and multiple
of these scope-guards can be concatenated. So if you have multiple
data struc- tures which are changed you can revert all changes with
multiple individual scope-guards.
After you changed everything you do sth. like a commit by disabling
the last scope-guard. The enabled state is propagated on destruction
to the depending scope-guards so you only have to disable the last
and all depening scope-guards are disabled also.
[...]
It actually does remind me of the original scope guard in a sense.
What's the "original" scope guard ?
The only semi-official version is experimental::scope_exit.
I guess Chris means ScopeGuard by Petru Marginean and Andrei
"https://web.archive.org/web/20121009071337/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758" (page 1)
"https://web.archive.org/web/20190317153736/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=2" (page 2)
"https://web.archive.org/web/20190317164052/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=3" (page 3)
Bingo!
That's nearly as much effort as to write a special handler class
for each resource. Sth. like scope_exit or my invoke_on_destruct
is much more convenient.
Chris M. Thomasson
2024-07-31 20:43:51 UTC
Permalink
Post by Bonita Montero
Post by Chris M. Thomasson
Post by Paavo Helde
Post by Bonita Montero
Post by Chris M. Thomasson
Post by Bonita Montero
For some time I use sth. like experimental::scope_exit, but sth.
more advanced. My class is called invoke_on_destruct and multiple
of these scope-guards can be concatenated. So if you have multiple
data struc- tures which are changed you can revert all changes with
multiple individual scope-guards.
After you changed everything you do sth. like a commit by disabling
the last scope-guard. The enabled state is propagated on destruction
to the depending scope-guards so you only have to disable the last
and all depening scope-guards are disabled also.
[...]
It actually does remind me of the original scope guard in a sense.
What's the "original" scope guard ?
The only semi-official version is experimental::scope_exit.
I guess Chris means ScopeGuard by Petru Marginean and Andrei
"https://web.archive.org/web/20121009071337/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758" (page 1)
"https://web.archive.org/web/20190317153736/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=2" (page 2)
"https://web.archive.org/web/20190317164052/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=3" (page 3)
Bingo!
That's nearly as much effort as to write a special handler class
for each resource. Sth. like scope_exit or my invoke_on_destruct
is much more convenient.
Do you notice the dates in those drdobbs links?

December 01, 2000 ?
Bonita Montero
2024-08-01 09:19:49 UTC
Permalink
Post by Chris M. Thomasson
Post by Bonita Montero
Post by Chris M. Thomasson
Post by Bonita Montero
Post by Chris M. Thomasson
Post by Bonita Montero
For some time I use sth. like experimental::scope_exit, but sth.
more advanced. My class is called invoke_on_destruct and multiple
of these scope-guards can be concatenated. So if you have multiple
data struc- tures which are changed you can revert all changes with
multiple individual scope-guards.
After you changed everything you do sth. like a commit by disabling
the last scope-guard. The enabled state is propagated on destruction
to the depending scope-guards so you only have to disable the last
and all depening scope-guards are disabled also.
[...]
It actually does remind me of the original scope guard in a sense.
What's the "original" scope guard ?
The only semi-official version is experimental::scope_exit.
"https://web.archive.org/web/20121009071337/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758" (page 1)
"https://web.archive.org/web/20190317153736/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=2" (page 2)
"https://web.archive.org/web/20190317164052/http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=3" (page 3)
Bingo!
That's nearly as much effort as to write a special handler class
for each resource. Sth. like scope_exit or my invoke_on_destruct
is much more convenient.
Do you notice the dates in those drdobbs links?
December 01, 2000 ?
I just wanted to say that in the days before lambdas specialized
RAII-classes for unsupported resouces as hard / easy to write
than this kind of pseudo scope-guard.

Loading...