Flow 1.0.1
Flow project: Full implementation reference.
|
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. More... | |
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. | |
Private Attributes | |
Value * | m_target_or_null |
Target object location; see ctors; if null then this is a moved-from Scoped_setter that intentionally no-ops. More... | |
Value | m_saved_value |
If and only if m_target_or_null is non-null, this saves *m_target_or_null . Otherwise meaningless. More... | |
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. |
An alternative implementation, which could even be reduced to just an alias, would have used unique_ptr
. However in this case I (ygoldfel) wanted maximum control for perf. The use case originally driving this was the thread-local verbosity override: log::Config::this_thread_verbosity_override_auto(). flow::log is fairly paranoid about performance, in general, although admittedly this particular call isn't necessarily ubiquitous.
|
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) . |
Definition at line 181 of file util.hpp.
References flow::util::Scoped_setter< Value >::m_target_or_null.
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. |
Definition at line 189 of file util.hpp.
References flow::util::Scoped_setter< Value >::m_target_or_null.
flow::util::Scoped_setter< Value >::~Scoped_setter |
|
private |
If and only if m_target_or_null is non-null, this saves *m_target_or_null
. Otherwise meaningless.
|
private |
Target object location; see ctors; if null then this is a moved-from Scoped_setter that intentionally no-ops.
Definition at line 170 of file util.hpp.
Referenced by flow::util::Scoped_setter< Value >::Scoped_setter().