Re: Opinions on `defer`?

Liste des GroupesRevenir à cl c 
Sujet : Re: Opinions on `defer`?
De : Bonita.Montero (at) *nospam* gmail.com (Bonita Montero)
Groupes : comp.lang.c
Date : 11. Jan 2025, 15:46:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vlu08f$lm8o$1@raubtier-asyl.eternal-september.org>
References : 1 2 3 4
User-Agent : Mozilla Thunderbird
Am 09.01.2025 um 08:24 schrieb Bonita Montero:
Am 08.01.2025 um 23:39 schrieb Chris M. Thomasson:
 
Reminds me of ScopeGuard. ;^)
 But better than this. If you have multiple changes on a data structure
and the last fails the invoke_on_destruct-s are chained and revert the
changes incrementally.
I optimized the whole thing now a bit. Now I have two classed called
defer and xdefer. defer isn't chainable and xdefer is chainable.
#pragma once
#include <utility>
#include <concepts>
template<std::invocable Fn>
struct defer final
{
defer( Fn &&fn ) :
m_enabled( true ),
m_fn( std::forward<Fn>( fn ) )
{
}
defer( defer const & ) = delete;
~defer()
{
if( m_enabled ) [[likely]]
m_fn();
}
void operator ()()
{
if( !m_enabled ) [[unlikely]]
return;
m_fn();
m_enabled = false;
}
void disable()
{
m_enabled = false;
}
private:
bool m_enabled;
#if defined(_MSC_VER)
[[msvc::no_unique_address]]
#else
[[no_unique_address]]
#endif
Fn m_fn;
};
template<std::invocable Fn, std::invocable FnNext = Fn>
struct xdefer final
{
xdefer( Fn &&fn, xdefer<FnNext> *next = nullptr ) :
m_enabled( true ),
m_next( next ),
m_fn( std::forward<Fn>( fn ) )
{
}
xdefer( xdefer const & ) = delete;
~xdefer()
{
bool enabled = m_enabled;
if( m_next ) [[likely]]
m_next->m_enabled = enabled;
if( enabled ) [[likely]]
m_fn();
}
void operator ()()
{
if( !m_enabled ) [[unlikely]]
return;
m_fn();
m_enabled = false;
if( !m_next ) [[likely]]
return;
m_next->m_enabled = true;
(*m_next)();
}
void disable()
{
m_enabled = false;
}
private:
template<std::invocable Fn1, std::invocable Fn2>
friend struct xdefer;
bool m_enabled;
xdefer<FnNext> *m_next;
#if defined(_MSC_VER)
[[msvc::no_unique_address]]
#else
[[no_unique_address]]
#endif
Fn m_fn;
};

Date Sujet#  Auteur
7 Jan 25 * Opinions on `defer`?31Alexis
7 Jan 25 +* Re: Opinions on `defer`?2Kaz Kylheku
8 Jan 25 i`- Re: Opinions on `defer`?1Thiago Adams
7 Jan 25 +* Re: Opinions on `defer`?2David Brown
7 Jan 25 i`- Re: Opinions on `defer`?1Phillip
7 Jan 25 +- Re: Opinions on `defer`?1John McCue
7 Jan 25 +* Re: Opinions on `defer`?4Bonita Montero
8 Jan 25 i`* Re: Opinions on `defer`?3Chris M. Thomasson
9 Jan 25 i `* Re: Opinions on `defer`?2Bonita Montero
11 Jan 25 i  `- Re: Opinions on `defer`?1Bonita Montero
8 Jan 25 +* Re: Opinions on `defer`?3Janis Papanagnou
8 Jan 25 i+- Re: Opinions on `defer`?1Kaz Kylheku
28 Feb 25 i`- Re: Opinions on `defer`?1Lawrence D'Oliveiro
8 Jan 25 +- Re: Opinions on `defer`?1BlueManedHawk
8 Jan 25 `* Re: Opinions on `defer`?17Tim Rentsch
8 Jan 25  +* Re: Opinions on `defer`?3Thiago Adams
8 Jan 25  i+- Re: Opinions on `defer`?1Thiago Adams
19 Feb 25  i`- Re: Opinions on `defer`?1Tim Rentsch
9 Jan 25  +* Re: Opinions on `defer`?4David Brown
9 Jan 25  i`* Re: Opinions on `defer`?3Kaz Kylheku
9 Jan 25  i `* Re: Opinions on `defer`?2David Brown
9 Jan 25  i  `- Re: Opinions on `defer`?1Michael S
9 Jan 25  `* Re: Opinions on `defer`?9Bonita Montero
9 Jan 25   `* Re: Opinions on `defer`?8Bonita Montero
10 Jan 25    +* Re: Opinions on `defer`?2Bonita Montero
11 Jan 25    i`- Re: Opinions on `defer`?1Bonita Montero
10 Jan 25    +- Re: Opinions on `defer`?1David Brown
13 Jan 25    +* Re: Opinions on `defer`?2Tim Rentsch
13 Jan 25    i`- Re: Opinions on `defer`?1Bonita Montero
24 Feb 25    `* Re: Opinions on `defer`?2Lawrence D'Oliveiro
3 Mar 25     `- Re: Opinions on `defer`?1Bonita Montero

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal