header_utils
Loading...
Searching...
No Matches
ghassanpl::scoped_value_change< T, OPTIONAL > Struct Template Reference

A RAII class that changes the value of a variable and reverts it to the original value on destruction. More...

#include <scope.h>

Public Member Functions

template<typename U >
requires (!OPTIONAL)
 scoped_value_change (T &ref, U new_val) noexcept(std::is_nothrow_move_constructible_v< T >)
 
template<typename U >
requires OPTIONAL
 scoped_value_change (T &ref, U new_val) noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_copy_constructible_v< T >)
 
 scoped_value_change (const scoped_value_change &)=delete
 
 scoped_value_change (scoped_value_change &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
 
scoped_value_changeoperator= (const scoped_value_change &)=delete
 
scoped_value_changeoperator= (scoped_value_change &&other)=delete
 Move assignment is not implemented because it's not clear and obvious what the order of reversions would be, and I don't want to force the user to read and remember the documentation.
 
bool valid () const noexcept
 
T constoriginal_value () const &noexcept
 Returns the original value.
 
original_value () &&noexcept(std::is_nothrow_move_constructible_v< T >)
 Returns the original value.
 
T constcurrent_value ()
 Returns the current value.
 
void revert () noexcept(std::is_nothrow_move_assignable_v< T >)
 Reverts the value to the original one.
 
revert_and_return () noexcept(std::is_nothrow_move_assignable_v< T > &&std::is_nothrow_move_constructible_v< T >)
 Reverts the value to the original one.
 
void release () noexcept
 Causes the value to not be reverted on destruction.
 
release_and_return () noexcept(std::is_nothrow_move_constructible_v< T >)
 Causes the value to not be reverted on destruction.
 

Detailed Description

template<typename T, bool OPTIONAL = false>
struct ghassanpl::scoped_value_change< T, OPTIONAL >

A RAII class that changes the value of a variable and reverts it to the original value on destruction.

Not exception-safe (probably).

Definition at line 258 of file scope.h.

Constructor & Destructor Documentation

◆ scoped_value_change() [1/3]

template<typename T , bool OPTIONAL = false>
template<typename U >
requires (!OPTIONAL)
ghassanpl::scoped_value_change< T, OPTIONAL >::scoped_value_change ( T &  ref,
U  new_val 
)
inlinenoexcept

Definition at line 261 of file scope.h.

◆ scoped_value_change() [2/3]

template<typename T , bool OPTIONAL = false>
template<typename U >
requires OPTIONAL
ghassanpl::scoped_value_change< T, OPTIONAL >::scoped_value_change ( T &  ref,
U  new_val 
)
inlinenoexcept

Definition at line 269 of file scope.h.

◆ ~scoped_value_change()

template<typename T , bool OPTIONAL = false>
ghassanpl::scoped_value_change< T, OPTIONAL >::~scoped_value_change ( )
inlinenoexcept

Definition at line 276 of file scope.h.

◆ scoped_value_change() [3/3]

template<typename T , bool OPTIONAL = false>
ghassanpl::scoped_value_change< T, OPTIONAL >::scoped_value_change ( scoped_value_change< T, OPTIONAL > &&  other)
inlinenoexcept

Definition at line 282 of file scope.h.

Member Function Documentation

◆ current_value()

template<typename T , bool OPTIONAL = false>
T const & ghassanpl::scoped_value_change< T, OPTIONAL >::current_value ( )
inline

Returns the current value.

Precondition
valid() == true

Definition at line 325 of file scope.h.

◆ original_value() [1/2]

template<typename T , bool OPTIONAL = false>
T ghassanpl::scoped_value_change< T, OPTIONAL >::original_value ( ) &&
inlinenoexcept

Returns the original value.

Precondition
valid() == true

Definition at line 318 of file scope.h.

◆ original_value() [2/2]

template<typename T , bool OPTIONAL = false>
T const & ghassanpl::scoped_value_change< T, OPTIONAL >::original_value ( ) const &
inlinenoexcept

Returns the original value.

Precondition
valid() == true

Definition at line 311 of file scope.h.

◆ release()

template<typename T , bool OPTIONAL = false>
void ghassanpl::scoped_value_change< T, OPTIONAL >::release ( )
inlinenoexcept

Causes the value to not be reverted on destruction.

Postcondition
valid() == false

Definition at line 369 of file scope.h.

◆ release_and_return()

template<typename T , bool OPTIONAL = false>
T ghassanpl::scoped_value_change< T, OPTIONAL >::release_and_return ( )
inlinenoexcept

Causes the value to not be reverted on destruction.

Postcondition
valid() == false
Returns
The original value.

Definition at line 377 of file scope.h.

◆ revert()

template<typename T , bool OPTIONAL = false>
void ghassanpl::scoped_value_change< T, OPTIONAL >::revert ( )
inlinenoexcept

Reverts the value to the original one.

If OPTIONAL is true, the change is not reverted if the current value is equal to the original one.

Postcondition
valid() == false

Definition at line 332 of file scope.h.

◆ revert_and_return()

template<typename T , bool OPTIONAL = false>
T ghassanpl::scoped_value_change< T, OPTIONAL >::revert_and_return ( )
inlinenoexcept

Reverts the value to the original one.

Precondition
valid() == true
Postcondition
valid() == false
Returns
The current value, or the original value if the change is not needed (when OPTIONAL is true).

Definition at line 353 of file scope.h.

◆ valid()

template<typename T , bool OPTIONAL = false>
bool ghassanpl::scoped_value_change< T, OPTIONAL >::valid ( ) const
inlinenoexcept

Definition at line 307 of file scope.h.