Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

We don't really understand it, so we'll give it to the programmers.


devel / comp.lang.c++ / The most versatile scope-guard

SubjectAuthor
o The most versatile scope-guardBonita Montero

1
The most versatile scope-guard

<us74fu$3p12c$1@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3266&group=comp.lang.c%2B%2B#3266

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: The most versatile scope-guard
Date: Tue, 5 Mar 2024 13:51:19 +0100
Organization: A noiseless patient Spider
Lines: 63
Message-ID: <us74fu$3p12c$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 5 Mar 2024 12:51:10 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="f14c7aad57de1eb32b96e464726dea52";
logging-data="3966028"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18gQeR47F+TH+DNA5ECg+hQtn/7DH4cHzk="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ETQG3LR1/ukYAIaDIXw0eBtsI4M=
Content-Language: de-DE
 by: Bonita Montero - Tue, 5 Mar 2024 12:51 UTC

I'm sometimes doing multiple changes with more than a single data
structure which have to be reverted if the latest change throws an
exception. Until today I used my home-brew experimental::scope_guard
like class multiple times and I disabled each individual scope-object
when all changes have finished.
So I got the idea to concatenate multiple scope-guards and If i disable
the a child scope-guard in the chain all depending scope-guards also
will be disabled; re-enabling the latest scope-guard also propagates
through the chain. Disabling parent scope-guards won't have an effect,
i.e only the last enable-state in the chain counts. Enabling and dis-
abling is propagated to the immediate parent on destruction.

#pragma once#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 )
{
}
};

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()
{
// enable or disable parent node according to our state;
// use a conditional move for efficiency
bool enabled = (m_next ? m_next : this)->m_enabled = m_enabled;
// are we enabled ?
if( enabled ) [[unlikely]]
// yes: invoke
m_fn();
}
void disable()
{
m_enabled = false;
}
invoke_on_destruct &enable()
{
m_enabled = true;
}
};

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor