Flow 1.0.2
Flow project: Public API.
|
A simple RAII-pattern class template that, at construction, sets the specified location in memory to a specified value, memorizing the previous contents; and at destruction restores the value. More...
#include <util.hpp>
Public Member Functions | |
Scoped_setter (Value *target, Value &&val_src_moved) | |
Post-condition: *target contains was val_src_moved contained at ctor entry; and the destructor invocation shall reverse this, so that *target is restored to its value at entry. More... | |
Scoped_setter (Scoped_setter &&src_moved) | |
Move constructor: *this acts as src_moved would-have, while src_moved becomes a no-op object permanently. More... | |
Scoped_setter (const Scoped_setter &)=delete | |
Prohibit copying: for each explicit ctor invocation, there shall be exactly 1 non-no-op dtor invocation. | |
~Scoped_setter () | |
Restores *target (from main ctor) to its value at entry to said ctor; or does nothing if *this has been moved-from via the move ctor. | |
Scoped_setter & | operator= (const Scoped_setter &)=delete |
Prohibit copying (see delete d copy ctor). | |
Scoped_setter & | operator= (Scoped_setter &&)=delete |
Prohibit modifying existing *this ; except that moving-from is enabled via the move ctor. | |
A simple RAII-pattern class template that, at construction, sets the specified location in memory to a specified value, memorizing the previous contents; and at destruction restores the value.
E.g.:
The object is movable, not copyable (which is similar to unique_ptr
) to prevent "double-restore." Related: one can easily return customized auto-setter/restorers:
This is a simple object: it just performs a few assignments without any added concurrency protection. If the memory location can be accessed simultaneously by other threads, watch out.
In particular it's a good fit for thread-local locations: &X
, where X
is declared thread_local
, or X == *(P.get())
where P
is a boost::thread_specific_ptr
.
Value | The stored type, which must be move-assignable and move-constructible. All Value writes are performed using exclusively these operations. Informally: For best performance when Value is a heavy-weight type, these operations should be be written to be light-weight, such as in terms of swapping a few scalars. In particular this is already the case for all STL-compliant container types. |
|
explicit |
Post-condition: *target
contains was val_src_moved
contained at ctor entry; and the destructor invocation shall reverse this, so that *target
is restored to its value at entry.
*this
cannot be copied, but it can be moved. As a result, it is guaranteed that the aforementioned destructor will execute exactly once; however it can be move-constructed-from-*this
other Scope_setter's destructor, while our own dtor therefore is a no-op.
target | The target object that shall be set to val_src_moved now and restored in our, or moved-object's, dtor. The current value of *target is saved internally via assignment of move(*target) . Behavior undefined (assertion may trip) if null. |
val_src_moved | Value to save to *target immediately, via assignment of move(val_src_moved) . |
flow::util::Scoped_setter< Value >::Scoped_setter | ( | Scoped_setter< Value > && | src_moved | ) |
Move constructor: *this
acts as src_moved
would-have, while src_moved
becomes a no-op object permanently.
src_moved | Source object. Its destructor shall do nothing after this returns. |