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

An 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, bool OPT = IS_OPTIONAL>
 scoped_value_change (T &ref, U new_val, typename std::enable_if_t<!OPT > *=0) noexcept(std::is_nothrow_move_constructible_v< T >)
template<typename U, bool OPT = IS_OPTIONAL>
 scoped_value_change (T &ref, U new_val, typename std::enable_if_t< OPT > *=0) 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_change & operator= (const scoped_value_change &)=delete
scoped_value_change & operator= (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 const & original_value () const &noexcept
 Returns the original value.
T original_value () &&noexcept(std::is_nothrow_move_constructible_v< T >)
 Returns the original value.
T const & current_value ()
 Returns the current value.
void revert () noexcept(std::is_nothrow_move_assignable_v< T >)
 Reverts the value to the original one.
T 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.
T 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 IS_OPTIONAL = false>
struct ghassanpl::scoped_value_change< T, IS_OPTIONAL >

An 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 264 of file scope.h.

Constructor & Destructor Documentation

◆ scoped_value_change() [1/3]

template<typename T, bool IS_OPTIONAL = false>
template<typename U, bool OPT = IS_OPTIONAL>
ghassanpl::scoped_value_change< T, IS_OPTIONAL >::scoped_value_change ( T & ref,
U new_val,
typename std::enable_if_t<!OPT > * = 0 )
inlinenoexcept

Definition at line 267 of file scope.h.

◆ scoped_value_change() [2/3]

template<typename T, bool IS_OPTIONAL = false>
template<typename U, bool OPT = IS_OPTIONAL>
ghassanpl::scoped_value_change< T, IS_OPTIONAL >::scoped_value_change ( T & ref,
U new_val,
typename std::enable_if_t< OPT > * = 0 )
inlinenoexcept

Definition at line 274 of file scope.h.

◆ ~scoped_value_change()

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

Definition at line 280 of file scope.h.

◆ scoped_value_change() [3/3]

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

Definition at line 286 of file scope.h.

Member Function Documentation

◆ current_value()

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

Returns the current value.

Precondition
valid() == true

Definition at line 329 of file scope.h.

◆ original_value() [1/2]

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

Returns the original value.

Precondition
valid() == true

Definition at line 322 of file scope.h.

◆ original_value() [2/2]

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

Returns the original value.

Precondition
valid() == true

Definition at line 315 of file scope.h.

◆ release()

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

Causes the value to not be reverted on destruction.

Postcondition
valid() == false

Definition at line 373 of file scope.h.

◆ release_and_return()

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

Causes the value to not be reverted on destruction.

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

Definition at line 381 of file scope.h.

◆ revert()

template<typename T, bool IS_OPTIONAL = false>
void ghassanpl::scoped_value_change< T, IS_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 336 of file scope.h.

◆ revert_and_return()

template<typename T, bool IS_OPTIONAL = false>
T ghassanpl::scoped_value_change< T, IS_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 IS_OPTIONAL is true).

Definition at line 357 of file scope.h.

◆ valid()

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

Definition at line 311 of file scope.h.